Nicholas Dodds

OpenCV with CUDA

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

ModulesCore, DNN with CUDA, CUDA image processing, feature and object detection, video I/O, and contributed modules such as aruco, face and text recognition
Image formatsJPEG, PNG, TIFF, WebP, JPEG 2000, OpenEXR, GIF
VideoFFmpeg, GStreamer, V4L/V4L2
GUIGTK3
Python3.12, the system Python at /usr/bin/python3
Not includedNon-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

1. Install the dependencies

  1. Install the CUDA Toolkit and cuDNN from NVIDIA, and CMake, GCC 12 and Python 3 from Ubuntu:

    PackageVersionWhere to get it
    CUDA ToolkitA CUDA 12 release compatible with your OS, compiler and GPUNVIDIA CUDA downloads
    cuDNN8.9.2 or compatibleNVIDIA cuDNN (free developer account)
    CMake3.15 or latersudo apt install cmake
    GCC and G++12 recommendedsudo apt install gcc-12 g++-12
    Python 3With headers and NumPysudo apt install python3 python3-dev python3-numpy

2. Find your GPU's compute capability

The build needs your GPU's compute capability.

  1. Check your GPU model with nvidia-smi.
  2. Look up its compute capability on NVIDIA's CUDA GPUs page.
  3. 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

  1. Make a working folder:

    mkdir opencv
    cd opencv
  2. 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
  3. Extract both:

    tar xf opencv-4.12.0.tar.gz
    tar xf opencv_contrib-4.12.0.tar.gz
  4. 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
OptionWhat it does
OPENCV_EXTRA_MODULES_PATHAdds the contributed modules
WITH_CUDA=ONTurns on CUDA
WITH_CUDNN=ONTurns on cuDNN for deep learning
OPENCV_DNN_CUDA=ONUses CUDA in the DNN module
CMAKE_C_COMPILER, CMAKE_CXX_COMPILERChoose GCC 12
CUDA_ARCH_BINYour 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

  1. Install:

    sudo cmake --install .

    Programs go in /usr/local/bin, libraries in /usr/local/lib and headers in /usr/local/include/opencv4. The Python module goes in the path CMake reported; check which copy Python loads with python3 -c "import cv2; print(cv2.__file__)".

  2. 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++

  1. 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;
    }
  2. 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

CMake reports cuDNN: NO

Errors about the CUDA driver version

Python can't load libopencv_core.so.4.12

The build fails with compiler errors

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.