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

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- 
sudo cp /var/cudnn-local-repo-ubuntu2004- /usr/share/keyrings/
sudo apt-get update
sudo apt-get install libcudnn8=
sudo apt-get install libcudnn8-dev=
sudo apt-get install libcudnn8-samples=

Verify the install following the cuDNN instructions.

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

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/

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
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
git clone

Build and Install OpenCV

cd opencv
mkdir build
cd build

	-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 \

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/

Check that OpenCV works

workon opencv_cuda
import cv2