Install cuDNN and CUDA in Ubuntu 18.04

How to Install CUDA and cuDNN on Ubuntu Linux

Installations of CUDA toolkit along with cuDNN can become very tiring because of all the dependencies involved. This is the step by step tutorial of how to install CUDA toolkit with cuDNN on Ubuntu Linux machine, but before we move to installation, you might want to know:

What is CUDA toolkit?

According to NVIDIA’s official site, the NVIDIA CUDA Toolkit provides a development environment for creating high performance GPU-accelerated applications. If you have a need to create a CPU intensive program (like games, video/audio processing etc) and want to use NVIDIA’s card if available, then this library is for you.

But then, what is NVIDIA cuDNN?

NVIDIA cuDNN is a GPU-accelerated library of primitives for deep neural networks. This library can greatly reduce build time of AI and machine learning programs. Most of the famous libraries like dlib support cuDNN library.

Enough with the theory,

These are the steps to install CUDA on Ubuntu

Note: These steps are tested on Ubuntu 18.04 operating system with latest CUDA 10.2 toolkit. Most of these steps should work with older installations.

1. Check your system detects Nvidia graphic card properly

First step is to create check whether you have installed nvidia hardware properly and is detected by Ubuntu. Type following command in Terminal:

lspci | grep -i nvidia

You should see your Nvidia hardware info as shown below

install cuda on linux

2. Install dependencies for CUDA library

Run the following command to install dependencies required for CUDA toolkit

sudo apt-get install build-essential gcc-multilib dkms

3. Remove any previous installations, if present by following command

sudo apt-get purge nvidia*

4. Download the CUDA Toolkit from nvidia

To download CUDA toolkit, go to Nvidia Downloads page.

Select options for your platform as shown in figure. Once the options are selected, you will see set of command to execute on terminal to install CUDA.

download cuda library - how to install cuda on ubuntu

5. Install CUDA toolkit

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-10-2-local-10.2.89-440.33.01/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

6. Update PATH variable to locate CUDA toolkit

sudo nano /etc/profile.d/cuda.sh

#write following lines in nano editor
export PATH=$PATH:/usr/local/cuda/bin
export CUDADIR=/usr/local/cuda
# save and exit

sudo nano /etc/ld.so.conf.d/cuda.conf
#write 
/usr/local/cuda/lib64
# save and exit 
sudo ldconfig

Now CUDA toolkit is installed and the path is also updated, good time to reboot your system.

After reboot, type `nvidia-smi` in command prompt to check whether nvidia toolkit installation.

Nvidia-smi output

7. Download cuDNN library

To download cuDNN library. You will need to register on Nvidia website. Once registered, you will redirected to cuDNN download page. Download latest cnDNN library for CUDA 10.2. At the time of writing, latest version of cnDNN library for Linux is v7.6.5.

Note: if you want to download the library using command line, follow these steps

Click the link of the library on browser window.

Once the download is started, pause the download and now copy the link address. Check below the Chrome download section.

Now download the file using wget command with copied URL in terminal. (Notice the part after ‘/?’ this is the access token of yours:

wget https://developer.download.nvidia.com/compute/machine-learning/cudnn/secure/7.6.5.32/Production/10.2_20191118/cudnn-10.2-linux-x64-v7.6.5.32.tgz?YOUR_ACCESS_TOKEN

This should start your download from command line.

After the file is downloaded, extract the library using tar command. It will be extracted in a new folder named cuda.

tar -zxf cudnn-10.2-linux-x64-v7.6.5.32.tgz

8. Install cnDNN library

To install cuDNN type the following commands in terminal.

  cd cuda/
  sudo cp lib64/* /usr/local/cuda/lib64/
  sudo cp include/* /usr/local/cuda/include/
  sudo reboot 

9. Verify cuDNN installation

After reboot you should have cuDNN installed on your computer. you can verify your installation via following command

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
# it should output

#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

So thats it. We have successfully installed CUDA toolkit with cuDNN library. Now, you should be able to build all the cool machine learning algorithms within your system. Cheers!

Next Step:

Installing dlib library in Ubuntu

Leave a Reply

%d bloggers like this: