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.


1 Comment on How to install OpenCV 3.4.0 on Ubuntu 16.04

  1. hello Arun, I have installed opencv for both python2 as well python3, till yesterday everything is fine, today I have upgraded my python3.5.2 to 3.6.8. And I set my default python as 3.6.8. So that I rebuild entire opencv. Now, when I do cmake, it shows the compilation is done to python2 and I can see .so file in python2 dist-package, but it’s not building for python3.6. Can you please help me. I am not using any anaconda or virtual env.

Leave a Reply

Your email address will not be published.

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