UbuntuUbuntu Based

How To Install OpenCV on Ubuntu 22.04 LTS

Install OpenCV on Ubuntu 22.04

In this tutorial, we will show you how to install OpenCV on Ubuntu 22.04 LTS. For those of you who didn’t know, OpenCV (Open Source Computer Vision Library) is a powerful and widely used library for computer vision and image processing tasks. It provides a comprehensive set of tools and functions that enable developers to create sophisticated applications in the fields of robotics, augmented reality, object detection, and more. OpenCV supports multiple programming languages, including C++, Python, and Java, making it versatile and accessible to a wide range of users.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the OpenCV on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install OpenCV on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install wget apt-transport-https gnupg2 software-properties-common

Step 2. Installing OpenCV on Ubuntu 22.04.

  • Install OpenCV using Python.

The simplest and quickest way to install OpenCV on Ubuntu 22.04 is by using the packages available in the official Ubuntu repository. While this method is convenient, it’s important to note that the repository may not always contain the latest version of OpenCV. To install OpenCV from the Ubuntu repository, follow these steps:

sudo apt install python3-opencv libopencv-dev

In addition, you can install OpenCV using Pip Python:

sudo apt install python3-pip
pip install opencv-python

Once finished, you can verify the installation by importing the cv2 module in a Python script and printing the OpenCV version:

import cv2
print(cv2.__version__)
  • Install OpenCV Ubuntu from the source code.

Compiling OpenCV from the source code offers several advantages, such as access to the latest features, bug fixes, and the ability to customize the build process by enabling or disabling specific modules. To compile OpenCV, we need to install the necessary build dependencies. Open a terminal and run 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

Next, we’ll download the OpenCV source code from the official GitHub repository. Choose a directory where you want to store the source code and clone the repository using the following commands:

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

 Now, we’ll use CMake to configure the OpenCV build. Create a build directory and navigate to it:

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

Run the following CMake command to configure the build:

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 ..

With the build configured, we can now compile OpenCV using the following command:

make -j$(nproc)

Once the compilation is complete, install OpenCV system-wide using the following command:

sudo make install

To verify that OpenCV is installed correctly, you can check the version in both C++ and Python. Create a new C++ file with the following code:

#include <opencv2/core.hpp>
#include 

int main() {
    std::cout << "OpenCV version: " << CV_VERSION << std::endl;
    return 0;
}

For Python, open a Python shell and run the following commands:

import cv2
print(cv2.__version__)

Congratulations! You have successfully installed OpenCV. Thanks for using this tutorial for installing OpenCV on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official OpenCV website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button