installing caffe on ubuntu

Download Caffe

Create a directory where you would like to install caffe. For all future reference, this will be called the <caffe-home>, for example if user ubuntu in directory /home/ubuntu/caffe
From inside the <caffe root> directory, execute the following commands :

$> git clone
$> cd caffe
$> cp Makefile.config.example Makefile.config  ///this command for rename
Makefile.config.example to Makefile.config

Edit the Makefile.config :

For ubuntuĀ  go to directory caffe, and then type sudo nano makefile.config

## Refer to
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

# CPU-only switch (uncomment to build with CPU support).

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
        -gencode arch=compute_20,code=sm_21 \
        -gencode arch=compute_30,code=sm_30 \
        -gencode arch=compute_35,code=sm_35 \
        -gencode arch=compute_50,code=sm_50 \
        -gencode arch=compute_50,code=compute_50

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
        # $(ANACONDA_HOME)/include/python2.7 \
        # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

# We need to be able to find or .dylib.
PYTHON_LIB := /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/

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

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)

BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.

# enable pretty build (comment to see full commands)
Q ?= @

Now lets look at the file step-by-step.

We leave this part commented out as we wont be using cuDNN in our installation. It speeds up the Caffe processes but for a general (simplistic and working) installation, it can be left out.

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

We build caffe without GPU support (following the idea of having a simplistic,working installation), uncomment

CPU_ONLY if we use CPU machine
# CPU-only switch (uncomment to build without GPU support).

Now this is a tricky part. To install Caffe with the python interface, PyCaffe (Recommended) you need to give the paths to your python include libs and the path where you have numpy stored. Path to numpy include folder must be given with great caution. You might land into unnecessary trouble by specifying this path incorrectly.

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \

This was the most important tweak required to get PyCaffe up and running. You might have different versions of python installed on your local machine and in your homebrew instance. This would lead to problems when importing caffe from your python interpreter. To overcome this problem, make sure that you provide the correct path to your brewed python.

# We need to be able to find or .dylib.
PYTHON_LIB := /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/

The rest of the parts can be left out as they are not used anyways (commented out) and hold not much importance. You are all set to compile Caffe! Almost there!

Compile :

Now hoping that all the above steps went as planned, we can finally compile Caffe. So keeping your fingers crossed, execute the following from your <caffe-home>:

$> make clean
$> make all
$> make test
$> make runtest

These commands will take a few minutes to execute. I’d suggest you skim through the output of these commands being printed on the stdout and make sure you see no alarming warnings/errors. You might see quite a few warnings of unused variables and parallel threads (-pthread). These are not things you should worry about.

The output of the ‘make runtest’ command would be somewhat as the image below,


Now to install and configure the python interface to Caffe, first lets make sure we have all the python dependencies installed. The requirements.txt file handles this list.

for req in $(cat requirements.txt); do pip install $req; done

Now, to compile the PyCaffe interface, from the <caffe-home> do

make pycaffe

IMP : Don’t forget to set the PYTHONPATH variable in your ~/.bash_profile (OSX) or ~/.bashrc (Linux) to the caffe python.

export PYTHONPATH=<caffe-home>/python:$PYTHONPATH

Some versions of the official documentation show that the PYTHONPATH variable needs to be set to <caffe-home>/python/caffe, however this is an error in the documentation and the above path should be set.

You might also run the following command to create a distribute directory with all the Caffe headers, compiled libraries, binaries, etc. needed for distribution to other machines.

make distribute

To finally import caffe in the python interpreter, go the python folder in <caffe-home> and execute

$> cd <caffe-home>/python
$> python
Python 2.7.9 (default, Jan 29 2015, 06:27:40)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe


Now you are all set to rock! Play around with a few examples like Training LeNet on MNIST or see how to use the pre-trained models like ImageNet.


source :

Be the first to comment

Leave a Reply

Your email address will not be published.