Installing OpenCV 4 with CUDA in Ubuntu 22.04
NOTE: this guide is also compatible and has been tested in Ubuntu 20.04.
Installing a pre-compiled version of OpenCV can lead to not take advantage of the possibilities of your CUDA-capable hardware, which can be very powerful in terms of GPU acceleration.
In this guide, we will be building OpenCV from source making use of a preinstalled environment with CUDA and cuDNN.
1. Pre-requisites
1.1. CUDA and cuDNN
The main pre-requisite is to have the NVIDIA CUDA Toolkit and the NVIDIA CUDA Deep Neural Network library (cuDNN) library installed.
If you do not have these installed already, I kindly recommend to follow my previous guide on how to install them:
Once you are set, let’s continue with the dependencies.
1.2. Dependencies
CMake and GCC are required to configure the installation and compile the project:
$ sudo apt install cmake$ sudo apt install gcc g++
To support Python 3 installations, we need Python (it comes pre-installed with most Ubuntu versions), Python-devel and Numpy:
$ sudo apt install python3 python3-dev python3-numpy
GTK is requried for GUI features, Camera support (v4l), Media Support (ffmpeg, gstreamer…), etc.:
$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev$ sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev$ sudo apt install libgtk-3-dev
The next dependencies are optional but add the latest support for PNG, JPEG, JPEG2000, TIFF, WebP, etc. formats:
$ sudo apt install libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev
Now, download the OpenCV repository using Git:
$ git clone https://github.com/opencv/opencv.git
We will also be downloading the OpenCV’s extra modules (CMake flag -D OPENCV_EXTRA_MODULES_PATH
) repository. These modules are required to use the CUDA funcionality with OpenCV.
$ git clone https://github.com/opencv/opencv_contrib.git
2. Determine the CUDA architecture version
CMake needs a CUDA_ARCH_BIN
flag to be set in order to compile the binaries with the correct CUDA architecture. If this flag is not set correctly, the final use of the binaries would fail.
To set the flag correctly, let’s first determine the NVIDIA GPU model using the nvidia-smi -L
command:
Now go to https://developer.nvidia.com/cuda-gpus and look for your GPU model. In my case, I had to go to CUDA-Enabled GeForce and TITAN Products and then find the GeForce GTX 1050:
Next to the model name, you will find the Comput Capability of the GPU. This is the NVIDIA GPU architecture version, which will be the value for the CMake flag: CUDA_ARCH_BIN=6.1
.
3. Prepare to compile with CUDA and cuDNN support
We will be using a bunch of CMake flags to compile OpenCV. You can find a detailed reference of these at the end of this article.
Prepare the build directory:
$ cd ~/opencv$ mkdir build$ cd build
Run CMake with the following flags:
$ cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D WITH_TBB=ON \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D CUDA_ARCH_BIN=6.1 \
-D OPENCV_EXTRA_MODULES_PATH=$HOME/opencv_contrib/modules \
-D BUILD_EXAMPLES=OFF \
-D HAVE_opencv_python3=ON \
..
The \
tell the console to expect a new line inside the same command. The following command is equivalent to that one:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUDNN=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D OPENCV_DNN_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON -D CUDA_ARCH_BIN=6.1 -D OPENCV_EXTRA_MODULES_PATH=$HOME/opencv_contrib/modules -D BUILD_EXAMPLES=OFF -D HAVE_opencv_python3=ON ..
You should have an output similar to the following one:
...-- The CXX compiler identification is GNU 9.4.0...-- Performing Test......-- General configuration for OpenCV 4.5.5-dev ======================...-- NVIDIA CUDA: YES (ver 11.6, CUFFT CUBLAS)
-- NVIDIA GPU arch: 61
-- NVIDIA PTX archs:
--
-- cuDNN: YES (ver 8.3.2)...-- Configuring done
-- Generating done
-- Build files have been written to: /home/j/opencv/build
That indicates that the pre-build was succesful.
4. Compile OpenCV with GPU acceleration support
If CMake exited without any errors, we are ready to compile OpenCV.
We just need to know how many cores our CPU has available. To do that, use the lscpu
command:
Look for the CPU(s)
setting, which we will use in the next command.
To finally compile OpenCV, use make
with the -j
or --jobs
option, which specifies the number of jobs to run simultaneously, and whose value must equal the number of cores we found out previously:
$ make -j 8
In my case it took half an hour to fully complete.
5. Install OpenCV
After compiling OpenCV with GPU acceleration support through CUDA and cuDNN, we are ready to install it as if we had downloaded a pre-compiled package.
Inside the build
directory, run this command:
$ sudo make install
Then use ldconfig
to create the necessary links and cache to our freshly-installed OpenCV:
$ sudo ldconfig
The final step is to create symbolic links to the OpenCV bindings for Python 3, to be used globally. This is required if you use the default Ubuntu installation, as its executable looks for packages in a dist-packages
directory, and OpenCV is installed at site-packages
by default.
To do this, run the following command:
$ sudo ln -s /usr/local/lib/python3.8/site-packages/cv2 /usr/local/lib/python3.8/dist-packages/cv2
Use the python3
interpreter and the cv2.cuda.printCudaDeviceInfo(0)
to verify that the library is working correctly:
And that’s it 😌.
Clean up
After the installation, there are some files that we will not be needing anymore, and can be safely deleted:
$ cd ~$ rm -rf opencv opencv_contrib
Used CMake flags reference
- CMAKE_BUILD_TYPE=RELEASE: to disable debug opions
- CMAKE_INSTALL_PREFIX=/usr/local: to set the root location for the produced binaries
- WITH_CUDA=ON: to enable CUDA support
- WITH_CUDNN=ON: (undocumented) to enable cuDNN support
- WITH_CUBLAS=ON: (undocumented) to enable cuBLAS support
- WITH_TBB=ON: to enable parallel programming with Threading Building Blocks
- OPENCV_DNN_CUDA=ON: to enable CUDA backend and build the
dnn
module (requires CUDA, cuDNN and cuBLAS) - OPENCV_ENABLE_NONFREE=ON: to enable patent-protected algorithms
- CUDA_ARCH_BIN=6.1: to specify the NVIDIA GPU architecture version (refer to section 3)
- OPENCV_EXTRA_MODULES_PATH=$HOME/opencv_contrib/modules: to include OpenCV’s extra modules
- BUILD_EXAMPLES=OFF: to disable building examples
- HAVE_opencv_python3=ON: to force the
opencv_python3
dependency
Sources
- Install OpenCV-Python in Ubuntu https://docs.opencv.org/4.5.5/d2/de6/tutorial_py_setup_in_ubuntu.html
- OpenCV installation overview https://docs.opencv.org/4.x/d0/d3d/tutorial_general_install.html
- OpenCV configuration options reference https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html
- OpenCV’s extra modules repository https://github.com/opencv/opencv_contrib