Caffe Installation on Ubuntu 18.04 LTS (Python 3.6)

Atinesh
3 min readApr 14, 2019

--

Caffe (Source)

In this blog, I will guide you to install caffe by building it from source.

This guide assumes that you have already installed CUDA 9.0 and CuDnn 7.4 in Ubuntu 18.04. If you haven’t then please refer this article.

Step 1: Configure gcc version 6 by following this article otherwise, you might get stuck with an error something like this


#error — unsupported GNU version! gcc versions later than 6 are not supported!

Step 2: Install dependencies

# OpenCV
$ sudo apt install python3-opencv
# Numpy
$ sudo apt-get install python3-numpy
# BLAS
$ sudo apt-get install libatlas-base-dev # Atlas
or
$ sudo apt-get install libopenblas-dev # OpenBLAS
# Other dependencies
$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo pip3 install protobuf
$ sudo apt-get install the python3-dev

Step 3: Go to the folder where you want to install caffe (I am using ~/Downloads), then clone the latest git repository by issuing the following command

$ git clone https://github.com/BVLC/caffe.git

Now go to the caffe directory

$ cd caffe

Step 4: Make a copy of Makefile.config.example and rename it as Makefile.config

$ cp Makefile.config.example Makefile.config

Step 5: Make the following changes in Makefile.config file

OPENCV_VERSION := 3 # if you are using OpenCV 3 or aboveCUDA_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_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
BLAS := atlas # if you’ve installed Atlas (default)
or
BLAS := open # if you’ve installed OpenBLAS
#PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m \
/usr/lib/python3.6/dist-packages/numpy/core/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

All the above changes are mentioned in this Makefile.config file

Step 6: Install caffe by issuing the following commands. Make sure you are in the caffe root directory

$ make all -j6       # 6 represents number of CPU Cores
$ make pycaffe -j6 # 6 represents number of CPU Cores

Note: If you encounter any error then don’t forget to REMOVE BUILD every time you resolve the error by running the following command and then try rebuilding

$ make clean

Step 7: Add the path of caffe/python directory to ~/.bashrc

export PYTHONPATH=~/Downloads/caffe/python:$PYTHONPATH

Reload ~/.bashrc file using below command

$ source ~/.bashrc

Step 8: Check by importing caffe in the python interpreter

$ python3
>>> import caffe

Tested with Ubuntu 18.04 LTS, CUDA 9.0 and CuDnn 7.4

Thank you for reading this post, If you like it then please clap and share it on Facebook, LinkedIn or Twitter so that other people get benefits from it.

You can follow me on Github, LinkedIn or contact me at atinesh.s@gmail.com

Happy Coding!

--

--