How To Install OpenCV on Ubuntu 22.04

In this guide, you will learn to Install OpenCV From both Ubuntu Repository and Source on Ubuntu 22.04.

OpenCV stands for Open Source Computer Vision. To put it simply, it is a library used for image processing. In fact, it is a huge open-source library used for computer vision applications, in areas powered by Artificial Intelligence or Machine Learning algorithms, and for completing tasks that need image processing. As a result, it assumes significance today in real-time operations in today’s systems. Using OpenCV, one can process images and videos to identify objects, faces, or even the handwriting of a human.

Steps To Install OpenCV on Ubuntu 22.04

To complete this guide, log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Install OpenCV from Ubuntu Repository

At this point, you can easily install your OpenCV from the Ubuntu repository.

First, update your local package index with the command below:

sudo apt update

Then, use the command below to install OpenCV:

sudo apt install python3-opencv libopencv-dev

This command will install all packages necessary to run OpenCV.

Note: If you want to install OpenCV with Python 2 bindings, install the python-opencv package.

You can verify your installation with the following command:

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

Install OpenCV from Source on Ubuntu 22.04

It’s recommended to use this way to install OpenCV on your server.  It will be optimized for your particular system, and you will have complete control over the build options.

To install the latest and stable version of OpenCV from the source follow the steps below.

First, update your local package index with the following command:

sudo apt update

Then, you need to install the dependencies with the following command:

sudo apt install git cmake gcc g++ python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk2.0-dev libgtk-3-dev -y

Now create a directory for OpenCV on Ubuntu 22.04 and switch to it with the following command:

sudo mkdir ~/opencv_build && sudo cd ~/opencv_build

Clone OpenCV Repositories

Next, you need to clone the OpenCV and OpenCV contrib repositories with the following commands:

# sudo git clone
# sudo git clone

When your download is completed, create a temporary build directory and switch to it with the following command:

# sudo cd ~/opencv_build/opencv
# sudo mkdir build && sudo cd build

Compile and Build OpenCV

Here you need to set up OpenCV build with CMake on Ubuntu 22.04:

sudo cmake ../

In your output you will see:

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

Now start the compilation process:

make -j2

Modify the -j flag according to your processor. If you do not know the number of cores in your processor, you can find it by typing nproc.

This will take some minutes or more to complete.

In your output you will see:

[100%] Building CXX object modules/gapi/CMakeFiles/opencv_test_gapi.dir/test/util/variant_tests.cpp.o
[100%] Linking CXX executable ../../bin/opencv_test_gapi
[100%] Built target opencv_test_gapi

Now you can install OpenCV with the following command:

sudo make install

You can check that your installation that is successful or not with the following command:

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

That’s it you have successfully installed OpenCV on Ubuntu 22.04.


At this point, you have learned to install OpenCV from the source, and also you can install it from the Ubuntu repository.

Hope you enjoy it. You may be interested in these articles on the Orcacore website:

Set up SFTP Server on Ubuntu 22.04

Install Monit Manager on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!