Installing Caffe/NVcaffe on Ubuntu 16.04 with CUDA8, cuDNN, OpenCV and FFMPEG (NVENC SDK)

Before following these steps make sure you have already installed Nvidia drivers and Cuda Toolkit 8

make sure everything is updated to the latest version:

sudo apt-get update
sudo apt-get upgrade

 let’s install all the necessary packages:

sudo apt-get install build-essential make cmake cmake-curses-gui g++ tmux git pkg-config libjpeg8-dev \
libjasper-dev libpng12-dev libglew1.6-dev libgtk2.0-dev \
libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libv4l-dev gfortran

sudo apt-get install libtiff5-dev
# allows for easy install/uninstall of packages from source
sudo apt-get install -y checkinstall

We need a library for computing optimization purpose. We will use openblas and linear algebra library.

sudo apt-get -y install libblas-dev libopenblas-dev libatlas-base-dev liblapack-dev gfortran

Now to install boost, run:

sudo apt-get install libboost-all-dev

If later in the installation process you find that any of the boost related files are missing, run the following command. You can skip this one for now but won’t hurt if you do it either.

sudo apt-get -y install --fix-missing libboost-all-dev

Go ahead and install libfaac-dev package

sudo apt-get install libfaac-dev

 FFMPEG (NVENC SDK) [Optional]

Now, we need to install ffmpeg. Let us also make sure that the ffmpeg version is one which OpenCV and Caffe approves. We will remove any previous versions of ffmpeg and  install new ones.

The following code will remove ffmpeg and related packages:

sudo apt-get -y remove ffmpeg x264 libx264-dev

Get NVENC SDK from the NVIDIA developer’s site for cuda enabled ffmpeg libav


Follow the steps provided in following document: FFMPEG-with-NVIDIA-Acceleration-on-Ubuntu_UG_v01.pdf

There is a seperate post on how to install FFMPEG (NVENC SDK):


You can simply install following for FFMPEG (without NVENC)

sudo apt-get install ffmpeg libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev x264 libx264-dev

We will install some optional packages as well. Run:

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev

sudo apt-get install libopenexr-dev libeigen2-dev yasm libopencore-amrnb-dev libtheora-dev libvorbis-dev libxvidcore-dev

sudo apt-get install python-tk libeigen3-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra default-jdk ant libvtk5-qt4-dev

In order to use OpenCV and Caffe, we need to install Python Development Tools package:

sudo apt-get install python2.7-dev

And of course, a powerful module for dealing with arrays, Numpy:

pip install numpy

Install opencv dependencies by running the following command:

sudo apt install libglew-dev libjpeg-dev libtiff5-dev zlib1g-dev libjasper-dev libpng12-dev libgtk2.0-dev libavcodec-dev libavformat-dev libpostproc-dev libswscale-dev libv4l-dev libeigen3-dev libtbb-dev libgflags-dev libgoogle-glog-dev unzip

 OpenCV Installation

let’s download OpenCV 2.4.13:

cd opencv-2.4.13
mkdir build
cd build


make -j4
sudo make install
sudo checkinstall #create deb package and install it (instead of make install)
echo "# Use OpenCV and other custom-built libraries." >> ~/.bashrc
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/" >> ~/.bashrc
source ~/.bashrc
sudo ldconfig

Now you have to configure OpenCV. First, open the opencv.conf file with the following code:

sudo gedit /etc/

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:


After the installation finishes, let’s check if everything works:

>>> import cv2
>>> cv2.__version__

The libcupti-dev library, which is the NVIDIA CUDA Profile Tools Interface.  This library provides advanced profiling support.  To install this library, issue the following command:

sudo apt-get install libcupti-dev

cuDNN Setup

Lets download cuDNN, which is a GPU-accelerated library of primitives for  deep neural networks provided by NVIDIA. With cuDNN, the computation speed  will be significantly accelerated. All we have to do is going to cuDNN home page,  register to the Accelerated Computing Developer Program (it’s free, but it’s a  must to download cuDNN). After registering and completing their short survey,  you will be redirected to the download page like below:


