Table of Contents
CUDA (Compute Unified Device Architecture) is a parallel computing platform developed by NVIDIA that allows developers to use the power of NVIDIA GPUs to accelerate compute-intensive applications. Installing CUDA on Ubuntu can greatly enhance the performance of applications that can be parallelized, such as machine learning algorithms and scientific simulations.
However, installing CUDA on Ubuntu can be a complex process that involves several steps and dependencies. In this article, we will provide a step-by-step guide on how to install CUDA on Ubuntu, starting with the necessary prerequisites and ending with a working CUDA installation. We will also provide tips and troubleshooting advice to help you overcome common issues and get the most out of your CUDA installation.
Whether you're a machine learning practitioner, a scientific researcher, or simply interested in exploring the capabilities of CUDA, this guide will provide you with the information you need to get started with CUDA on Ubuntu.
Pre-requisites
Check the compute capability of your GPU
You can check the compute capability of your
$ nvidia-smi --query-gpu=compute_capability --format=csv
compute_capability
3.5
In this example, the compute capability of the GPU is 3.5, which means it is compatible with CUDA. If the compute capability of your GPU is lower than 3.0, it may not be compatible with CUDA.
Install the latest NVIDIA drivers
You can install the latest NVIDIA drivers on Ubuntu using the Ubuntu package manager. First, add the NVIDIA PPA to your system:
sudo add-apt-repository ppa:graphics-drivers/ppa
Then, update the package list and install the latest NVIDIA drivers:
sudo apt-get update sudo apt-get install nvidia-driver-470
Replace "nvidia-driver-470" with the latest version of the NVIDIA driver available for your system.
Install development tools
Development tools are essential for installing and building software applications on a Linux system. In the context of CUDA installation on Ubuntu, we need development tools such as gcc, g++, and make to build the CUDA toolkit and to compile and link CUDA applications.
You can install the necessary development tools on Ubuntu using the Ubuntu package manager. Run the following command to install gcc, g++, and make:
sudo apt-get install build-essential
Install CMake
CMake is a cross-platform build system that is used to build software applications. It generates build files that can be used to compile and link the application on different platforms and build systems.
For CUDA applications, CMake is used to generate Makefiles that are used to build the application. CMake allows developers to write platform-independent build scripts that can be used to build CUDA applications on different platforms and build systems.
You can install CMake on Ubuntu using the Ubuntu package manager. Run the following command to install CMake:
sudo apt-get install cmake
Different methods to install CUDA on Ubuntu
Cuda Toolkit is a toolkit for developing and executing Cuda applications. We will download the latest version of Cuda Toolkit from Nvidia website and explain the installation in 3 different ways.
Method-1: Install From .deb (local)
First, wget downloads the cuda-ubuntu2204.pin
file, which contains priority settings for how CUDA packages should be installed or updated:
$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
--2023-03-24 22:32:13-- https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
Resolving developer.download.nvidia.com (developer.download.nvidia.com)... 152.199.20.126
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|152.199.20.126|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 190 [application/octet-stream]
Saving to: ‘cuda-ubuntu2204.pin’
cuda-ubuntu2204.p 100%[=============>] 190 --.-KB/s in 0s
2023-03-24 22:32:13 (9,02 MB/s) - ‘cuda-ubuntu2204.pin’ saved [190/190]
The downloaded file is moved under the /etc/apt/preferences.d/
directory:
$ sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
Pull the .deb
package with wget, including the repository addresses for CUDA:
$ wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb
The downloaded .deb package is system installed:
$ sudo dpkg -i cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb
The CUDA gpg key file is copied:
$ sudo cp /var/cuda-repo-ubuntu2204-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
Then the package list is updated using apt command:
$ sudo apt update -y
And the CUDA package is installed together with its dependencies:
$ sudo apt install cuda -y
You can list the installed CUDA packages like this:
$ dpkg -l | grep cuda ii cuda 12.1.0-1 amd64 CUDA meta-package ii cuda-12-1 12.1.0-1 amd64 CUDA 12.1 meta-package ii cuda-cccl-12-1 12.1.55-1 amd64 CUDA CCCL ii cuda-command-line-tools-12-1 12.1.0-1 ...
In this method, a customized package was installed with the pin file.
Method-2: Install From .deb (network)
The key of the repository where the CUDA package will be installed is pulled with wget:
$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
--2023-03-24 20:10:07-- https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
Resolving developer.download.nvidia.com (developer.download.nvidia.com)... 152.199.20.126
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|152.199.20.126|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4332 (4.2K) [application/x-deb]
Saving to: ‘cuda-keyring_1.0-1_all.deb’
cuda-keyring_1.0- 100%[=============>] 4.23K --.-KB/s in 0s
2023-03-24 20:10:08 (50.1 MB/s) - ‘cuda-keyring_1.0-1_all.deb’ saved [4332/4332]
The key file is installed with a root user or a sudo authorized user as follows:
$ sudo dpkg -i cuda-keyring_1.0-1_all.deb
[sudo] password for foc:
Selecting previously unselected package cuda-keyring.
(Reading database ... 157659 files and directories currently installed.)
Preparing to unpack cuda-keyring_1.0-1_all.deb ...
Unpacking cuda-keyring (1.0-1) ...
Setting up cuda-keyring (1.0-1) ...
A deprecated public CUDA GPG key appear to be installed.
To remove the key, run this command:
sudo apt-key del 7fa2af80
After installing the .deb
package, the following file is created under the /etc/apt/sources.list.d/
directory:
$ cat /etc/apt/sources.list.d/cuda-ubuntu2204-x86_64.list
deb [signed-by=/usr/share/keyrings/cuda-archive-keyring.gpg] https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /
Then the package list is updated:
$ sudo apt update -y
And the CUDA package is installed:
$ sudo apt install cuda -y
In this method, the package can be updated and a secure installation is ensured as the repository key is added to the system.
Method-3: Install From .runfile (local)
In this method, the installation will be done with the executable .run
file, not with the .deb package. Pull the .run file from the Nvidia official site with wget:
$ wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda_12.1.0_530.30.02_linux.run
This process may take some time depending on your internet speed. The 4.0G cuda_12.1.0_530.30.02_linux.run
file will be downloaded.
If there is no gcc package after downloading the file, install it:
$ sudo apt install gcc -y
Then give run permission to the .run file:
$ chmod +x cuda_12.1.0_530.30.02_linux.run
Now run the .run
file:
$ sudo sh cuda_12.1.0_530.30.02_linux.run
Accept the license agreement:
┌─────────────────────────────────────────────────────────────────────── │ End User License Agreement │ -------------------------- │ │ NVIDIA Software License Agreement and CUDA Supplement to │ Software License Agreement. Last updated: October 8, 2021 │ │ The CUDA Toolkit End User License Agreement applies to the │ NVIDIA CUDA Toolkit, the NVIDIA CUDA Samples, the NVIDIA │ Display Driver, NVIDIA Nsight tools (Visual Studio Edition), │ and the associated documentation on CUDA APIs, programming │ model and development tools. If you do not agree with the │ terms and conditions of the license agreement, then do not │ download or use the software. │ │ Last updated: October 8, 2021. │ │ │ Preface │ ------- │ │─────────────────────────────────────────────────────────────────────── │ Do you accept the above EULA? (accept/decline/quit): │accept └───────────────────────────────────────────────────────────────────────
Check the CUDA driver and Toolkit:
┌─────────────────────────────────────────────────────────────────────── │ CUDA Installer │ - [X] Driver │ [X] 530.30.02 │ + [X] CUDA Toolkit 12.1 │ [ ] CUDA Demo Suite 12.1 │ [ ] CUDA Documentation 12.1 │ - [ ] Kernel Objects │ [ ] nvidia-fs │ Options │ Install │ │ │ │ Up/Down: Move | Left/Right: Expand | 'Enter': Select | 'A': Advanced o └───────────────────────────────────────────────────────────────────────
Select "Install" with the keyboard and start the installation.
Note :Â If "Ensure there is enough space in /tmp and that the installation package is not corrupt." If you get the error, follow the steps below and restart the installation:
$ mkdir ~/temp $ sudo sh cuda_12.1.0_530.30.02_linux.run --tmpdir=./temp
Installation completed:
$ sudo sh cuda_12.1.0_530.30.02_linux.run --tmpdir=./temp
===========
= Summary =
===========
Driver: Installed
Toolkit: Installed in /usr/local/cuda-12.1/
Please make sure that
- PATH includes /usr/local/cuda-12.1/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-12.1/lib64, or, add /usr/local/cuda-12.1/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-12.1/bin
To uninstall the NVIDIA Driver, run nvidia-uninstall
Logfile is /var/log/cuda-installer.log
Setup Environment Variables
After installing the CUDA toolkit on Ubuntu, you need to set up the environment variables so that your system can find the CUDA libraries and binaries. The two environment variables that need to be updated are PATH and LD_LIBRARY_PATH.
PATH is an environment variable that specifies the directories where executable files are located. By adding the path to the CUDA binaries to the PATH variable, you can ensure that your system can find the CUDA tools and utilities.
LD_LIBRARY_PATH is an environment variable that specifies the directories where shared libraries are located. By adding the path to the CUDA libraries to the LD_LIBRARY_PATH variable, you can ensure that your system can find the CUDA runtime libraries.
To set up the environment variables, you can add the following lines to your ~/.bashrc
file:
export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
This adds the path to the CUDA binaries and libraries to the PATH and LD_LIBRARY_PATH variables respectively. Once you have added these lines to your .bashrc file, you can save the file and run the following command to apply the changes:
source ~/.bashrc
By setting up the environment variables correctly, you can ensure that your system can find the CUDA libraries and binaries, allowing you to develop and run CUDA applications on your Ubuntu system.
Verifying Installation
Verify the NVIDIA driver installation
This output indicates that the NVIDIA driver is installed and that two NVIDIA GPUs are present on the system.
# nvidia-smi +-----------------------------------------------------------------------------+ | NVIDIA-SMI 460.73.01 Driver Version: 460.73.01 CUDA Version: 11.2 | |-------------------------------+----------------------+----------------------+ | 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 GeForce GTX 1650 Off | 00000000:01:00.0 Off | N/A | | N/A 45C P8 1W / N/A | 410MiB / 3911MiB | 2% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ | 1 GeForce GTX 1650 Off | 00000000:02:00.0 Off | N/A | | N/A 43C P8 1W / N/A | 166MiB / 3911MiB | 0% Default | | | | N/A | +-------------------------------+----------------------+----------------------+
Verify the CUDA installation
This command should display information about the version of the CUDA toolkit installed on your system. If it displays an error message or if the version information is not displayed, it indicates that the CUDA toolkit may not be installed correctly.
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Feb__7_19:32:13_PST_2023
Cuda compilation tools, release 12.1, V12.1.66
Build cuda_12.1.r12.1/compiler.32415258_0
Summary
In summary, installing CUDA on Ubuntu involves downloading the CUDA toolkit installer from the NVIDIA website, installing the required development tools and dependencies, running the installer, configuring environment variables, and verifying the installation.
First, you need to ensure that your system meets the hardware and software requirements for CUDA. Then, install the development tools and dependencies required by CUDA using the terminal commands provided.
Next, run the CUDA toolkit installer, accept the license agreement, and select the components to install. After installation is complete, you need to add the CUDA binaries to your system PATH and set the LD_LIBRARY_PATH environment variable by modifying your ~/.bashrc file.
Finally, verify the installation by running the nvidia-smi
command in the terminal, which should display information about the NVIDIA GPUs installed on your system.
By following these steps, you should be able to successfully install CUDA on Ubuntu and begin developing and running CUDA programs.
You can get help from Nvidia forum with your CUDA related problems.
Feel free to comment on the problems you encounter with CUDA installation on Ubuntu.
References
docs.nvidia.com - NVIDIA CUDA Installation Guide for Linux