Introduction

This tutorial will show you how to install CUDA and cuDNN for tensorflow-gpu in Ubuntu 18.04. There are some online tutorials that always gave me errors when I followed them to do the setup, which may be due to the new Ubuntu OS version and other version conflicts, so I’ve written a detailed guide to assist you with this.

Firstly, you should know your development envirnment clearly, here is my pc environment and library that will be used (on 26 Sep 2018):

  • Ubuntu 18.04.1
  • Python 3.6.6
  • GCC 5 (default is 7.3 for Ubuntu 18.04.1)
  • NVIDIA-driver-390
  • CUDA 9.0.176
  • cuDNN 7.2.1.38
  • Tensorflow-gpu 1.10.1

Verify hardware and software

Verify your NVIDIA GPU CUDA enable

$ lspci | grep -i nvidia

You shoud see the NVIDIA GPU information, for my case, I use GTX 1080Ti.

Verify your current NVIDIA GPU driver version

$ nvidia-smi

You shoud see something like “nvidia-384”, “nvidia-390”, “nvidia-396” …

Check gcc complier version

$ gcc --version

The current default gcc version is 7.3 in Ubuntu 18.04.1, we need to downgrade gcc version for later CUDA installation.

Intall NVIDIA GPU driver

Note: For CUDA 9.0, it requires at least nvidia-384 driver, so if your current driver version is equal to or newer than 384, you do not need to upgrade or reinstall the driver, can skip the following driver installation steps.

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt update
$ sudo apt install nvidia-384 nvidia-384-dev

Environment Setup

Install dependent library

$ sudo apt install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

Downgrade gcc version

To install CUDA, gcc must not be newer than gcc 6.x, I choose to downgrade to gcc 5.x

$ sudo apt install gcc-5 g++-5
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50

Install CUDA

Prepare/Download CUDA installtion packages

Download the runfile installation packager from CUDA website legacy download, download base installer (cuda_9.0.176_384.81_linux-run for Ubuntu 16.04) and four patch file(cuda_9.0.176.x_linux.run)/

Installation

$ chmod +x cuda_9.0.176_384.81_linux-run
$ sudo ./cuda_9.0.176_384.81_linux-run --override

Answer following questions while installtion begin

You are attempting to install on an unsupported configuration. Do you wish to continue? yes
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81? no  (MUST SELECT NO!!!)
Install the CUDA 9.0 Toolkit? yes
...... yes

Then you can install the cuBLAS patch file(total 4):

$ chmod +x cuda_9.0.176.x_linux.run
$ sudo ./cuda_9.0.176.x_linux.run

set up symlinks for gcc

$ sudo ln -s /usr/bin/gcc /usr/local/cuda/bin/gcc
$ sudo ln -s /usr/bin/g++ /usr/local/cuda/bin/g++

set up cuda system path

$ echo 'export PATH=/usr/local/cuda-9.0/bin:$PATH' >> ~/.bashrc
$ echo 'export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
$ source ~/.bashrc

Vefify the installation

$ cd /usr/local/cuda/samples/1_Utilities/deviceQuery
$ sudo make 
$ ./deviceQuery

You shoud see the passed output results.

Install cuDNN

Download cuDNN installtion file

Download the file(cudnn-9.0-linux-x64-v7.2.1.38.tgz) from cuDNN website, please take note that you need to login in as the developer to start download.

Install

$ tar -xzvf cudnn-9.0-linux-x64-v7.2.1.38.tg
$ sudo cp -P cuda/include/cudnn.h /usr/local/cuda-9.0/include
$ sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64/
$ sudo chmod a+r /usr/local/cuda-9.0/lib64/libcudnn*

Vefify the installation

$ nvcc -V

Currently, you should install CUDA 9.0 and cuDNN 7.2 successfully.

Install Tensorflow-gpu

The current TF version is 1.10.1, it only supports up to CUDA 9.0 if using pip installation, that’s why I choose to install CUDA 9.0, this TF installation should be very easy in this situation.

$ sudo apt install python3-pip
$ pip3 install tensorflow-gpu==1.10.1

Hope this can help you.