You may want to download the latest version, as NVIDIA claimed that it’s much faster  than its predecessor. If you don’t have any intention to play around with Faster R-CNN,  then you can grab the latest version. But if you want to play around with the most  outstanding Object Detection algorithm out there, then I highly recommend you to  choose the v4 Library. And if you intend to install TensorFlow then its recommended that you download v5.1 library. Now before downloading, make sure that you  choose the right version for Linux, the upper most one below the install guide:


After the download process completes, let’s extract the downloaded file  (assuming that you’re placing it under Downloads folder):

cd ~/Downloads
tar -xvf cudnn-8.0-linux-x64-v5.1.tgz

In the next step, you just have to copy the two extracted folders to where CUDA was installed,  which is most likely at /usr/local/cuda:

cd cuda
sudo cp lib64/* /usr/local/cuda/lib64
sudo cp include/* /usr/local/cuda/include
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

Add this line at the end of ~/.bashrc

export CUDA_HOME=/usr/local/cuda

Edit file /etc/ like this (append to it):


Now run linker config

sudo ldconfig


[ERROR] /sbin/ldconfig.real: /usr/local/cuda/lib64/ is  not a symbolic link

This may happen when you run sudo ldconfig after copying cuDNN files.

After installing cuDNN, copying the extracted files to /usr/lib/cuda/lib64  and creating the symlinks, things may go wrong with the symlinks.

So go to /usr/local/cuda/lib64/ and run ls -lha libcudnn*.

You should see two symlinks (bold teal) and one single file. Something like this:

The exact version of maybe be a little different for you  (maybe In that case, adapt the code accordingly

/usr/local/cuda/lib64$ ls -lha libcudnn*
lrwxrwxrwx 1 root root  13 Dez 25 23:56 ->
lrwxrwxrwx 1 root root  17 Dez 25 23:55 ->
-rwxr-xr-x 1 root root 76M Dez 25 23:27

If and are not symlinks then this is the reason why you got  this error. If so, this is what you need to do:

/usr/local/cuda/lib64$ sudo rm
/usr/local/cuda/lib64$ sudo rm
/usr/local/cuda/lib64$ sudo ln
/usr/local/cuda/lib64$ sudo ln

Run sudo ldconfig again and there should be no errors

Caffe Setup

We will now install some more crucial dependencies of Caffe:

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev hdf5-tools libhdf5-serial-dev \
libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-filesystem-dev libboost-python-dev libboost-system-dev libboost-thread-dev python-all-dev python-h5py python-matplotlib python-numpy python-opencv python-pil python-pip python-protobuf python-scipy python-skimage python-sklearn
sudo apt-get install ipython ipython-notebook python-pandas python-sympy python-nose python-nltk python-markupsafe
sudo pip install protobuf

We will install Cython and Scipy now. (I wanted it to install scikit-image properly)

sudo pip install cython git+

Now that we have Cython, go ahead and run the code below to install Scikit Image  and Scikit Learn.

sudo pip install scikit-image scikit-learn

Installing Pydot will be beneficial to view our net by saving it off in an image file.

sudo apt-get install python-pydot

[Optional] if error regarding symbolic link for libhdf5 then do the following

sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/
sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/

Note: If you are wanting to use multiple GPU’s you must install NVIDIA nccl for  caffe to work. Follow these instructions to install nccl if needed.

git clone
cd nccl
make CUDA_HOME=/usr/local/cuda test
sudo make PREFIX=/usr/local install

There are two versions of caffe. One provided by berkeley and another modified by Nvidia called nv-caffe.

For Berkeley version,
let’s go to BVLC GitHub repository and grab the latest version of Caffe:

git clone
cd caffe


for nv-caffe provided by nvidia (recommended caffe 0.15 as it supports cuDNN 5.1)

git clone
cd caffe
git checkout caffe-0.15

Rest of the steps are same for whichever caffe version you install.

Then we will create the Makefile.config file from the example file, just like before:

cp Makefile.config.example Makefile.config

Let’s apply those modifications below:

# cuDNN acceleration switch (uncomment to build with cuDNN).

# Uncomment if you're using OpenCV 3

# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \

# Uncomment to support layers written in Python (will link against Python libs)

# NCCL acceleration switch (uncomment to build with NCCL)
# (last tested version: v1.2.3-1+cuda8.0)

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial/

At this point, we can go through the compilation of Caffe without any error:

make all & make test && make runtest && make pycaffe

Next, in order to use Caffe inside our Python code, we have to add pycaffe  to the PYTHONPATH:

gedit ~/.bashrc
#Caffe location set
export CAFFE_ROOT=~/caffe

#caffe binding for python (import caffe)
export PYTHONPATH=$HOME/caffe/python:$PYTHONPATH

Then execute the command below to make the change take effect:

source ~/.bashrc

Now let’s check if we have things work properly:

>>> import caffe

If it don’t show any import error, then Congratulations, you have just successfully installed Caffe.  The installation itself was confusing a little, but it didn’t require any complicated  modifications, so somehow we still made it till the end. We can finally exhale now 🙂

Extra caffe utilities [optional]

git clone
cd caffe-tools
cp config.json.template config.json
gedit config.json (edit appropriately)


Linking Error in Leveldb e.g

Linking CXX executable caffe
../lib/ undefined reference to `leveldb::Status::ToString[abi:cxx11]() const'
../lib/ undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)'
collect2: error: ld returned 1 exit status
tools/CMakeFiles/caffe.bin.dir/build.make:122: recipe for target 'tools/caffe' failed
make[2]: *** [tools/caffe] Error 1

Solution, recompile Leveldb with GCC 5

sudo apt-get install libsnappy-dev
wget\ntar -xzf leveldb-1.9.0.tar.gz\ncd leveldb-1.9.0make
sudo mv libleveldb.* /usr/local/lib
cd include
sudo cp -R leveldb /usr/local/include
sudo ldconfig

Linking Error in Protobuf e.g

Linking CXX executable caffe
CMakeFiles/caffe.bin.dir/caffe.cpp.o: In function `std::string* google::MakeCheckOpString<cudaError, cudaError>(cudaError const&, cudaError const&, char const*)':
caffe.cpp:(.text._ZN6google17MakeCheckOpStringI9cudaErrorS1_EEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringI9cudaErrorS1_EEPSsRKT_RKT0_PKc]+0x43): undefined reference to google::base::CheckOpMessageBuilder::NewString()'
CMakeFiles/caffe.bin.dir/caffe.cpp.o: In function `std::string* google::MakeCheckOpString<unsigned long, int>(unsigned long const&, int const&, char const*)':
caffe.cpp:(.text._ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc]+0x43): undefined reference to `google::base::CheckOpMessageBuilder::NewString()'
CMakeFiles/caffe.bin.dir/caffe.cpp.o: In function `main':
caffe.cpp:(.text.startup+0x3e): undefined reference to `google::SetVersionString(std::string const&)'
caffe.cpp:(.text.startup+0x6e): undefined reference to `google::SetUsageMessage(std::string const&)'
../lib/ undefined reference to `google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::string const&, google::protobuf::io::CodedOutputStream*)


  • Solution, change your current compiler to GCC Use gcc --version to make sure the correct version, and which gcc to check the softlinks to actual gcc
  • Relevant issues to checkout and
  • In Ubuntu 15.10 when using GCC 5.2, compiling might fail as Protobuf was compiled with wrong flags. Add the following to Makefile CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0
  • Issues with Google Lib Protobuf (even after the ABI CXX11 fix)
cd /usr/local/lib
sudo mkdir libprotobuf_
mv libprotobuf* libprotobuf_
cd ~
apt-get source libprotobuf-dev
cd protobuf-2.6.1
make -j12
sudo make install


mkdir build
cd build
cmake .. # Make kept giving me issues, but CMake worked.
make all
make test
make runtest
make pycaffe # To be able to import caffe

One thought on “Installing Caffe/NVcaffe on Ubuntu 16.04 with CUDA8, cuDNN, OpenCV and FFMPEG (NVENC SDK)

Add yours

Join the Discussion

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: