How to install OpenCV (CV2) on Ubuntu 16.04/18.04

This post will guide you how to install OpenCV on your Ubuntu Linux server. How do I install OpenCV-Python from source code on Ubuntu Linux 16.04/18.04.

What is OpenCV (CV2)?


OpenCV (Open Source Computer Vision Library) is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV has a modular structure, which means that the package includes several shared or static libraries. And OpenCV is a multi-platform open source computer vision library.

install opencv1

Installing OpenCV With Python 2 from the Default Repository


The OpenCV package is available from the default Ubuntu Repository. So if you want to install OpenCV on your Ubuntu system with Python 2, you can use the following apt install command to install it, type:

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install python-opencv

Outputs:

devops@devops:~$ sudo apt install python-opencv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
augeas-lenses cpu-checker cryptsetup cryptsetup-bin db-util db5.3-util
dmeventd extlinux hfsplus icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms
......
0 upgraded, 112 newly installed, 0 to remove and 0 not upgraded.
Need to get 81.8 MB of archives.
After this operation, 383 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y
.....
mpirun) in auto mode
Setting up libarmadillo8 (1:8.400.0+dfsg-2) ...
Setting up libkmlengine1:amd64 (1.3.0-5) ...
Setting up libchromaprint1:amd64 (1.4.3-1) ...
Setting up libavformat57:amd64 (7:3.4.4-0ubuntu0.18.04.1) ...
Setting up odbcinst1debian2:amd64 (2.3.4-1.1ubuntu3) ...
Setting up odbcinst (2.3.4-1.1ubuntu3) ...
Setting up libgdal20 (2.2.3+dfsg-2) ...
Setting up libopencv-imgcodecs3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libvtk6.3 (6.3.0+dfsg1-11build1) ...
Setting up libopencv-videoio3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-viz3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-superres3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-highgui3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-objdetect3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-features2d3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-calib3d3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-stitching3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-videostab3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up libopencv-contrib3.2:amd64 (3.2.0+dfsg-4ubuntu0.1) ...
Setting up python-opencv (3.2.0+dfsg-4ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

After OpenCV package is installed with the above commands on your system, you can use the following commands to load the appropriate cv2 module in python terminal, so that you can verify if the installation of OpenCV has been installed successfully:

import cv2cv2.__version__

Outputs:

devops@devops:~$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>>

From the above outputs, you should notice that the current version of OpenCV is 3.2.0.

Installing OpenCV With Python 3 from the Default Repository


If you want to install the OpenCV with Python3 bindings on your Ubuntu 16.04 or 18.04 Linux, you need to install the following package with apt install command, type:

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install python3-opencv -y

Outputs:

devops@devops:~$ sudo apt install python3-opencv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
augeas-lenses cpu-checker cryptsetup cryptsetup-bin db-util db5.3-util
dmeventd extlinux hfsplus icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms
ldmtool libafflib0v5 libaio1 libaugeas0 libbfio1 libcacard0 libconfig9
libdate-manip-perl libdevmapper-event1.02.1 libewf2 libfdt1 libhfsp0
libhivex0 libintl-perl libintl-xs-perl libiscsi7 libldm-1.0-0 liblvm2app2.2
liblvm2cmd2.02 librados2 librbd1 libsdl1.2debian libspice-server1
libstring-shellquote-perl libsys-virt-perl libtsk13 libusbredirparser1
libvirt0 libwin-hivex-perl libxen-4.9 libxenstore3.0 linux-headers-4.15.0-45
linux-headers-4.15.0-45-generic linux-headers-generic-hwe-16.04
linux-image-4.15.0-45-generic linux-image-generic-hwe-16.04
linux-modules-4.15.0-45-generic linux-modules-extra-4.15.0-45-generic lsscsi
lvm2 msr-tools osinfo-db qemu-block-extra qemu-system-common qemu-system-x86
qemu-utils scrub seabios sgabios sleuthkit supermin zerofree
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
python3-numpy
Suggested packages:
gfortran python-numpy-doc python3-dev python3-nose python3-numpy-dbg
The following NEW packages will be installed:
python3-numpy python3-opencv
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,477 kB of archives.
After this operation, 13.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python3-numpy amd64 1:1.13.3-2ubuntu1 [1,943 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security/universe amd64 python3-opencv amd64 3.2.0+dfsg-4ubuntu0.1 [534 kB]
Fetched 2,477 kB in 1s (3,398 kB/s)
Selecting previously unselected package python3-numpy.
(Reading database ... 269204 files and directories currently installed.)
Preparing to unpack .../python3-numpy_1%3a1.13.3-2ubuntu1_amd64.deb ...
Unpacking python3-numpy (1:1.13.3-2ubuntu1) ...
Selecting previously unselected package python3-opencv.
Preparing to unpack .../python3-opencv_3.2.0+dfsg-4ubuntu0.1_amd64.deb ...
Unpacking python3-opencv (3.2.0+dfsg-4ubuntu0.1) ...
Setting up python3-numpy (1:1.13.3-2ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up python3-opencv (3.2.0+dfsg-4ubuntu0.1) ...

After installed Python3-opencv on your system, you can try to load the appropriate cv2 library in python3 terminal to confirm the installation of OpenCV, type:

import cv2
cv2.__version__

Outputs:

devops@devops:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>>

Installing OpenCV from Official Pip Package


You can also install OpenCV library by using official pip package on your Ubuntu system, just do the following steps:

#1 you need to install pip package manager on your system with the following command:

$ sudo apt install python-pip

Outputs:

devops@devops:~$ sudo apt install python-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
augeas-lenses cpu-checker cryptsetup cryptsetup-bin db-util db5.3-util
dmeventd extlinux hfsplus icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms
ldmtool libafflib0v5 libaio1 libaugeas0 libbfio1 libcacard0 libconfig9
libdate-manip-perl libdevmapper-event1.02.1 libewf2 libfdt1 libhfsp0
libhivex0 libintl-perl libintl-xs-perl libiscsi7 libldm-1.0-0 liblvm2app2.2
liblvm2cmd2.02 librados2 librbd1 libsdl1.2debian libspice-server1
libstring-shellquote-perl libsys-virt-perl libtsk13 libusbredirparser1
libvirt0 libwin-hivex-perl libxen-4.9 libxenstore3.0 linux-headers-4.15.0-45
linux-headers-4.15.0-45-generic linux-headers-generic-hwe-16.04
linux-image-4.15.0-45-generic linux-image-generic-hwe-16.04
linux-modules-4.15.0-45-generic linux-modules-extra-4.15.0-45-generic lsscsi
lvm2 msr-tools osinfo-db python3-numpy qemu-block-extra qemu-system-common
qemu-system-x86 qemu-utils scrub seabios sgabios sleuthkit supermin zerofree
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libexpat1-dev libpython-all-dev libpython-dev libpython2.7-dev python-all
python-all-dev python-asn1crypto python-cffi-backend python-crypto
python-cryptography python-dbus python-dev python-enum34 python-gi
python-idna python-ipaddress python-keyring python-keyrings.alt
python-pip-whl python-pkg-resources python-secretstorage python-setuptools
python-six python-wheel python-xdg python2.7-dev
Suggested packages:
python-crypto-doc python-cryptography-doc python-cryptography-vectors
python-dbus-dbg python-dbus-doc python-enum34-doc python-gi-cairo
libkf5wallet-bin python-fs python-gdata python-keyczar
python-secretstorage-doc python-setuptools-doc
The following NEW packages will be installed:
libexpat1-dev libpython-all-dev libpython-dev libpython2.7-dev python-all
python-all-dev python-asn1crypto python-cffi-backend python-crypto
python-cryptography python-dbus python-dev python-enum34 python-gi
python-idna python-ipaddress python-keyring python-keyrings.alt python-pip
python-pip-whl python-pkg-resources python-secretstorage python-setuptools
python-six python-wheel python-xdg python2.7-dev
0 upgraded, 27 newly installed, 0 to remove and 0 not upgraded.
Need to get 32.1 MB of archives.
After this operation, 53.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
......
Setting up libpython2.7-dev:amd64 (2.7.15~rc1-1ubuntu0.1) ...
Setting up python-dbus (1.2.6-1) ...
Setting up python-ipaddress (1.0.17-1) ...
Setting up python-pip (9.0.1-2.3~ubuntu1) ...
Setting up python2.7-dev (2.7.15~rc1-1ubuntu0.1) ...
Setting up python-all (2.7.15~rc1-1) ...
Setting up python-xdg (0.25-4ubuntu1) ...
Setting up libpython-dev:amd64 (2.7.15~rc1-1) ...
Setting up python-setuptools (39.0.1-2) ...
Setting up python-dev (2.7.15~rc1-1) ...
Setting up libpython-all-dev:amd64 (2.7.15~rc1-1) ...
Setting up python-keyrings.alt (3.0-1) ...
Setting up python-all-dev (2.7.15~rc1-1) ...
Setting up python-cryptography (2.1.4-1ubuntu1.2) ...
Setting up python-secretstorage (2.3.1-2) ...
Setting up python-keyring (10.6.0-1) ...

You can try to check the version of pip tool to verify if the pip package is installed normally on your system, type:

$ pip --version

Outputs:

devops@devops:~$ pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

So you can get the pip version from the above outputs.

#2 you can try to use the following command to install OpenCV library, type:

$ sudo pip install opencv-python

Outputs:

devops@devops:~$ pip install opencv-python
Collecting opencv-python
Downloading https://files.pythonhosted.org/packages/e8/04/925d40d1aca43b949453c9350345ac0b4ca122d479d6d254a54ad0d1dd23/opencv_python-4.0.0.21-cp27-cp27mu-manylinux1_x86_64.whl (25.4MB)
100% |████████████████████████████████| 25.4MB 56kB/s
Collecting numpy>=1.11.1 (from opencv-python)
Downloading https://files.pythonhosted.org/packages/c4/33/8ec8dcdb4ede5d453047bbdbd01916dbaccdb63e98bba60989718f5f0876/numpy-1.16.2-cp27-cp27mu-manylinux1_x86_64.whl (17.0MB)
100% |████████████████████████████████| 17.0MB 91kB/s
Installing collected packages: numpy, opencv-python
Successfully installed numpy-1.16.2 opencv-python-4.0.0.21

#3 you can type the following codes to load cv2 library in Python terminal,type:

import cv2
cv2.__version__

Outputs:

devops@devops:~$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0'
>>>

So the installed version of OpenCV is 4.0.0 by using pip package manager on your Ubuntu system.

Installing OpenCV from Source Code


As the version in the default Ubuntu repositories or pip repository may be not the latest version. And the current latest version of OpenCV is 4.1.0. So if you want to install the latest version of OpenCV on your Ubuntu system, you can install it from source code of OpenCV. Just do the following steps:

#1 You need to update package manager cache, then upgrade pacakges that have updates. type:

$ sudo apt update
$ sudo apt upgrade

#2 you need to install some dependency packages with the following command:

$ sudo apt install build-essential
$ sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

Note: if you want to process images, you still need to install the below dependencies,type:

$ sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

If you want to process videos with OpenCV library, you need to install the following dependency packages, type:

$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt install libxvidcore-dev libx264-dev

If you want to build OpenCV binding for both python2 and python3 on your system, you need to install the following packages:

$ sudo apt install python2.7-dev python3.6-dev

#3 Downloading the latest source of OpenCV library from OpenCV’s GitHug Repository with the following command:

$ sudo apt install git
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

Outputs:

devops@devops:~$ git clone https://github.com/opencv/opencv.git
Cloning into 'opencv'...
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 251008 (delta 0), reused 0 (delta 0), pack-reused 251006
Receiving objects: 100% (251008/251008), 457.08 MiB | 493.00 KiB/s, done.
Resolving deltas: 100% (175064/175064), done.
Checking out files: 100% (5800/5800), done.

devops@devops:~$ git clone https://github.com/opencv/opencv_contrib.git
Cloning into 'opencv_contrib'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 29321 (delta 0), reused 1 (delta 0), pack-reused 29318
Receiving objects: 100% (29321/29321), 126.37 MiB | 1.07 MiB/s, done.
Resolving deltas: 100% (17989/17989), done.
devops@devops:~$

devops@devops:~$ ls opencv*
opencv:
3rdparty cmake CONTRIBUTING.md doc LICENSE platforms samples
apps CMakeLists.txt data include modules README.md

opencv_contrib:
CONTRIBUTING.md doc LICENSE modules README.md samples

Note: It will create two folders “opencv” and “opencv_contrib” in current directory. The cloning may take some time depending upon your internet connection. OpenCV_contrib module has very useful algorithms which is a must for anyone working on Computer Vision.

#4 changing the current directory to opencv, and create a new build directory and navigate to it. type:

$ cd opencv
$ mkdir build
$ cd build

#5 setup OpenCV build using Cmake, type:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..

Outputs:

devops@devops:~/opencv/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
> -D CMAKE_INSTALL_PREFIX=/usr/local \
> -D INSTALL_C_EXAMPLES=ON \
> -D INSTALL_PYTHON_EXAMPLES=ON \
> -D OPENCV_GENERATE_PKGCONFIG=ON \
> -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
> -D BUILD_EXAMPLES=ON ..
-- The CXX compiler identification is GNU 7.3.0
-- The C compiler identification is GNU 7.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
......
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2019.0.0 Gold [2019.0.0]
-- at: /home/devops/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2019.0.0)
-- at: /home/devops/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: /home/devops/opencv/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.15)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.15rc1)
-- numpy: /home/devops/.local/lib/python2.7/site-packages/numpy/core/include (ver 1.16.2)
-- install path: lib/python2.7/dist-packages/cv2/python-2.7
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.6.7)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.7)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
-- install path: lib/python3.6/dist-packages/cv2/python-3.6
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/devops/opencv/build

