Mohanad Kaleia

Idea for a cause

Install Caffe on Ubuntu with no GPU

If you are reading this post then for sure you know what is Caffe and most probably you want to install it on your machine so I don’t want to speak about what is Caffe and how to use it, this post is just to explain how to install it without having a hard time hopefully. Okay, the installation of Caffe was not straight forward to me, I ran into several problems, first my system was not clean, I had a lot of broken packages that drove me crazy, until I decided eventually to install Caffe on a clean Ubuntu system in a virtual machine without GPU not the best option but for testing and get hands dirty it is okay. I need to mention that my operating system is Ubuntu 16.04.

1. Install Opencv

Caffe depends on the image processing library opencv, so before installing Caffe we need to install opencv, the installation of it is kind of straight forward and it does not take a lot of time just follow the following official instruction and come back when you done:

2. Install Prerequisites

Caffe depends on the following packages, you need to install them before installing Caffe, you should not get errors while installing these packages, (I got a lot of broken dependencies while installing them until I moved to fresh installed Ubuntu system): 
sudo apt-get install -y --no-install-recommends libboost-all-dev

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

sudo apt-get install libopenblas-dev

sudo apt-get install libatlas-base-dev

3. Clone Caffe

Cool, now we need to download and install Caffe, unfortinatly we need to pull it from its repository and compile it from its source code, lets first pull it into a directory, from the terminal change the directory to the one you need to install Caffe in and run the following:
git clone
cd caffe
cp Makefile.config.example Makefile.config

4. Install python packages

sudo apt-get install python-pip python-dev build-essential

sudo pip install scikit-image protobuf
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done

5. Change the configuration

Since we are working on a CPU-only environment, we have to tell Caffe that we don’t have GPU, we can do that from Makefile.config file, also we need to tell Caffe which opencv we are working on (on the time of writing this post I installed opencv 3.2 which is the latest), let’s set our configuration then:  
cd ..
sudo gedit Makefile.config
Now a text file of the configuration is opened, uncomment the CPU_ONLY := 1” and uncomment OPENCV_VERSION as follow:
# CPU-only switch (uncomment to build without GPU support).
# Uncomment if you're using OpenCV 3
Also we need to include hdf5 library in the INCLUDE_DIRS variable to avoid future problems, so modify INCLUDE_DIRS variable as follow (believe me just do that to make your life easier ):
# add some directory to the include director variable
--- INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
+++ INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/

6. Modify the Makefile

Now we need to modify the Makefile as well, open it as follow:
sudo gedit Makefile
Modify the library to add hdf5 headers path
--- LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
+++ LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

7. Solve problems even before they happen

If you jump immediately to compile Caffe you would get compilation errors about hdf5 library because the installed hdf5 has an extension of the version number, so we need to create a link to that library that does not have an extension, to do so create a link as follow:
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 that in my case the version number is 10.1.0 and 10.0.2 for the libhdf5_serial_hl, just pay attention that you have same version, otherwise just change it to the version you have

8. Compile the Caffe

Okay, finally we can compile Caffe package we need make and test the compilation as follow:
make all
make test
make runtest

If everything went fine hopefully the compilation process you should get similar results:

9. Compile Caffe-Python Interface

Since Caffe support Python so we will install pycaffe (a python interface with Caffe), as follow:

make pycaffe
echo export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH >> ~/.bashrc
source ~/.bashrc

10. Try python caffe module

Now as a final step, just make sure that your pycaffe is working by importing caffe module into python, you can test that using python console as follow:

>>>import caffe
Does it work with you? then congratulations!, if not, please leave a comment so I can help you with that.
Note I used multiple references and websites that helped me to do the installation, here is the complete list that can help you as well:


Master Student at University of Oklahoma,
Electrical and Computer Engineering

Post a comment

  • *