How to install OpenCV 3.4.0 on Ubuntu 16.04

Install OpenCV 3.4.0 on Ubuntu 16.04 for all versions of python (2.7, 3.5 and 3.6)

OpenCV is a most popular free and open-source computer vision library among students, researchers, and developers alike. It has  C, C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS, and Android. At the time of writing of this blog, the latest version of OpenCV is 3.4.0. This tutorial is designed to help you install OpenCV 3.4.0 on Ubuntu 16.04. This tutorial works for versions 2.7, 3.5 and 3.6 of python. If you want to know how to install python 3.6 on Ubuntu, check out our other tutorial Install Python 3.6 on Ubuntu.

You can install opencv by using official prebuilt pip package or build from source.

To install from official pip package:

Use following commands in a new active empty virtual environment.

To install Opencv Only:

pip install opencv-python

To add contrib package in opencv:

pip install opencv-contrib-python

To install opencv by building from source:

OpenCV can be used in python as C++ python binding. I am going to explain how to build OpenCV from source and generate python binding.

The steps needed to be taken in order to install OpenCV 3.4.0 are:

  1. Update and Upgrade and Cleanup (Required for error-free installation)
  2. Install Dependencies
  3. Download and Build OpenCV

Update and Upgrade and Cleanup (Required for error-free installation)

sudo apt-get update
sudo apt-get upgrade

Now that we have the updates ready, we are ready to install dependencies.

Install Dependencies

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

The following command is needed to process images:

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

To process videos:

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

For GUI:

sudo apt-get install libgtk-3-dev

For optimization:

sudo apt-get install libatlas-base-dev gfortran pylint

To build OpenCV binding for both python 2 and 3.

sudo apt-get install python2.7-dev python3.5-dev

We are now ready to download and build OpenCV on our Ubuntu machine.

Download and Build OpenCV

To Download OpenCV 3.4.0, go to terminal and type:

wget -O

To Download OpenCV Contrib 3.4.0 (Contrib has very useful algorithms which is a must for anyone working on Computer Vision), go to terminal and type:

wget -O

We need to unzip to extract the zip files. Install unzip if not installed using the command:

sudo apt-get install unzip

Now extract OpenCV and OpenCV Contrib:


Make a directory named build inside OpenCV-3.4.0:

cd  opencv-3.4.0
mkdir build
cd build

Now we are going to configure cmake:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True ..

The command will take some time to execute. Wait for few seconds.

If the configuration is done without error, we will use make command to build.

make -j4 (where 4 is number of cores)

This command will take a few minutes to complete. On my PC, it took about 20 minutes.

Install in the location /usr/local using command

sudo make install

Reinitialize static libs using the following command

sudo ldconfig

Check if the OpenCV file exists

  • python2
  • python3.5
  • python3.6

To use OpenCV 3.4.0 in virtual environment like virtualenv, conda or any other, copy related .so file in your virtual environment python directory


Finally, check OpenCV version. Go to the terminal and typepython
On the python terminal, type

import cv2

If you ran into any problem while installing OpenCV or have any query related to OpenCV then feel free to comment below. I will try my best to solve it.


