How to Install OpenCV in Ubuntu 18.04 LTS for Python

In this post we will see How to Install OpenCV in Ubuntu for Python
OpenCV is an image processing library created by Intel and later supported by Willow Garage and now maintained by Itseez. OpenCV means Intel® Open Source Computer Vision Library. It is a collection of C functions and a few C++ classes that implement some popular Image Processing and Computer Vision algorithms. OpenCV is Available on Mac, Windows, Linux (Terminal environment).

Step 1 – Updating Ubuntu

$ sudo apt-get update

$ sudo apt-get upgrade

Step 2 – Install dependencies

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

$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev

$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

Step 3 –  Get OpenCV

$ sudo -s

$ cd /opt

/opt$ git clone

/opt$ git clone

Step 4 – build and install OpenCV

/opt$ cd opencv

/opt/opencv$ mkdir release

/opt/opencv$ cd release


/opt/opencv/release$ make -j4

/opt/opencv/release$ make install

/opt/opencv/release$ ldconfig

/opt/opencv/release$ exit

/opt/opencv/release$ cd ~

Now to check if OpenCV is installed on a machine, run the following commands

$ pkg-config --modversion opencv

We will get the opencv version installed
in this case it’s 3.2.x

Online Course – OpenCV Python Tutorial For Beginners. At the end of this course, you will have a firm grasp of Computer Vision techniques using OpenCV libraries. This course will be your gateway to the world of data science.

Create a Python program

Follow the commands

$ mkdir python_test

$ cd python_test

$ touch

The above command will create a folder called python_test and create a file inside it
Now place any .jpeg image inside the python_test folder.
So Now your python_test folder will contain two files as follows
├── sample.jpeg

Now open the and add the following code

# import the OpenCV package
import cv2
# load the image with imread()
imageSource = 'sample.png'
image = cv2.imread(imageSource)
# display the image on screen with imshow()
# after checking that it loaded
if image is not None:
    cv2.namedWindow( 'Display window', cv2.WINDOW_AUTOSIZE );
    cv2.imshow('Display window',image)
elif image is None:
    print ("Could not open or find the image")
# wait time in milliseconds
# this is required to show the image
# 0 = wait indefinitely
k = cv2.waitKey(0)
# destroy the window

Now run the python program with the following command

$ python3

press ESC to exit

Partner Sites

1 Comment

  1. this guide sucks.

    pkg-config –modversion opencv outputs 4.0.0 what the fuck?

    import cv2 leads to the error “ModuleNotFoundError: No module named ‘cv2′”

Leave a Reply

Your email address will not be published.