How to Install OpenCV on Ubuntu

July 27, 2023

Introduction

OpenCV (Open Source Computer Vision Library) is a software library containing over 2500 algorithms for computer vision and machine learning projects.

OpenCV supports multithreading, multi-core processing, and hardware acceleration, making it very fast and suitable for high-performance computing tasks. The library is easily integrated with scientific computation libraries like NumPy, TensorFlow, and PyTorch.

Find out how to install OpenCV on Ubuntu 20.04 and 22.04 using four different methods.

An article on how to install OpenCV on Ubuntu.

Prerequisites

How to Install OpenCV on Ubuntu?

Use one of the following four methods to install OpenCV on Ubuntu:

1. Install OpenCV from Ubuntu Repositories. This is the fastest and simplest way to install OpenCV on Ubuntu. However, official Ubuntu repositories may not have the latest version of OpenCV, which can cause compatibility issues with other software solutions.

2. Build OpenCV from Source. Retrieving the source code from the OpenCV repository and compiling it on your machine allows you to install the latest version and customize the build options.

3. Install OpenCV using Anaconda. Developers who use Anaconda for Python development can utilize the conda package manager to install OpenCV.

4. Install OpenCV using pip. Users can also install OpenCV directly within the Python ecosystem via pip, Python's package manager.

Method 1: Install OpenCV from Ubuntu Repositories

To install OpenCV from official Ubuntu repositories:

1. Access the command line and update the Ubuntu packages list:

sudo apt update

2. Install OpenCV using the apt package manager:

sudo apt install libopencv-dev
Install OpenCV from official Ubuntu repositories.

The command installs the latest OpenCV development library available in Ubuntu repositories.

3. Check the OpenCV version to verify the installation:

dpkg -l libopencv-dev
Check OpenCV version in Ubuntu.

The system confirms the OpenCV version installed on the system.

Method 2: Install OpenCV on Ubuntu from the Source

To retrieve the OpenCV source code from official repositories and install OpenCV:

1. Update the Ubuntu packages list:

sudo apt update

2. Install developer tools, image and video libraries, and other dependencies necessary to build OpenCV:

sudo apt install build-essential cmake git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev openexr libatlas-base-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-dev gfortran -y

3. Create and access a directory for the OpenCV build. In this example, the directory is called opencv_build:

mkdir ~/opencv_build && cd ~/opencv_build

4. Use git to clone the official OpenCV repository:

git clone https://github.com/opencv/opencv.git
Clone official OpenCV repository using git.

5. Clone the additional OpenCV modules:

git clone https://github.com/opencv/opencv_contrib.git
Clone OpenCV modules using git in Ubuntu.

8. Create the build directory within the opencv directory and access it:

mkdir -p ~/opencv_build/opencv/build && cd ~/opencv_build/opencv/build
Create OpenCV build directory in Ubuntu.

9. Configure the build using cmake, and include the path to the cloned extra modules:

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 BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules ..

Note: The sample command above installs C++ and Python code examples. If you want to customize the build further, review available options using the cmake -LH command.

10. Use the make command to build OpenCV:

make -j3

The -j3 flag tells the system to use three cores for the operation. Change the number of cores to reflect your system's capabilities and setup.

11. After the successful build, install OpenCV using the following command:

sudo make install

12. Check the OpenCV version to verify the installation:

pkg-config --modversion opencv4
Check OpenCV version when installing from source.

The system displays the OpenCV library version. In this example, the version number is 4.8.0.

Method 3: Install OpenCV using Anaconda

Developers using the Anaconda distribution of Python can utilize the conda command-line tool to install OpenCV:

1. Access the terminal and create a new Python environment:

conda create -n myenv python=3.10.6

Replace myenv with the name of your environment and 3.10.6 with the desired Python version.

2. Activate the new environment using the following command:

conda activate myenv

Note: Each time you start a new terminal session, use the conda activate myenv command to activate the environment where OpenCV is installed.

3. Anaconda includes a version of OpenCV in its distribution. Enter the following command to install OpenCV:

conda install -c conda-forge opencv
Use conda to install OpenCV in Ubuntu.

4. Access the Python interactive shell:

python3

5. Enter the following commands to verify the OpenCV version:

import cv2

print(cv2.__version__)
Verify OpenCV version after conda installation.

The system shows the OpenCV version installed on the system.

Method 4: Install OpenCV using Pip

It is recommended to create a virtual environment before using pip to install OpenCV to keep dependencies for different projects isolated. The venv module is the preferred method to create and manage virtual environments:

1. Enter the following command to install the venv module in Python 3.10:

sudo apt install python3.10-venv -y

Adjust the command if you are using a different Python version.

2. Use the venv module to create a Python environment:

sudo python3 -m venv myenv

Replace myenv with the name of your virtual environment.

3. Activate the myenv environment using the following command:

source myenv/bin/activate
Activate Python virtual environment to install OpenCV.

The environment name is in parentheses (myenv) and indicates that the user is now operating within the virtual space.

Note: Use the source myenv/bin/activate command every time you start a new terminal session. The command activates the environment where OpenCV is installed.

4. Enter the following command to install OpenCV using the pip package manager:

sudo pip3 install opencv-python

5. (Optional) Install the OpenCV version that includes the main modules and the extended (contrib) modules using the following command:

sudo pip3 install opencv-contrib-python
Install OpenCV in Ubuntu using pip.

6. Access the Python shell to verify the installation:

python3

7. Check the OpenCV version in Python using the following commands:

import cv2

print(cv2.__version__)
Check OpenCV version using Python.

The system displays the OpenCV version.

Conclusion

You now know how to install OpenCV on Ubuntu using different methods. Choose the installation method based on your project's needs, the OpenCV version, specific libraries, and how you want to configure your development environment.

Learn more about deep learning frameworks and how neural networks help you tackle complex AI, machine learning, and computer vision tasks.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
Best Python IDEs And Code Editors
November 6, 2023

An IDE is like a toolbox, and a Code editor is like a power tool. This article reviews 9 IDEs and 5 Code editors for Python. Discover the best one for you.
Read more
Intel Xeon Scalable Processors
July 6, 2023

Intel launched the fourth generation of their Intel Xeon Scalable processor series in Q1 2023, with a slew of improvements in performance, efficiency, and security. This article will review all the previous generations of Xeon Sca
Read more
How to Fix the "python: command not found" Error
July 13, 2023

This tutorial provides multiple solutions for resolving the python: command not found error in Linux. The solutions include installing Python and editing symlinks and PATH variables.
Read more
Structured vs. Unstructured Data
March 7, 2023

Depending on how data looks, we can categorize information as structured and unstructured. Learn about the differences to maximize the usefulness of your data.
Read more