devops@devops:~/opencv/build$ echo $?
0

From the above outputs, you should see the following lines in your CMake output, it means that Python is properly found.

- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.15)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.15rc1)
-- numpy: /home/devops/.local/lib/python2.7/site-packages/numpy/core/include (ver 1.16.2)
-- install path: lib/python2.7/dist-packages/cv2/python-2.7
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.6.7)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.7)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
-- install path: lib/python3.6/dist-packages/cv2/python-3.6

#6 Compiling OpenCV with the following make command, type:

$ make

Note: The compiling process will take a few minutes to complete. If your compiling fails or hangs, you need to delete your build directory, and recreate it, and re-run the above cmake command and make command.

#7 Installing OpenCV with make install command, type:

$ sudo make install

#8 you can verify if OpenCV is installed successfully on your system with the following command to check the OpenCV version:

$ pkg-config --modversion opencv

Outputs:

$ pkg-config --modversion opencv
4.1.0

Or you can open a python3 terminal to try import “cv2” module, type:

import cv2
print (cv.__version__)

Conclusion


You should know that how to install OpenCV library on Ubuntu 16.04 or 18.04 from this guide, and you also know how to install the latest version of OpenCV from source on Ubuntu Linux server. If you want to see more information about OpenCV, you can go the official web site of OpenCV directly.

You might also like:

Sidebar



back to top