Install NVIDIA DIGITS with Caffe and Torch on Ubuntu 16.04

Let’s install NVIDIA DIGITS on Ubuntu 16.04. We’ll use DIGITS with both Caffe and Torch.

Instructions

Prepare the environment

1
2
3
4
5
#update package manager
sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade
sudo shutdown -r now
#install compiler
sudo apt-get install make cmake gcc g++ -y

Install NVIDIA GPU drivers

1
2
#Remove existing nvidia drivers etc.
sudo apt-get purge nvidia*

You may choose to install the drivers from the nvidia repo through apt-get or download and install manually to ensure the latest version from the short lived branch. Either way you should check the NVIDIA drivers site to check the latest version / version required by your GPU.

Install from the repository

1
2
3
4
5
6
7
8
9
10
11
12
#Add NVIDIA drivers repository
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
#Check the repo for the version you are after e.g.
sudo apt search nvidia | grep nvidia-375
#Install the driver
sudo apt-get install nvidia-375 nvidia-settings -y
sudo reboot
sudo apt-get install ubuntu-desktop
sudo reboot
#check the driver version
nvidia-smi

Install manually from the NVIDIA site download

Visit the NVIDIA drivers site and copy the appropriate run file download address (here we use the .run file)

1
2
3
4
5
cd ~/downloads/
#download the run file
wget http://us.download.nvidia.com/XFree86/Linux-x86_64/378.13/NVIDIA-Linux-x86_64-378.13.run
#make the download executable
chmod +xr NVIDIA-Linux-x86_64-378.13.run

This can’t be installed from inside the desktop environment. Drop to shell with Ctrl+Alt+F1

1
2
3
4
5
6
sudo service lightdm stop
sudo init 5
sudo ./NVIDIA-Linux-x86_64-378.13.run --dkms
sudo reboot
#check the driver version
nvidia-smi

Install NVIDIA CUDA Toolkit

Go to the CUDA Downloads site and get the download URL for the toolkit.

1
2
3
4
5
cd ~/downloads/
wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run
chmod +xr NVIDIA-Linux-x86_64-378.13.run
#run the installer. Use the switches to hide the five minute scroll through the license.
sudo ./cuda_8.0.44_linux-run -override -silent

Install NVIDIA cuDNN

The “Deep Neural Network” library requires a membership to the NVIDIA developer program. If you don’t have a membership you will need to go to the cuDNN Site to sign up. From the cuDNN Site copy the download URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#get the download archive
wget https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v6/prod/8.0_20170307/cudnn-8.0-linux-x64-v6.0-tgz
#extract the archive
tar -xzvf cudnn-8.0-linux-x64-v5.1.tgz
#set up the directory structure and copy files
sudo mkdir /usr/local/cuda
sudo mkdir /usr/local/cuda/lib64/
sudo mkdir /usr/local/cuda/include
cd cuda/lib64
sudo cp ./cuda/lib64 /usr/local/cuda/lib64
sudo cp ./cuda/include /usr/local/cuda/include
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
sudo reboot
#copy files to the cuda-8.0 directory - you could probably use a symbolic link here instead.
sudo cp -P include/cudnn.h /usr/local/cuda-8.0
sudo cp -P lib64/libcudnn* /usr/local/cuda-8.0/lib64/
sudo chmod a+r /usr/local/cuda-8.0/lib64/libcudnn*

Add the required variables to your .bashrc

1
2
3
4
5
6
7
echo 'export PATH=$PATH:/usr/local/cuda-8.0/bin' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64' >> ~/.bashrc
source .bashrc
#run the oft forgotten ldconfig
ldconfig -p | grep cuda
#check
nvcc --version

Install Caffe (NVIDIA/caffe)

DIGITS only officially supports the NVIDIA caffe build. The official BVLC/caffe will work in most cases. It is my understanding that the two are mostly the same but there are some specialized cases especially when using batch normalization, and ImageData layers (etc.) the NVIDIA flavor is required.

The Building Caffe instructions are available. You should read those.

1
2
3
4
5
6
7
8
9
10
11
#dependencies
#DIGITS will require these
sudo apt-get install --no-install-recommends git graphviz gunicorn python-dev python-flask python-flaskext.wtf python-gevent python-h5py python-numpy python-pil python-protobuf python-scipy -y
#Caffe will require these (some overlap)
sudo apt-get install --no-install-recommends build-essential cmake git gfortran libatlas-base-dev libboost-filesystem-dev libboost-python-dev libboost-system-dev libboost-thread-dev libgflags-dev libgoogle-glog-dev libhdf5-serial-dev libleveldb-dev liblmdb-dev libopencv-dev libprotobuf-dev libsnappy-dev protobuf-compiler python-all-dev python-dev python-h5py python-matplotlib python-numpy python-opencv python-pil python-pip python-protobuf python-scipy python-skimage python-sklearn
#make the directory for caffe
cd ~/
mkdir caffe-nvidia
export CAFFE_ROOT=~/caffe-nvidia
source ~/.bashrc
git clone https://github.com/NVIDIA/caffe.git $CAFFE_ROOT

Install PIP if you don’t have it

1
2
sudo apt-get install python-pip
sudo pip install --upgrade pip

More deps

1
2
3
sudo pip install -r $CAFFE_ROOT/python/requirements.txt
#if theres an error try this as per the build instructions from NVIDIA
cat $CAFFE_ROOT/python/requirements.txt | xargs -n1 sudo pip install

Now build Caffe. Note that if you need to support multiple GPUs you will need to build NVIDIA NCCL before building caffe.

Skip this if you don’t need NCCL

1
2
3
4
5
6
#read the NCCL readme first.
cd ~/
git clone https://github.com/NVIDIA/nccl.git
cd nccl
make CUDA_HOME=/usr/local/cuda-8.0/ test
sudo make PREFIX=/usr/local install

Now build caffe

1
2
3
4
5
cd $CAFFE_HOME
mkdir build && cd $_
cmake ..
#use 4 cores - adjust for your system.
make --jobs=4

Install Torch (Optional for DIGITS)

TBD

Install DIGITS

You should review the install instructions on the DIGITS github page. Since we are installing on 16.04 we’ll need to build from source. If you are on 14.04 you can use the .deb file if you prefer.

1
2
3
4
5
6
7
8
9
10
11
12
#set up the directories etc.
cd ~/
mkdir digits
echo 'export CAFFE_HOME=/home/adon/caffe-nvidia' >> ~/.bashrc
echo 'export DIGITS_HOME=/home/adon/digits' >> ~/.bashrc
source .bashrc
echo $CAFFE_HOME
echo $DIGITS_HOME
#grab the code
git clone https://github.com/NVIDIA/DIGITS.git $DIGITS_HOME
#install the requirements
sudo pip install -r $DIGITS_HOME/requirements.txt

If you need to use plugins run the following:

1
sudo pip install -e $DIGITS_HOME

Now you can start the DIGITS server on localhost:5000

1
./digits-devserver