OpenCV with CUDA
Build OpenCV 4.12 from source with CUDA and cuDNN acceleration on Ubuntu 24.04, for NVIDIA GPUs.
Recorded on an NVIDIA H200 server with Ubuntu 24.04, CUDA 12.0 and cuDNN 8.9.2. Not reconfirmed since; see Testing and references.
What you'll build
OpenCV and its contributed modules, built from source with CUDA, cuDNN and the CUDA backend for the DNN module, installed under /usr/local for C++ and Python. The steps work on other Ubuntu or Debian systems with NVIDIA GPUs, given a compatible CUDA, compiler and driver.
What the build includes
| Modules | Core, DNN with CUDA, CUDA image processing, feature and object detection, video I/O, and contributed modules such as aruco, face and text recognition |
|---|---|
| Image formats | JPEG, PNG, TIFF, WebP, JPEG 2000, OpenEXR, GIF |
| Video | FFmpeg, GStreamer, V4L/V4L2 |
| GUI | GTK3 |
| Python | 3.12, the system Python at /usr/bin/python3 |
| Not included | Non-free (patent-encumbered) algorithms, Java and MATLAB bindings |
To add any of these, rebuild with extra CMake options; see OpenCV's configuration reference.
Before you start
- Ubuntu 24.04 as recorded; other releases need a compatible CUDA and compiler.
- An NVIDIA GPU with CUDA support, and its driver installed.
- About 5 GB of free disk space for the build.
- Root or
sudoaccess.
1. Install the dependencies
-
Install the CUDA Toolkit and cuDNN from NVIDIA, and CMake, GCC 12 and Python 3 from Ubuntu:
Package Version Where to get it CUDA Toolkit A CUDA 12 release compatible with your OS, compiler and GPU NVIDIA CUDA downloads cuDNN 8.9.2 or compatible NVIDIA cuDNN (free developer account) CMake 3.15 or later sudo apt install cmakeGCC and G++ 12 recommended sudo apt install gcc-12 g++-12Python 3 With headers and NumPy sudo apt install python3 python3-dev python3-numpy -
Install the image and video libraries, which add support for common formats:
sudo apt 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 libtbb-dev \ libdc1394-dev libopenexr-dev libgstreamer-plugins-base1.0-dev \ libgstreamer1.0-dev libwebp-dev
2. Find your GPU's compute capability
The build needs your GPU's compute capability.
- Check your GPU model with
nvidia-smi. - Look up its compute capability on NVIDIA's CUDA GPUs page.
- Note the value, for example 9.0 for H100 and H200, 8.6 for RTX 3090, or 7.5 for RTX 2080.
3. Get the source
-
Make a working folder:
mkdir opencv cd opencv -
Download OpenCV and its contributed modules:
curl -Lo opencv-4.12.0.tar.gz https://github.com/opencv/opencv/archive/4.12.0.tar.gz curl -Lo opencv_contrib-4.12.0.tar.gz https://github.com/opencv/opencv_contrib/archive/4.12.0.tar.gz -
Extract both:
tar xf opencv-4.12.0.tar.gz tar xf opencv_contrib-4.12.0.tar.gz -
Make the build folder:
mkdir build cd build
4. Configure with CMake
Use your compute capability
The command below uses CUDA_ARCH_BIN=9.0 for an H200. Replace 9.0 with your GPU's value from step 2.
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.12.0/modules \
../opencv-4.12.0 \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CMAKE_C_COMPILER=gcc-12 \
-D CMAKE_CXX_COMPILER=g++-12 \
-D CUDA_ARCH_BIN=9.0
| Option | What it does |
|---|---|
OPENCV_EXTRA_MODULES_PATH | Adds the contributed modules |
WITH_CUDA=ON | Turns on CUDA |
WITH_CUDNN=ON | Turns on cuDNN for deep learning |
OPENCV_DNN_CUDA=ON | Uses CUDA in the DNN module |
CMAKE_C_COMPILER, CMAKE_CXX_COMPILER | Choose GCC 12 |
CUDA_ARCH_BIN | Your GPU's compute capability |
The build installs under /usr/local in step 6, where the system package manager doesn't track it, as OpenCV notes. To keep it separate, add -D CMAKE_INSTALL_PREFIX= with another location to this command.
The configuration summary shows CUDA and cuDNN as found. If either says NO, see Troubleshooting before compiling.
5. Compile
This uses 60 parallel jobs; set -j to your core count, or use -j $(nproc) to use them all. It takes 15–60 minutes.
cmake --build . -j 60
6. Install
-
Install:
sudo cmake --install .Programs go in
/usr/local/bin, libraries in/usr/local/liband headers in/usr/local/include/opencv4. The Python module goes in the path CMake reported; check which copy Python loads withpython3 -c "import cv2; print(cv2.__file__)". -
Refresh the library cache so the system finds the new libraries:
sudo ldconfig
7. Verify
Check the build from Python, then from C++.
Python
python3 -c "import cv2; print(f'OpenCV version: {cv2.__version__}')"
OpenCV version: 4.12.0
CUDA support
python3 -c "import cv2; print(f'CUDA devices: {cv2.cuda.getCudaEnabledDeviceCount()}')"
The count is greater than 0.
Build information
python3 -c "import cv2; print(cv2.getBuildInformation())" | grep -A 5 "NVIDIA CUDA"
It shows your CUDA version and GPU architecture.
C++
-
Create
test_opencv.cpp:#include <opencv2/opencv.hpp> #include <opencv2/cudaarithm.hpp> #include <iostream> int main() { std::cout << "OpenCV version: " << CV_VERSION << std::endl; std::cout << "CUDA devices: " << cv::cuda::getCudaEnabledDeviceCount() << std::endl; return 0; } -
Compile and run it:
g++ test_opencv.cpp -o test_opencv $(pkg-config --cflags --libs opencv4) ./test_opencv
It prints version 4.12.0 and a CUDA device count above 0.
Troubleshooting
CMake reports CUDA: NO
- Check CUDA is installed:
nvcc --version. - Make sure
/usr/local/cuda/binis on yourPATH. - Check the CUDA libraries are in
/usr/local/cuda/lib64.
CMake reports cuDNN: NO
- Check cuDNN is in
/usr/local/cuda/includeand/usr/local/cuda/lib64. - Make sure the cuDNN version matches your CUDA toolkit.
- Set
CUDNN_INCLUDE_DIRandCUDNN_LIBRARYexplicitly in CMake.
Errors about the CUDA driver version
nvidia-smishows both the driver version and the CUDA version it supports.- Make sure the driver supports your CUDA toolkit.
- Update it with
sudo apt install nvidia-driver-xxx, replacingxxxwith the right version.
Python can't load libopencv_core.so.4.12
- Run
sudo ldconfig. - Check that
/usr/local/libis listed in/etc/ld.so.conf.d/. - Check Python's path:
python3 -c "import sys; print(sys.path)".
The build fails with compiler errors
- Check every prerequisite is installed.
- Try another GCC version (9, 10, 11 or 12).
- Check there's about 5 GB of free disk space.
- Empty the build folder and configure again:
rm -rf * && cmake ..., from insideopencv/buildonly.
Testing and references
The original notes record Ubuntu 24.04, CUDA 12.0, cuDNN 8.9.2 and an H200. That exact environment hasn't been reconfirmed, and CUDA 12.0's supported distributions don't include Ubuntu 24.04. Check the toolkit, compiler, driver and build logs before treating it as a supported recipe. Selected commands were checked against documentation in September 2026; the build wasn't rerun.