How to build Caffe

Building Caffe from source is easy. There are two options provided: using a Makefile or using CMake.

Required packages

  • Download or clone the source code of Caffe from here.
  • Not surprisingly, you will need a GPU, a graphics driver that can work with that GPU and an installation of CUDA.
  • Other than that, you will need to install these packages:
$ sudo apt install libboost-all-dev libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler libgflags-dev libgoogle-glog-dev liblmdb-dev
  • For BLAS on CPU, the best performance comes from installing Intel’s MKL libraries. If you do not have that, you can install either ATLAS or OpenBLAS:
$ sudo apt install libatlas-base-dev
$ sudo apt install libopenblas-dev
  • To build the Python interface to Caffe, which is highly recommended, make sure these packages are installed:
$ sudo apt install libboost-python-dev python-skimage python-protobuf
  • If you do not have one or more of the above Python-related packages, you might see these errors:
The dependency target "pycaffe" of target "pytest" does not exist.

ImportError: No module named

ImportError: No module named google.protobuf.internal

Build using CMake

Just follow the usual steps for CMake:

$ mkdir build
$ cd build
$ cmake ..
$ make

To build and run the 2000+ unittests, which can take quite a while to finish:

$ make runtest

Finally to build the Python interface to Caffe:

$ make pycaffe
$ make install

By default, the install directory will be a subdirectory inside the build directory. Add this build/install/python path to PYTHONPATH environment variable before you import caffe in Python.

Build using Make

On one system, I found that using Makefile was easier, since CMake was erroneously complaining about -lpthreads (which is a Clang library, not a GCC library).
Copy the Makefile config file, open it in an editor and check whether all the paths are set correctly:

$ cp Makefile.config.example Makefile.config
$ vim Makefile.config
$ make

Build MatCaffe

Building support for MATLAB to use Caffe takes a few more steps:

  • In Makefile.config, set the MATLAB_DIR to the path that contains bin/mex. On my computer, this was the path /usr/local/MATLAB/R2014a.

  • Compiling the MATLAB wrapper gave an error about C++11 on my Ubuntu 14.04 due to the older GCC compilers. So, I had to add -std=c++11 to the CXXFLAGS variable in Makefile as shown here.

  • Finally, compiling MatCaffe is easy:

$ make all matcaffe

4 thoughts on “How to build Caffe

  1. i use matlab r2014a and ubuntu 14.04 ,
    when i run make matcaffe , it said this problem:

    mm@mm:~$ cd code/caffe
    mm@mm:~/code/caffe$ make all matcaffe
    make: Nothing to be done for `all’.
    MEX matlab/+caffe/private/caffe_.cpp
    /usr/local/MATLAB/R2014a/bin/glnxa64/mex: error while loading shared libraries: cannot open shared object file: No such file or directory
    make: *** [matlab/+caffe/private/caffe_.mexa64] Error 127

    I had to add -std=c++11 to the CXXFLAGS variable in Makefile


  2. Hello, I met a problem when I compile caffe:

    ... ...
    -- Configuring done
    CMake Error at CMakeLists.txt:104 (add_dependencies):
      The dependency target "pycaffe" of target "pytest" does not exist.
    ... ...

    cmake relevant parameters:

    mkdir build
    cd build
    # Configure, build, test, and install.
    cmake -DCPU_ONLY=1                                          \
          -DBLAS="${BLAS}"                                      \
          -DCMAKE_INSTALL_PREFIX="${PREFIX}"                    \
          -DNUMPY_INCLUDE_DIR="${SITE_PKGS}/numpy/core/include" \
          -DNUMPY_VERSION=${NPY_VER}                            \
          -DPYTHON_EXECUTABLE="${PREFIX}/bin/python"            \
    make -j${CPU_COUNT}
    make install

    Looking forward to your reply, thanks !!!


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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.