Installing OpenCV on Ubuntu

Since I often reinstall Ubuntu as well as OpenCV, it would be better to note here. Thanks to Dr. Dailey for his tutorial on VGLWiki.

These are the main packages we need to install first.

  • libcv1
  • libcvaux1
  • libcvaux-dev
  • libcv-dev
  • libhighgui1
  • libhighgui-dev
  • opencv-doc (optional)

If you want to use gstreamer, here are the steps:

  1. Install two additional packages: libgstreamer0.10-dev and libdc1394-13-dev.
  2. Download OpenCV from sourceforge and extract to a folder, let's say "opencv".
  3. Change directory to that folder and run this command:
    ./configure --prefix=${HOME}/opencv --without-xine --without-quicktime --with-gstreamer --with-1394libs --with-v4l
  4. make
  5. make install
  6. The last thing is to is to tell the dynamic linker where the DLLs are; so, add the line:
    export LD_LIBRARY_PATH=${HOME}/opencv/lib
    to ~/.bashrc and then relogin.To check if it is working, type: env | grep LD_L Then you should see the result like this:
    LD_LIBRARY_PATH=/home/<your username>/opencv/lib

If you want to use ffmpeg, follow the steps (Thanks to Phong for his guidance):

  1. Install two additional packages: libswscale-dev and ffmpeg.
  2. create a folder /usr/include/ffmpeg.
  3. For Jaunty Jackalope (Ubuntu 9.04), we need to create some soft links under /usr/include/ffmpeg as follows:
    • avcodec.h to ../libavcodec/avcodec.h
    • avformat.h to ../libavformat/avformat.h
    • avio.h to ../libavformat/avio.h
    • avutil.h to ../libavutil/avutil.h
    • swscale.h to ../libswscale/swscale.h
  4. Then run this: ./configure --prefix=${HOME}/opencv --with-ffmpeg --without-quicktime
  5. make
  6. make install
  7. And don't forget to tell the dynamic linker: export LD_LIBRARY_PATH=${HOME}/opencv/lib

For the system-wide installation, change --prefix=${HOME}/opencv to --prefix=/usr/. If you want to remove OpenCV, use the commands: make uninstall or make distclean

Here I also give an example of Makefile to compile with OpenCV (Thanks to Dr. Dailey again for his example).

#What needs to be built (TARG) from what source code (SRC)
SRC = opencv-test.c
TARG = opencv-test

#Tell make what compiler we use
CC = gcc

#Tell gcc about non-standard places to find include (.h) files
#For a system wide installation use -I /usr/include/opencv
INC = -I $(HOME)/opencv/include/opencv

#Also tell gcc to include debug symbols (-g), give all possible warnings
#(-Wall), but don't generate the annoying "unused function" warning
CFLAGS = -g -Wall -Wno-unused-function $(INC)

#Tell the linker where to look for libraries and what's needed for OpenCV
#For a system wide installation -L isn't necessary
LIB = -L $(HOME)/opencv/lib -lcxcore -lcv -lhighgui -lcvaux -lml
LDFLAGS = $(LIB)

#The object files we want are just the source files with .c -> .o
OBJ = $(SRC:.c=.o)

#What make should try to build by default
all: $(TARG)

#What object files the target executable depends on
$(TARG): $(OBJ)

#Clean up executables, object files, and temp files
clean: rm -f *~ *.o $(TARG)

That's it. Enjoy OpenCV!

Author: zkan

Soon to be a newbie data scientist. I ♥ machine learning, computer vision, robotics, image processing, data visualization, and data analytics.

8 thoughts on “Installing OpenCV on Ubuntu”

  1. Thanx for guide...

    I want to do the re-installation for opencv (during previous installation i missed ffmpeg and gtk+ and few other things) I am having trouble removing the installed version of opencv on ubuntu 9.04.
    i tried using $ apt-get remove opencv
    which gave error E: Couldn't find package opencv
    I am able to locate the opencv-doc files but don't know exactly how to remove it.

    apt-cache search opencv gives:-

    libcv-dev - development files for libcv
    libcv1 - computer vision library
    libcvaux-dev - development files for libcvaux
    libcvaux1 - computer vision extension library
    libhighgui-dev - development files for libhighgui
    libhighgui1 - computer vision GUI library
    opencv-doc - OpenCV documentation and examples
    python-opencv - Python bindings for the computer vision library

    hope you will help with this ...tia, kl

  2. @zkan thanks for replying back...but problem persists.
    I tried to find the opencv folder that contains *make* file but couldn't.
    I did fresh installation and it gave some linking error so I removed that using command u mentioned.

    when i do $locate opencv i get following results:

    /usr/bin/opencv-performance
    /usr/include/opencv
    /usr/lib/pkgconfig/opencv.pc
    /usr/local/bin/opencv-createsamples
    ...

    /usr/local/lib/pkgconfig/opencv.pc
    /usr/local/lib/python2.6/site-packages/opencv
    /usr/local/share/opencv
    /usr/local/share/opencv/doc
    /usr/local/share/opencv/haarcascades
    ....

    /usr/local/share/opencv/samples/python/watershed.py
    /usr/share/opencv
    /usr/share/doc/opencv-doc
    /usr/share/doc/opencv-doc/AUTHORS
    /usr/share/doc/opencv-doc/README.Debian
    /usr/share/doc/opencv-doc/examples/c/watershed.cpp
    ....
    /usr/share/man/man1/opencv-createsamples.1.gz
    /usr/share/man/man1/opencv-haartraining.1.gz
    /usr/share/man/man1/opencv-performance.1.gz
    /var/lib/dpkg/info/opencv-doc.list
    /var/lib/dpkg/info/opencv-doc.md5sums

    i tried autoremove libcv and other dependencies but couldn't get rid of these files.

    could you please suggest something in this regard ..or i think system reinstall would be the only way.
    tia- kl

  3. I am using Ubuntu Linux,
    And i am developing OpenCV application in Java using Eclipse editor.
    How to set up OpenCV for Java development in Eclipse?
    Can u help?

  4. Hi, nice howto. Maybe it is a helpful tip to AMD Athlon XP (and below...) users to disable the default SSE2 support when compiling (./configure ... --disable-sse), because the CPU does not support it and you will end up having segfaults else. Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *