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.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF