Signup/Sign In

How to Install openCV on Ubuntu 18.04 or above

Posted in Machine Learning   LAST UPDATED: AUGUST 23, 2021

    In this blog, we are going to install OpenCV on Ubuntu 18.04 or above. The main thing about this setup guide is that we will be using Python 3.0+ and not Python 2.7 because by default the Ubuntu 18.04 or above supports directly Python 3.0+ and they have completely dropped python 2.7 from the operating system.

    To check Python 3 version(and also if it is pre-installed or not) on Ubuntu we have to use the following command:

    $ python3 --version
    
    Python 3.6.9

    how to install OpenCV on Ubuntu

    Figure 1: The above screenshot shows the Python3 version

    So let's start the installation.

    Step 1: Installation of the OpenCV dependency

    First, we will start by updating our operating system and making sure all the OpenCV dependencies are installed.

    Open your Ubuntu laptop or Desktop and then start the terminal either click on the show Application option and then search terminal or else press Ctrl + Alt + T from your keyboard.

    We are going to update the operating system using the following commands:

    $ sudo apt-get update
    $ sudo apt-get upgrade

    Now we are going to install some tools which will help us while installing OpenCV on Ubuntu.

    $ sudo apt-get install build-essential
    $ sudo apt-get install cmake
    $ sudo apt-get install unzip
    $ sudo apt-get install pkg-config

    Now we are going to install some image tools and video libraries (input and output). These libraries help us to read images from the local drive along with the video.

    $ sudo apt-get install libjpeg-dev
    $ sudo apt-get install libpng-dev
    $ sudo apt-get install libtiff-dev

    Now we have to install the libjasper-dev.

    $ sudo apt-get install libjasper-dev

    If you are facing any type of error during the installation of libjasper-dev library, follow the below steps. If you are not getting any error while installing the above library, then skip this step:

    $ sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
    $ sudo apt update
    $ sudo apt install libjasper1
    $ sudo apt install libjasper1 libjasper-dev

    We continue with installing other libraries:

    $ sudo apt-get install libavcodec-dev
    $ sudo apt-get install libavformat-dev
    $ sudo apt-get install ibswscale-dev
    $ sudo apt-get install libv4l-dev
    $ sudo apt-get install libxvidcore-dev
    $ sudo apt-get install libx264-dev

    Now we are going to install the GTK library.

    $ sudo apt-get install libgtk-3-dev

    Now we are going to install the optimization methods library for the OpenCV using the following commands:

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

    Now we are going to install header files of the Python3 using the below command:

    $ sudo apt-get install python3-dev

    And with this, we have completed step 1.

    Step 2: Download OpenCV and OpenCV_Contrib

    Now we are going to download OpenCV 4.3.0 and openCV_contrib 4.3.0. With the help of the opencv_contrib, we will get more functions and modules. So while installing OpenCV, we can install both OpenCV and opencv_contrib at once.

    To download, the OpenCV and opencv_contrib use the following command. We are installing OpenCV 4.3.0 but you can install any OpenCV version which exists on the OpenCV website. While writing this blog, the current OpenCV version is 4.3.0

    To download the OpenCV 4.3.0 use the following command:

    $ cd ~
    $ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.3.0.zip

    To download the OpenCV_Contrib 4.3.0 use the following command:

    $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.3.0.zip

    Note: OpenCV and OpenCV_Contrib both versions should be the same.

    The installation of the Contrib along with OpenCV is required because we do not get some patent algorithms directly while installing OpenCV like SURF and SIFT algorithm. If you want to use these patent algorithms and daily research algorithms then you must install OpenCV_Contrib along with the OpenCV.

    Now in the next step, we have to unzip both OpenCV and OpenCV_Contrib using the following commands. To do this, navigate to that folder or location where you downloaded both zip files and then run the below commands.

    $ unzip opencv.zip
    $ unzip opencv_contrib.zip

    After unzipping the files, you will get your file name with the version which you downloaded. So we can rename the files. This step is optional and if you do not rename, then be careful while writing the below commands ahead because we will be using the rename file name.

    $ mv opencv-4.3.0 opencv
    $ mv opencv_contrib-4.3.0 opencv_contrib

    Step 3: Setup Virual Environment

    Now we will use the pip for further installation. To configure pip from the old version to the new version using the following commands:

    $ wget https://bootstrap.pypa.io/get-pip.py
    $ sudo python3 get-pip.py

    Before going to the next step of the installation of the OpenCV on an Ubuntu machine, we have to know about the virtual environment. The virtual environment helps us to keep the same package of different versions on the same machine. Because sometimes we need to run a project of the lower version but already we have installed the latest library on our machine. So, in that case, we can use a virtual environment which helps to keep different version library on the same machine.

    For example, you are working on a project and you are using TensorFlow latest version on your system but to run that project you need Tensorflow version 2.0 or older. So if you remove the latest version then that might affect your other projects. So the best thing is that you create a virtual environment and install all the requirements libraries on that virtual environment and execute your project without affects the other projects.

    To create a virtual environment there are two methods in python:

    1. virtualenv

    2. virtualenvwrapper

    Any above methods you can choose, but the latest method is virtualenv, so I am also going to use this method here.

    $ sudo pip install virtualenv virtualenvwrapper
    $ sudo rm -rf ~/get-pip.py ~/.cache/pip

    Now we have to update the path of the virtualenv to bash. So for that, we have to use the following steps:

    export WORKON_HOME=$HOME/.virtualenvs

    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

    source /usr/local/bin/virtualenvwrapper.sh

    We can also add these paths using the below method. Choose either the above or below the method for this step.

    $ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
    $ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
    $ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

    Now after done the above steps using any above methods, we have to save that file back to bash using the below command:

    $ source ~/.bashrc

    Now we are going to create a virtual environment along with python for my project. To create the environment we use the following command:

    $ mkvirtualenv cv -p python3

    Explanation: The above command will create a virtual environment of the name cv. Here cv is not fixed as the name of the environment. You can keep any name which you desire but it is always recommended to keep the name which you remember always so that you can work easily way. To test whether our virtual environment got created or not, we can use the work on command for that as given below:

    how to install OpenCV on Ubuntu

    Figure 2: To create a virtual environment

    Figure 2: We can see that the virtual environment cv is created and showing active. So the screen which we have seen that is a virtual environment screen and here we can install all packages according to our project requirements either latest or old versions and it will not affect the main system packages or libraries which we already installed.

    Now let's install NumPy package in our virtual environment.

    Numpy is required to work with OpenCV along with Python. As our virtual environment is already active, so we just need to run the below command:

    $ pip install numpy

    Step 4: Compile and install OpenCV on Ubuntu

    Before we move to the next step, we must ensure that we are working in the virtualenv which we recently created with name cv (you use the name of your virtual environment). If you are not in the virtual environment, then run the following command to enter inside of that:

    $ workon cv

    how to install OpenCV on Ubuntu

    Figure 3: Activate the virtual environment.

    Why we need the virtual environment to be active? Because we want to build and compile OpenCV inside of this, so that we can get familiar with the virtual environment and we can install different versions of OpenCV in a single system without affecting the other versions.

    Now finally we are going to configure the OpenCV with Python on Ubuntu machine using CMake.

    $ cd ~/opencv
    $ mkdir build
    $ cd build
    $ cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=/usr/local \
            -D WITH_CUDA=OFF \
            -D INSTALL_PYTHON_EXAMPLES=ON \
            -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
            -D OPENCV_ENABLE_NONFREE=ON \
            -D BUILD_EXAMPLES=ON ..

    how to install OpenCV on Ubuntu

    Figure 4: We are going to build OpenCV with Python and also added non - free algorithm flag also.

    Make sure that you must have mentioned the flag value of OPENCV_ENABLE_NONFREE=ON because all the patented algorithms like we discuss before like SURF and SIFT come inside of this option and if we keep this flag value OFF then we won't be able to access those algorithms.

    Also, be careful to look at the OpenCV build output as shown in the below output image. If you see carefully, then you will see there is a virtual environment cv mentioned and all python libraries installed inside of this environment (your environment name will be different, if you choose a different name). Also, look carefully at the output screen of the build, you did not get any errors or compile go fail.

    how to install OpenCV on Ubuntu

    Figure 5: After successfully configuring the OpenCV with Python result.

    If you take notice of the final output screen you will see that the Python3 only show the details of the libraries or installation path. This is because of the reason we build the OpenCV along with the Python3 only.

    Also, if we scroll the output to some 30 to 40 lines above, then we can see an entry non-free algorithm included during installation. This means we have set up the patent algorithms too in our OpenCV installation.

    how to install OpenCV on Ubuntu

    Figure 6: This screenshot shows Non-free algorithms included (Yes).

    Caution: If you find any stdlib.h error like no such file or directory found then we have to add one more command during CMake which is:

    -D ENABLE_PRECOMPILED_HEADERS=OFF. After adding this line, delete the build directory from the OpenCV and recreate the build and repeat the process. Now you will not get any error.

    Now we are going to compile the OpenCV with Python on Ubuntu 18.04 machine.

    This compile of OpenCV takes approximately time 40 mins to 60 mins which depends upon the number of processors you have in your system. We can adjust the speed of the compilation by providing the cores during the process. In my system, I have 12 cores, so I am using 8 cores for compilation. The more the number of cores used, less will be time of the compilation.

    The command of compilation for OpenCV is:

    $ make -j4

    how to install OpenCV on Ubuntu

    Figure 7: We compile the OpenCV with Python 3 on Ubuntu 18.04 and now after a successful build, we will get results like the above screenshot.

    The value of 4 is the number of cores. You can adjust the numeral value depending upon available cores in your system.

    During the compilation, if you are getting an error or installation hangs your system then it means the compilation is going to fail. So you must stop the compilation and repeat the whole process from creating the build directory inside of the OpenCV directory (downloaded unzip folder). To create a new build, you must have to delete the old build directory.

    If you don't want to set the number of cores to be used, use the simple make command:

    $ make

    Now after successful compilation, we can install OpenCV using the following commands:

    $ sudo make install

    how to install OpenCV on Ubuntu

    Figure 8: After the successful compile, we install the OpenCV and you will see results like above.

    $ sudo ldconfig

    Also Read: Dlib 68 points Face landmark Detection with OpenCV and Python

    Step 5: Final Step

    Here in this step, we are going to bind the Python3 with the OpenCV. In this step, you must be careful because most people will get an error in this step..

    So you must check either your Python OpenCV must reside in this location. We can check it using the below command:

    $ ls /usr/local/lib/python3.6/site-packages/cv2/python-3.6

    And you will see some results as shown in the screenshot of my system.

    how to install OpenCV on Ubuntu

    Figure 9: We migrate to the location of cv2 which inside of the site-packages to check whether OpenCV is successfully installed or not.

    After that, we have to navigate to that location (above) and rename the above compile OpenCV with some other small name as shown below:

    $ cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6

    Now type ls and see the OpenCV python compile file which looks something like this:

    cv2.cpython-36m-x86_64-linux-gnu.so

    If you have reached here, then you are going good and rename the file like below:

    $ sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

    how to install OpenCV on Ubuntu

    Caution: If you have installed another version of OpenCV in your system then you must choose a different name so that it will not conflict with the already existing OpenCV version.

    For example, if I have already installed OpenCV in my system and the first time I did rename with cv2.so. So while installing the next time with another version of OpenCV, I will choose some different name like cv2.opencv3.4.4.so

    Now after renaming it, we do a system link of our OpenCV (cv2.so) to the virtual environment cv using the following command:

    $ cd ~/.virtualenvs/cv/lib/python3.6/site-packages/
    $ ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so cv2.so

    If you are getting an error during run the above command like Permission denied, then run the command with sudo as shown below in the screenshot below:

    If you did not get any error then it means your OpenCV is bound with the virtualenv, which we created.

    how to install OpenCV on Ubuntu

    Step 6: Test the Installation

    Now that we have completed all steps of installation of OpenCV with Python 3.0+ on Ubuntu machine. In this step, we are going to test our installation.

    To check installation verification, we must activate the virtualenv on which we installed our OpenCV version. And after that, we have to run the python command to run the python environment as shown in the screenshot. After that just write import command and the version command to test.

    $ cd ~
    $ workon cv
    $ python
    
    Python 3.6.9 (default, Jul 17 2020, 12:50:27)
    [GCC 8.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> import cv2
    
    >>> cv2.__version__
    
    '4.0.0'
    
    >>> quit()

    how to install OpenCV on Ubuntu

    Figure 12: To verify the proper installation of OpenCV with Python 3.0+ on Ubuntu machine.

    As in the above screenshot, I run a different virtualenv as it was already installed on my system. So follow the same above command to test your OpenCV installation. Now after that, we can delete the OpenCV and OpenCV_Contrib folders along with their zip files.

    $ cd ~
    $ rm opencv.zip opencv_contrib.zip
    $ rm -rf opencv opencv_contrib

    We have successfully installed the OpenCV with Python 3.0+ on Ubuntu machine. Like this we can install different versions of OpenCV on the same machine.

    Now in the next blog, we will install the OpenCV for C++.

    Happy Coding.

    You may also like:

    About the author:
    I am an Artificial Intelligence Engineer. Doing research work in Virtual Reality and Augmented Reality. I would like to write article or share my work with others apart from my professional life.
    Tags:OpenCV UbuntuLinuxPython
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS