Skip to content

Building on Linux

Install the required packages with apt:

Terminal window
# Update package index
sudo apt-get update
# Core build tools (choose one compiler)
sudo apt-get install -y build-essential cmake git pkg-config
# Optional: use Ninja for faster builds
sudo apt-get install -y ninja-build
# Required media/codec libraries
sudo apt-get install -y \
libgtk2.0-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev
# Python 3 bindings
sudo apt-get install -y \
python3-dev \
python3-numpy
# Image format libraries
sudo apt-get install -y \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libopenexr-dev
# Optional performance libraries
sudo apt-get install -y \
libtbb2 \
libtbb-dev \
libeigen3-dev
# Optional camera support
sudo apt-get install -y \
libdc1394-dev \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev
  1. Clone the three repositories:

    Terminal window
    git clone https://github.com/opencv/opencv.git
    git clone https://github.com/opencv/opencv_contrib.git
    git clone https://github.com/opencv/opencv_extra.git
  2. Create a build directory:

    Terminal window
    mkdir opencv/build
    cd opencv/build
  3. Configure with CMake (development / debug build):

    Terminal window
    cmake \
    -DCMAKE_BUILD_TYPE=Debug \
    -DBUILD_TESTS=ON \
    -DBUILD_PERF_TESTS=ON \
    -DBUILD_EXAMPLES=ON \
    -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -DOPENCV_TEST_DATA_PATH=../../opencv_extra/testdata \
    ..
  4. Build using all available CPU cores:

    Terminal window
    cmake --build . -j$(nproc)
    # Or using make directly:
    make -j$(nproc)
  5. Verify the build:

    Terminal window
    # Check build info from the command line
    ./bin/opencv_version
    # Check from Python (no install needed with PYTHONPATH set)
    export PYTHONPATH=$(pwd)/python_loader:$(pwd)/lib/python3/
    export LD_LIBRARY_PATH=$(pwd)/lib:$LD_LIBRARY_PATH
    python3 -c "import cv2; print(cv2.getBuildInformation())"

Using OpenCV from the Build Directory (No Install)

Section titled “Using OpenCV from the Build Directory (No Install)”

Downstream CMake projects can use the build directory directly by setting OpenCV_DIR:

Terminal window
# In your own project's CMakeLists.txt:
# find_package(OpenCV REQUIRED)
# ...then configure pointing to the build dir:
cmake -DOpenCV_DIR=/path/to/opencv/build ..
Terminal window
cmake -G Ninja \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_TESTS=ON \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
..
ninja -j$(nproc)
Build TypeGCC FlagsUse Case
Debug-g -O0Full symbols, no optimization — step through code in a debugger
Release-O3 -DNDEBUGMaximum performance, no debug info
RelWithDebInfo-O2 -g -DNDEBUGOptimized + symbols — best for profiling
MinSizeRel-Os -DNDEBUGSmallest binary size
Terminal window
# Build only specific modules (auto-resolves dependencies)
cmake -DBUILD_LIST=core,imgproc,imgcodecs ..
# Disable individual modules
cmake -DBUILD_opencv_java=OFF \
-DBUILD_opencv_gapi=OFF \
..
# Disable OpenCV's internal test apps
cmake -DBUILD_opencv_apps=OFF ..