Install OpenCV on Ubuntu 22.04 with Easy Steps

In this guide on the Orcacore website, you will learn to Install OpenCV on Ubuntu 22.04 From both Ubuntu Repository and Source. 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 for installing OpenCV Python on Ubuntu 22.04, 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 Python 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 Python:

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 Python 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 https://github.com/opencv/opencv.git
# sudo git clone https://github.com/opencv/opencv_contrib.git

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:

Build OpenCV Python

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:

Build OpenCV Ubuntu 22

Now you can install OpenCV with the following command:

sudo make install

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

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

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

Conclusion

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 also interested in these articles:

Set up SFTP Server on Ubuntu 22.04

Install Monit Manager on Ubuntu 22.04

Install ProFTPD Ubuntu 22.04

Install Chromium Browser Ubuntu 22.04

Okular Document Viewer Install Ubuntu 22.04

Clang LLVM Setup For Ubuntu 22.04

Share your love

Stay informed and not overwhelmed, subscribe now!