25 Comments on How to install OpenCV 3.4.0 on Ubuntu 16.04

  1. Hi, I have facing some of problem when I will follow tutorial until to the {Install in the location /usr/local using command} then after I change my directory to /usr/local then use the command it shows me that “make: *** No rule to make target ‘install’. Stop.”. May I know how to solve this problem? or it is because I miss some of the lib ?Thank you so much

      • I seem to be facing the same issue, tried doing it in build as well but get the same error. Has anyone found a solution to this ?

      • Facing the same issue, the directory is in build yet I get the same error. Has anyone managed to find a solution to this ?

  2. Hello, i also faced the same issue that opencv lib got compiled for python 2.7 only. The following command fixed the problem for me. I am using conda environment, so first i activated the environment.

    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DINSTALL_PYTHON_EXAMPLES=ON -DINSTALL_C_EXAMPLES=OFF -DPYTHON_INCLUDE_DIR=/media/miniconda3/envs/deap/include/python3.6m -DPYTHON_LIBRARY=/media/miniconda3/envs/deap/lib/python3.6 -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True -DBUILD_EXAMPLES=ON -DPYTHON_EXECUTABLE=/media/miniconda3/envs/deap/bin/python ..

  3. 1. You can’t build with python3.6-dev on ubuntu LTS 16.04 at this time, strange but true. Sure you can use python3.6 on ubuntu as I do but for opencv the cmake files needs to find the 3.6-dev headers which it cannot in this case.

    2. Build using python3.5. Since I use anaconda I made a new env for open pose as openpose very quickly and of course it was install the latest version of numpy.

    3. Install as per instructions above. Then copy over the below .so file to your environments site-packages repository as an example here is mine (minus my computers name or user account put yours in if using anaconda).

    cp /home//anaconda3/envs/openpose/lib/python3.5/site-packages

    Then it should work. Why build from source, why go through the hassle? Because some applications also using cmake may be expecting or looking for the build directories of opencv and under the pip version they are not in the expected place. Now suddenly openCV won’t couple with other sophisticated applications. At least that is what happened to me.

  4. I followed your guidelines to install. But I could not find any of the Python version. What do I do?python2

  5. Hey I rand into an error when running this command:
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True ..

    What can I do?

  6. Thanks ! I find it in the directory “/usr/local/lib/python2.7/”. So, i have to use openCV only with python2. Is there any solution to move on python 3

    • You can’t use python2 file in python3 it should be separately built. See the location of python3 in console after cmake configuration done.

      • I have the same problem. I only find for python2. And there is no folder named python3 in my build/lib/. I checked my /usr/local/lib, and find an empty folder named python3.5. Is there any solution to fix it?

      • I check the output of cmake, and the outputs are as below:
        General configuration for OpenCV 3.4.0 =====================================
        — Version control: 0ab3336

        — Extra modules:
        — Location (extra): /home/ljc/opencv_contrib-3.4.0/modules
        — Version control (extra): 0ab3336

        — Platform:
        — Timestamp: 2018-03-21T08:14:43Z
        — Host: Linux 4.13.0-37-generic x86_64
        — CMake: 3.5.1
        — CMake generator: Unix Makefiles
        — CMake build tool: /usr/bin/make
        — Configuration: Release

        — CPU/HW features:
        — Baseline: SSE SSE2 SSE3
        — requested: SSE3
        — Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
        — requested: SSE4_1 SSE4_2 AVX FP16 AVX2
        — SSE4_1 (3 files): + SSSE3 SSE4_1
        — SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
        — FP16 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
        — AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
        — AVX2 (9 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

        — C/C++:
        — Built as dynamic libs?: YES
        — C++ Compiler: /usr/bin/c++ (ver 5.4.0)
        — C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
        — C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
        — C Compiler: /usr/bin/cc
        — C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
        — C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
        — Linker flags (Release):
        — Linker flags (Debug):
        — ccache: NO
        — Precompiled headers: YES
        — Extra dependencies: dl m pthread rt
        — 3rdparty dependencies:

        — OpenCV modules:
        — To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dpm face features2d flann freetype fuzzy hdf highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
        — Disabled: js world
        — Disabled by dependency: –
        — Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern java matlab ovis python3 sfm viz
        — Applications: tests perf_tests apps
        — Documentation: NO
        — Non-free algorithms: YES

        — GUI:
        — GTK+: YES (ver 3.18.9)
        — GThread : YES (ver 2.48.2)
        — GtkGlExt: NO
        — VTK support: NO

        — Media I/O:
        — ZLib: /home/ljc/anaconda3/lib/ (ver 1.2.11)
        — JPEG: /home/ljc/anaconda3/lib/ (ver 90)
        — WEBP: build (ver encoder: 0x020e)
        — PNG: /home/ljc/anaconda3/lib/ (ver 1.6.34)
        — TIFF: /home/ljc/anaconda3/lib/ (ver 42 / 4.0.9)
        — JPEG 2000: /home/ljc/anaconda3/lib/ (ver 1.900.1)
        — OpenEXR: build (ver 1.7.1)

        — Video I/O:
        — DC1394: YES (ver 2.2.4)
        — FFMPEG: YES
        — avcodec: YES (ver 56.60.100)
        — avformat: YES (ver 56.40.101)
        — avutil: YES (ver 54.31.100)
        — swscale: YES (ver 3.1.101)
        — avresample: NO
        — GStreamer: NO
        — libv4l/libv4l2: NO
        — v4l/v4l2: linux/videodev2.h
        — gPhoto2: NO

        — Parallel framework: pthreads

        — Trace: YES (with Intel ITT)

        — Other third-party libraries:
        — Intel IPP: 2017.0.3 [2017.0.3]
        — at: /home/ljc/opencv-3.4.0/build/3rdparty/ippicv/ippicv_lnx
        — Intel IPP IW: sources (2017.0.3)
        — at: /home/ljc/opencv-3.4.0/build/3rdparty/ippicv/ippiw_lnx
        — Lapack: NO
        — Eigen: NO
        — Custom HAL: NO

        — NVIDIA CUDA: NO

        — OpenCL: YES (no extra features)
        — Include path: /home/ljc/opencv-3.4.0/3rdparty/include/opencl/1.2
        — Link libraries: Dynamic load

        — Python 2:
        — Interpreter: /usr/bin/python2.7 (ver 2.7.12)
        — Libraries: /usr/lib/x86_64-linux-gnu/ (ver 2.7.12)
        — numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0)
        — packages path: lib/python2.7/dist-packages

        — Python (for build): /usr/bin/python2.7
        — Pylint: /home/ljc/anaconda3/bin/pylint (ver: 1.8.2, checks: 113)

        — Java:
        — ant: NO
        — JNI: NO
        — Java wrappers: NO
        — Java tests: NO

        — Matlab: NO

        — Install to: /usr/local
        — —————————————————————–

        — Configuring done
        — Generating done
        — Build files have been written to: /home/ljc/opencv-3.4.0/build

        It seems that I only build it for python2.7, how to change it for python 3.6?

        • Activate conda environment or virtual environment with python3 as interpreter and then configure.

          • emmm, I tried activating my py3.6 env and then cmake. It’ still the same. And I also tried cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True -DPYTHON_LIBRARY=/home/ljc/anaconda3/lib/ -DPYTHON_INCLUDE_DIR=/home/ljc/anaconda3/include/python3.6m/ -DPYTHON_EXECUTABLE=/home/ljc/anaconda3/bin/python ..
            and it doesn’t help. Could you please give some detailed steps to solve it? Many thanks

            • May be you have numpy not installed. Install using `pip3 install numpy` also do `sudo apt-get install python3.6-dev && sudo apt-get install python3.5-dev && sudo apt-get install python3-numpy` then configure.

  7. Many thanks for the tutorial.
    But i have a problem. I don’t find the file “/usr/local/lib/python3.5/dist-packages/”.

    • You can get the location from the log in console. Also you can find the so file inside build/lib/python3/ for python3 and build/lib/ for python2.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.