Install TensorFlow and Caffe on Ubuntu 16.04 with Anaconda

Mon 23 October 2017 / In categories Notes

Caffe, CUDA, TensorFlow, Ubuntu

Previously I have written about 在Ubuntu 安裝 TensorFlow 的紀錄 (Installing TensorFlow on Ubuntu). Years have passed, and even though Ubuntu is still on 16.04,TensorFlow has already made great progress. I’ve decided to write another post to show how to install TensorFlow and Caffe with Anaconda.

GPU Settings

Basic

Firstly set integrated video card as the main GPU in the BIOS, and connect your monitor to the output port of the integrated video card so your Nvidia GPU is completely dedicated to deep learning computation.

Install CUDA

Afterwards, download the deb files from the download page of CUDA. Do not install at this time. We would reboot Ubuntu and enter Ctrl-Alt-F1 to login via terminal.

Execute the following command to stop lightdm.

sudo service lightdm stop

According to this article, this must be done when you want to use your Nvidia GPU solely for CUDA. When this is not done, we might not be able to login to the desktop after CUDA installation.

So now we would install CUDA 8.0.

sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb
sudo apt update
sudo apt install cuda-8-0

Record which version of nvidia driver is installed and add the PATH to .bashrc:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/usr/lib/nvidia-384
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin

where /usr/lib/nvidia-384 should point to the actual version installed.

Reboot again to confirm that we could login:

sudo reboot

Setup Anaconda Environment

Install Anaconda

Download Python 3 version of Anaconda at the Anaconda download page, and execute the installation command:

bash ~/Downloads/Anaconda3-{VERSION}-Linux-x86_64.sh

Install Anaconda onto a directory such as $HOME/anaconda3.

Create TensorFlow Environment

Activate Anaconda:

source $HOME/anaconda3/bin/activate

And create a new environment named tf:

conda create -n tf anaconda

Finally activate tf environment:

source activate tf

Install TensorFlow

Execute this command and tensorflow-gpu, cudnn, cudatoolkit would be installed automatically.

conda install tensorflow-gpu

Install Caffe

Firstly install the required packages:

sudo apt install build-essential
conda install atlas boost gflags glog hdf5 leveldb lmdb openblas protobuf
conda install -c menpo opencv3

Download Caffe, and create the configuration file.

git clone https://github.com/BVLC/caffe
cd caffe
cp Makefile.config.example Makefile.config

Modify Makefile.config, the number in python3.6 could be changed to the current Python version:

5c5
< # USE_CUDNN := 1
---
> USE_CUDNN := 1
21c21
< # OPENCV_VERSION := 3
---
> OPENCV_VERSION := 3
25c25
< # CUSTOM_CXX := g++
---
> CUSTOM_CXX := /usr/bin/g++-4.9
68,69c68,69
< PYTHON_INCLUDE := /usr/include/python2.7 \
<       /usr/lib/python2.7/dist-packages/numpy/core/include
---
> # PYTHON_INCLUDE := /usr/include/python2.7 \
> #         /usr/lib/python2.7/dist-packages/numpy/core/include
72,75c72,75
< # ANACONDA_HOME := $(HOME)/anaconda
< # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
<       # $(ANACONDA_HOME)/include/python2.7 \
<       # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
---
> ANACONDA_HOME := $(HOME)/anaconda3/envs/tf
> PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
>       $(ANACONDA_HOME)/include/python3.6m \
>       $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
83,84c83,84
< PYTHON_LIB := /usr/lib
< # PYTHON_LIB := $(ANACONDA_HOME)/lib
---
> # PYTHON_LIB := /usr/lib
> PYTHON_LIB := $(ANACONDA_HOME)/lib
94c94
< INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
---
> INCLUDE_DIRS := $(PYTHON_INCLUDE)

Caffe can now be compiled:

make all
make pycaffe
Notebook
Load Disqus Comments