Deep Learning GPU Installation on Ubuntu 18.4 | by Ana Solaguren-Beascoa, PhD | Towards Data Science

After several times of installing with drivers, CUDA etc... to make my deep learning libraries talk to my GPU, I have decided to write the installation steps that work for Ubuntu18.04.
The first thing one needs to do, is ensure that the GPU is an NVIDIA GPU with the following command:
$ ubuntu-drivers devices
The first thing one needs to do is run the following commands to ensure any preinstalled driver is purged:
$ sudo apt-get autoremove$ sudo apt-get remove nvidia*$ sudo apt-get purge cuda*
This step is sometimes not necessary after the previous one, but I strongly recommend doing it anyway (doing it doesn’t hurt!).
First, you need to open the blacklist-nouveau.conf file (I use gedit, but feel free to use any other type of editor). The command I use is the following:
$ sudo gedit /etc/modprobe.d/blacklist-nouveau.conf
Now, add the following lines to the file.
blacklist nouveaublacklist lbm-nouveauoptions nouveau modeset=0alias nouveau offalias lbm-nouveau off
Save and close the file.
Disable the Kernel nouveau:
$ echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
To add the ppa:graphics-drivers/ppa repository into your system type the following command:
$ sudo add-apt-repository ppa:graphics-drivers/ppa
In the past, I used to download the drivers from the NVIDIA download site (https://www.nvidia.co.uk/Download/index.aspx?lang=en-uk).
The simplest and most reliable way to do it (this has been the direct recommendation from NVIDIA on one of my posts on the NVIDIA forum) is to install it directly from the terminal. The command to install the drivers would be (ensure you install the right driver!):
$ sudo apt install nvidia-driver-435
The other option is to install it directly from the Software & Updates settings in Ubuntu:
Now, chose the correct driver (this is the tricky bit, you might have to come back and test a new driver if this step hasn’t worked). Click on Apply Changes, and you are good to go!
Don’t forget this step!
You will know that the installation has been successful by typing the following command:
$ nvidia-smi
You should see something similar to
If you get an NVIDIA error here, try all the steps from the beginning, but choosing a new driver this time!
When installing CUDA, I usually follow the CUDA installation guide, which is very complete (https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html).
First check that your GPU is can deal with CUDA:
$ lspci | grep -i nvidia
Check you have a supported Linux version:
$ uname -m && cat /etc/*release
This should return something like
x86_64DISTRIB_ID=UbuntuDISTRIB_RELEASE=18.04DISTRIB_CODENAME=bionicDISTRIB_DESCRIPTION=”Ubuntu 18.04.3 LTS”NAME=”Ubuntu”VERSION=”18.04.3 LTS (Bionic Beaver)”ID=ubuntuID_LIKE=debianPRETTY_NAME=”Ubuntu 18.04.3 LTS”VERSION_ID=”18.04"HOME_URL=”https://www.ubuntu.com/"SUPPORT_URL=”https://help.ubuntu.com/"BUG_REPORT_URL=”https://bugs.launchpad.net/ubuntu/"PRIVACY_POLICY_URL=”https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"VERSION_CODENAME=bionicUBUNTU_CODENAME=bionic
Check gcc is installed
$ gcc --version
Check your kernel headers are compatible with CUDA (you can find the compatibility table on the CUDA installation guide):
$ uname -r
Install the kernel headers and packages with the following command:
$ sudo apt-get install linux-headers-$(uname -r)
You can download the CUDA toolkit through the following link:
developer.nvidia.com
For Ubuntu 18.04 you need to download the following
Now, you just need to wait, this download might take a while!
The steps to install CUDA are pretty simple. Just type the following commands on your terminal:
$ wget http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run$ sudo sh cuda_10.1.243_418.87.00_linux.run
You should get the following screen:
Make sure this installation does not install new drivers!!!!!
If the installation has been successful, you should get the following screen:
These are the last couple of steps before you can start using your GPU for deep learning!
The following link takes you to the cuDNN download site:
https://developer.nvidia.com/rdp/form/cudnn-download-survey
You have to login first before you can actually download anything. Once you are in, download:
The cuDNN Library
The cuDNN Runtime Library (Deb)
The cuDNN Developer Library (Deb)
The cuDNN Code Samples (Deb)
These steps mainly follow the instructions in the cuDNN installation site https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html
cd into you Download folder (or wherever the downloaded files are)
Unpack the archive with the following command:
$ tar -zxvf cudnn-10.1-linux-x64-v7.6.5.32.tgz
Copy the files into the CUDA Toolkit directory and change the permisions
$ 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*
Install the three libraries with the following commands
$ sudo dpkg -i libcudnn7_7.6.5.32–1+cuda10.1_amd64.deb$ sudo dpkg -i libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64.deb$ sudo dpkg -i libcudnn7-doc_7.6.5.32-1+cuda10.1_amd64.deb
In order to do so, cuDNN have created a sample code which will run some computation on the MNIST dataset using your GPU. To do so, you need to follow these steps:
$ cp -r /usr/src/cudnn_samples_v7/ $HOME$ cd  $HOME/cudnn_samples_v7/mnistCUDNN$ make clean && make$ ./mnistCUDNN
If everything worked fine, you should see something like:
And now you are good to go with your GPU!