Back
Featured image of post Installing CUDA Toolkit v11.4.2 on Ubuntu

Installing CUDA Toolkit v11.4.2 on Ubuntu

Steps on how to install CUDA Toolkit v11.4.2 on Linux

Table of Content

This guide covers the basic instructions needed to install CUDA and verify that a CUDA application can run on Linux.

CUDA on Linux can be installed using an RPM, Debian, Runfile, or Conda package, depending on the platform being installed on.

Installing CUDA Toolkit on Ubuntu

When installing CUDA on Ubuntu, you can choose between the Runfile Installer and the Debian Installer. The Runfile Installer is only available as a Local Installer. The Debian Installer is available as both a Local Installer and a Network Installer. The Network Installer allows you to download only the files you need. The Local Installer is a stand-alone installer with a large initial download. In the case of the Debian installers, the instructions for the Local and Network variants are the same. For more details, refer to the Linux Installation Guide.

Pre-installation Actions

Some actions must be taken before the CUDA Toolkit and Driver can be installed on Linux:

  • Verify the system has a CUDA-capable GPU.
  • Verify the system is running a supported version of Linux.
  • Verify the system has gcc installed.
  • Verify the system has the correct kernel headers and development packages installed.
  • Download the NVIDIA CUDA Toolkit.
  • Handle conflicting installation methods.

Verify the system has a CUDA-capable GPU

To verify that your GPU is CUDA-capable, go to your distribution’s equivalent of System Properties, or, from the command line, run:

1
lspci | grep -i nvidia

It must return your detected Graphic Card

1
01:00.0 VGA compatible controller: NVIDIA Corporation GP107GL [Quadro P620] (rev a1)

Verify the system is running a supported version of Linux

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:

1
uname -m && cat /etc/*release

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 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=bionic
UBUNTU_CODENAME=bionic

Verify the system has gcc installed

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:

1
gcc --version
1
2
3
4
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Verify the system has the correct kernel headers and development packages installed

The kernel headers and development packages for the currently running kernel can be installed with:

1
sudo apt-get install linux-headers-$(uname -r)

Download the NVIDIA CUDA Toolkit

The NVIDIA CUDA Toolkit is available at https://developer.nvidia.com/cuda-downloads.

Handle conflicting installation methods.

Before installing CUDA, any previously installations that could conflict should be uninstalled. This will not affect systems which have not had CUDA installed previously, or systems where the installation method has been preserved

Using Debian installer

Basically, the steps to install using this method are. Install the repository meta-data, install GPG key, update the apt-get cache, and install CUDA.

Note that is sudo is installed on your system, you need to execute following commands with sudo and the current user must be in sudoers.

1
2
3
4
5
6
7
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 https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda-repo-ubuntu1804-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu1804-11-4-local/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1804-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo apt-get update
sudo apt-get -y install cuda

Now you to reboot the machine and setup your environment.

1
2
export PATH=/usr/local/cuda-11.4/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.4/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Finally, if you want the samples you can get them with

1
2
3
4
cuda-install-samples-11.4.sh ~
cd ~/NVIDIA_CUDA-11.4_Samples/5_Simulations/nbody
make
./nbody

Remember that you have to run the samples by navigating to the executable’s location, otherwise it will fail to locate dependent resources.

Using runfile installer

DISABLE Nouveau drivers

Create a file at /etc/modprobe.d/blacklist-nouveau.conf with the following contents

1
2
blacklist nouveau
options nouveau modeset=0

Regenerate the kernel initramfs

1
sudo update-initramfs -u

After this step, reboot into runlevel 3 by temporarily adding the number 3 and the word nomodeset to the end of the system’s kernel boot parameters. Finally, run the installer:

1
2
wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda_11.4.2_470.57.02_linux.run
sudo sh cuda_11.4.2_470.57.02_linux.run --silent

Create an xorg.conf file to use the NVIDIA GPU for display:

1
sudo nvidia-xconfig

CUDA Toolkit Post-installation steps

The post-installation actions must be manually performed. These actions are split into mandatory, recommended, and optional sections.

  • Environment Setup
  • Install Persistence Daemon

Install Persistence Daemon

NVIDIA is providing a user-space daemon on Linux to support persistence of driver state across CUDA job runs. The daemon approach provides a more elegant and robust solution to this problem than persistence mode. For more details on the NVIDIA Persistence Daemon.

The NVIDIA Persistence Daemon can be started as the root user by running

1
/usr/bin/nvidia-persistenced --verbose

References



💬 Share this post in social media

Thanks for checking this out and I hope you found the info useful! If you have any questions, don't hesitate to write me a comment below. And remember that if you like to see more content on, just let me know it and share this post with your colleges, co-workers, FFF, etc.

You are free to use the content, but mention the author (@MrSergioAnguita) and link the post!
Last updated on Oct 14, 2021 17:52 CEST
Please, don't try to hack this website servers. Guess why...