Installing TensorFlow and Keras on Ubuntu 16.04

Full installation guide for TensorFlow, Keras, CUDA and cuDNN on Ubuntu 16.04 (64bit) with Python 3.5.

GPU driver installation

Check for system updates and install them.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade

Open configuration for grub.

$ sudo gedit /etc/default/grub

Find line GRUB_CMDLINE_LINUX_DEFAULT and change it:

GRUB_CMDLINE_LINUX_DEFAULT="nouveau.blacklist=1 quiet splash nomodeset"

Save and close the file, then update grub settings.

$ sudo update-grub2

We need to make sure there is no other installation of GPU driver and eventually remove it. Reboot your computer after this step.

$ sudo apt-get remove nvidia*
$ sudo apt-get autoremove
$ sudo reboot

Download from NVIDIA webpage correct driver for your card. Now make the .run file executable.

# File name can be different
$ cd Downloads
$ chmod +x

Open file blacklist.conf

$ sudo gedit /etc/modprobe.d/blacklist.conf

Place these line at the end of the file, save and close.

blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist nvidiafb
blacklist rivatv
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off

Open putty terminal with Ctrl + Alt + F1 and stop running graphical enviroment, so we can successfully install driver.

$ sudo service lightdm stop
$ cd Downloads
$ sudo ./chmod +x

Open grub config again.

$ sudo nano /etc/default/grub

Find line GRUB_CMDLINE_LINUX_DEFAULT and edit it.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"

Finally, update driver and restart your computer.

$ sudo update-grub2
$ sudo reboot

You should now have working driver for your graphic card.

[1] Original source

NVIDIA CUDA installation

Download installation file, choose deb (local) version and install it.

$ cd Downloads
$ sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get install cuda

NVIDIA cuDNN installation

You need to register on NVIDIA website in order to download cuDNN. After your approval, download correct version of cuDNN (newest), which is compatible with your version of CUDA.

Unzip the archive and move files into the CUDA installation folder.

$ cd Downloads
$ tar xvzf cudnn-8.0-linux-x64-v5.1-ga.tgz
$ sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

It is useful to add paths into bash settings. Open bash config file.

$ gedit ~/.bash_profile

Put these lines at the end.

$ export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
$ export CUDA_HOME=/usr/local/cuda

TensorFlow installation

You need a Python installation before installing TensorFlow. If you do not have one yet, you can install Anaconda distribution, preferably with Python 3.5.

Open terminal and create new enviroment for TensorFlow installation.

$ conda create -n tensorflow python=3.5
$ source activate tensorflow

Set URL for TensorFlow to download. This URL can be different for other versions. If you are not on 64bit version of Ubuntu, check line for TensorFlow original installation beneath.

$ export TF_BINARY_URL=

And finally install TensorFlow.

$ pip3 install --ignore-installed --upgrade $TF_BINARY_URL

[2] TensorFlow installation

Keras installation

This step is optional.

You can install Keras from PyPI, so all you need to do is switch to tensorflow enviroment and install it using pip.

$ source activate tensorflow
$ sudo pip install keras


You now have installation of TensorFlow and Keras support GPU. When you want to use TensorFlow, type source activate tensorflow into terminal, source deactivate to switch back to default enviroment.

If you need to install new packages into this enviroment, use conda install or pip install. Remember that these packages will only be installed into currently used enviroment.

One useful thing when you use your graphic card is nvidia-smi. It is a small CLI program, that let you monitor GPU utilization. Type watch nvidia-smi into terminal and it will by updated every second.