Install OpenCV on Ubuntu Linux

opencvubuntu

Note: This tutorial has been tested on Ubuntu 12.04 LTS with OpenCV 2.4.3.

Install OpenCV on Ubuntu Linux is a bit long but very easy. Simply, follow these steps:

1. UPDATE AND UPGRADE UBUNTU

Open your terminal and execute:

sudo apt-get update

sudo apt-get upgrade

2. INSTALL THE DEPENDENCIES

Now execute:

sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen2-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

3. DOWNLOAD AND DECOMPRESS OPENCV

Enter the OpenCV official website and download the latest version for Ubuntu Linux. Then decompress the downloaded file.

4. COMPILE OPENCV

Now, in your terminal, make sure you are within the OpenCV directory and run the following commands:

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

make

sudo make install

5. CONFIGURE OPENCV

In your terminal, execute:

sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line and save it:

/usr/local/lib

Now, in your terminal, execute:

sudo ldconfig

Again, execute:

sudo gedit /etc/bash.bashrc

Add the following 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, reboot your computer.

6. TRY AN EXAMPLE

To test an example, from your terminal enters in the OpenCV folder. Once you're inside it enters in samples/c.

Now, execute:

chmod +x build_all.sh

./build_all.sh

./facedetect lena.jpg

lenaopencv

