How to install OpenCV on Ubuntu 20.04

Posted by magicrobotmonkey on Fri, 18 Feb 2022 04:56:56 +0100

How to install OpenCV on Ubuntu 20.04

OpenCV(Open Source Computer Vision Library) is an open source computer vision library that supports C++, Python, and Java on all major operating systems. It can work with multicore processes and GPU Accelerated for real-time operation.

OpenCV is widely used, including medical picture analysis, street views Picture Processing , monitor videos, detect and identify faces, track moving objects, extract 3D models, and so on.

This article describes how to install OpenCV on Ubuntu 20.04. To install the latest version of OpenCV from the source code, slide down to the Installing OpenCV from the Source section. Please choose the installation method that best suits you.

1. Install OpenCV from Ubuntu Source Warehouse

OpenCV is available in Ubuntu 20.04 software source. To install it, run:

sudo apt update
sudo apt install libopencv-dev python3-opencv

The above command will install all necessary packages to run OpenCV:

Verify the installation results by importing the cv2 module and printing the OpenCV version:

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

When writing, the source version is 4.2:



2. Install OpenCV from Source Code

Installing OpenCV from source allows you to install the latest available version. It will also be optimized for your particular system, and you have full control over all the build options. This is the most recommended way to install OpenCV.

Follow these steps to install the latest OpenCV version from source code:

01. Install the build tools and all dependent packages:

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

02. Clone all OpenCV and OpenCV contrib sources:

mkdir ~/opencv_build && cd ~/opencv_build
git clone
git clone

When writing, the default version in the github software source is 4.3.0. If you want to install an older version of OpenCV, cd to opencv and opencv_contrib directory, and run git checkout <opencv-version>.

03.Once the download is complete, create a temporary build directory and switch to it:

cd ~/opencv_build/opencv
mkdir -p build && cd build

Configure OpenCV builds using the CMake command:

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \

The output will be as follows:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/vagrant/opencv_build/opencv/build

04. Start the compilation process:

make -j8

Modify the -f value according to your processor. If you do not know your processor core number, you can enter nproc to find it.

Compiling will take a few minutes, or more, depending on your system configuration.

05.Install OpenCV:

sudo make install

06. Verify the installation results and enter the following command, then you will see the OpenCV version:

C++ bindings:

pkg-config --modversion opencv4



Python bindings:

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



3. Summary

We have shown two different ways to install OpenCV on your Ubuntu 20.04 server. Your first choice depends on your requirements and preferences.

Even though it's easy to install packages directly from Ubuntu sources, installing OpenCV from source builds gives you more flexibility and should be your first choice for installing OpenCV.

Topics: OpenCV Ubuntu Computer Vision