Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install OpenCV on Ubuntu

Install OpenCV from the Ubuntu repositories (for C++), or with pip in a virtual environment (for Python).

How-To Guides2 min read5 steps8 screenshots

What OpenCV is

OpenCV is a free library for computer vision: images, video, face and object detection, and more. It is used in robotics, self-driving vehicles, medical imaging and many other areas.

Method 1: Ubuntu repositories (C++ development files)

Step 1: Update

sudo apt update
Updating the system

Step 2: Install

sudo apt install libopencv-dev
Installing libopencv-dev

(For the Python bindings from the repository, install python3-opencv.)

Step 3: Check

dpkg -l libopencv-dev
Verifying the installation

You can also see the version with pkg-config --modversion opencv4.

Method 2: pip in a virtual environment

Step 1: Install the venv module

sudo apt install python3-venv -y
Installing python3-venv

Step 2: Create a virtual environment

python3 -m venv myenv
Creating the virtual environment

Step 3: Activate it

source myenv/bin/activate
Activating the environment

Step 4: Install OpenCV

pip3 install opencv-python
Installing opencv-python

On a server without a display, use opencv-python-headless in place of opencv-python.

Step 5: Test it

python3
import cv2
print(cv2.__version__)
The OpenCV version

A quick example

import cv2

img = cv2.imread("photo.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite("photo_gray.jpg", gray)

Why use OpenCV

  1. Robotics. Robots see and move around.
  2. Self-driving vehicles. Detect lanes, signs and objects.
  3. Medical imaging. Analyze scans.
  4. Security and monitoring. Detect motion and faces.
  5. Augmented reality and photo apps.

Quick recap

  • sudo apt install libopencv-dev for C++.
  • A venv with pip install opencv-python for Python.
  • Test with cv2.__version__.