Comprehensive Guide: Installing Caffe2 with GPU Support by Building from Source on Ubuntu 16.04

In the previous posts, we have gone through the installation processes for deep learning infrastructure, such as Docker, nvidia-docker, CUDA Toolkit and cuDNN. With the infrastructure setup, we may conveniently start delving into deep learning: building, training, and validating deep neural network models, and applying the models into a certain problem domain. Translating deep learning primitives into low level bytecode execution can be an enormous task, especially for practitioners without interests in the deep learning calculus. Fortunately, there are several deep learning frameworks that provide the high level programming interface to assist in performing deep learning tasks.

In this post, we will go through the installation of Caffe2, one of the major deep learning frameworks. Caffe2 is adopted from Caffe, a deep learning framework developed by the Barkeley Vision and Learning Center (BVLC) of UC Berkeley. Caffe2 was started with the aim to improve Caffe especially to better support large-scale distributed model training, mobile deployment, reduced precision computation, new hardware, and flexibility of porting to multiple platforms.

I am launching a campaign for Amikelive System Architect, a course for advanced app, web, and system development. You can donate and enjoy the perk exclusively offered for the backers. Visit this page to become a backer.

Why Installing from Source?

It is important to note that instead of installation by building from source, there are some easier options, as for example by pulling and running Caffe2 Docker images. The Docker repository for Caffe2 images is accessible here. Nonetheless, if you observe the , the images are not recent and they were built against older version of CUDA toolkit and cuDNN. Therefore, one who intends to fiddle with latest Caffe2, CUDA, or cuDNN features may seriously consider installing from source.

Caffe2 Is Now A Part of Pytorch

In the past, Caffe2 source was maintained as an independent repository on Github. This changed from the end of March 2018 since Caffe2 repository was merged into Pytorch repository. Consequently, the common build process is now integrated into that of Pytorch.

There can be questions about the motivation of the source code merging. As explained in the announcement post, the merging was preceded by the development infrastructure sharing between Caffe2 and Pytorch that led into increasing amount of shared code and common libraries. It was then concluded that merging the two projects into one codebase would result in better engineering efficiency and increase the robustness of the framework especially in the area of model deployment.

What if Caffe2 has been previously installed? When installing Caffe2 prior to the source code merging, the build process would output header files, dynamic library and python library that subsequently copied to designated directories. In theory, it would be suffice to replace the old header and library files with the one from the newest build. If it does not work, a clean up is necessary by first removing the old files from their installed directories prior to proceeding with the installation


We will be installing Caffe2 with GPU support. This article only covers the installation process for machines with NVIDIA GPUs. Verify that these items are satisfied prior to going through the installation.

1. Verify that NVIDIA graphics driver has been properly installed.

You can refer to this article for NVIDIA graphics driver installation and functionality checking.

2. Verify that CUDA toolkit has been installed.
If you have not installed CUDA toolkit, are unsure whether it is already installed, or want to install the newer version of CUDA toolkit, refer to this article that provides more elaboration about CUDA toolkit installation.

3. Verify that cuDNN has been installed.
You can refer to this article for more details about cuDNN installation

4. Remove files from previous Caffe2 installation
If you have installed Caffe2 earlier and want to do an upgrade, you may need to clean the files created and copied in the previous Caffe 2 installation.
Note: this can usually be skipped as the build tool will first check the existing version of Caffe2 installed in the system prior to replacing it with a new one

$ sudo rm -vRf /usr/local/include/caffe2
$ sudo rm -vf /usr/local/lib/libcaffe2*

Additionally, we may need to remove the python APIs from the Python package directory (path may vary depending on the installed Python version).

$ sudo rm -vRf /usr/local/lib/python2.7/dist-packages/caffe2

After checking all the prerequisites, we can now proceed with the installation.

Installation Steps

The installation can be accomplished by going through these steps in order.

1. Update apt package index
$ sudo apt-get update

2. Install apt package dependencies

remote$ sudo apt-get install -y --no-install-recommends build-essential cmake git libgoogle-glog-dev libgtest-dev libiomp-dev libleveldb-dev liblmdb-dev libopencv-dev libopenmpi-dev libsnappy-dev libprotobuf-dev openmpi-bin openmpi-doc protobuf-compiler python-dev python-pip

3. Install pip dependencies

$ sudo pip install --upgrade pip
$ sudo pip install setuptools future numpy protobuf
$ sudo apt-get install -y --no-install-recommends libgflags-dev

4. Clone Caffe 2 into a local directory
Note: We create a directory named caffe2-pytorch and clone Pytorch git repository into this directory.

$ mkdir caffe2-pytorch && cd caffe2-pytorch
$ git clone --recursive ./
$ git submodule update --init

5. Build Caffe 2

$ mkdir build && cd build
$ cmake ..
$ sudo make -j"$(nproc)" install

Note: when building the source, we supply the -j flag. The flag refers to the number of threads that can be spawned by the compiler (i.e. GCC) when building the source code. The nproc command itself will print the number of the CPUs available. In short, we want to speed up the compilation time by creating a number of threads that amount to the number of CPUs to perform the compilation in parallel.

