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.
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-opencvor 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.
Step 1: Install OpenCV from apt (recommended)
Refresh indexes and inspect candidates:
sudo apt update
apt-cache policy libopencv-dev python3-opencvExample on Ubuntu 25.04:
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-5Install with apt:
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
python3 -c "import cv2; print('cv2 version:', cv2.__version__); print('path:', cv2.__file__)"cv2 version: 4.10.0
path: /usr/lib/python3/dist-packages/cv2.cpython-313-x86_64-linux-gnu.soInteractive check:
python3import cv2
print(cv2.__version__)
exit4.10.0Step 3: Verify C++ / pkg-config paths
Where OpenCV lives after an apt install (Ask Ubuntu: where is OpenCV installed):
opencv_version
pkg-config --modversion opencv4
pkg-config --list-all | grep -i opencv
ls /usr/include/opencv4/opencv2/ | head -5
which opencv_version4.10.0
4.10.0
opencv4 OpenCV - Open Source Computer Vision Library
alphamat.hpp
aruco
aruco.hpp
bgsegm.hpp
bioinspired
/usr/bin/opencv_versionCompile flags for a C++ project:
pkg-config --cflags --libs opencv4-I/usr/include/opencv4 -lopencv_stitching -lopencv_alphamat ... -lopencv_coreExample minimal compile (requires g++ and libopencv-dev):
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-helloExpected 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.
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__)"pip cv2: 4.13.0For contrib modules via PyPI :
pip install opencv-contrib-pythonDeactivate 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:
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-numpyClone and configure (example layout):
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 ldconfigVerify a /usr/local install:
pkg-config --modversion opencv4
/usr/local/bin/opencv_versionSource 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:
sudo apt purge -y python3-opencv libopencv-dev
sudo apt autoremove -ypip virtual environment:
deactivate 2>/dev/null || true
rm -rf ~/opencv-venvManual /usr/local source install:
sudo rm -rf /usr/local/include/opencv4 /usr/local/lib/libopencv* /usr/local/bin/opencv_*
sudo ldconfigTroubleshooting
| 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
- OpenCV — Install OpenCV-Python in Ubuntu
- OpenCV — Installation in Linux (C++)
- Ask Ubuntu: Installing OpenCV
- Ask Ubuntu: python-opencv on 20.04
- Ask Ubuntu: Where is OpenCV installed?
- On-site: apt command, install CMake, wget command
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.

