Filled Under: , , , ,

How to install Opencv on Ubuntu Desktop/Ubuntu Server

To install OpenCV on your system run the following commands in terminal. You should have Ubuntu/Server 12 or 14 LTS to install it.


  • sudo apt-get update
  • sudo apt-get upgrade
  • sudo apt-get install libtiff5-dev
  • sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff5-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev
You can change OpenCV version in following command, just go to link and change the version in following OpenCV URL, like I'm using 2.4.11 here.
  • cd~
    wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.11/opencv-2.4.11.zip
    unzip opencv-2.4.11.zip
    cd opencv-2.4.11
  • mkdir build
    cd build
    cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON ..
Now, you are ready to compile and install OpenCV 2.4.11:

  • make
  • sudo make install
Now, you have to configure OpenCV first. Open the opencv.conf file with the following command if you are using Ubuntu desktop

  • sudo gedit /etc/ld.so.conf.d/opencv.conf
If you are using Ubuntu Server then open file with following command

  • sudo nano /etc/ld.so.conf.d/opencv.conf
Add following lines at the end of the file (it may be an empty file, that is ok) and then save it:

  • /usr/local/lib
To save file use CTRL+X then press y then hit enter.
Run the following command to configure the library

  • sudo ldconfig
Now you have to open another file:

  • if you are using Ubuntu Desktop then use this command
    • sudo gedit /etc/bash.bashrc
  • if you are using Ubuntu Server then use this command
    • sudo nano /etc/bash.bashrc
Add these two lines at the end of the file and save it:

  • PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    export PKG_CONFIG_PATH
Finally, close the terminal and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

To see where opencv installed use following command

  • pkg-config --cflags opencv
To see lib files directory use following command

  • pkg-config --libs opencv
Let run simple c++ code with opencv code using g++ compiler. Create new file and named it as test.cpp and add following code in it

  • #include "stdfix.h"#include <iostream>
    #include <opencv/cv.h>
    #include <opencv/highgui.h>
    #include <opencv2/core.hpp>

    #include <opencv2/opencv.hpp>

    #include <opencv2/imgproc/imgproc.hpp>

    #include <opencv2/highgui/highgui.hpp>


    using namespace std;
    using namespace cv;

    int main()
    {
            Mat image;
            image  = cv::imread("/home/test.png");
            namedWindow("Display window", 1);
            imshow("Display window",image);
            waitKey(0);
            return 0;
    }
Save the file, open terminal cd to directory where you save test.cpp file and write following command in terminal

  • g++ `pkg-config --cflags opencv` text.cpp `pkg-config --libs opencv` -o test
Your file is build with opencv, just write following command to run test file


  • ./test
Thanks :)

0 comments:

Post a Comment

Any suggesstion or issue please comment here