Install Caffe on Ubuntu 18.04 with OpenCV 4.4 - Q-engineering
Go to content
Caffe on Ubuntu 18.04

Install OpenCV 4.4.0 and Caffe

on Ubuntu 18.04 for Python 3
Ubuntu 20.04
Caffe on Raspberry Pi


This article helps you install OpenCV 4.4.0 and Caffe on Ubuntu 18.04 for Python 3.
As the support of Python 2 ended, many software packages aren't updated for Python 3 yet. And a framework such as Caffe is highly dependent on these packages. For example, the necessary library of scikit image is now no longer available for a Python 2 Caffe build.
Another issue is OpenCV. When Caffe was developed in 2013, version 2.4.13 of OpenCV was used everywhere. Now we have OpenCV 4.4.0. with a whole lot of differences. Not only the used C ++ compiler version, but also the Linux installation folders. Time to install Caffe and OpenCV 4.4.0 for Python3.
OpenCV Dependencies.
OpenCV uses intensively third-party software libraries. These must be installed on Ubuntu before OpenCV can be set up. When they are already installed, no harm is done. This way, you are always sure you have the latest version.
# a fresh start in the morning
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libavresample-dev libvorbis-dev libxine2-dev
$ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
$ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev libeigen3-dev gfortran
$ sudo apt-get install libhdf5-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev
# a symlink to videodev.h
$ cd /usr/include/linux
$ sudo ln -s -f ../libv4l1-videodev.h videodev.h
$ cd ~

Download OpenCV.

When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. Check before downloading the latest version at If necessary, change the names of the zip files according to the latest version. After downloading, you can unzip the files.
$ cd ~
$ wget -O
$ wget -O

$ unzip
$ unzip
The next step is some administration. First, rename your directories with more convenient names like opencv and opencv_contrib. This makes live later on easier. Next, make a directory where all the build files are located.
$ mv opencv-4.4.0 opencv
$ mv opencv_contrib-4.4.0 opencv_contrib

$ cd opencv
$ mkdir build
$ cd build


As known, the CUDA toolkit significantly improve the performance of deep learning software. If you have an NVIDIA graphics card, you probably already have CUDA installed in the past. If not, NVIDIA has an excellent website that guides you through the installation process. You need to determine your architecture number of the graphics card. Use this command to get all info about your card.
With the given information, it is easy to find the architecture version of your GPU card on this page Keep in mind, Caffe recommended a CUDA capability greater or equal to 3.0. Below that figure, the software must be rewritten to reduce the number of threads and batch sizes. An adventure that is not worth the effort. We use the found architecture number when building OpenCV and later with the Caffe build.

Build Make.

Here you tell CMake what, where and how to make OpenCV on your Ubuntu system. There are many flags involved. The most you will recognize. We save memory by excluding any (Python) examples or tests.
Note, there are only bare spaces before the -D flags, not tabs. The two last dots are not a typo it tells CMake where the CMakeLists.txt file is located; one directory up. Set the OPENCV_GENERATE_PKGCONFIG flag because we are going to use the package config when we install Caffe.

Two different sets of cmake options are provided, one with CUDA support and one without. If you enable CUDA, check your architecture number earlier found. We have CUDA_ARCH_BIN=7.0, but modify the number if it is different from yours. Also check the path to the cuDNN directories, if you enabled the WITH_CUDNN=ON as we did. Change if necessary.

-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/ \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
If everything went well, CMake comes with a report that looks something like the screenshot below.
Report after cmake

Make OpenCV.

Now everything is ready for the build. This takes some time. You can speed things up by using all your cores in your machine working simultaneously. So take coffee and start building with the next command.
$ make -j$(nproc)
Hopefully, your build was successful. Now to complete, install all the generated packages to the database of your system with the next commands.
$ sudo make install
$ sudo ldconfig
$ sudo apt-get update
Now it is time to check your installation in Python 3. It all speaks for itself.

Caffe dependencies.

With OpenCV on place, it is time to start installing Caffe. Just like OpenCV, Caffe is dependent on other libraries. Some of these libraries were downloaded during the installation of OpenCV, some are new. For the sake of completeness, all required libraries are listed below.
$ sudo apt-get install cmake git unzip
$ sudo apt-get install libprotobuf-dev libleveldb-dev liblmdb-dev
$ sudo apt-get install libsnappy-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev
$ sudo apt-get install the python3-dev python3-skimage
$ sudo pip3 install pydot
$ sudo apt-get install graphviz

