How to install OpenCV on Ubuntu 20.04

s2 0

(Open Source Computer Vision Library) is an open source computer vision library with bindings for C ++, Python and Java and supports all major operating systems. It can take advantage of multi-core processing and offers GPU acceleration for real-time operation.

OpenCV is used for a variety of applications including medical image analysis, street view image stitching, surveillance video, face detection and recognition, moving object tracking, 3D model extraction, and much more.

This article describes how to install OpenCV on Ubuntu 20.04. To install the latest stable version of OpenCV from the source, scroll down to the Install OpenCV from the source
Section of this tutorial. Choose the installation method that works best for you.

Install OpenCV from the Ubuntu repository

OpenCV is available to install from the standard Ubuntu 20.04 repositories. To install it, do the following:

sudo apt updatesudo apt install libopencv-dev python3-opencv

The above command installs all of the packages required to run OpenCV.

Verify the installation by checking the cv2 Module and printing of the OpenCV version:

python3 -c "import cv2; print(cv2.__version__)"

At the time of writing, the version in repositories is 4.2:


Install OpenCV from the source

Building the OpenCV library from source will give you the latest version available. It will be optimized for your particular system and you will have complete control over the build options. This is the recommended method for installing OpenCV.

To install the latest version of OpenCV from the source, do the following:

  1. Install the build tools and dependencies:

    sudo apt install build-essential cmake git pkg-config libgtk-3-dev     libavcodec-dev libavformat-dev libswscale-dev libv4l-dev     libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev     gfortran openexr libatlas-base-dev python3-dev python3-numpy     libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev     libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
  2. Clone the OpenCV and OpenCV contrib repositories:

    mkdir ~/opencv_build && cd ~/opencv_buildgit clone clone

    At the time of writing, the default version on the Github repositories is version 4.3.0. If you want to install an older version of OpenCV, cd on both opencv and opencv_contrib Directories and run git checkout <opencv-version>

  3. After the download is complete, create a temporary build directory and navigate to it:

    cd ~/opencv_build/opencvmkdir -p build && cd build

    Set up the OpenCV build with CMake:


    The output looks something like this:

    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/vagrant/opencv_build/opencv/build
  4. Start the compilation process:

    make -j8

    Modify the -j Flag according to your processor. If you don’t know the number of cores your processor has, you can get it by typing. determine nproc.

    Compilation can take several minutes or longer, depending on the system configuration.

  5. Install OpenCV with:

    sudo make install
  6. To verify the installation, enter the commands below and you should see the OpenCV version.

    C ++ bindings:

    pkg-config --modversion opencv4

    Python bindings:

    python3 -c "import cv2; print(cv2.__version__)"


We showed you two different ways to install OpenCV on your Ubuntu 20.04 machine. Which method you choose depends on your needs and preferences. While installing the packaged version from the Ubuntu repository is easier, building OpenCV from source gives you more flexibility and should be your first option when installing OpenCV.

If you have any questions or feedback, feel free to comment below.