Skip to main content

Installation Overview

OpenCV can be installed in two ways: using prebuilt binaries or building from source. This guide covers both methods across different platforms.
For most users, especially Python developers, using prebuilt packages (pip, conda, or system package managers) is the quickest way to get started.

Quick Install

For rapid setup, use these package managers:
# Using pip
pip install opencv-python

# With contrib modules (extra features)
pip install opencv-contrib-python

# Using conda
conda install -c conda-forge opencv
The opencv-python package includes prebuilt binaries for most platforms and is the easiest way to get started with Python.

Building from Source

Building OpenCV from source gives you full control over features, optimizations, and dependencies.

Prerequisites

1

Install Build Tools

Install a C++ compiler and build tools for your platform:Linux:
# GCC/G++
sudo apt-get install build-essential

# Or Clang
sudo apt-get install clang
Windows:
  • Visual Studio 2015 or later (Community Edition is free)
  • Or MinGW-w64 compiler
macOS:
xcode-select --install
2

Install CMake

CMake 3.9 or higher is required:
# Linux (Ubuntu/Debian)
sudo apt-get install cmake

# macOS
brew install cmake

# Windows: Download from https://cmake.org/download/
Verify installation:
cmake --version
3

Install Git

Required to download OpenCV source:
# Linux (Ubuntu/Debian)
sudo apt-get install git

# macOS (if not already installed)
brew install git

# Windows: Download from https://git-scm.com/

Download OpenCV Source

# Clone main repository
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.x  # or specific version like 4.8.0

# Optional: Clone contrib modules
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.x  # must match opencv version
When using both opencv and opencv_contrib, ensure both repositories are at the same version/tag to avoid compatibility issues.

Platform-Specific Build Instructions

Building on Linux

1

Install Dependencies

# Required dependencies
sudo apt-get install 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 gfortran openexr libatlas-base-dev python3-dev \
  python3-numpy libtbb2 libtbb-dev libdc1394-22-dev

# Optional: for Python bindings
pip install numpy
2

Configure Build

cd opencv
mkdir build && cd build

# Basic configuration
cmake -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX=/usr/local \
      -DBUILD_EXAMPLES=ON \
      ..

# With contrib modules
cmake -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX=/usr/local \
      -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -DBUILD_EXAMPLES=ON \
      ..
Use cmake -DCMAKE_BUILD_TYPE=Release for optimized builds. Use Debug for development.
3

Compile

# Build using all CPU cores
make -j$(nproc)

# This may take 15-60 minutes depending on your system
4

Install

# Install to system directories (requires sudo)
sudo make install
sudo ldconfig
Installation locations:
  • Binaries: /usr/local/bin
  • Libraries: /usr/local/lib
  • Headers: /usr/local/include/opencv4
  • CMake config: /usr/local/lib/cmake/opencv4

Verify Installation

# Check OpenCV version
pkg-config --modversion opencv4

# Test Python bindings
python3 -c "import cv2; print(cv2.__version__)"

Configuration Options

Customize your OpenCV build with these CMake options:

Common Options

# Performance optimizations
-DENABLE_FAST_MATH=ON
-DWITH_TBB=ON              # Threading Building Blocks
-DWITH_OPENMP=ON           # OpenMP support
-DWITH_IPP=ON              # Intel Performance Primitives

# Hardware acceleration
-DWITH_CUDA=ON             # NVIDIA CUDA support
-DWITH_OPENCL=ON           # OpenCL support

# Module options
-DBUILD_EXAMPLES=ON        # Build example applications
-DBUILD_TESTS=OFF          # Skip tests (faster build)
-DBUILD_PERF_TESTS=OFF     # Skip performance tests
-DBUILD_DOCS=ON            # Build documentation

# Language bindings
-DBUILD_opencv_python3=ON  # Python 3 bindings
-DBUILD_opencv_java=ON     # Java bindings

# Extra modules
-DOPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib/modules
For a list of all available options, run cmake -L in your build directory after initial configuration.

Language-Specific Setup

Python

Verify Python bindings:
import cv2
print(f"OpenCV version: {cv2.__version__}")
print(f"Build info:\n{cv2.getBuildInformation()}")

C++

Create a CMakeLists.txt for your project:
cmake_minimum_required(VERSION 3.9)
project(MyOpenCVApp)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(myapp main.cpp)
target_link_libraries(myapp ${OpenCV_LIBS})

Java

Add OpenCV to your Java project:
// Load native library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

// Verify installation
System.out.println("OpenCV version: " + Core.VERSION);

Troubleshooting

Install missing packages or specify paths manually:
cmake -DPYTHON3_EXECUTABLE=/usr/bin/python3 \
      -DPYTHON3_INCLUDE_DIR=/usr/include/python3.8 \
      -DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.8/site-packages/numpy/core/include \
      ..
Reduce parallel jobs:
make -j2  # Use only 2 cores instead of all
Check installation path:
import sys
print(sys.path)
Add OpenCV to Python path:
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.8/site-packages
Ensure OpenCV bin directory is in PATH:
echo %PATH%
Copy DLLs to your application directory as a workaround.
If you encounter errors, check the OpenCV forum or GitHub issues for solutions.

Next Steps

Now that OpenCV is installed, you’re ready to write your first computer vision application!

Quickstart Guide

Build your first OpenCV application with step-by-step examples

Build docs developers (and LLMs) love