OpenCV (Open Source Computer Vision Library) is an open-source  computer vision library and has bindings for C++, Python, and Java. It  is used for a very wide range of applications, including medical image  analysis, stitching street view images, surveillance video, detecting  and recognizing faces, tracking moving objects, extracting 3D models,  and much more. OpenCV can take advantage of multi-core processing and features GPU acceleration for real-time operation.

This tutorial explains how to install OpenCV on Ubuntu 18.04.

For  most people, the easiest way to install OpenCV on Ubuntu is to install  it using the apt package management tool. If you want to install the  latest stable version of OpenCV from source, scroll down to the Installing OpenCV from the Source section of this tutorial.

Choose one of the installation options that works best for you.

Install OpenCV from the Ubuntu Repository

The  OpenCV package is available from the Ubuntu 18.04 distribution  repository. At the time of writing, the version in the repositories is  3.2, which is not the latest version.

To install OpenCV from the Ubuntu 18.04 repositories, follow these steps:

Refresh the packages index and install the OpenCV package by typing:

sudo apt update

The command above will install all packages necessary to run OpenCV.

To verify the installation, import the cv2 module and print the OpenCV version:

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



The default Python version in Ubuntu 18.04 LTS is version 3.6. If you want to install OpenCV with python 2 bindings install the python-opencv package.

Installing OpenCV from the Source

Building  the OpenCV library from source is the recommended way of installing  OpenCV. It will be optimized for your particular system and you will  have complete control over the build options.

To install the latest OpenCV version from the source, perform the following steps:

Install the required dependencies:

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \

Clone the OpenCV’s and OpenCV contrib repositories:

mkdir ~/opencv_build && cd ~/opencv_build

At  the time of writing, the default version in the github repositories is  version 4.2.0. If you want to install an older version of OpenCV, cd to  both opencv and opencv_contrib directories and run git checkout <opencv-version>

Once the download is complete, create a temporary build directory, and switch to it:

cd ~/opencv_build/opencv

Set up the OpenCV build with CMake:


When the CMake build system is finalized, you will see something like below:

Start the compilation process:

make -j8

Modify the -j flag according to your processor. If you do not know the number of cores your processor, you can find it by typing nproc.

The  compilation may take several minutes or more, depending on your system  configuration. Once it is completed you will see something like below:

Install OpenCV with:

sudo make install

To verify whether OpenCV has been installed successfully, type the following command and you should see the OpenCV version:

pkg-config --modversion opencv4
python3 -c "import cv2; print(cv2.__version__)"




We  have shown you two different ways to install OpenCV on your Ubuntu  18.04 server. The method you choose depends on your requirements and  preferences. Even though installing the packaged version from the Ubuntu  repository is easier, building OpenCV from source gives you more  flexibility, and it should be your first option when installing OpenCV.

If you have any questions or feedback, feel free to comment below.