Categories: LinuxShellUbuntu

How to install OpenCV on Ubuntu 20.04

OpenCV is a library of computer vision abbreviated as Open Source Computer Vision Library. Open means it is an open-source library with bindings for python, C++, and Java and supports different Operating Systems like Windows and Linux. It is able to use multi-core processing and GPU acceleration for real-time operating tasks.  OpenCV library can be used for a wide range of applications, including face recognition and detection, medical image analysis, motion tracking, surveillance video, 3D model extraction, and much more.

In this article, we will describe how to install the OpenCV library on Linux distribution Ubuntu 20.04.

You can install OpenCV using two different ways:

  1. Install OpenCV using Ubuntu repository
  2. Install OpenCV through the source

If you want to install the latest stable version of the OpenCV library on your system then, using the source method you can install it. Open the terminal by pressing ‘Ctrl+Alt+t’ and start the installation of OpenCV using the following methods:

Method 1: Installation of OpenCV using the Ubuntu repository

OpenCV library is available in the official Ubuntu repository in Ubuntu 20.04. Update the apt cache by using the following command:

$ sudo apt update

Install OpenCV by running the following command:

$ sudo apt install libopencv-dev python3-opencv

The above command will install all the necessary packages on your system.

After completing the installation of OpenCV, you can verify the installation by importing the cv2 module and print the installed version of OpenCV by executing the following command:

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

The following output will display on your screen. Here, the 4.2.0 OpenCV version has been installed on this system.

Method 2: Install OpenCV through the source

This method is recommended to install the OpenCV library because using this way you can install the latest stable version of OpenCV on your system. This method will be particularly optimized for your system and you can easily control the build options as well.

Follow the following step to install the latest OpenCV version from the source:

Step 1: Install build tools

Install the required build tools and dependencies on your system by using the following command:

$ 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

Step 2: Clone OpenCV’s repositories

Make the directory and navigate in it. Clone the OpenCV repository by running the following command:

$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git

Then, clone the OpenCV contrib repositories by using the following command:

$ git clone https://github.com/opencv/opencv_contrib.git

At the time of writing this article, OpenCV 4.4.0 is available as the latest stable version.

Step 3: Setup OpenCV build

Once the download is completed, create a directory named ‘build’ and navigate to it as follows:

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

Now, setup OpenCV build with CMake by using the following command:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..

You will see the following output on the terminal at the end:

Step 4: Start a compilation

Start the compilation process by using the following command:

$ make j8

Modify the value of the ‘j’ flag according to your processors. If you don’t know about your processor cores then, type the nproc command. You can easily find the core of your processor in this way. The compilation process will take time according to your system configurations. So, be patient.

Step 5:  Install OpenCV

Install the OpenCV by executing the following command:

$ sudo make install

After completing the installation process, type the following command to verify the OpenCV installation. For C++ binding:

$ pkg-config --modversion opencv4

For python binding run the command as follows:

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

Congratulations! The newest OpenCV version 4.4.0 has been installed on your system now.

Conclusion

We have explained two different ways in this article to install OpenCV on Linux system Ubuntu 20.04. Choose the methods according to your preferences and requirements. Or works best for you. Installing the packaged OpenCV version from the Ubuntu repository is quite easier, but, building OpenCV from the source provides you more flexibility, and it recommended that you should choose this method first when installing OpenCV on your system.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Recent Posts

How to Install Magento 2 on AlmaLinux

Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…

1 year ago

How to Install ISPConfig Hosting Control Panel with Apache Web Server on Ubuntu 24.04

ISPConfig is an open-source control panel that allows users to manage multiple servers from a…

1 year ago

How to Test your Email Server (SMTP) Using the Telnet Command

As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…

1 year ago

Managing Network Interfaces and Settings on Ubuntu 24.04 with nmcli

Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…

2 years ago

Using Restic Backup on Ubuntu 24.04

Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…

2 years ago

Installing phpMyAdmin on Rocky Linux 9 and Securing it with Let’s Encrypt SSL

phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…

2 years ago