6. Create the symbolic link for Caffe 2 shared library
$ sudo ldconfig

7. Verify that Caffe 2 library and the headers have been installed in the proper directory
– Update the locate database
$ sudo updatedb

– Ensure that is located in /usr/local/lib
$ locate

– Ensure that the header files have been copied into /usr/local/include
$ locate caffe2 | grep /usr/local/include/caffe2

8. Add Caffe2 into python and library paths so that it can be immediately discovered and linked by other applications.
$ vim ~/.profile

# set python path
if [ -z “$PYTHONPATH” ]; then

#set library path
if [ -z “$LD_LIBRARY_PATH” ]; then

We immediately export the new paths by invoking the source command as follows.
$ source ~/.profile

Alternatively, we can use the dot (.) command, to execute ~/.profile file and enable the new environment variables in the current shell.
$ . ~/.profile

9. Verify that the Caffe2 python module can be properly invoked
$ python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

10. Verify that Caffe2 can run with GPU support
$ python2 -c 'from caffe2.python import workspace; print(workspace.NumCudaDevices())'

The installation is now complete. We can now start using Caffe2 for deep learning modeling and implementation. If you have problem with your Caffe2 installation, simply write it in the comment section.

11 thoughts on “Comprehensive Guide: Installing Caffe2 with GPU Support by Building from Source on Ubuntu 16.04

  1. Pingback: How to Properly Install NVIDIA Graphics Driver on Ubuntu 16.04 « Amikelive | Technology Blog

  2. Adão

    Hi, I have a problem in verification

    python -c ‘from caffe2.python import core’ 2>/dev/null && echo “Success” || echo “Failure”

    python2 -c ‘from caffe2.python import workspace; print(workspace.NumCudaDevices())’

    WARNING:root:This caffe2 python run does not have GPU support. Will run in CPU only mode.
    WARNING:root:Debug message: No module named caffe2_pybind11_state_gpu
    CRITICAL:root:Cannot load caffe2.python. Error: No module named caffe2_pybind11_state

  3. Hadi Gorak

    Hi, Thanks for the detailed installing instruction. I am trying to install caffe2 on my machine, but keep getting this error. Could you guide me to resolve the issue.

    [ 88%] Linking CXX shared module python/
    [ 88%] Built target caffe2_pybind11_state_gpu
    Makefile:138: recipe for target ‘all’ failed
    make: *** [all] Error 2

    1. Mikael Fernandus Simalango Post author

      Error code 2 means that the file ( does not exist. If the source was building properly the .so file should have beeen located under SRC_HOME/build/caffe2/python/ directory. A possible cause is that cmake could not find CUDA. Could you paste the output of “echo $LD_LIBRARY_PATH”, “echo $PYTHONPATH”, and the output of cmake for CUDA:
      CUDA version : x.x.x

      If you follow the steps explained in the article, USE_CUDA must be ON and CUDA version must be 9.1.

  4. Hadi

    Hi Thank for the prompt respond :

    the output for cmake is
    — ******** Summary ********
    — General:
    — CMake version : 3.5.1
    — CMake command : /usr/bin/cmake
    — Git version : v0.1.11-8684-g36c3859
    — System : Linux
    — C++ compiler : /usr/bin/c++
    — C++ compiler version : 5.4.0
    — BLAS : Eigen
    — CXX flags : -fvisibility-inlines-hidden -DONNX_NAMESPACE=onnx_c2 -O2 -fPIC -Wno-narrowing -Wno-invalid-partial-specialization -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-unused-parameter -Wno-unknown-warning-option -Wno-unknown-pragmas
    — Build type : Release
    — Compile definitions :

    — Link local protobuf : ON
    — Python version : 2.7.12
    — Python includes : /usr/include/python2.7
    — USE_ASAN : OFF
    — USE_CUDA : ON
    — CUDA static link : OFF
    — USE_CUDNN : ON
    — CUDA version : 9.2
    — cuDNN version : 6.0.21
    — CUDA root directory : /usr/local/cuda
    — CUDA library : /usr/local/cuda-9.2/lib64/stubs/
    — cudart library : /usr/local/cuda/lib64/libcudart_static.a;-pthread;dl;/usr/lib/x86_64-linux-gnu/
    — cublas library : /usr/local/cuda/lib64/;/usr/local/cuda/lib64/libcublas_device.a
    — cufft library : /usr/local/cuda/lib64/
    — curand library : /usr/local/cuda/lib64/
    — cuDNN library : /home/paperspace/anaconda3/lib/
    — nvrtc : /usr/local/cuda-9.2/lib64/
    — CUDA include path : /usr/local/cuda/include
    — NVCC executable : /usr/local/cuda/bin/nvcc
    — CUDA host compiler : /usr/bin/cc
    — USE_ROCM : OFF
    — USE_GLOG : ON
    — USE_GLOO : ON
    — LevelDB version : 1.20
    — Snappy version : ..
    — USE_LMDB : ON
    — LMDB version : 0.9.21
    — USE_MKL :
    — USE_MPI : ON
    — USE_NCCL : ON
    — OpenCV version : 3.3.1
    — USE_PROF : OFF
    — USE_ZMQ : OFF

    and echo returns are

    /home/paperspace/src/torch/install/lib:/home/paperspace/src/torch/install/lib:/usr/local/lib:/home/paperspace/src/torch/install/lib:â, âecho /home/paperspace/src/caffe/python:


    paperspace@psx6p289c:~$ echo $PYTHONPATH

    1. Mikael Fernandus Simalango Post author

      At first glance, that configuration looks fine. I suppose CUDA 9.2 did not play nice with cuDNN 6.0, but this should be further validated because i have yet to configure with that kind of CUDA-cuDNN version pair.

      If you can also provide the output for “nvidia-smi” and “nvcc –version” commands, it can add more datapoints for the root cause analysis.

      Additionally, you may consider upgrading cuDNN to 7.1.x and recompile again.

  5. Hadi Gorak

    Hi, so I redid the installation again :

    paperspace@psx6p289c:~$ nvidia-smi
    Mon Jun 4 11:53:05 2018
    | NVIDIA-SMI 396.26 Driver Version: 396.26 |
    | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
    | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
    | 0 Quadro P6000 Off | 00000000:00:05.0 On | Off |
    | 26% 31C P8 17W / 250W | 586MiB / 24449MiB | 1% Default |

    | Processes: GPU Memory |
    | GPU PID Type Process name Usage |
    | 0 2436 G /usr/lib/xorg/Xorg 372MiB |
    | 0 2766 G /usr/bin/gnome-shell 120MiB |
    | 0 3484 G …-token=480B4330CA48007D74D9DD70DFC02263 88MiB |

    paperspace@psx6p289c:~$ nvcc –version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2018 NVIDIA Corporation
    Built on Wed_Apr_11_23:16:29_CDT_2018
    Cuda compilation tools, release 9.2, V9.2.88

    Cmake :

    — Python version : 2.7.12
    — Python includes : /usr/include/python2.7
    — USE_ASAN : OFF
    — USE_CUDA : ON
    — CUDA static link : OFF
    — USE_CUDNN : ON
    — CUDA version : 9.2
    — cuDNN version : 7.1.4


    echo $PYTHONPATH




    /usr/bin/ld: CMakeFiles/mpi_gpu_test.dir/mpi/ undefined reference to symbol ‘_ZN3MPI8Datatype4FreeEv’
    //usr/lib/ error adding symbols: DSO missing from command line
    collect2: error: ld returned 1 exit status
    caffe2/CMakeFiles/mpi_gpu_test.dir/build.make:110: recipe for target ‘bin/mpi_gpu_test’ failed
    make[2]: *** [bin/mpi_gpu_test] Error 1
    CMakeFiles/Makefile2:2319: recipe for target ‘caffe2/CMakeFiles/mpi_gpu_test.dir/all’ failed
    make[1]: *** [caffe2/CMakeFiles/mpi_gpu_test.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….
    [ 88%] Linking CXX executable ../bin/predictor_test
    [ 88%] Linking CXX executable ../bin/string_ops_test
    [ 88%] Linking CXX executable ../bin/event_gpu_test
    [ 88%] Built target predictor_test
    [ 88%] Built target string_ops_test
    [ 88%] Built target event_gpu_test
    [ 88%] Linking CXX executable ../bin/math_gpu_test
    [ 88%] Built target math_gpu_test
    [ 88%] Linking CXX executable ../bin/blob_test
    CMakeFiles/blob_test.dir/core/ In function `caffe2::(anonymous namespace)::ContentChunks_Serialization_Test::TestBody()’: warning: the use of `tmpnam’ is dangerous, better use `mkstemp’
    [ 88%] Built target blob_test
    [ 88%] Linking CXX shared module python/
    [ 88%] Built target caffe2_pybind11_state_gpu
    Makefile:138: recipe for target ‘all’ failed
    make: *** [all] Error 2


    I notice that I don’t have “” in my ~/caffe2-pytorch/caffe2/python what you think I should do to add that libarary to the path.

    1. Mikael Fernandus Simalango Post author

      Looking at the log this time, it’s more obvious that the error was caused by Open MPI. You may have not installed Open MPI or the library files are not linked properly.

      Quick resolve: Build without Open MPI support
      $ cmake .. -DUSE_MPI=OFF

      Robust resolve: Track the Open MPI status, install if it has not been installed or fix the library linking
      – To check if Open MPI has been installed
      $ mpirun --version

      – To check where the library is located
      $ locate libmpi

      – To search Open MPI in apt repositories
      $ apt-cache search openmpi

      – To install relevant Open MPI libraries
      $ sudo apt-get install libopenmpi-dev libopenmpi1.10 openmpi-bin openmpi-common openmpi-doc

      This issue is a known Caffe2 issue. You can find more info about it here:

    1. Mikael Fernandus Simalango Post author

      Glad to know it resolved your issue. As a side note, if you’re building a cluster (GPU/CPU), it’s suggested to compile with Open MPI support. -Mike-

Leave a Reply

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