Building on Linux
Prerequisites
Section titled “Prerequisites”Install the required packages with apt:
# Update package indexsudo 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 buildssudo apt-get install -y ninja-build
# Required media/codec librariessudo apt-get install -y \ libgtk2.0-dev \ libavcodec-dev \ libavformat-dev \ libswscale-dev
# Python 3 bindingssudo apt-get install -y \ python3-dev \ python3-numpy
# Image format librariessudo apt-get install -y \ libjpeg-dev \ libpng-dev \ libtiff-dev \ libopenexr-dev
# Optional performance librariessudo apt-get install -y \ libtbb2 \ libtbb-dev \ libeigen3-dev
# Optional camera supportsudo apt-get install -y \ libdc1394-dev \ libgstreamer1.0-dev \ libgstreamer-plugins-base1.0-devStep-by-Step Build
Section titled “Step-by-Step Build”-
Clone the three repositories:
Terminal window git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.gitgit clone https://github.com/opencv/opencv_extra.git -
Create a build directory:
Terminal window mkdir opencv/buildcd opencv/build -
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 \.. -
Build using all available CPU cores:
Terminal window cmake --build . -j$(nproc)# Or using make directly:make -j$(nproc) -
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_PATHpython3 -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:
# 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 ..Ninja Build (Faster Alternative)
Section titled “Ninja Build (Faster Alternative)”cmake -G Ninja \ -DCMAKE_BUILD_TYPE=Debug \ -DBUILD_TESTS=ON \ -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ ..
ninja -j$(nproc)Release vs Debug vs RelWithDebInfo
Section titled “Release vs Debug vs RelWithDebInfo”| Build Type | GCC Flags | Use Case |
|---|---|---|
Debug | -g -O0 | Full symbols, no optimization — step through code in a debugger |
Release | -O3 -DNDEBUG | Maximum performance, no debug info |
RelWithDebInfo | -O2 -g -DNDEBUG | Optimized + symbols — best for profiling |
MinSizeRel | -Os -DNDEBUG | Smallest binary size |
Disabling Specific Modules
Section titled “Disabling Specific Modules”# Build only specific modules (auto-resolves dependencies)cmake -DBUILD_LIST=core,imgproc,imgcodecs ..
# Disable individual modulescmake -DBUILD_opencv_java=OFF \ -DBUILD_opencv_gapi=OFF \ ..
# Disable OpenCV's internal test appscmake -DBUILD_opencv_apps=OFF ..