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

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

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

  1. pankaj kushwaha

    Thanks, its working 🙂

    1. Anonymous

      You’re Welcome!!

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