How to Install OpenCV on Ubuntu

Install OpenCV on Ubuntu with sudo apt install libopencv-dev python3-opencv (4.10.0 on 25.04), verify with import cv2 and pkg-config opencv4, use pip opencv-python in a venv for newer wheels, or build from source when you need the latest contrib modules.

Published

Updated

Tech reviewed byDeepak Prasad

Install OpenCV on Ubuntu banner with computer vision camera grid and Ubuntu orange accent

OpenCV is the standard open-source computer vision library for C++ and Python—used for image processing, camera capture, object detection, and machine-learning pipelines. On Ubuntu you can install it from apt (fastest), pip in a virtual environment (newer wheels), or a source build (maximum control). The package name python3-opencv replaced legacy python-opencv on modern Ubuntu (Ask Ubuntu).

This guide shows how to install OpenCV on Ubuntu, verify Python and C++ toolchains, locate headers and libraries, and avoid common import errors. Commands below were tested on Ubuntu 25.04.

Tested on: Ubuntu 25.04 (Plucky Puffin); kernel 6.14.0-37-generic; amd64.

NOTE
For most users sudo apt install libopencv-dev python3-opencv is enough. Use pip install opencv-python inside a venv when you need a newer wheel than apt ships—avoid mixing pip and apt cv2 on the same Python interpreter without understanding which module wins on import cv2.

Quick command summary

Task Command
Install apt packages (C++ + Python) sudo apt install -y libopencv-dev python3-opencv
Check apt candidate apt-cache policy libopencv-dev python3-opencv
Verify Python binding python3 -c "import cv2; print(cv2.__version__)"
Verify C++ pkg-config pkg-config --modversion opencv4
CLI version opencv_version
pip in venv (newer wheel) python3 -m venv ~/opencv-venv && source ~/opencv-venv/bin/activate && pip install opencv-python
Contrib via pip pip install opencv-contrib-python
Fix libGL.so.1 import error sudo apt install -y libgl1
Remove apt OpenCV sudo apt purge -y python3-opencv libopencv-dev

Prerequisites

  • Ubuntu 22.04 LTS, 24.04 LTS, or newer (25.04 tested here) on amd64.
  • sudo for system packages.
  • Python 3 for python3-opencv or pip wheels.
  • build-essential and cmake only if you compile OpenCV from source (install CMake guide).
  • For GUI functions (cv2.imshow), a desktop with libGL / Mesa installed.

Choose an install method

Method Best for Version on 25.04 (tested)
apt install libopencv-dev python3-opencv System Python + C++ projects 4.10.0
pip install opencv-python (venv) Newer Python wheel without compiling 4.13.0 (PyPI at test time)
pip install opencv-contrib-python Extra contrib modules via pip Latest PyPI wheel
Source build (cmake + make install) Bleeding-edge or custom flags Whatever you compile (OpenCV docs)

For embedded and server boards that do not need a custom build, the simple apt pair libopencv-dev + python3-opencv is usually enough.


Refresh indexes and inspect candidates:

bash
sudo apt update
apt-cache policy libopencv-dev python3-opencv

Example on Ubuntu 25.04:

text
libopencv-dev:
 Candidate: 4.10.0+dfsg-5
 Version table:
 4.10.0+dfsg-5 500
 500 http://archive.ubuntu.com/ubuntu plucky/universe amd64 Packages
python3-opencv:
 Candidate: 4.10.0+dfsg-5

Install with apt:

bash
sudo apt install -y libopencv-dev python3-opencv
Package Purpose
libopencv-dev C/C++ headers, .so symlinks, pkg-config file for opencv4
python3-opencv System cv2 module for python3

Do not run sudo apt install python-opencv on Ubuntu 20.04+—that name is gone (Ask Ubuntu #1330968).


Step 2: Verify Python OpenCV

bash
python3 -c "import cv2; print('cv2 version:', cv2.__version__); print('path:', cv2.__file__)"
text
cv2 version: 4.10.0
path: /usr/lib/python3/dist-packages/cv2.cpython-313-x86_64-linux-gnu.so

Interactive check:

bash
python3
python
import cv2
print(cv2.__version__)
exit
Output
text
4.10.0

Step 3: Verify C++ / pkg-config paths

Where OpenCV lives after an apt install (Ask Ubuntu: where is OpenCV installed):

bash
opencv_version
pkg-config --modversion opencv4
pkg-config --list-all | grep -i opencv
ls /usr/include/opencv4/opencv2/ | head -5
which opencv_version
text
4.10.0
4.10.0
opencv4 OpenCV - Open Source Computer Vision Library
alphamat.hpp
aruco
aruco.hpp
bgsegm.hpp
bioinspired
/usr/bin/opencv_version

Compile flags for a C++ project:

bash
pkg-config --cflags --libs opencv4
text
-I/usr/include/opencv4 -lopencv_stitching -lopencv_alphamat ... -lopencv_core

Example minimal compile (requires g++ and libopencv-dev):

bash
cat > /tmp/opencv-hello.cpp <<'EOF'
#include <opencv2/opencv.hpp>
#include <iostream>
int main {
 std::cout << "OpenCV " << CV_VERSION << std::endl;
 return 0;
}
EOF
g++ /tmp/opencv-hello.cpp -o /tmp/opencv-hello $(pkg-config --cflags --libs opencv4)
/tmp/opencv-hello

Expected output shape: OpenCV 4.10.0.


Step 4: Install OpenCV with pip (virtual environment)

Ask Ubuntu notes pip as the quickest path when you only need Python and want a newer wheel than apt.

bash
sudo apt install -y python3-venv python3-pip
python3 -m venv ~/opencv-venv
source ~/opencv-venv/bin/activate
pip install --upgrade pip
pip install opencv-python
python3 -c "import cv2; print('pip cv2:', cv2.__version__)"
text
pip cv2: 4.13.0

For contrib modules via PyPI :

bash
pip install opencv-contrib-python

Deactivate when done: deactivate.


Step 5: Build OpenCV from source (advanced)

Build from GitHub when apt is too old, you need a specific commit, or you must disable/enable codecs and CUDA flags manually. Follow the official Ubuntu setup tutorial and Linux install guide.

Install build dependencies:

bash
sudo apt update
sudo apt install -y 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 \
 libatlas-base-dev gfortran python3-dev python3-numpy

Clone and configure (example layout):

bash
mkdir -p ~/opencv_build && cd ~/opencv_build
git clone --depth 1 --branch 4.10.0 https://github.com/opencv/opencv.git
git clone --depth 1 --branch 4.10.0 https://github.com/opencv/opencv_contrib.git
mkdir -p opencv/build && cd opencv/build
cmake -D CMAKE_BUILD_TYPE=Release \
 -D CMAKE_INSTALL_PREFIX=/usr/local \
 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
 -D OPENCV_GENERATE_PKGCONFIG=ON \
 -D BUILD_EXAMPLES=OFF ..
make -j"$(nproc)"
sudo make install
sudo ldconfig

Verify a /usr/local install:

bash
pkg-config --modversion opencv4
/usr/local/bin/opencv_version

Source installs land under /usr/local/include/opencv4 and /usr/local/lib—which can shadow apt packages if /usr/local precedes /usr on PKG_CONFIG_PATH. Remove or adjust paths when switching back to apt.


Uninstall

APT packages:

bash
sudo apt purge -y python3-opencv libopencv-dev
sudo apt autoremove -y

pip virtual environment:

bash
deactivate 2>/dev/null || true
rm -rf ~/opencv-venv

Manual /usr/local source install:

bash
sudo rm -rf /usr/local/include/opencv4 /usr/local/lib/libopencv* /usr/local/bin/opencv_*
sudo ldconfig

Troubleshooting

Symptom Likely cause Fix
E: Unable to locate package python-opencv Renamed on modern Ubuntu Install python3-opencv (Ask Ubuntu #1330968)
import cv2 shows unexpected version pip wheel shadowing apt or vice versa python3 -c "import cv2; print(cv2.__file__)"; use a venv for pip
ImportError: libGL.so.1 Missing Mesa GL on minimal image sudo apt install -y libgl1; or pip install opencv-python-headless
Headers not in /usr/local/include after apt Apt uses /usr/include/opencv4 pkg-config --cflags opencv4; do not assume /usr/local (Ask Ubuntu #656461)
pkg-config: opencv4 not found libopencv-dev not installed sudo apt install libopencv-dev
Old OpenCV 2.x tutorials Deprecated packages Ignore libopencv-dev 2.4 compile guides; use apt 4.x or pip
cv2.imshow fails on SSH server No display Use X forwarding, a desktop session, or process images without highgui

References


Summary

The fastest way to install OpenCV on Ubuntu is sudo apt install -y libopencv-dev python3-opencv, which on Ubuntu 25.04 provides OpenCV 4.10.0 with headers in /usr/include/opencv4 and cv2 under /usr/lib/python3/dist-packages. Confirm with opencv_version, pkg-config --modversion opencv4, and python3 -c "import cv2; print(cv2.__version__)".

Use pip install opencv-python (or opencv-contrib-python) inside a virtual environment when you need a newer PyPI wheel. Compile from GitHub source only when apt and pip cannot meet your module, codec, or GPU requirements.

Frequently Asked Questions

1. How do I install OpenCV on Ubuntu?

For Python and C++ development on Ubuntu, run sudo apt update && sudo apt install -y libopencv-dev python3-opencv. Verify with python3 -c "import cv2; print(cv2.version)" and pkg-config --modversion opencv4.

2. What is the difference between libopencv-dev and python3-opencv?

libopencv-dev ships C/C++ headers and libraries under /usr/include/opencv4 for compiling native apps. python3-opencv provides the cv2 Python module linked against the same Ubuntu OpenCV build. Install both when you need Python and C++.

3. Why does apt say python-opencv package not found?

Ubuntu 20.04+ dropped the python-opencv metapackage name. Use python3-opencv instead. Python 2 is end-of-life—do not search for python-opencv on current releases.

4. Where is OpenCV installed on Ubuntu?

Apt installs headers in /usr/include/opencv4, Python bindings in /usr/lib/python3/dist-packages/cv2*.so, libraries under /usr/lib/x86_64-linux-gnu, and opencv_version at /usr/bin/opencv_version. Run pkg-config --cflags opencv4 for compile flags.

5. How do I install OpenCV with pip on Ubuntu?

Create a virtual environment (python3 -m venv ~/opencv-venv), activate it, then pip install opencv-python or opencv-contrib-python. Pip wheels are often newer than apt but are separate from system python3-opencv.

6. How do I fix ImportError libGL.so.1 when importing cv2?

Install Mesa GL libraries: sudo apt install -y libgl1. On minimal servers or Docker images without a GPU stack, also try opencv-python-headless via pip for GUI-free OpenCV.

7. Should I use apt or pip for OpenCV?

Use apt for system-wide Ubuntu integration and apt-managed upgrades. Use pip inside a venv when you need a newer OpenCV wheel (for example 4.13.x) or opencv-contrib-python without compiling from source.

8. How do I install the latest OpenCV with contrib modules?

Either pip install opencv-contrib-python in a venv, or clone opencv and opencv_contrib from GitHub, build with cmake -D OPENCV_EXTRA_MODULES_PATH=... and sudo make install to /usr/local. Apt python3-opencv on Ubuntu 25.04 already includes contrib-enabled builds.

9. How do I uninstall OpenCV from Ubuntu?

For apt: sudo apt purge -y python3-opencv libopencv-dev && sudo apt autoremove -y. For pip venv: deactivate and rm -rf the venv. For source /usr/local installs: remove /usr/local/include/opencv4 and /usr/local/lib/libopencv* then sudo ldconfig.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …