Install latest OpenCV on Ubuntu

OpenCV can be installed on Ubuntu in three common ways: from the Ubuntu package repository, from Python packages, or from source code. The right method depends on whether you need the Ubuntu-maintained version, the Python cv2 module, or a newer OpenCV build with custom options.

The original steps on this page were written for Ubuntu 16 and older Ubuntu variants. Some package names in those legacy commands are no longer available on current Ubuntu releases. This updated tutorial keeps the older source-build commands for reference and also adds current Ubuntu installation paths that are easier to use on Ubuntu 20.04, 22.04, 24.04, and later releases.

Choose the correct OpenCV Ubuntu installation method

Before installing OpenCV, decide what you need from the library.

  • Use Ubuntu packages when you want a simple system-wide installation for C++ development and the Python module available from Ubuntu repositories.
  • Use pip packages when you mainly need OpenCV in a Python virtual environment and do not want to build from source.
  • Build OpenCV from source when you need a newer release tag, OpenCV contrib modules, CUDA or other custom build flags, or control over the installation path.

For most Python learners, the pip method is the quickest. For C++ projects that use compiler and linker flags, the Ubuntu package or source-build method is usually more suitable.

Install OpenCV on Ubuntu using apt packages

The fastest system-level installation is through apt. This installs the OpenCV version packaged for your Ubuntu release.

</>
Copy
sudo apt update
sudo apt install -y libopencv-dev python3-opencv

Verify the Python module after installation.

</>
Copy
python3 -c "import cv2; print(cv2.__version__)"

If you installed OpenCV for C++ development, check whether pkg-config can find the OpenCV package metadata.

</>
Copy
pkg-config --modversion opencv4

The apt method is simple, but it may not provide the newest OpenCV release. If you need the latest stable upstream code, build from a release tag instead of relying on Ubuntu repository versions.

Install OpenCV Python on Ubuntu with pip

If you only need OpenCV for Python scripts, create a virtual environment and install one OpenCV Python package. The package name is opencv-python, but the module imported in code is still cv2.

</>
Copy
python3 -m venv opencv-env
source opencv-env/bin/activate
python -m pip install --upgrade pip
python -m pip install opencv-python

For server, Docker, or non-GUI environments where you do not need display windows such as cv2.imshow(), install the headless package instead.

</>
Copy
python -m pip install opencv-python-headless

Do not install multiple OpenCV Python packages in the same environment unless you have a specific reason. If you switch between normal and headless packages, uninstall the old package first to avoid import conflicts.

</>
Copy
python -m pip uninstall opencv-python opencv-python-headless opencv-contrib-python opencv-contrib-python-headless

Then install only the package you need. For contrib modules, use opencv-contrib-python or opencv-contrib-python-headless instead of the main package. You can compare package options on the opencv-python PyPI page.

Build latest stable OpenCV on Ubuntu from source

Building from source gives you control over the OpenCV version and compile options. For a stable setup, prefer a release tag from the OpenCV releases page or the OpenCV GitHub releases page instead of building directly from the moving development branch.

Install OpenCV source build dependencies on Ubuntu

Start by installing compilers, CMake, Git, image/video codec libraries, GUI dependencies, and Python build dependencies.

</>
Copy
sudo apt update
sudo apt install -y build-essential cmake git pkg-config
sudo apt install -y libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt install -y libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev
sudo apt install -y gfortran openexr libatlas-base-dev python3-dev python3-numpy

Package availability can vary by Ubuntu release. If a dependency name is not available, search for the replacement package with apt search before continuing.

Download OpenCV and opencv_contrib source code

The main OpenCV repository contains core modules. The opencv_contrib repository contains extra modules. If you use contrib modules, check out the same release tag in both repositories.

</>
Copy
mkdir -p ~/opencv_build
cd ~/opencv_build

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

List release tags and check out the stable tag you want to build. Replace 4.x.x with the release tag you selected from the official releases page.

</>
Copy
cd ~/opencv_build/opencv
git tag --sort=-v:refname | head
git checkout 4.x.x

cd ~/opencv_build/opencv_contrib
git checkout 4.x.x

Configure OpenCV build options with CMake

Create a build directory and configure OpenCV with CMake. The following command enables the Python 3 module, installs files under /usr/local, enables package metadata generation, and includes contrib modules.

</>
Copy
cd ~/opencv_build/opencv
mkdir -p build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D OPENCV_GENERATE_PKGCONFIG=ON \
      -D BUILD_EXAMPLES=ON \
      -D BUILD_opencv_python3=ON \
      ..

Read the CMake summary before running make. Confirm that Python 3, NumPy, GUI support, video I/O, and contrib modules are configured as expected.

Compile and install OpenCV on Ubuntu

Compile OpenCV using all available CPU cores. On small virtual machines, reduce the job count if the build fails because of low memory.

