Compiling and Installing OpenCV with CUDA support from scratch

Compiling and Installing OpenCV with CUDA support from scratch

Many times, I find myself trying to install OpenCV with CUDA support and in every post, I find a different answer, so I figured why not try to add one from scratch for the people that like to see what might be going wrong under the hood instead of using precompiled packages.

Here are the specs and versions we will be using over this blog:

Component Version
OS Ubuntu 24.04
CUDA 12.5
OpenCV 5.x
GCC 13.2

Installing CUDA

As we want to have OpenCV with CUDA from scratch, what better way to start than installing CUDA?

To install CUDA, the instructions provided by Nvidia are too clear, which means that these are too detailed, and you might get lost (as I did). The important section is the following:

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/#download-the-nvidia-cuda-toolkit

In there you can reach a point in which you can select which OS you have, the way you want to install it, and more. The issue that was presented to me was that no Ubuntu24 package is there for CUDA 12.5, I just solved it by using the local runfile (.run file), which installes CUDA toolkit manually. Just be aware, that using this runfile will ask you if you also want to install an Nvidia Driver, which you may want if you haven’t already.

After the process is done, you must export the cuda binaries into your path, personally I do it simple and export them in the ~/.bashrc as I use bash:
export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64
export PATH=$PATH:$CUDA_HOME/bin

 

After this, you can reload a new terminal or source ~/.bashrc (however you want it), then check that CUDA is installed:

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Thu_Jun__6_02:18:23_PDT_2024
Cuda compilation tools, release 12.5, V12.5.82
Build cuda_12.5.r12.5/compiler.34385749_0

I strongly recommend to check if nvidia-smi is working too:

$ nvidia-smi
Fri Aug 23 14:18:19 2024       
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 555.42.06              Driver Version: 555.42.06      CUDA Version: 12.5     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 3070 Ti     Off |   00000000:05:00.0  On |                  N/A |
| 56%   50C    P3             42W /  310W |     589MiB /   8192MiB |      7%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
                                                                                         
+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A      2718      G   /usr/lib/xorg/Xorg                            272MiB |
|    0   N/A  N/A      3055      G   /usr/bin/gnome-shell                           61MiB |
|    0   N/A  N/A      3712      G   ...seed-version=20240821-050100.700000         54MiB |
|    0   N/A  N/A      4579      G   ...erProcess --variations-seed-version        176MiB |
+-----------------------------------------------------------------------------------------+

Installing OpenCV with CUDA support

Now, what I believe is the real deal here, installing OpenCV with CUDA support… composed of the following steps:

Install dependencies

The following are some core dependencies, not all are 100% needed, just depending on what you need OpenCV for, also others might need to be added.
sudo apt install \
    build-essential \
    cmake \
    cmake-data \
    git \
    gcc-13 \
    g++-13 \
    libavcodec-dev \
    libavformat-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    libpng-dev \
    pkg-config \
    python3-numpy \
    python3-pip

Clone and set OpenCV repos

We want to build full OpenCV 5.x, even with the contrib modules.

git clone --branch 5.x --depth 1 --single-branch https://github.com/opencv/opencv.git
cd opencv
git clone --branch 5.x --depth 1 --single-branch https://github.com/opencv/opencv_contrib.git
export SRC_DIR=$(pwd)

Build and install OpenCV

First thing is to configure the OpenCV to build:

mkdir build && cd build
cmake \
    -D WITH_CUDA=ON \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=OFF \
    -D CUDA_ARCH_BIN=$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader) \
    -D CMAKE_C_COMPILER=gcc-13 \
    -D CMAKE_CXX_COMPILER=g++-13 \
    -D OPENCV_EXTRA_MODULES_PATH=${SRC_DIR}/opencv_contrib/modules/ \
    -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
    -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
    -D PYTHON3_EXECUTABLE="/usr/bin/python3" \
    ${SRC_DIR}

In this case you can see that you don’t need to modify anything, all the paths for the python3 bindings and the compute capability of the GPU for CUDA is already obtained from a command. We use GCC13, which is supported by both this CUDA and OpenCV versions. But be aware! There is a catch, we didn’t want to use DNN, so we disabled it too, but you can just set it to ON after installing DNN package.

After configure, execute build in that same directory:

make

After that, you can install the library and the Python bindings by using a simple make install:

make install

You can test the installation with a basic command on Python:

python3 -c "import cv2"

If that worked, we have completed the installation of OpenCV with CUDA support, if you run into any trouble, please don’t hesitate to contact us at https://proventusnova.com/contact-us/