How to Compile OpenCV 4.5.2 with CUDA 11.2 and cuDNN 8.1 on Ubuntu 20.04

Notes to self after fresh install of 20.04 (16.04 finally reached EOL).

Install NVIDIA drivers

Launch “Software & Updates.” Under “Additional Drivers” install metapackage from nvidia-driver-460.

Install CUDA 11.2

Download CUDA from NVIDIA using deb (network)

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda

Per the installation instructions, after installation add the following entries to the end of .bashrc

export PATH=/usr/local/cuda-11.2/bin${PATH:+:${PATH}}:/home/jlee/bin
export LD_LIBRARY_PATH=/usr/local/cuda-11.2/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

Install cuDNN 8.1

Download cuDNN from NVIDIA

sudo dpkg -i libcudnn8_8.1.1.33-1+cuda11.2_amd64.deb
sudo dpkg -i libcudnn8-dev_8.1.1.33-1+cuda11.2_amd64.deb
sudo dpkg -i libcudnn8-samples_8.1.1.33-1+cuda11.2_amd64.deb

Verify the the install following the cuDNN instructions. libfreeimage-dev is needed to compile the samples. The test should pass.

sudo apt-get install libfreeimage-dev libfreeimageplus-dev

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

Install OpenCV 4.5.2

Setup python, use virtualenvwrapper, add virtualenvwrapper entries to .bashrc

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

sudo pip install virtualenv virtualenvwrapper

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, install numpy, activate virtualenv

mkvirtualenv opencv_cuda
pip install -U numpy
workon opencv_cuda

Install necessary system packages. I might have missed a couple packages and some might be unnecessary since it was an iterative process to get CMake to reduce the gaps and errors.

sudo apt-get install build-essential cmake pkg-config unzip yasm git checkinstall
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libavresample-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 libmp3lame-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

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

mkdir opencvbuild
cd opencvbuild
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.2.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.2.zip
unzip opencv.zip
unzip opencv_contrib.zip

Build and Install OpenCV

cd opencv-4.5.2
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=6.1 \
	-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.2/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.8/site-packages/
ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so cv2.so

Check that OpenCV works

workon opencv_cuda
python
import cv2
cv2.__version__