How to Compile OpenCV 4.6.0-dev with CUDA 11.7 and cuDNN 8.4.1 on Ubuntu 22.04

Install CUDA 11.7

Follow the CUDA installation guide

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64//cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda
sudo apt-get install nvidia-gds
sudo reboot

Per the CUDA post-installation instructions, add the following entries to the end of .bashrc

export PATH=/usr/local/cuda-11.7/bin${PATH:+:${PATH}}:/home/jlee/bin
export LD_LIBRARY_PATH=/usr/local/cuda-11.7/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Install third party libraries

sudo apt-get install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev libfreeimage-dev

Install cuDNN 8.4.1

Download cuDNN from NVIDIA. As of June 2022, cuDNN hasn’t been added to the 22.04 repo, so install the local deb file for Ubuntu 20.04 per cuDNN installation guide. cuDNN with CUDA 11.7 wasn’t available, so fall back to 11.6.

sudo dpkg -i cudnn-local-repo-ubuntu2004-8.4.1.50_1.0-1_amd64.deb 
sudo cp /var/cudnn-local-repo-ubuntu2004-8.4.1.50/cudnn-local-E3EC4A60-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get install libcudnn8=8.4.1.50-1+cuda11.6
sudo apt-get install libcudnn8-dev=8.4.1.50-1+cuda11.6
sudo apt-get install libcudnn8-samples=8.4.1.50-1+cuda11.6

Verify the install following the cuDNN instructions.

cp -r /usr/src/cudnn_samples_v8/ $HOME
cd  $HOME/cudnn_samples_v8/mnistCUDNN
make clean && make
./mnistCUDNN

Install OpenCV 4.6.0-dev

Setup python using virtualenvwrapper

sudo apt-get install python3-dev python3-pip python3-testresources
sudo pip install virtualenv virtualenvwrapper

Add virtualenvwrapper entries to .bashrc

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/pyenvs
export PROJECT_HOME=$HOME/projects
source /usr/local/bin/virtualenvwrapper.sh

Create virtualenv for OpenCV, activate virtualenv (should be activated after making it), and install numpy.

mkvirtualenv opencv_cuda
workon opencv_cuda
pip install -U numpy

Install necessary system packages

sudo apt-get install build-essential cmake pkg-config unzip yasm git checkinstall
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libopenjp2-7-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libfaac-dev libvorbis-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libtbb-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

sudo apt-get install libgtkglext1 libgtkglext1-dev

sudo apt-get install libopenblas-dev liblapacke-dev libva-dev libopenjp2-tools libopenjpip-dec-server libopenjpip-server libqt5opengl5-dev libtesseract-dev 

Install Ceres Solver

sudo apt-get install cmake libeigen3-dev libgflags-dev libgoogle-glog-dev libsuitesparse-dev libatlas-base-dev libmetis-dev

git clone https://ceres-solver.googlesource.com/ceres-solver
cd ceres-solver
mkdir build && cd build
cmake ..
make -j4
make test
sudo make install

Download OpenCV and OpenCV Contrib. In June 2022, the 4.6.0 release didn’t include the fix to compile properly with the latest Ceres release. Cloning and building 4.6.0-dev from the repos includes the fix. Future OpenCV releases shouldn’t have this issue.

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Build and Install OpenCV

cd opencv
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D WITH_TBB=ON \
	-D OPENCV_ENABLE_NONFREE=ON \
	-D WITH_CUDA=ON \
	-D WITH_CUDNN=ON \
	-D OPENCV_DNN_CUDA=ON \
	-D ENABLE_FAST_MATH=1 \
	-D CUDA_FAST_MATH=1 \
	-D CUDA_ARCH_BIN=8.6 \
	-D WITH_CUBLAS=1 \
	-D WITH_OPENGL=ON \
	-D WITH_QT=ON \
	-D OpenGL_GL_PREFERENCE=LEGACY \
	-D OPENCV_EXTRA_MODULES_PATH=/home/jlee/software/opencvbuild/opencv_contrib-4.5.5/modules \
	-D PYTHON_DEFAULT_EXECUTABLE=/home/jlee/pyenvs/opencv_cuda/bin/python \
	-D BUILD_EXAMPLES=ON ..

make -j$(nproc)
sudo make install
sudo ldconfig

Create symbolic link to OpenCV in virtualenv

cd ~/pyenvs/opencv_cuda/lib/python3.10/site-packages/
ln -s /usr/local/lib/python3.10/site-packages/cv2/python-3.10/cv2.cpython-39-x86_64-linux-gnu.so cv2.so

Check that OpenCV works

workon opencv_cuda
python
import cv2
cv2.__version__