Install OpenCV C C++ in Ubuntu 18.04 LTS : Step by Step Guide

Install OpenCV in Ubuntu 18.04

Keywords: OpenCV, Ubuntu

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects etc. To know more about OpenCV visit OpenCV.org

Please follow steps mentioned in this post to install OpenCV in Ubuntu 18.04 (LTS). All the terminal commands are summarized in the end of the post and you can access the same by clicking  here.

Step 1. Update the Ubuntu System Package

Open the terminal and Run command to update Ubuntu system package index.

Run commands one by one.

Step 2. Install Required tools and packages

If you encounter error like below screenshot, then follow the step 2.a

if you encounter error like below screenshot, then follow the step 2.b

Step 2.a Add repository and ppa

Step 2.b Add repository and ppa

Step 3. Download OpenCV Sources using git

We need to clone the OpenCV sources using “git” to build and install it. We will download the source in /opt/ directory. Downloading, building and installation process requires root permission. Execute the commands to proceed further.

Step 4. Build & Install OpenCV

Execute the commands one by one:

Step 5. Check OpenCV version installed

Run the command in terminal

If you encounter error like “pakage opencv not found” then follow step 5.a.

Step 5.a. Find & Set “opencv.pc” file path

Run the command in terminal

Step 6. Compile & Run a Test Program 

Create a directory and put 1 sample image in it. Create a .cpp file and copy and paste the code posted here.

Change the image name & extension (.jpeg/.png etc). Compile and Run the program.

(A) Make a directory and .cpp file

(B)Copy the below code and paste in the file just created

(C) Compile the Code

(D) Execute the Code

Command Summary

This is the summary of all commands needed to install OpenCV in Ubuntu. If you encounter any problem during installation please check the respective Step. You might get the resolution in the post itself as I have also covered the problem & resolution of the errors I faced during the installation.

Please like and share Techawarey.  Find and Like Techawarey on Facebook.😊

Note: Logo of OpenCV & Ubuntu used are the property of OpenCV & Ubuntu respectively.

[Update]: A lot of thanks to Ryton1729 for his commnet & suggestions to improve this post.

Related posts

18 Thoughts to “Install OpenCV C C++ in Ubuntu 18.04 LTS : Step by Step Guide”

  1. Akash

    Had to do step 2a and 2b. Was able to do step 2a but getting error at step 2b. Error message is:
    E: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/main/binary-arm64/Packages 404 Not Found [IP: 91.189.91.39 80]
    E: Some index files failed to download. They have been ignored, or old ones used instead.

  2. Eliseu

    Why do I get this message ?

    terminate called after throwing an instance of ‘cv::Exception’ what(): OpenCV(4.5.3-dev) /opt/opencv/modules/core/src/array.cpp:2494: error: (-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type in function ‘cvGetMat’ Aborted

    I have two cameras, I tried cap(0), cap(1), cap(-1)… When I list my devices it returns /dev/video2 /dev/video3
    So i tried to cap(“/dev/video2”) and tried with video3 too But i get the same error. I
    ‘m compiling it with g++ testeVideo.cpp -o testoutput -std=c++11 pkg-config --cflags --libs opencv
    Thanks

  3. Anonymous

    Thank you, it works!

  4. Anonymous

    root@websdk-backend-79f4d879f6-bz4cj:~/opencv_test# 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
    root@websdk-backend-79f4d879f6-bz4cj:~/opencv_test#

      1. Anonymous

        Thank you for the tutorial! I tried step 5.a after getting the same error but it still does not fix the issue. could youo let me know if there is any other way to fix this?

  5. Anonymous

    your tutorial is the best to install opencv. thanks so much

    1. Daniel

      How do you use in code:blocks? I’ve installed it and it works from console but when I try to run from code:blocks it gives undefined reference to cv. Sorry if this is basics.

      1. Hi Daniel,

        It look likes linker issue. I personally do not use code:block. So can not comment much.
        Please check in code:block settings that it’s have correct path of opencv.

  6. Great ! It worked nicely for me <3

  7. pankaj kushwaha

    Thanks, its working 🙂

    1. Anonymous

      You’re Welcome!!

  8. Superb Walktrough!

    Two-three side steps/error messages you might consider adding/discussing; which i ran into:
    + add in at/before step 2.b: (required if apt-add-repository is missing)
    ”’ $ sudo apt install software-properties-common ”’

    + add in 5.a.0: (required for + apt-file)
    sudo apt install apt-file

    (and make a step for 5a “Create a directory and put 1 sample image in i”, as else it trows an error (obviously)

    But overall, a great and concise overview!

    Kind regards.

    1. Anonymous

      @Ryton1729 .. Thanks a lot for your suggestions.. I have edited the post to include instruction shared by you.

Leave a comment....