52 comments:

  1. Replies
    1. It worked like a charm! Thank you very much.

      Delete
    2. it worked. thanks!

      Delete
    3. This works perfectly, no problems at all. Much thanks! Ubuntu 13.10.

      Delete
    4. Perfect! Thanks a lot! :)

      Delete
  2. Hi,

    Thanks for the tutorial..It was just what i needed to get Opencv installed.

    Great Job!!!

    Cheers

    ReplyDelete
  3. it took me a while, but got it running at last, you blog has help me a lot, Thank you

    ReplyDelete
  4. Nice article. Precise & Lucid !
    Great Work.
    Thanks.

    ReplyDelete
  5. Great!
    this is the best tutorial I ever seen about OpenCV.
    Thanks

    ReplyDelete
  6. at last getting error ... like
    https://www.dropbox.com/s/1yv0x60xz776qu7/Screenshot%20from%202013-09-12%2000%3A59%3A11.png

    ReplyDelete
    Replies
    1. It is because you don't apply correctly the step 5: Configure OpenCV.

      Delete
  7. Very good one...Thanks :-)

    ReplyDelete
  8. Hello, I am having problem with hough transform . can u help? it is giving some error

    ReplyDelete
  9. Hello, very nice manual
    However, in the example part, after ./build_all.sh I get this output

    g++: error: tbb: No such file or directory
    g++: error: rt: No such file or directory
    g++: error: pthread: No such file or directory
    g++: error: m: No such file or directory
    g++: error: dl: No such file or directory

    Can you please inform me about this error?

    ReplyDelete
    Replies
    1. Hi,Have you solved that error ?,I meet the same problem, can you tell me how to do with it?

      Delete
    2. Hi I've solved this problem by doing this
      First locate opencv.pc file, its usually in /usr/lib/../pkg-config/opencv.pc
      goto last but one line and it should be like this

      tbb rt pthread m dl

      Need to change this like below

      -ltbb -lrt -lpthread -lm -ldl

      Now try compiling it should work fine
      got this solution from some question in stackoverflow

      Delete
  10. I am getting the error of which Anonymous is getting up there ^ ?? Thanks in advance.

    ReplyDelete
    Replies
    1. Hi

      You guys should download opencv again, and make sure you compile with in the opencv directory i.e step 4

      Delete
  11. thank you!!! literally spent months trying to get it to work

    ReplyDelete
  12. hi all ,
    my name is hagai,
    I have run this steps ./facedetect lena.jpg
    i got this massage


    Illegal instruction (core dumped)
    can anyone help?

    ReplyDelete
  13. Thanks for this! saved me some time!

    ReplyDelete
  14. Thanks man... one of the best and easy way to install...i have some doubts related to the subject. Is there any blog of yours related to the opencv and opencv related programs.If it is there can you tell?

    ReplyDelete
  15. do we need any prerequisites before starting with the first step?
    coz wen i tried the step 2 it showed no such entry
    please help me

    ReplyDelete
  16. Thank you so much!!!!!

    ReplyDelete
  17. After I try to execute step 2 (installing the dependencies), I get the following error:

    Package 'libtbb-dev' has no installation candidate
    Unable to locate package libfaac-dev

    Are there some other dependencies that I need to install beforehand? Is the order of the dependencies incorrect?

    ReplyDelete
  18. thank you! works perfectly! saved me a lot of time!

    ReplyDelete
  19. Hi, I am having a problem when it comes to step 4 when i execute the command 'make'

    it gives me this error

    Linking CXX executable ../../bin/opencv_perf_gpu
    /usr/bin/ld: ../../lib/libopencv_highgui.so.2.4.3: don't know how to handle section ` ' [0x 2372]
    ../../lib/libopencv_highgui.so.2.4.3: could not read symbols: File in wrong format
    collect2: ld returned 1 exit status
    make[2]: *** [bin/opencv_perf_gpu] Error 1
    make[1]: *** [modules/gpu/CMakeFiles/opencv_perf_gpu.dor/all] Error 2
    make:: *** [all] Error 2

    i hope that you can help me resolve this problem as I need to rush with the completion of my project and opencv is a very important aspect of it, thanks so much for ur reply

    ReplyDelete
  20. To install OpenCV using the terminal on Ubuntu:
    $ su -
    # apt-get update
    # apt-get install build-essential
    # apt-get install libavformat-dev
    # apt-get install x264 v4l-utils ffmpeg
    # apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

    http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

    ReplyDelete
    Replies
    1. can this be the same for raspbian on raspberry pi too? thanks

      Delete
  21. while running samples/c file I m getting fatal error of file not found or directory not found can some one solve this problem

    ReplyDelete
  22. Thanks a bunch! I've been trying to install OpenCV on Win8 for the past four months but with no success. Now I'm on Ubuntu 13.10 and this really worked for me!

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Thanks, I am also installed Opencv 2.4.8 in Ubuntu 13.10 Its worked......

      Delete
  24. THANK YOU VERY MUCH

    ReplyDelete
  25. Superb blog....Follow every word as it is and your are done

    ReplyDelete
  26. Thank you so much for your tutorial. This helped me a lot. perfect blog for openCV beginners.

    ReplyDelete
  27. help! I have this message error before passage 5:
    CMake Error at CMakeLists.txt:62 (include):
    include could not find load file:

    cmake/OpenCVUtils.cmake


    CMake Error at CMakeLists.txt:64 (ocv_clear_vars):
    Unknown CMake command "ocv_clear_vars".


    -- Configuring incomplete, errors occurred!
    what can I do?

    ReplyDelete
  28. When i try and run step 5, i get a permission denied error. any ideas?

    ReplyDelete
    Replies
    1. you need to enter sudo before the command, and it will work.

      Delete
  29. TY for this tutorial, simple, precise and helpful !

    ReplyDelete
  30. Hi, can I delete opencv folder after these steps?
    I'm using opencv with java, and cmake created both build/bin/opencv-249.jar and build/lib/libopencv_java249.so , but make didn't installed them in usr/local/... , or at least I coulnd't find them.
    I don't know where to copy theese files, nor if I have to copy more files in order to safe delete originally decompressed opencv directory.
    Thank you very much.

    ReplyDelete
  31. It worked thank u so much.......................... Great........

    ReplyDelete
  32. after i install openCV,can i use IDE such as Eclipse?

    ReplyDelete
    Replies
    1. Yes you can, I use Eclipse CDT (C++ IDE). If you have eclipse you can add cdt plugin, or you can download Eclipse with CDT. Here are some tutorials
      http://docs.opencv.org/2.4.9/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html#table-of-content-introduction

      Delete
  33. sir i want to create a app that uses opencv to play virtual piano through gesture can u help me to do it plz ....sir

    ReplyDelete