Dayalan Saravanan


Install Nvidia drivers and the CUDA toolkit in Ubuntu 20.04

Friday, May 06, 2022

The installation instructions for the proprietary NVIDIA drivers and the CUDA toolkit in Ubuntu 20.04 LTS (Focal Fossa).

To use CUDA on your system, you will need the following installed:

  1. CUDA-capable GPU

    To verify that your GPU is CUDA-capable, from the command line, enter:

    lspci | grep -i nvidia

    If you do not see any settings, update the PCI hardware database that Linux maintains by entering update-pciids (generally found in /sbin) at the command line and rerun the previous lspci command.

    If your graphics card is from NVIDIA and it is listed in https://developer.nvidia.com/cuda-gpus, your GPU is CUDA-capable. The release notes for the CUDA toolkit also contains a list of supported products.


  2. A supported version of Linux with a gcc compiler and toolchain

    The CUDA development tools are only supported on some specific distributions of Linux. These are listed in the CUDA toolkit release notes. To determine which distribution and release number you're running, type the following at the command line:

    uname -m && cat /etc/*release

    You should see output similar to the following, modified for your particular system:

    x86_64 
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=20.04
    DISTRIB_CODENAME=focal
    DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"
    NAME="Ubuntu"
    VERSION="20.04.4 LTS (Focal Fossa)"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu 20.04.4 LTS"
    VERSION_ID="20.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=focal
    UBUNTU_CODENAME=focal

    The x86_64 line indicates you are running on a 64-bit system. The remainder gives information about your distribution.

    The gcc compiler is required for development using the CUDA toolkit. It is not required for running CUDA applications. It is generally installed as part of the Linux installation, and in most cases the version of gcc installed with a supported version of Linux will work correctly.

    To verify the version of gcc installed on your system, type the following on the command line:

    gcc --version

    If an error message displays, you need to install the development tools from your Linux distribution or obtain a version of gcc and its accompanying toolchain.


  3. NVIDIA CUDA Toolkit (available at https://developer.nvidia.com/cuda-downloads)
  4. With either a new or old Ubuntu 20.04 installation, it's likely that you'll have NVIDIA drivers of one sort or another on your system. We need to make sure these are fully removed before attempting to install a new set.

    When working with graphics drivers, it's best to quit from the X.org graphical environment and work on the command line. This can be done by entering the following into a terminal:

    sudo systemctl isolate multi-user.target

    To remove your current NVIDIA drivers, enter the following:

    sudo apt-get remove --purge nvidia*

    It's safer to reboot your machine at this point, although this isn't strictly necessary.

    wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin 
    sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
    wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
    sudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
    sudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pub
    sudo apt-get update
    sudo apt-get -y install cuda

    In .bashrc, set up the PATH environment variables:

    export PATH="$PATH:/usr/local/cuda-11.4/bin/" 
    export PATH="$PATH:/usr/local/cuda-11.4/lib64:/usr/local/cuda-11.4/lib"

    You'll need to reboot your machine after installing the NVIDIA drivers. Once the driver is installed, you can check it with the following command:

    nvidia-smi

    The output will include details similar to the following:

    Thu May  5 22:17:48 2022
    +-----------------------------------------------------------------------------+
    | NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4     |
    |-------------------------------+----------------------+----------------------+
    | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
    | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
    |                               |                      |               MIG M. |
    |===============================+======================+======================|
    |   0  NVIDIA GeForce ...  On   | 00000000:01:00.0 N/A |                  N/A |
    | N/A   44C    P8    N/A /  N/A |     54MiB /  1994MiB |     N/A      Default |
    |                               |                      |                  N/A |
    +-------------------------------+----------------------+----------------------+
    
    +-----------------------------------------------------------------------------+
    | Processes:                                                                  |
    |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
    |        ID   ID                                                   Usage      |
    |=============================================================================|
    |  No running processes found                                                 |
    +-----------------------------------------------------------------------------+

    You can then check CUDA toolkit is installed correctly by running /usr/local/cuda-11.4/bin/nvcc --version, which produces output similar to the following:

    nvcc: NVIDIA (R) Cuda compiler driver 
    Copyright (c) 2005-2021 NVIDIA Corporation
    Built on Sun_Aug_15_21:14:11_PDT_2021
    Cuda compilation tools, release 11.4, V11.4.120
    Build cuda_11.4.r11.4/compiler.30300941_0

    Being correctly installed doesn't necessarily mean CUDA is correctly linked to the NVIDIA driver. To make sure this is working, use the bandwidthTest utility from CUDA's demo_suite folder, usually found in /usr/local/cuda-11.4/extras/demo_suite/

    [CUDA Bandwidth Test] - Starting...
    Running on...
    
     Device 0: NVIDIA GeForce GT 710
     Quick Mode
    
     Host to Device Bandwidth, 1 Device(s)
     PINNED Memory Transfers
       Transfer Size (Bytes)	Bandwidth(MB/s)
       33554432			1500.1
    
     Device to Host Bandwidth, 1 Device(s)
     PINNED Memory Transfers
       Transfer Size (Bytes)	Bandwidth(MB/s)
       33554432			1636.9
    
     Device to Device Bandwidth, 1 Device(s)
     PINNED Memory Transfers
       Transfer Size (Bytes)	Bandwidth(MB/s)
       33554432			26237.7
    
    Result = PASS
    
    NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

    If everything is working correctly, you should see Result = PASS at the end of the output.

    The CUDA Toolkit and CuPy need (should) match versions. Refer to CuPy Installation and install with the following command in the virtual environment.

    pip install cupy-cuda114

Reference: NVIDIA CUDA installation guide for Linux in html version.