How to install CUDA and cuDNN on Ubuntu 16.04 LTS

I’ve been running this machine for TensorFlow and Keras with Jupyter notebook.

These are my environments:

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

$ lspci | grep VGA
01:00.0 VGA compatible controller: NVIDIA Corporation GP104 [GeForce GTX 1080] (rev a1)

Disable secure boot on UEFI

First, we need to disable “secure boot” on UEFI menu because it prevents NVIDIA driver from loading into OS. So enter UEFI menu while boot time by pressing the key something like the delete key.

That’s the first time I heard that. I used to use BIOS, but now, a lot of motherboards recently released use UEFI. And UEFI has secure boot (see detail here, this was helpful for me).

If you use an ASUS motherboard, this document also can be helpful.

Remove old NVIDIA driver and CUDA

$ dpkg -l | grep nvidia
$ dpkg -l | grep cuda
$ sudo apt-get --purge remove nvidia-*
$ sudo apt-get --purge remove cuda-*

Install new NVIDIA driver and CUDA9.0

Download CUDA Toolkit 9.0

Download from NVIDIA developer site.

Instatll

$ sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
$ sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install cuda

Add PATH and LD_LIBRARY_PATH

~/.bashrc

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

Then reboot.

sudo reboot

Check that the driver was installed

$ nvidia-smi

This command shows the status of the graphics card installed on your machine like this:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.25                 Driver Version: 390.25                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1080    Off  | 00000000:01:00.0  On |                  N/A |
| 27%   36C    P8    12W / 180W |   7753MiB /  8116MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1351      G   /usr/lib/xorg/Xorg                            24MiB |
|    0     12638      C   /home/jupyter/tensorflow/bin/python3        7717MiB |
+-----------------------------------------------------------------------------+

Install cuDNN

Download cuDNN

Download Debian package from NVIDA developer site.

Install the rutime library

$ sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb

Install the developer library

$ sudo dpkg -i libcudnn7-dev_7.0.5.15-1+cuda9.0_amd64.deb

Add CUDA_HOME

~/.bashrc

export CUDA_HOME=/usr/local/cuda-9.0

Check that the cuDNN works

$ cp -r /usr/src/cudnn_samples_v7/ $HOME
$ cd  $HOME/cudnn_samples_v7/mnistCUDNN
$ make clean && make
$ ./mnistCUDNN
Test passed!

Stop LightDM

LightDM start to run when you install CUDA. If you use Ubuntu server, it’s not necessary to run.

Disable LigthDM

Rewrite GRUB_CMDLINE_LINUX like below:

/etc/default/grub

GRUB_CMDLINE_LINUX="systemd.unit=multi-user.target"

Update and reboot.

sudo update-grub
sudo reboot