In this guide, you will learn 4 different ways to install OpenCV on Debian 12 Bookworm. OpenCV which means Open Source Computer Vision Library is a software library that has many algorithms for computer vision and machine learning projects.
This tutorial provides you with different ways to install OpenCV on Debian 12 which are:
- Debian APT Repository
- Build it from the source
- Use Anaconda
- Use Python Pip
4 Ways To Install OpenCV on Debian 12 Bookworm
You must have access to your server as a non-root user with sudo privileges before you start your OpenCV installation. For this purpose, you can visit this guide on Initial Server Setup with Debian 12 Bookworm.
Now proceed to the following steps to select your desired way and complete this guide.
Number 1 – Install OpenCV from Debian 12 Repository
You can easily install OpenCV via the Debian 12 APT repository in this step. First, run the system update with the command below:
sudo apt update
Then, use the following command to install the libopencv-dev package on Debian 12:
sudo apt install libopencv-dev
This command will install the latest OpenCV package that is available on your server.
Now you can verify your OpenCV installation by using the following command:
dpkg -l libopencv-dev
Output Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-==============-=============-============-================================= ii libopencv-dev 4.6.0+dfsg-12 amd64 development files for opencv
As you can see, the OpenCV version available in Debian 12 is 4.6.0.
Number 2 – Install the Latest OpenCV From Source on Debian 12
Those who are looking for the latest version of OpenCV can use this way to build OpenCV from the source. First, run the system update with the command below:
sudo apt update
Then, you must install the required packages and dependencies to build OpenCV from the source. To do this, run the following command:
sudo apt install build-essential cmake git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev openexr libatlas-base-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-dev gfortran -y
Next, create a project directory for OpenCV and switch to the directory with the following commands:
# sudo mkdir ~/opencv_build # sudo cd ~/opencv_build
Now use the following git command to clone OpenCV from the source on Debian 12:
sudo git clone https://github.com/opencv/opencv.git
When you are done, you must clone the OpenCV modules too with the command below:
sudo git clone https://github.com/opencv/opencv_contrib.git
At this point, inside your OpenCV project directory, create a build directory and switch to it with the commands below:
# sudo mkdir -p ~/opencv_build/opencv/build # sudo cd ~/opencv_build/opencv/build
Then, you must configure your OpenCV build process by using the command below:
sudo 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 BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules ..
Now start your build process by using the following make command:
sudo make -j2
The -j2 specifies the cores of the operating system. You can check it by using the nproc command.
Finally, use the command below to install OpenCV from the source on Debian 12:
sudo make install
When it is completed, verify your installation with the command below:
pkg-config --modversion opencv4
Number 3 – OpenCV installation with Anaconda on Debian 12
If you plan to use Anaconda to start your OpenCV installation, proceed to the following steps.
First, you must have Anaconda installed and activated on your server. To do this, you can visit this guide on Installing Anaconda Python on Debian 12.
Then, you must create a new Python environment with the command below you can use your desired Python version:
conda create -n myenv python=3.11.2
Next, activate your environment with the following conda command:
conda activate myenv
Anaconda has an OpenCV package in its distribution. To install it, you can use the following command on Debian 12 Bookworm:
conda install -c conda-forge opencv
Then, access the Python interactive shell with the command below:
Finally, verify your OpenCV installation with the command below:
import cv2 print(cv2.__version__)
Number 4 – OpenCV installation with Python Pip on Debian 12
One of the ways that you can install OpenCV on your server is to use Python pip. First, install the venv module in Python 3.11 with the command below:
sudo apt install python3.11-venv -y
Then, use the venv module to create a Python environment:
sudo python3 -m venv myenv
Activate your environment with the following command:
sudo source myenv/bin/activate
Your shell prompt will look like this:
Now use the Pip package manager to install OpenCV on Debian 12:
sudo pip3 install opencv-python
Output Collecting opencv-python Downloading opencv_python-18.104.22.168-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (61.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.7/61.7 MB 18.4 MB/s eta 0:00:00 Collecting numpy>=1.21.2 Downloading numpy-1.25.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.2/18.2 MB 45.4 MB/s eta 0:00:00 Installing collected packages: numpy, opencv-python Successfully installed numpy-1.25.2 opencv-python-22.214.171.124
Also, you can install OpenCV modules with the command below:
sudo pip3 install opencv-contrib-python
Output Collecting opencv-contrib-python Downloading opencv_contrib_python-126.96.36.199-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 67.8/67.8 MB 12.2 MB/s eta 0:00:00 Requirement already satisfied: numpy>=1.21.2 in ./myenv/lib/python3.11/site-packages (from opencv-contrib-python) (1.25.2) Installing collected packages: opencv-contrib-python Successfully installed opencv-contrib-python-188.8.131.52
For more information, you can visit OpenCV Docs.
At this point, you have learned 4 Different Ways to Install OpenCV Library on Debian 12 Bookworm. You can choose your desired way and start your installation and enjoy using it.
Hope this was helpful for you. You can comment for us if you need any help.
Also, you may be interested in these articles: