Installing OpenCV on Debian Linux


: 16

OpenCV_DebianIn this post I will describe the process of installing OpenCV (both versions 2.4.2 and 2.4.3) on Debian Linux (especially Debian-6). After installing, we will do some tests to verify the installation and also see some examples. As I was trying to install OpenCV on Debian I found that although there is lot of information on similar topics (see the references at the end), I still had to dig around a bit to understand the process completely. Some of the excellent guides are now outdated. Also, I found a few that have all the “what to do” but not “why to do”. Then as I started jotting down my own notes into my OneNote notebook, I realized that I should share these notes with others who are interested. I hope to update this document as I find more useful information in future.

It is recommended [Reference 2] that you update and upgrade your current system before installing OpenCV. You can do this from the terminal using the following two commands:

sudo apt-get update
sudo apt-get upgrade

The following packages may be installed using the terminal [sudo apt-get install package-name] or using the synaptic package manager. The first 3 packages are NOT optional and must be installed. The commands are shown after the names of the packages:

2.1. The first 3 packages are required and not optional:

  1. build-essential (GCC 4.4.x or higher C/C++ compiler )
  2. cmake 2.6 or higher (control software compilation)
  3. pkg-config (manage compile and link flags for libraries, used at the configuration stage)
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install pkg-config

2.2. The following packages are optional:

  1. libgtk2.0-dev (development files for GTK+ graphical user interfaces library. This is the default GUI backend for highgui on OSes other than Windows and MacOSX)
  2. python-dev (Python 2.6 or later with developer packages)
  3. python-numpy (Numpy)

Use the following line of code to install all the 3 packages simultaneously.

sudo apt-get install libgtk2.0-dev python-dev python-numpy

The packages below are used to read and write images in the respective formats (note, that if you don’t install these packages, OpenCV will use the default versions supplied with OpenCV. Also, openexr is not supplied, by default, with OpenCV)

  1. libpng12-0,  libpng12-dev, libpng++-dev, libpng3 (libpng library including development and runtime files)
  2. libpnglite-dev, libpngwriter0-dev, libpngwriter0c2
  3. zlib1g-dbg, zlib1g, zlib1g-dev
  4. pngtools
  5. libjasper1, libjasper-dev, libjasper-runtime
  6. libjpeg8, libjpeg8-dbg, libjpeg62, libjpeg62-dev,  libjpeg-progs (NOTE: The reason of installing libjpeg62-dev development package instead of libjpeg8-dev is that libtiff4-dev depends on libjpeg62-dev and libjpeg8-dev conflicts with libjpeg62-dev)
  7. libtiff4, libtiff4-dev, libtiffxx0c2, libtiff-tools
  8. ffmpeg, libavcodec-dev, libavcodec52, libavformat52, libavformat-dev (ffmpeg multimedia player, server, encoder, codec library, demuxer library. For more information see FFMPEG)
  9. libswscale0, libswscale-dev (ffmpeg video scaling library)
  10. openexr,libopenexr-dev, libopenexr6 (HDR image file development tools)

Use the following line of code to install all the packages mentioned above.

sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev libpngwriter0-dev libpngwriter0c2 zlib1g-dbg zlib1g zlib1g-dev pngtools libjasper-dev libjasper-runtime libjasper1 libjpeg8 libjpeg8-dbg libjpeg62 libjpeg62-dev libjpeg-progs libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev libswscale0 libswscale-dev openexr libopenexr6 libopenexr-dev

The packages below are used to add video capturing, video decoding and video encoding capabilities to highgui. The output of the cmake will show which of the packages have been detected and will be used in highgui.

  1. libgstreamer0.10-0-dbg, libgstreamer0.10-0,  libgstreamer0.10-dev, libgstreamer-plugins-base0.10-dev (gstreamer streaming media framework)
  2. libxine1-bin, libxine-dev, libxine1-ffmpeg (xine video/media player and core libraries)
  3. libunicap2, libunicap2-dev, libucil2, libucil2-dev (uniform interface to video capture devices)
  4. libdc1394-22-dev, libdc1394-22, libdc1394-utils (high-level programming interface for IEEE1394 digital cameras)
  5. libv4l-0, libv4l-dev (video for linux support libraries)

Use the following line of code to install the packages used for video processing.

sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libucil2 libucil2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils libv4l-0 libv4l-dev

2.3. Other optional packages:

  1. swig (integrate C and C++ code with other languages like Python)
  2. bzip2 (this usually comes along with the Debian distributions)

You can also install other optional packages (depending on your needs) such as

  • libtbb-dev : parallelism library (TBB) for C++ – development files
  • libqt4-dev and libqt4-opengl-dev : Qt 4 development files and Qt 4 OpenGL library development files. To use Qt (Qt 4.6 or later) as the HighGUI backend (instead of GTK). In such case, use WITH_QT=ON flag with Cmake when configuring OpenCV.
  • texlive-latex-extra and python-sphinx: LaTeX supplementary packages (TeX Live) and sphinx tool for producing documentation.
  • libeigen2-dev : lightweight C++ template library for linear algebra
  • yasm : modular assembler with multiple syntaxes support
  • CUDA Toolkit with GPU support for Nvidia GPUs.
  • IPP 7 or later — OpenCV doesnot require IPP, but can be configured to use IPP to make color conversion, Haar training and DFT functions run faster.

For the purpose of this guide, I assume that your current working directory is “opencv”. This is where you are going to download the source code.

At the time of this writing, the latest stable release is OpenCV-2.4.2. You may download the source tarball (OpenCV-2.4.2.tar.bz2) to your current working directory (opencv) from SourceForge  either by using the browser or by using the following command in current working directory:


Note: If you try to build the above version of OpenCV on Debian 6 using the standard instructions that are available, you will most likely get some build errors (explained later at the appropriate place) due to some errors in the ZLIB library that comes along with OpenCV. Resolving this error is very simple. Also, the said error has been fixed in the latest 2.4.3 release of OpenCV, i.e. OpenCV-2.4.3.tar.bz2 which you can download from here. Also, you may download it using the command


Once you have downloaded the tarball, extract it using the command

tar xvjf OpenCV-2.4.2.tar.bz2

if you have downloaded 2.4.2, or

tar xvjf OpenCV-2.4.3.tar.bz2

if you have downloaded 2.4.3rc. The above command will extract the source code into a directory called “OpenCV-2.4.2” or “OpenCV-2.4.3” respectively.

5.1. “cd” to OpenCV-2.4.2 (or OpenCV-2.4.3) directory.

cd ~/opencv/OpenCV-2.4.2 (or cd ~/opencv/OpenCV-2.4.3)

5.2. Create a directory called “release” within the above directory and “cd” to the release directory.

mkdir release
cd release

5.3. Configure OpenCV build by running Cmake

This step generates a makefile (to be used by make later) that defines which parts of OpenCV needs to be compiled.

The syntax for the cmake commad is:

cmake [<some optional parameters>] <path to the OpenCV source directory>

If you are configuring OpenCV-2.4.2 and you want to install OpenCV with Python support, you can use the following command:


Note: The switch that must be turned ON in order to avoid build errors using the OpenCV-2.4.2 is the “BUILD_ZLIB=ON” option.

The following command is what I used (to configure OpenCV with Python, OpenEXR, Gstreamer, video capture and v4l video libraries:


Note: In the above command, “~/opencv/OpenCV-2.4.2” has been replaced with just “..” (two dots) at the end of the command. It is an argument to cmake to indicate that the source directory is the parent directory (of the current “release” directory).

If you are configuring OpenCV-2.4.3, you don’t need to use the “BUILD_ZLIB=ON” switch:


You should expect to see something similar to the following at the end of a successful configuration step:

"Cmake configuration"

Portion of the CMake configuration output step.

Generally, you should not see any errors in the above step. If you do, make sure that you have installed all the required packages. Also ensure that you have the following flags in the output of the CMake configuration step set to “YES”:

  • GTK+ 2.x:   YES

If you are interested in working with video, ensure that the following are also set to “YES”:

  • GStreamer:  YES
  • V4L/V4L2: YES

5.4. Compile using GCC from the command line (within the “release” directory) by running “make”:


This is where you may see an “error” if you did not use “BUILD_ZLIB=ON” option during the configuration using cmake for OpenCV-2.4.2.  (If you are interested to know more about this error you can see here.)

From the command line (within the “release” directory), execute the following command

sudo make install

Note: It is NOT necessary to “install” OpenCV if you are using CMake (and, therefore, OpenCVConfig.cmake) for your projects. In fact it is not recommended to actually install OpenCV using “make install” especially if you plan to use different OpenCV versions. If you don’t install, then use OpenCV from where you built it [Reference 4].

7.1. Configure the system wide library search path:

The path to the OpenCV libraries (/usr/local/lib) is specified by creating a file called “opencv.conf” in “/etc/”. In order to do so, append the line “/usr/local/lib” at the end of the file “opencv.conf” (the file could be empty) using the following command, and as shown in the following figure, and then save it:

sudo gedit /etc/
"OpenCV search path configuration"

Configuration of OpenCV search path

Now, to configure the library, run the following command:

sudo ldconfig -v

In this case, running the ldconfig command creates the necessary links and cache to the most recent shared libraries found (OpenCV libraries in our case)  in the directories specified in the file /etc/

Note: (alternatively you could do the following)

(I haven’t verified this step) Instead of creating the file “opencv.conf”, modifying it, and then running ldconfig, you could also do the following [Reference 3]:

Export the path to LD_LIBRARY_PATH environment variable, and dynamically link to the OpenCV library using ldconfig [Reference 3] (do this ONLY if you didn’t do sudo gedit /etc/ followed by ldconfig)

export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
sudo ldconfig

Also, should you choose to avoid installing OpenCV library, you need to explicitly direct your system to the OpenCV library build directory by executing the following commands (assuming that you built OpenCV in the directory “~/opencv/OpenCV-2.4.3/release/”):

export LD_LIBRARY_PATH=~/opencv/OpenCV-2.0.3/release/:$LD_LIBRARY_PATH
sudo ldconfig

7.2. Setup the PKG_CONFIG_PATH variable

Now open the “bash.bashrc” file using gedit as follows:

sudo gedit /etc/bash.bashrc

Add the following two lines to the “bash.bashrc” file and save the file:

"bash.bashrc file"

bash.bashrc file with the two added lines

That brings us to the end of the OpenCV installation on Debian-6. Before, testing the installation, it is highly recommended, that you restart the computer.

 8.1. Test C samples:

We shall test the OpenCV installation by building  and running the OpenCV samples:

cd ~/opencv/OpenCV-2.4.3/samples/c

We would like to build all the examples in the directory by using the script “”. However, before doing so, we need to ensure that the script is executable.

chmod +x

Now we will try some examples:

First one, is going to be the “delaunay” example. Run the example using the following command in the terminal (assuming that we are in the same directory)




Output of the sample code delaunay.c

Second, let’s try the “morphology” example:




Output of the sample morphologyc.c

See some more examples in References 2 & 3.

8.2. Compiling our own simple c program:

Before compiling using pkg-config, test the following commands:

pkg-config --cflags opencv
pkg-config --libs opencv

The output of the first command should be:

-I/usr/local/include/opencv -I/usr/local/include

And the output should be something similar to the following for the second command:

/usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/

Our simple C code:

This is a very simple c code to display an image:

/* first.c :Code to display an image using OpenCV */
#include "opencv2/highgui/highgui.hpp"
int main(int argc, char** argv){

// load the image
  IplImage* img = cvLoadImage(argv[1],-1);
// create a window to display the image
// display the image
// wait for keystroke
// release the image object
// destroy the window

I had placed the code in the directory ~/opencv/mycprogs/basic/

The code was compiled using the following command:

gcc `pkg-config --cflags --libs opencv` -o first1 first.c

Note for new users: Please be aware that there are backquote (`) symbols (generally found on the same U.S. computer keyboard key as the tilde) in the above command and not single-quote (‘) or apostrophe symbol! Using a single-quote will mostly spit the following error:

"gcc: pkg-config --cflags --libs opencv: No such file or directory"

On successful compilation, you can run the program as shown below:

./first1 lena.jpg

You should expect to see the following output:


Output of our simple program to display an image

Note: Using pkg-config to get all dependencies to compile ensures that you will be able to compile almost all code written using the OpenCV libraries. However, it my produce a binary with excessive dependencies [Reference 3].  As it is shown in [Reference 3], you can see a list of the OpenCV based share libraries which are loaded dynamically if the above code is compiled using the pkg-config by running the following command:

gcc `pkg-config --cflags --libs opencv` -o first1 first.c
ldd first1 | grep local

Output of the above command: => /usr/local/lib/ (0xb772e000) => /usr/local/lib/ (0xb7657000) => /usr/local/lib/ (0xb7455000) => /usr/local/lib/ (0xb73b7000) => /usr/local/lib/ (0xb734d000) => /usr/local/lib/ (0xb72fd000) => /usr/local/lib/ (0xb72b1000) => /usr/local/lib/ (0xb703c000) => /usr/local/lib/ (0xb6f26000) => /usr/local/lib/ (0xb6eaf000) => /usr/local/lib/ (0xb6e99000) => /usr/local/lib/ (0xb6e22000) => /usr/local/lib/ (0xb6e06000) => /usr/local/lib/ (0xb6d8d000) => /usr/local/lib/ (0xb6cf7000) => /usr/local/lib/ (0xb6cb7000) => /usr/local/lib/ (0xb6c84000)

As you can see all the opencv libraries (including the ones that we really don’t require for this program) have been linked. As a result of this the executable file size is unnecessarily large. Although, it is not that critical for desktop based applications, it is important to be aware of this, especially if would like to use your code for some embedded application.

The above is equivalent to compiling the code by manually providing all the libraries, like so:

gcc -o first1 first.c&nbsp; -I/usr/local/include/opencv -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -lopencv_gpu -lopencv_nonfree -lopencv_photo -lopencv_stitching -lopencv_ts -lopencv_videostab

You could also compile the code by manually providing the just the needed libraries as follows:

gcc -o first first3.c -I/usr/local/include/opencv -lopencv_highgui

Finally, if we compare the file sizes of the executable(s) produced by the above 3 methods, we will find that the size of the executable produced by handpicking the libraries is the least (Well, no surprise there). Note that I have named the output files as first1, first2 and first3 respectively.

"first.c comp"

Comparison of file sizes for the different type of compilation methods.

8.3. Test Python samples:

Change directory to ~/opencv/OpenCV-2.4.3/samples/python/ and then run


Output of python sample

(Note: You need to press “ESC” button to stop the loop and exit the convexhull program.)

Now, lets write a small python code, and call it

# Import the CV module
import as cv
# Load the image
img = cv.LoadImage('baboon.jpg',cv.CV_LOAD_IMAGE_COLOR)
# Create a named window
# Display the image
# Wait for keystroke

Before running the code, you need to first make this script an executable using the following command:

chmod 755

Also, make sure you have some image (in this case it is baboon.jpg) in the same directory. Then, from the terminal you can use either of the following two ways to run the script:




(both commands should work)

The output of the above script is shown below:


Output of

8.3. Trouble shoot python related problems:

Try to “import” the cv module within python as shown below in the python interactive mode:

"python test"

Try to import OpenCV modules

According to [Reference 6], python may return an error like “No module named cv” because python module is installed in /usr/local/lib/python2.6/site-packages, but on Debian (and on Ubuntu), Python only looks in /usr/local/python2.6/dist-packages. The following block-quote is from [Reference 6]

 You can fix it using three ways (Use only one of those, the first is the best):

  • move the file from the site-packages to the dist-packages :
sudo mv /usr/local/lib/python2.6/site-packages/ /usr/local/lib/python2.6/dist-packages/
  • add to /usr/local/lib/python2.6/dist-packages os.path in python :
>>>import sys
>>>print sys.path
  • Using bash : export $ PYTHONPATH=/usr/local/lib/python2.6/site-package. Add this line to your ~/.bashrc if you want it to be permanent.

Also, in case, you chose not to install OpenCV, then put and (called cv.pyd and cv2.pyd on Windows) to opencv/samples/python directory, or rather add the directory containing to PYTHONPATH [Reference 4], e.g.

export PYTHONPATH=~/projects/opencv/release/lib:$PYTHONPATH

In case, there is still some problem running python code, then please refer to the section “Making Python work” in [Reference 6].

That brings us to the end of this post. I hope you will find this guide helpful.

  1. “Installation in Linux”, OpenCV v2.4.3 documentation. Link:
  2. “Installing OpenCV 2.4.1 in Ubuntu 12.04 LTS”, Sabastian Montabone, June 24, 2012. Link:
  3. “Introduction to Computer Vision with the OpenCV Library on Linux”, Lubos Rendek, January 2011. Link:
  4. “OpenCV installation Guide”, Link:
  5. “Installing OpenCV on Linux”, Link:
  6. “OpenCV Installation Guide on Debian and Ubuntu”, Link:
  7. “A Comprehensive Guide to Installing and Configuring OpenCV 2.4.2 on Ubuntu,” by  Osman Eralp, June 6, 2012. Link:
  8. “How to compile OpenCV based programs in Linux,” Link: