Install OpenCV 4.5.1 with CUDA and cuDNN acceleration, with GStreamer and Python 3 bindings on PopOS 20.10

OpenCV is awesome. OpenCV + CUDA = More awesome. OpenCV + CUDA + cuDNN (deep neural networks) = MIND BLOWN.

What is cuDNN?

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers.

Compiling from source is always a pain. I’m here to make your life easier. Today I’m going to show you how to build and install OpenCV 4.5.1 with CUDA and cuDNN acceleration, with GStreamer and Python 3 bindings on PopOS 20.10. Since PopOS is based on Ubuntu, the instructions will work on that as well.



My workstation specs are:

AMD Ryzen 3950X.


Fresh install of PopOS 20.10 with NVIDIA drivers.

Check your GPUs CUDA version support

Go to NVIDIA’s website to check your which version of CUDA it supports:

Make a note of the version number as we’ll need this information when we compile OpenCV. For my RTX 2080TI, the CUDA version is 7.5.

Update PopOS

sudo apt updatesudo apt dist-upgrade -y --autoremove

Install CUDA and cuDNN libraries

Installing cuDNN will also install matching CUDA version.

sudo apt install system76-cudnn-11.1

If for some reason CUDA was not installed, you can install it with:

sudo apt install system76-cuda-11.1

Download cuDNN source files from NVIDIA.

These source files are needed to compile OpenCV.

The file you want is: cudnn-11.1-linux-x64-v8.0.5.39.tgz

Put the file in your home directory and we’ll move these into the right place:

cd ~mv cudnn-11.1-linux-x64-v8.0.5.39.tgz cudnn.tgztar -xvf cudnn.tgz

Make the appropriate directories:

sudo mkdir -p /usr/local/cuda/lib64/sudo mkdir -p /usr/local/cuda/include

Copy the extracted files to the folders we created:

cd ~/cuda/lib64sudo cp * /usr/local/cuda/lib64/
cd ~/cuda/includesudo cp * /usr/local/cuda/include

Create Python virtual environment

Install virtual environment tools:

sudo pip install virtualenv virtualenvwrappersudo rm -rf ~/.cache/pip

Add the appropriate entries to bashrc:

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrcecho "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrcecho "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrcecho "source /usr/local/bin/" >> ~/.bashrcsource ~/.bashrc

Create a virtual environment called CV:

mkvirtualenv cv -p python3workon cv

Install numpy in our virtual environment as it will be need to compile OpenCV:

pip install numpy

Install dependencies

sudo apt install -y \
build-essential \
cmake \
git \
gcc \
g++ \
yasm \
checkinstall \
gfortran \
libatlas-base-dev \
libavcodec-dev \
libavformat-dev \
libavresample-dev \
libcanberra-gtk3-module \
libdc1394-22-dev \
libeigen3-dev \
libglew-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-good1.0-dev \
libgstreamer1.0-dev \
libgtk-3-dev \
libjpeg-dev \
libjpeg8-dev \
libjpeg-turbo8-dev \
liblapack-dev \
liblapacke-dev \
libopenblas-dev \
libpng-dev \
libpostproc-dev \
libswscale-dev \
libtbb-dev \
libtbb2 \
libtesseract-dev \
libtiff-dev \
libv4l-dev \
libxine2-dev \
libxvidcore-dev \
libx264-dev \
pkg-config \
python3-dev \
python3-pip \
python3-numpy \
python3-matplotlib \
python3-testresources \
qv4l2 \
unzip \
v4l-utils \
zlib1g-dev \
libfaac-dev \
libmp3lame-dev \
libtheora-dev \
libfaac-dev \
libvorbis-dev \
libopencore-amrnb-dev \
libopencore-amrwb-dev \
libopenexr-dev \
libprotobuf-dev \
protobuf-compiler \
libgoogle-glog-dev \
libgflags-dev \
libgphoto2-dev \
libhdf5-dev \
doxygen \

Prepare OpenCV source files

Make our working folder in home:

cd ~mkdir build_opencvcd build_opencv

Clone OpenCV into our working directory:

git clone — depth 1 — branch “4.5.1”

Clone OpenCV libraries:

git clone --depth 1 --branch "4.5.1"

Make build folder inside the OpenCV folder:

cd opencvmkdir buildcd build

Configure and build OpenCV

Make sure you have the right GPU version number from earlier and change this line to match your GPU CUDA version:


Configure the build:

-D CMAKE_C_COMPILER=/usr/bin/gcc-9 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_cudacodec=OFF \
-D CUDNN_VERSION='8.0.5' \
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/ \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D OPENCV_EXTRA_MODULES_PATH=~/build_opencv/opencv_contrib/modules \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \

If the configuration completed without errors, we can build it. Change the number to the number of cpu threads to use. I have a 16 core, 32 thread CPU so I use 30 threads to leave some cpu headroom for me to watch YouTube videos while its building:

make -j30

Let’s install!

sudo make installsudo ldconfig

Final setup

Let’s check to see if the OpenCV Python module is installed in the right path:

ls /usr/local/lib/python3.8/site-packages/cv2/python-3.8/#

We’ll rename the module and link it into our virtual environment:

cd /usr/local/lib/python3.8/site-packages/cv2/python-3.8/sudo mv

Lets create a symbolic link to our virtual environment:

cd ~/.virtualenvs/cv/lib/python3.8/site-packages/ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/

Sanity check

pkg-config — modversion opencv# 4.5.1

Check with python:

python3 -c "import cv2; print(cv2.__version__)"# 4.5.1

You are done. Now go make stuff!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store