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.Requirements
What you need before building
| Tool | Version | Download |
|---|---|---|
| Visual Studio | 2019 or 2022 | visualstudio.com (Community is free) |
| CMake | 3.16+ | cmake.org/download |
| Python | 3.8+ (optional) | python.org (for Python bindings) |
| Git | Any | git-scm.com (optional) |
Step 1
Get the OpenCV source code
# 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\
Step 2
Configure with CMake
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.Step 3
Build and install
# 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/...
Python bindings
Install Python bindings from the build
# 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 .
FAQ
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.libBuild 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.