How to install OpenCV in Ubuntu?

OpenCV, also known as the Open Source Computer Vision Library, is a very useful image processing library, with links to C++, Python and Java. It can use a dedicated graphics processor and multiple processor cores to process images in real time.

You can use this library to create applications that can recognise faces, colours, moving objects and many other things in photos and videos.

In this article we will see how to install OpenCV on Ubuntu 20.04.

Also read : How to enable SSH on Ubuntu?

There are two ways to install OpenCV on an Ubuntu machine.

  • Use the apt package manager.
  • By downloading and building OpenCV from source (recommended).

Installing OpenCV with apt

OpenCV is available for installation in the standard Ubuntu 20.04 repositories. This means you can quickly download and install OpenCV with a few commands.

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

You can check the installation by importing the cv2 function into the Python 3 terminal and displaying the OpenCV version.

python3 -c import cv2 ; print(cv2.__version_)

While this method is very easy to install, it is not recommended when it comes to installing OpenCV, especially since the version you end up installing may not be the most recent.

Also read : How can I check my Ubuntu version?

Installing OpenCV from source

By downloading the source code, building and installing the library, you will get the latest stable version of OpenCV. In this way, the installation is optimised for your system and you also have control over the construction options.

Step one: First we need to install the build tools and all other 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

If the above command fails, run sudo apt-get update and try again.

Step two: Now we clone the latest OpenCV github repositories. At the time of writing, the latest version is 4.5.1.

Type the following commands in sequence and press Enter after each command.

mkdir ~/opencv_build && cd ~/opencv_build

If you want to install an older version, add the version number between the file name and the .git extension. For example: opencv4.3.0.git

Step three: Once the download is complete, we create a temporary build directory and enter it to continue.

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

Step four: Now configure the build with CMake.


Wait for the installation to complete successfully. Before starting work, make sure that there are no errors in the assembly.

Step 5: Start the compilation process with the following command,

mark -j1

Make sure you set the -j flag based on the number of cores in the processor. You can use the nproc command to see how many cores you have. Since we are running Ubuntu on a virtual machine, we only have one kernel to work with.

Compiling can take a few minutes, depending on the hardware.

Step six: Once compiled, install OpenCV

Shipbuilding company

You can validate the installed version the same way we did for the OpenCV installation with apt.

python3 -c import cv2 ; print(cv2.__version_)

Also read : How do I install Chrome on Ubuntu?

The process of installing OpenCV on Ubuntu 18.04 is exactly the same as described above for 20.04. Note that if you use the package manager to install OpenCV, install version 3.4.0, as this is the default version in the 18.04 repositories.

Also read : Mint vs. Ubuntu: Comparison of Linux distributions

The one who writes/cuts/films/owns all the technology, and when he’s not around, switches to virtual machine races. You can contact Yadullah at [email protected], or follow him on Instagram or Twitter.

frequently asked questions

How to download OpenCV on Ubuntu?

The post How to install-open-open……

Where is Ubuntu OpenCV installed?

Step Five: Complete the installation of Python+ OpenCV + Ubuntu 18.04 by Previous versions of OpenCV installed the links in a different location ( /usr/local/lib/python3. 6/site-packages ), so look carefully at the paths below.

How do I install OpenCV manually?


Related Tags:

install opencv ubuntu c++,install opencv with ffmpeg ubuntu,install opencv pycharm ubuntu,install opencv python3 ubuntu,install opencv ubuntu 18.04 c++,pip install opencv,Feedback,Privacy settings,How Search works,how to install opencv in ubuntu using pip,install opencv 4 ubuntu

Leave a Reply

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

Related Post