Installing OpenCV on Ubuntu 18.04 13

OpenCV (Open Source Computer Vision Library) is a computer vision library released under the BSD license, free for both personal and commercial use. It has interfaces for C ++, Python and Java, supported by Windows, Linux, MacOS, IOS and Android. This library is used to analyze videos and images.

OpenCV has a modular structure, which means that the package consists of several static and dynamic libraries. In this article, we will look at how to install OpenCV on Ubuntu 18.04 from the official repository or sources.

The content of the article:



Install OpenCV from Ubuntu Repository

The OpenCV package is available in the official repositories of Ubuntu 18.04. At the moment, version 3.2 is in the repositories, and version 4.0 can already be found on the official website. To install OpenCV on Ubuntu 18.04 along with Python 3, run:

sudo apt update
sudo apt install python3-opencv

Then you need to import the cv2 module in the python interpreter and look at the version to make sure that the installation was successful:

python3 -c "
import cv2


On Ubuntu 18.04 LTS, the default version of Python is 3.6. If you need to install OpenCV with Python 2 bindings, download the python-opencv package.

Installing OpenCV Using PIP

The pip package manager allows you to install various python packages on your system. First you need to install the package manager itself:

sudo apt install python-pip

Then it remains to install the library itself:

sudo pip install opencv-python

After that, you can check the library version as described in the previous paragraph.

Install OpenCV from source

The official repository and pip repository is not the latest version. At the time of writing, the newest version is 4.1.0. If you want to install it, you will need to compile OpenCV from the source code.

Step 1. Installing dependencies

First, install the dependencies:

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

Step 2. Cloning the repository

Create a directory in which the OpenCV repositories will be saved and go to it

mkdir ~/opencv_build
cd ~/opencv_build

Then download the repositories. Two repositories will be downloaded – opencv and opencv-contrib. The second repository contains computer vision algorithms and will be useful to everyone who wants to work with this technology.

git clone
git clone

If you want to install an old version of OpenCV, go to the directory of both repositories and run the command in them:

git checkout <нужная-версия>

Step 3. Preparing for assembly

When the download is complete, create a temporary assembly directory and switch to it:

cd ~/opencv_build/opencv
mkdir build
cd build

Prepare your OpenCV build using CMake:

-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules

Once the CMake build is complete, you will see something similar to the one shown below:

Installing OpenCV on Ubuntu 18.04 14

Step 4. Compiling OpenCV

Change the -j flag to suit your processor. You can find out the number of processor cores by using the nproc command. For example, the system has 4 cores, so the –j4 flag is used. Start the compilation process:


Compilation may take some time. It depends on your system configuration. Upon completion, you will see something like this:

Installing OpenCV on Ubuntu 18.04 15

Step 5. Installing OpenCV

When the compilation process is complete, install OpenCV with the command:

sudo make install

Installing OpenCV on Ubuntu 18.04 16

Step 6. OpenCV Version

To verify that the OpenCV installation was successful, enter the following commands. You should see the version of OpenCV:

pkg-config --modversion opencv4



python3 -c "
import cv2




In this article, we examined how to install OpenCV on Ubuntu 18.04 from a repository or source code. Now you know how to get the latest version of the program. More information on using the library is available on the official website.

How to Check the Monitor for Broken Pixels and Fix it

Previous article

How to Split Screen on Android

Next article

You may also like


Leave a Reply

More in How To