How to Install OpenCV on Debian 10 Linux

By 

Published on

4 min read

Install OpenCV on Debian 10

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library and has bindings for C++, Python, and Java. It is used for a vast range of applications, including medical image analysis, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and much more.

OpenCV can take advantage of multi-core processing and features GPU acceleration for real-time operation.

This tutorial, shows how to install OpenCV on Debian 10, Buster. For most people, the easiest way to install OpenCV on Debian is to install it using the apt package management tool. If you want to install the latest stable version of OpenCV from source, scroll down to the Installing OpenCV from the Source section of this tutorial.

Choose one of the installation options that works best for you.

Install OpenCV from the Debian Repository

The OpenCV Python module is available from the standard Debian repository. At the time of writing, the standard Debian repositories include OpenCV version 3.2, which is outdated.

To install OpenCV Python module, enter:

Terminal
sudo apt update
sudo apt install python3-opencv

The command above will install all packages necessary to run OpenCV.

To verify the installation, import the cv2 module and print the OpenCV version:

sh
python3 -c "import cv2; print(cv2.__version__)"
output
3.2.0

If you want to install OpenCV with Python 2 bindings, install the python-opencv package.

Installing OpenCV from the Source

Building the OpenCV library from source is the recommended way of installing OpenCV. It will be optimized for your particular system, and you will have complete control over the build options.

To install the latest OpenCV version from the source, perform the following steps:

  1. Install the required and optional dependencies:

    Terminal
    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
  2. Clone the OpenCV’s and OpenCV contrib repositories with the following commands:

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

    At the time of writing, the default version in the github repositories is version 4.2.0. If you want to install an older version of OpenCV, cd to both opencv and opencv_contrib directories and run git checkout <opencv-version>

  3. Once the download is completed, create a temporary build directory, and navigate to it:

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

    Set up the OpenCV build with CMake:

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

    When the CMake build system is finalized, you will see something like below:

    output
    ...
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/pi/opencv_build/opencv/build
  4. Start the compilation process:

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

    The compilation may take several minutes or more, depending on your system configuration. Once done, you will see something like below:

    output
    ...
    [100%] Linking CXX executable ../../bin/example_tutorial_imgcodecs_imwrite
    [100%] Built target example_tutorial_goodFeaturesToTrack_Demo
    [100%] Built target example_tutorial_imgcodecs_imwrite
  5. Install OpenCV by typing:

    Terminal
    sudo make install
    output
    ...
    -- Installing: /usr/local/share/opencv4/samples/python/video_threaded.py
    -- Installing: /usr/local/share/opencv4/samples/python/video_v4l2.py
    -- Installing: /usr/local/share/opencv4/samples/python/watershed.py
  6. To check whether OpenCV has been installed successfully, enter the following command and you should see the OpenCV version:

    Terminal
    pkg-config --modversion opencv4
    output
    4.2.0
    sh
    python3 -c "import cv2; print(cv2.__version__)"
    output
    4.2.0-dev

Conclusion

We have shown you two different ways to install OpenCV on Debian 10. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu repository is easier, building OpenCV from source gives you more flexibility, and it should be your first option when installing OpenCV.

If you have any questions or feedback, feel free to comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page