Two different ways to install OpenCV on Ubuntu 18.04 server

Two different ways to install OpenCV on Ubuntu 18.04 server

This tutorial explains how to install OpenCV on Ubuntu 18.04

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.

We will show you two different ways to install OpenCV on Ubuntu. Choose one of the installation options that will work best for you.

This tutorial explains how to install OpenCV on Ubuntu 18.04.

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 may not always be the latest version.

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

  1. Install OpenCV

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

    sudo apt update
    sudo apt install python3-opencv

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

  2. Verify the OpenCV installation

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

python3 -c "\
import cv2


Install OpenCV from Source

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

To install the latest OpenCV version from the source, follow these steps:

  1. Installing the necessary packages

    The following commands will install all required and optional dependencies:

    sudo apt install build-essential cmake git pkg-config libgtk-3-dev
    sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
    sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev
    sudo apt install python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
  2. Cloning the OpenCV source code

    Create a directory which will hold the repositories and clone the OpenCV's and OpenCV contrib repositories with the following commands:

    mkdir ~/opencv_build && cd ~/opencv_build
    git clone
    git clone

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

  3. Configuring OpenCV with CMake

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

    cd ~/opencv_build/opencv
    mkdir build && cd build

    Set up the OpenCV build with CMake:

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

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

This is image title

  1. Compiling OpenCV

    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. My system has 8 cores, so I am using the -j8 flag.

    Start the compilation process:

    make -j8

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

This is image title

  1. Installing OpenCV

    Once the compile process is completed install OpenCV with:

    sudo make install

This is image title

  1. Verifying OpenCV installation

    To check 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



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. I hope this tutorial will surely help and you if you liked this tutorial, please consider sharing it with others.

opencv python ubuntu

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

OpenCV Python | Python OpenCV Tutorial | Python for Beginners

OpenCV Python in Python will explain all the basics of OpenCV. It also explains how to create a face recognition system and a motion detector.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

How to Install Pyenv on Ubuntu 18.04

How to Install Pyenv on Ubuntu 18.04 - What is Pyenv? Pyenv is a fantastic tool for installing and managing multiple Python versions. It enables a developer to quickly gain access to newer