UbuntuUbuntu Based

How To Install OpenCV on Ubuntu 24.04 LTS

Install OpenCV on Ubuntu 24.04

In this tutorial, we will show you how to install OpenCV on Ubuntu 24.04 LTS. OpenCV (Open Source Computer Vision Library) is a powerful and widely used library for computer vision and machine learning tasks. It provides a comprehensive set of tools and functions that enable developers to create sophisticated applications in the fields of image and video processing, object detection, and pattern recognition.

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 24.04 (Noble Numbat). 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 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).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
  • An Ubuntu 24.04 system with root access or a user with sudo privileges.

Install OpenCV on Ubuntu 24.04 LTS Noble Numbat

Step 1. Updating the Package Repository.

Before installing any package, it’s a good practice to update the package lists to ensure you have access to the latest available versions. Open a terminal and run the following command:

sudo apt update

This command will fetch the latest package information from the Ubuntu repositories, allowing you to install the most recent version of OpenCV and its dependencies. Updating the package repository is crucial to maintaining the security and stability of your system.

Step 2. Installing Dependencies.

Next, install the necessary dependencies for OpenCV:

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 3. Installing OpenCV on Ubuntu 24.04.

  • Method 1: Installing OpenCV from the Ubuntu Repository

The simplest way to install OpenCV on Ubuntu 24.04 is by using the pre-built packages available in the Ubuntu repository. To do this, run the following command:

sudo apt install libopencv-dev python3-opencv

This command will install OpenCV along with its Python bindings. To verify the installation, open a Python interpreter and try importing the OpenCV module:

python3
>>> import cv2
>>> cv2.__version__
'4.5.2'

If the import succeeds and the version number is displayed, OpenCV has been successfully installed.

  • Method 2: Building OpenCV from Source

Building OpenCV from source provides greater flexibility and allows you to optimize the library for your specific needs. Follow these steps to build OpenCV from source on Ubuntu 24.04:

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

Create a build directory and navigate to it:

cd opencv
mkdir build
cd build

Configure the build using CMake:

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_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

Compile OpenCV:

make -j$(nproc)

Install OpenCV:

sudo make install

Add OpenCV to the system path:

echo '/usr/local/lib' | sudo tee /etc/ld.so.conf.d/opencv.conf
sudo ldconfig

To verify that OpenCV has been installed correctly, create a simple Python script that uses OpenCV to load and display an image:

import cv2

img = cv2.imread('image.jpg')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

If the script runs without errors and displays the image, OpenCV is working as expected.

Congratulations! You have successfully installed OpenCV. Thanks for using this tutorial for installing the OpenCV on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the 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