Bobcares

How to install OpenCV on Ubuntu 20.04 – Two different methods

by | Feb 27, 2021

Wondering how to install OpenCV on Ubuntu 20.04? We can help you.

Many of our customers use OpenCV for a wide range of applications. Some of which are medical image analysis, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, etc.

As part of our Server Management Services, we assist our customers with several Ubuntu queries.

Today, let us see how to install OpenCV on Ubuntu 20.04 in two different ways.

 

How to install OpenCV on Ubuntu 20.04

Generally, there are two different ways to install OpenCV:

  • Using Ubuntu repository
  • Through the source

 

Install OpenCV from the Ubuntu Repository

OpenCV is available for installation from the default Ubuntu 20.04 repositories:

$ sudo apt update
$ sudo apt install libopencv-dev python3-opencv

Now we have all packages necessary to run OpenCV.

We verify the installation by importing the cv2 module and printing the OpenCV version:

$ python3 -c “import cv2; print(cv2.__version__)”

For example, the output will like this:

Output
4.2.0

This implies, we have the 4.2.0 OpenCV version.

 

Install OpenCV from the Source

If we build the OpenCV library from the source we can optimize it for our particular system. In addition, we will have complete control over the build options.

Our Support Engineers recommend this method of installing OpenCV.

In order to install the latest OpenCV version from the source:

  • Install the build tools and 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 libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
  • Clone the OpenCV’s and OpenCV contrib repositories:
$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

For an older version of OpenCV, cd to both opencv and opencv_contrib directories and run git checkout <opencv-version>

  • Once done, create a temporary build directory and navigate to it:
$ cd ~/opencv_build/opencv
$ mkdir -p build && cd build

Set up the OpenCV build with CMake:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Output
— Configuring done
— Generating done
— Build files have been written to: /home/vagrant/opencv_build/opencv/build
  • Start the compilation process:
$ make -j8

Initially, we modify the -j flag according to the processor. To find the number of cores in the processor, type nproc.

Wait for the compilation to finish.

  • Install OpenCV:
$ sudo make install

In order to verify, type the following commands.

C++ bindings:

$ pkg-config –modversion opencv4
Output
4.3.0

Python bindings:

python3 -c “import cv2; print(cv2.__version__)”
Output
4.3.0-dev
  • Pip Install OpenCV For Desktop Environments

To install OpenCV, run:

$ pip3 install opencv-python
$ sudo pip3 install scikit-build
$ sudo pip3 install cmake
$ sudo pip3 install opencv-python

open-cv python will install only main modules. However, to install both main and contrib modules, run:

$ sudo pip3 install opencv-contrib-python
  • Pip install OpenCV For Server (headless) Environments

To install for headless environments where there is no GUI available, run:

For main modules only:

$ sudo pip3 install opencv-python-headless

For both main and contrib modules:

$ sudo pip3 install opencv-contrib-python-headless

To verify installation, invoke Python3 console and run:

$ import csv2

 

Error while installing OpenCV on Ubuntu 20.04

While installing OpenCV one of our customers came across the following error:

Error: ModuleNotFoundError: No module named ‘skbuild’

In order to resolve this, we make sure that Pip is the latest. If not, update Pip.

Eventually run the following commands:

$ sudo pip3 install scikit-build
$ sudo pip3 install cmake
$ sudo pip3 install opencv-python

[Stuck with similar error? We’d be happy to assist you]

 

Conclusion

In short, we discussed two different ways to install OpenCV on Ubuntu 20.04. However, our Support Techs prefer installing OpenCV from the Source.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. Balaji

    Hallo, thanks for this tutorial. I need ur help. I have installed the opencv from the source method. But how can I link this installation to spyder python? whenever i import opencv, the kernel shows no module named ‘opencv’ or ‘cv2’. KIndly help me

    Reply
    • Maheen Aboobakkar

      Hello Balaji,

      We’ll be happy to talk to you on chat (click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.