How to install OpenCV in Ubuntu?

OpenCV is an open-source computer vision library that was initially launched in 1999. It uses GPU acceleration for real-time operations. It is written in C++ but it supports other programming languages including Python, Java, MATLAB, etc.

The OpenCV has a wide variety of applications some of them are –

  • Facial recognition by analyzing image
  • Gesture recognition
  • Street view image stitching.
  • Automated inspection and surveillance.
  • Mobile robotics and driver-less car navigation and control.
  • Medical image analysis.
  • Video/image search and retrieval.
  • Movies – 3D structure from motion.
  • Augmented reality
  • Interactive art installations etc.

In this article, I will discuss how to install OpenCV in Ubuntu.

How to install OpenCV in Ubuntu

There are multiple ways to install OpenCV in a Ubuntu system.

Installing OpenCV from Ubuntu repository

The easiest way to install OpenCV is to download and install it from the Ubuntu repository but you may get an older version of this software.

To install OpenCV first update the local package database in Ubuntu by using –

sudo apt update

Now use the given command to install the Open CV –

sudo apt install libopencv-dev python3-opencv -y

This will download the required packages and install the OpenCV on your system.

You can verify the installation of OpenCV by importing the cv2 module and printing its version –

python3 -c "import cv2; print(cv2.__version__)"

Now this will display output something like given in the image below.

opencv version

Installing OpenCV from source

When you install OpenCV from this method you will have its latest version on your system.  To install OpenCV from this method follow the given steps.

First, install the build tools by using the given command –

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
    libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

Create a new directory with the name opencv_build or anything that you want and move to it –

mkdir ~/opencv_build && cd ~/opencv_build

Now clone OpenCV and OpenCV contrib repositories from Github by using –

git clone
git clone

After downloading the repositories create another directory called build and move inside it –

mkdir -p build && cd build

Use the Cmake to set up the OpenCV build –

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \

Next, start the compilation by using –

make -j6

Where 6 is the number of cores that will be used in the compilation process you can mention any number based on your system capacity. It can take several minutes to compile, using more cores will make the compilation time shorter.

Once the compilation is completed you can use the given command to install the OpenCV on your system –

sudo make install

Again you can check the installation by using the given command –

python3 -c "import cv2; print(cv2.__version__)"


I hope now you understand how to use OpenCV in your system. Now if you have a query then write us in the comments below.

Leave a Comment