How to Install OpenCV on Ubuntu 20.04

Install OpenCV on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure OpenCV on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

OpenCV, also known as Open Source Computer Vision Library, is an open-source cross-platform computer vision algorithm library. Nowadays, OpenCV is being widely used in all kinds of visual processing areas, such as facial recognition, gesture recognition, human-computer interaction, Object identification, motion tracking, etc.

Install OpenCV on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install OpenCV on Ubuntu system.

  • Installation of OpenCV using the Ubuntu repository:

Install OpenCV by running the following command:

sudo apt install libopencv-dev python3-opencv

Once 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__)"

Output:

4.2.0
  • Installation of OpenCV from source:

First, install all 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

Next, clone the OpenCV’s and OpenCV contrib repositories from GitHub:

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

Once the download is complete, create a temporary build directory:

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

Now, setup OpenCV builds 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 ..

Start the compilation process by using the following command:

make -j8

Next, Install the OpenCV by using the following command:

sudo make install

To verify the installation, type the following commands:

pkg-config --modversion opencv4

Output:

4.3.0

That’s all you need to do to install OpenCV on Ubuntu 20.04 LTS Focal Fossa. I hope you find this quick tip helpful. For further reading on the OpenCV, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.