This post describes how to install nvidia driver, CUDA and cuDNN for ubuntu.

Prerequisites

Note: Make sure to check version compatibility between drivers.

Install Nvidia Driver

You can install nvidia driver when you install CUDA. But If you want to install a specific version of the nvidia driver, download it from official website, or download it directly using wget. For example,

$ wget http://us.download.nvidia.com/XFree86/Linux-x86_64/418.56/NVIDIA-Linux-x86_64-418.56.run

CUDA

Download runfile from the official website. I selected Linux-x86_64-Ubuntu-16.04-runfile (local). Or, you can also run the following line.

$ wget https://developer.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.168_418.67_linux.run

cuDNN

Download cuDNN from here. You need to have an nvidia developer account to download it. Download cuDNN 1. Runtime, 2. Developer, and 3. Code Samples and User Guide for Ubuntu 16.04 (The last one is optional).

Installation

Step 1. Nvidia driver

Note: Install dkms before installation (sudo apt-get install dkms).

  • Stop lightdm and enter into text login mode.
    $ sudo service stop lightdm
    $ sudo init 3
    
  • Install nvidia driver
    $ sudo sh NVIDIA-Linux-x86_64-418.56.run --dkms --no-opengl-files --no-x-check
    

    When there is a problem installing the driver (e.g. "You appear to be running an X server"), --no-x-check gives you detail error, such as "nvidia-uvm appears to already be loaded in your kernel". And if there is such error, run the following command:

    $ sudo stop nvidia-digits-server
    $ sudo service nvidia-docker stop
    $ sudo rmmod nvidia-uvm
    

Step 2. Install CUDA

  • Run following command, and select only CUDA toolkit.
    $ sudo sh cuda_10.1.168_418.56.run
    

Step 3. Install cuDNN

  • Install runtime, developer, code samples and the cuDNN library User Guide (optional) with following lines.
    $ sudo dpkg -i libcudnn7_7.6.0.64-1+cuda10.1_amd64.deb
    $ sudo dpkg -i libcudnn7-dev_7.6.0.64-1+cuda10.1_amd64.deb
    $ sudo dpkg -i libcudnn7-doc_7.6.0.64-1+cuda10.1_amd64.deb
    

Step 4. Set Environment Variables

  • Prepend cuda path to LD_LIBRARY_PATH and PATH path in .zshrc or .bashrc.
    $ vim ~/.zshrc
    
    export LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH"
    export PATH="/usr/local/cuda/bin:$PATH"
    

References

  1. cuDNN installation guide
  2. nvidi-uvm error