How to install NVIDIA CUDA 8.0, cuDNN 5.1, TensorFlow, and Keras on Ubuntu 16.04

Please follow the instructions below and you will be rewarded with Keras with Tenserflow backend and, most importantly, GPU support.

The latest version of CUDA Toolkit you can download from here. It is also clear from that page which versions of Ubuntu are supported.

screen-shot-2017-07-10-at-1-02-15-am.png

The latest version of cuDNN you can download from here. TensorFlow, however, requires cuDNN 5.1 and GPU card with CUDA Compute Capability 3.0 or higher.

Step 1. Linux

Update apt repositories and install the linux -image-extra-virtual package.
This package includes the kernel module that’s required by the NVIDIA drivers.

sudo apt-get update
sudo apt-get install -y linux-image-extra-virtual

Install the version of the headers that matches the freshly installed kernel from the previous step.

sudo apt-get install linux-source linux-headers-`uname-r`
sudo reboot

Step 2. Python

Download (from here) and Install Anaconda Python 3.6 64 bit

chmod +x Anaconda3-4.4.0-Linux-x86_64.sh
sudo ./Anaconda3-4.3.1-Linux-x86_64.sh

Step 3. NVIDIA Drivers and CUDA

Blacklist Noveau which has a conflict with the NVIDIA Drivers

echo -e "blacklist nouveau\nblacklist lbm-nouveau\noptions nouveau modeset=0\nalias nouveau off\nalias lbm-nouveau off\n" | sudo tee /etc/modprobe.d/blacklist-nouveau.conf

Disable the Kernel Nouveau

echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
sudo update-initramfs -u
sudo reboot

Download the Installer and make it executable

chmod +x cuda_8.0.61_375.26_linux.run

Hit Ctrl + Alt + F1

Kill X server

sudo systemctl stop lightdm.service
sudo init 3

Run the Installer and accept the license agreement and install samples

sudo sh cuda_8.0.61_375.26_linux.run

Enable NVIDIA Driver

sudo modprobe nvidia

Restart X server

sudo service lightdm restart

Compile and run the deviceQuery sample from the CUDA distribution to validate the NVIDIA driver installation was successful.

cd /home/evg/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery/
make ./deviceQuery

valid-results-from-sample-cuda-devicequery-program.png

Step 4. cuDNN v5.1 for CUDA 8.0

Download cuDNN

Unzip the .tar archive

tar -xzf cudnn-8.0-linux-x64-v5.1.tgz

Copy the cuDNN libraries and header file to the CUDA folders

sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

Add some environment variables

gedit ~/.bashrc

Insert the following lines and save the changes

export CUDA_HOME="/usr/local/cuda"
export LD_LIBRARY_PATH="/usr/local/cuda-8.0/lib64"
export PATH="/usr/local/cuda-8.0/bin:$PATH"

Enable changes in bashrc

source ~/.bashrc

Check if the environment variables contain the paths from the previous step

echo $CUDA_HOME
echo $PATH
echo $LD_LIBRARY_PATH

alias sudo='sudo env PATH=$PATH'

Step 5. Tensorflow

Create a conda environment named tensorflow to run a version of Python by invoking the following command:

conda create -n tensorflow

Activate the conda environment by issuing the following command:

source activate tensorflow

Issue a command of the following format to install TensorFlow inside your conda environment:

sudo pip install –ignore-installed –upgrade TF_PYTHON_URL where TF_PYTHON_URL is the URL of the TensorFlow Python package. For example, the following command installs the CPU-only version of TensorFlow for Python 3.6:

sudo pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp36-cp36m-linux_x86_64.whl

Test Tensorflow

Invoke python from your shell as follows:

python

Enter the following short program inside the python interactive shell:

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

Exit python interactive shell

exit()

Step 6. Keras

sudo pip install keras