Share your love
How To Install OpenCV on Debian 11

In this article, we want to teach you How To Install OpenCV on Debian 11.
OpenCV is an open-source computer programming library developed to support applications that use computer vision.
How To Install OpenCV on Debian 11
Before you start to install OpenCV on your server, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article about the Initial Server Setup with Debian 11.
The easiest way to install OpenCV on Debian is to install it using the apt package management tool. But you can install the latest stable version of OpenCV from the source.
In this guide, we will show both ways.
Install OpenCV from the Source
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 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
Now create a directory for OpenCV on Debian 11 and switch to it with the following command:
mkdir ~/opencv_build && cd ~/opencv_build
Next, you need to clone the OpenCV and OpenCV contrib repositories with the following commands:
$git clone https://github.com/opencv/opencv.git
$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:
$cd ~/opencv_build/opencv
$mkdir build && cd build
Here you need to set up OpenCV build with CMake on Debian 11:
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 ..
In your output you will see:
Output
-- 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:
Output
...
[100%] Built target example_tutorial_periodic_noise_removing_filter
[100%] Linking CXX executable ../../bin/example_tutorial_cornerSubPix_Demo
[100%] Built target example_tutorial_cornerSubPix_Demo
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:
pkg-config --modversion opencv4
Output
4.5.4
Or you can use:
python3 -c "import cv2; print(cv2.__version__)"
That’s it you have successfully installed OpenCV on Debian 11.
Install OpenCV from the Debian Repository
The OpenCV package is available on the default Debian repository. First, update your local package index with the following command:
sudo apt update
Then, install the OpenCV Python module with the following command:
sudo apt install python3-opencv
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__)"
Conclusion
At this point, you learn to install OpenCV from the source, and also you can install it from the Debian repository.
Hope you enjoy it.
May this article about How To install OpenCV on Centos 7 be useful for you.