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

This installation guide is tested on Ubuntu 16.04 LTS (please don’t use no LTS version).

  1. Download CUDA toolkit. Actually the latest version is the 9.1, but is not well configured for the use with TensorFlow and Keras. Then please install CUDA 9.0 (see the legacy releases box).
    sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb
    sudo apt-key add /var/cuda-repo-/
    sudo apt update
    sudo apt install cuda

    After the installation, please also install the patches, if they are available.

  2. Download cuDNN (tar file). This step requires a registration to nVidia website. After the registration, select the version of cudNN, that matches with the version of CUDA, that you have installed on your PC. Now the correct version of cuDNN is the v.7.1.2 for CUDA 9.0.
    Installation steps:

    tar -xzvf cudnn-9.0-linux-x64-v7.tgz
    sudo cp cuda/include/cudnn.h /usr/local/cuda/include
    sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
    sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
  3. Open a terminal and install python or python3 and pip
    sudo apt install python3 python3-pip
  4. Install TensorFlow ( GPU-version. Check to staify all the prerequisites
    sudo apt-get install cuda-command-line-tools

    and then install the package using pip

    sudo pip3 install tensorflow-gpu
  5. Install Keras ( through pip
    sudo pip3 install keras

That’s all!

Test correct installation

  1. Open a terminal
  2. Open a python shell
  3. Import TensorFlow
    import tensorflow as tf
  4. Check if the import will produce some mistakes. If there are not errors you have terminated and you can view the version of the installed TensorFlow