How to Install OpenCV in Ubuntu 18.04 LTS for C / C++ (Linux)

In this post we will see How to Install OpenCV in Ubuntu for C/C++
OpenCV is an image processing library created by Intel and later supported by Willow Garage and now maintained by Itseez. OpenCV means Intel® Open Source Computer Vision Library. It is a collection of C functions and a few C++ classes that implement some popular Image Processing and Computer Vision algorithms. OpenCV is Available on Mac, Windows, Linux (Terminal environment).

Step 1 – Updating Ubuntu

Step 2 – Install dependencies

Step 3 –  Get OpenCV

Step 4 – build and install OpenCV

Now to check if OpenCV is installed on a machine, run the following commands

We will get the opencv version installed
in this case it’s 3.2.x

Online Course – OpenCV Python Tutorial For Beginners. At the end of this course, you will have a firm grasp of Computer Vision techniques using OpenCV libraries. This course will be your gateway to the world of data science.

Create a C++ program

Follow the commands

The above command will create a folder called cpp_test and create a main.cpp file inside it
Now place any .jpeg image inside the cpp_test folder.
So Now your cpp_test folder will contain two files as follows
├── sample.jpeg
└── main.cpp

Now open the main.cpp and add the following code

Now compile your code with the following command

Now run the C++ program with the following command

press ESC to exit

Video instruction – How to Install OpenCV in Ubuntu 18.04 LTS for C / C++

Partner Sites

  • Parth

    How to install openCV for c++ on ubuntu 17.10? Kindly guide with some specific tutorial. Thank you for support in advance.

  • Roinoss

    (was bug in make -j4, but i lost connection to internet, so use cmake second time helps)

  • Rider

    opencv installation is really a mess, should be more modular instead of having to hope opencv & contrib have not diverged & are still compatible when it comes to compilation

  • disqus_kf0NYP9mhq

    With linux LTS 18.04 and Openc cv4 I had that error while runing the command pkg-config –modversion opencv

    Package opencv was not found in the pkg-config search path.

    Perhaps you should add the directory containing `opencv.pc'

    to the PKG_CONFIG_PATH environment variable

    No package 'opencv' found

    I was able to fix it using that post :

    I create the opencv.pc file and change it a bit to run the example:





    Name: opencv

    Description: The opencv library

    Version: 4.0.1

    Cflags: -I${includedir}/opencv -I${includedir}/opencv2 -I${includedir}/opencv4

    Libs: -L${libdir} -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_imgcodecs

    I think other the libs may be required.

    I there a better way to solve that package error ?

    • Tarun Kumar Tella

      Try installing this: sudo apt-get install libopencv-dev

  • Edoardo Guardigli

    Hi, i have some trouble with this string:
    sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev
    I dont know why, maybe becose i am new of this environment, but terminals says me this:
    The following packages have unmet dependencies:
    python3.5-dev: Depends: python3.5 (= 3.5.2-2ubuntu0 ~ 16.04.5) but it’s not going to be installed
    Depends: libpython3.5-dev (= 3.5.2-2ubuntu0 ~ 16.04.5) but it’s not going to be installed
    Depends: libpython3.5 (= 3.5.2-2ubuntu0 ~ 16.04.5) but it’s not going to be installed
    E: Unable to correct problems, there are damaged packages blocked.

    So what do i have to do?
    Thanks so much for the availability.

  • The Gravel

    I failed to install /opt/opencv/release$ make -j4 when 99%. What should I do?

  • Tarun Kumar Tella

    If the final version is not showing up, try installing this: sudo apt-get install libopencv-dev.
    It worked for me 🙂