</>
Copy
make -j"$(nproc)"
sudo make install
sudo ldconfig

After installation, verify both the C++ package metadata and Python module if you built Python support.

</>
Copy
pkg-config --modversion opencv4
python3 -c "import cv2; print(cv2.__version__)"

Legacy OpenCV source build commands for Ubuntu 14 and Ubuntu 16

The following section preserves the earlier Ubuntu 16 source-build flow. Use it only when you are maintaining an older system. On current Ubuntu releases, packages such as libjasper-dev, libgstreamer0.10-dev, libtiff4-dev, and Python 2 development packages may be unavailable or unnecessary.

Steps to install OpenCV on Ubuntu

Following is the step by step process.

Step 1: Update Ubuntu packages.

$ sudo apt-get update

Step 2: Install packages and tools required for building OpenCV.

$ sudo apt-get install libopencv-dev build-essential checkinstall cmake pkg-config yasm libtiff4-dev libtiff5-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg

Note: If you are installing on Ubuntu 16, you may get error for libtiff4-dev package. Please ignore. This package is for Ubuntu 14.

Step 3: Clone OpenCV Git repository for source code.

$ mkdir OpenCV
$ cd OpenCV
$ git clone https://github.com/opencv/opencv.git

The first two commands are for creating a new directory for OpenCV and navigating to the directory.

Cloning into 'opencv'...
remote: Counting objects: 224315, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 224315 (delta 0), reused 0 (delta 0), pack-reused 224311
Receiving objects: 100% (224315/224315), 439.82 MiB | 1.33 MiB/s, done.
Resolving deltas: 100% (155909/155909), done.
Checking connectivity... done.
Checking out files: 100% (5770/5770), done.


A folder named opencv is created inside OpenCV.

Step 4: Build OpenCV from Source.

Create a directory called release, for compiled files to go into.

 $ cd opencv
 $ mkdir release
 $ cd release
 $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..


You may get lot of debug lines, but at the end, you may notice following :

-- Configuring done
-- Generating done
-- Build files have been written to: /media/arjun/0AB650F1B650DF2F/softs/OpenCV/opencv/release


Step 5: Make and Install the build.

$ make
$ sudo make install

Fix common OpenCV installation errors on Ubuntu

Python cannot import cv2 after OpenCV installation

If import cv2 fails, confirm which Python interpreter you are using. A common issue is installing OpenCV into one Python environment and running the script with another.

</>
Copy
which python3
python3 -m pip show opencv-python
python3 -c "import cv2; print(cv2.__file__)"

CMake cannot find OpenCV after source installation

If a C++ project cannot find OpenCV, check the install prefix and package metadata path. Source builds installed under /usr/local may need ldconfig and a valid PKG_CONFIG_PATH.

</>
Copy
sudo ldconfig
pkg-config --cflags --libs opencv4

OpenCV build stops because the Ubuntu machine runs out of memory

Compiling OpenCV can use a lot of memory. If make -j"$(nproc)" fails on a small system, run fewer parallel jobs.

</>
Copy
make -j2

FAQs on installing latest OpenCV on Ubuntu

Should I install OpenCV on Ubuntu with apt, pip, or source code?

Use apt for a simple system-wide installation, pip for Python projects inside a virtual environment, and source code when you need a specific OpenCV release tag, contrib modules, or custom CMake options.

How do I install the latest stable OpenCV release on Ubuntu?

To install the latest stable upstream OpenCV release, build from source using a release tag from the official OpenCV releases page. Avoid relying on the default development branch unless you specifically want development code.

Why are old Ubuntu 16 OpenCV dependency commands failing?

Some older dependency names were removed or replaced in newer Ubuntu releases. Packages related to Python 2, older GStreamer versions, and older TIFF or Jasper development libraries are common causes. Use current Ubuntu package names for new installations.

What is the difference between opencv-python and opencv-python-headless?

opencv-python includes GUI-related features needed for functions such as cv2.imshow(). opencv-python-headless is intended for server or container environments where GUI windows are not required.

How do I check the OpenCV version installed on Ubuntu?

For Python, run python3 -c "import cv2; print(cv2.__version__)". For C++ package metadata, run pkg-config --modversion opencv4 if OpenCV was installed with pkg-config support.

Editorial QA checklist for Ubuntu OpenCV installation tutorial

  • Confirm that current Ubuntu commands use apt package names available on supported Ubuntu releases.
  • Keep the older Ubuntu 14/16 commands clearly labelled as legacy source-build instructions.
  • Check that new command-line examples use language-bash and that output-only examples use an output class when added.
  • Verify that the tutorial separates Python pip installation from C++ or system-wide OpenCV installation.
  • Ensure the source-build flow tells readers to use a stable OpenCV release tag rather than blindly building the development branch.