Download Caffe.

When all required libraries are installed, Caffe can be downloaded.
On our GitHub page, you will find the famous Caffe clone from WeiLui89. Unlike the original Caffe, this version supports SSD operations. WeiLui was one of the original developers of the  Single Shot MultiBox Detector. For detailed information, see his article.
We special adapted his fork for OpenCV 4.4 and Python3. Also, we added some additional Makefile scripts for Raspberry Pi and Ubuntu.
$ cd ~
$ wget -O
$ unzip
$ mv caffe-ssd caffe

cuDNN 8.0.

As of version 8.0, NVIDIA has dropped the support of some cuDNN API calls which Caffe depends.
The compilation now generates "CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT was not declared in this scope".
The cudnnGetConvolutionForwardAlgorithm, cudnnGetConvolutionBackwardDataAlgorithm can be replaced by other calls.
However, the cudnnGetConvolutionBackwardFilterAlgorithm has no longer a suitable alternative.
It leaves you with three options.
  • Downgrade to cuDNN version 7.6, which we strongly advise against. It can cause an avalanche of other dependency problems.
  • Compile Caffe without cuDNN acceleration. Save and with CUDA still working a not too bad option.
  • Use the fix in out GitHub Caffe repo. We have set the outcome of the cudnnGetConvolutionBackwardFilterAlgorithm to a constant. The routine searches for the fastest cuDNN solution given the layer topography. Using the general CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0 you may not have the optimal choice, but still, one that works. By the way, the fix works only for cuDNN release 8.0. An older cuDNN version will compile with the original code.

Build Caffe.

We have created several configuration files. In most cases, the configuration works immediately, without errors. Select the appropriate one and start building Caffe.
$ cd ~/caffe
# If you use CUDA, copy the cp36_x86_64-linux-gnu_CUDA_example
$ cp Makefile.config.cp36_x86_64-linux-gnu_CUDA_example Makefile.config
# Or without CUDA, copy the cp36_x86_64-linux-gnu_example
$ cp Makefile.config.cp36_x86_64-linux-gnu_example Makefile.config

# After you select one of the above configurations, the build can start
$ make clean
$ make all -j$(nproc)
$ make test -j$(nproc)
$ make runtest -j$(nproc)
Hopefully, you will see the screen below after make runtest, indicating that everything went well.

Run test Caffe


The most common error is when you try to install this 18.04 version of Caffe on a Ubuntu 20.04 or 20.10 machine. You get the following screen.
If this is the case, please use the Ubuntu 20.04 guide. Otherwise, consult our Caffe configuration diagnostic page.
Python build.
There is some additional software you can run. First, there is pycaffe. You will need this interface if you like to run Caffe in Python 3. Otherwise, Caffe will only run from the command line or from within a C++ program. See the original Caffe documentation for more info.
You can also run Caffe inside MATLAB. This time you need to make matcaffe. However, some lines in the Makefile.config needs to be set before you can successfully build matcaffe. Again, the original documentation is your friend here. After the pytest, your screen looks like something like the one below.
# Assuming you're still in ~/caffe
$ make pycaffe
$ make pytest
Run python test Caffe

Time for the last instructions and your Caffe installation is ready to run on Ubuntu 18.04 with OpenCV 4.4.0 and Python 3.
You need to add Caffe to your PYTHONPATH. This can be done in ~/.bashrc or ~/.profile. We choose bashrc. Open the file with and add the line export PYTHONPATH="${PYTHONPATH}:$HOME/caffe/python" at the very end of the file.  Save and close with Crtl+X, Y and Enter as usual.
In the end, you can import Caffe now from any location as can be seen in the screenshot.
$ cd ~
$ sudo nano ~/.bashrc
# at the end of the file add the next line
export PYTHONPATH="${PYTHONPATH}:$HOME/caffe/python"
# save and close with
Ctrl+X, Y, Enter

Caffe ssd version

If you run into any problems building Caffe on Ubuntu 18.04, please refer to the configuration page.
Caffe configuration
Back to content