Build guide

Build OpenCV from source on Windows with CMake — step-by-step guide

Build OpenCV from source on Windows using CMake and Visual Studio. Covers getting the source, CMake configuration, build flags, CUDA options and installing Python bindings.

Building from source is required for CUDA support, custom modules or the latest git version. For most users, pip install opencv-python is faster and easier.

What you need before building

ToolVersionDownload
Visual Studio2019 or 2022visualstudio.com (Community is free)
CMake3.16+cmake.org/download
Python3.8+ (optional)python.org (for Python bindings)
GitAnygit-scm.com (optional)

Get the OpenCV source code

PowerShell
# Clone from GitHub:
PS> git clone https://github.com/opencv/opencv.git
PS> git clone https://github.com/opencv/opencv_contrib.git
# Or download the .zip from GitHub Releases and extract
# Place both folders side by side:
C:\build\opencv\
C:\build\opencv_contrib\

Configure with CMake

PowerShell — from opencv folder
PS> mkdir build; cd build
# Basic build (Python bindings + contrib):
PS> cmake .. `
-G "Visual Studio 17 2022" -A x64 `
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules `
-DBUILD_TESTS=OFF `
-DBUILD_PERF_TESTS=OFF `
-DBUILD_EXAMPLES=OFF `
-DCMAKE_INSTALL_PREFIX=C:/opencv_build
# With CUDA (add these flags):
-DWITH_CUDA=ON -DCUDA_ARCH_BIN="8.6" -DWITH_CUDNN=ON
Use -G "Visual Studio 17 2022" for VS 2022 or "Visual Studio 16 2019" for VS 2019. Always use -A x64 for 64-bit.

Build and install

PowerShell
# Build Release configuration (use all CPU cores):
PS> cmake --build . --config Release --parallel
# Takes 30-90 minutes depending on CPU and options
# Install to CMAKE_INSTALL_PREFIX:
PS> cmake --install . --config Release
-- Install configuration: "Release"
-- Installing: C:/opencv_build/include/opencv2/...

Install Python bindings from the build

PowerShell
# Find the built .pyd file:
PS> Get-ChildItem -Recurse -Filter "cv2*.pyd" .
build\lib\python3\Release\cv2.cpython-311-x64.pyd
# Copy to Python site-packages:
PS> python -c "import site; print(site.getsitepackages())"
['C:\\Python311\\Lib\\site-packages']
PS> copy build\lib\python3\Release\cv2*.pyd C:\Python311\Lib\site-packages\cv2\
# Or use pip install -e (editable):
PS> pip install --no-build-isolation -e .

CMake build questions

CMake cannot find Python
Specify Python paths explicitly in cmake: -DPYTHON3_EXECUTABLE=C:/Python311/python.exe -DPYTHON3_INCLUDE_DIR=C:/Python311/include -DPYTHON3_LIBRARY=C:/Python311/libs/python311.lib
Build fails with "CUDA not found"
Ensure CUDA Toolkit is installed and nvcc is on PATH: run nvcc --version in a new terminal. Also set -DCUDA_TOOLKIT_ROOT_DIR="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3" explicitly.
How long does the build take?
A basic build without CUDA takes 20-40 minutes on a modern CPU. A full CUDA build takes 60-120 minutes. Use --parallel to use all CPU cores. Avoid building tests and examples to save time.

Building for CUDA?

Full CUDA build guide with CUDA_ARCH_BIN values.

CUDA guide