How To Install Caffe On Ubuntu

mainThe concept of deep learning is becoming increasingly pervasive. It is a new area of research in machine learning that focuses on learning optimal representations of data. Now what does it mean? In the realm of classical machine learning, we have the build the features first and then the machine learning algorithm will learn how to classify the data based on these features. The problem is that feature-building is a trial-and-error process and we want to avoid manual intervention. This is where deep learning tends to shine! Instead of manually building the features ourselves, we can just let our deep neural networks learn the features and then build a system to classify that data. In this field, people work towards building a set of algorithms that can model abstractions in our data using multilayered neural networks. Caffe is one of the most popular libraries available out there for deep learning. Let’s go ahead and see how to get it up and running on Ubuntu, shall we?  

Installing Ubuntu packages

Deep learning is a computationally intensive paradigm, so it can heavily benefit from GPU acceleration. Caffe is designed to take advantage of the GPU and it’s very well-suited to do it. If you are trying this on AWS, it would be better if you spin up a GPU instance.

We will be downloading a bunch of packages, so let’s just create a folder to download these packages:

$ mkdir deeplearning
$ cd deeplearning

Let’s download the package lists and update them:

$ sudo apt-get update

Let’s install the newest versions of all the packages:

$ sudo apt-get -y dist-upgrade

Installing prerequisites

Let’s install the dependencies:

$ sudo apt-get install -y gcc g++ gfortran build-essential git wget linux-image-generic python-dev python-pip python-nose python-numpy python-scipy libblas-dev libatlas-base-dev libopenblas-dev

We should install a couple of useful python packages required for general machine learning:

$ sudo pip install numpy scipy scikit-learn

We are now ready to install the Caffe prerequisities:

$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
$ sudo pip install protobuf
$ sudo apt-get install --no-install-recommends libboost-all-dev

Installing CUDA

If you have a GPU enabled machine, you should install CUDA. What is CUDA? It is a parallel computing platform designed and developed by Nvidia. It enables us to harness the power of the GPU and increase the computing capacity by leaps and bounds. Let’s go ahead and download the CUDA toolkit:

$ wget

Let’s depackage the toolkit:

$ sudo dpkg -i cuda-repo-ubuntu1404_7.0-28_amd64.deb

Now we need to update our package lists:

$ sudo apt-get update

We are now ready to install it:

$ sudo apt-get install -y cuda

Let’s go ahead and update our environment variables:

$ echo "export PATH=/usr/local/cuda-7.5/bin:$PATH" >> ~/.bashrc
$ echo "export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH" >> ~/.bashrc
$ source ~/.bashrc

We need a Python module called CUDAMat. It is a module for performing basic dense linear algebra computations on the GPU using CUDA. Let’s download and install CUDAMat:

$ git clone
$ cd cudamat
$ python build
$ sudo python install

Installing Caffe

We have to install a few remaining dependencies before we install Caffe:

$ sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev

If you are on AWS, you need to disable the camera driver or it will complain when you are working with images:

$ sudo ln /dev/null /dev/raw1394

We are now ready to install Caffe:

$ cd ..
$ git clone
$ cd caffe
$ cp Makefile.config.example Makefile.config
$ make all -j4
$ make test

At this point, you need to reboot your machine. Let’s go ahead and do that:

$ sudo reboot

Let’s see if things are in place. Type the following on your terminal:

$ nvidia-smi

You should see the details of your GPU printed on the terminal. Let’s run some tests to see if Caffe is working as expected:

$ cd /path/to/downloaded/caffe
$ make runtest

Let’s go ahead and install the Python interface:

$ make pycaffe

We are all set! You are now ready to use Caffe in Python.


One thought on “How To Install Caffe On Ubuntu

  1. I am getting errors while running commands like make all j-4 (collect2: error: ld returned 1 exit status
    make: *** [.build_release/test/test_all.testbin] Error 1)
    how to interface caffe with pycharm.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s