The best way to install CUDA on Ubuntu

Stargazer ZJ

Important: This post applies to Ubuntu installed on bare metal. For beginners who try to use CUDA on their laptops, I strongly recommend that you use WSL2 instead, which is beyond the scope of this post.

To begin with, lets’s define what is considered good for researching and development purpose. Criterions include:

  • Latest version: Access to the newest features and bug fixes.
  • Trusted source: Prioritizing Ubuntu official repository > NVIDIA official repository > Third-party PPA.
  • Easy updates: Simple process for getting new driver and CUDA versions.

The steps below applies to fresh installations of Ubuntu on a PC with supported hardware.

Step 1: Add NVIDIA official repository

Open a terminal and run the following commands:

1
2
3
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update

You may need to replace ubuntu2404 and x86_64 with your Ubuntu version and architecture according to official documentation if they are different.

Step 2: Install the meta package

1
sudo apt-get install cuda

Both the driver and CUDA toolkit will be installed.

Step 3: Update environment variables

Add the following lines to your /etc/environment:

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

Reboot your system to apply the changes to the driver and environment variables.

Step 4: Verify the installation

Run nvidia-smi to check the driver version and nvcc -V to check the CUDA version.

Why this is good

  • Latest: Installs the latest CUDA version available in NVIDIA’s repository, often newer than Ubuntu’s official repository.
  • Trusted: Uses NVIDIA’s official repository, ensuring security and stability.
  • Easy updates: apt update && apt upgrade updates both the driver and CUDA.
  • Clean: Avoids conflicts with distribution packages.

If you need a specific version

It’s common for a project to require a specific version of CUDA, and it’s not a good idea to run it with the latest CUDA and expect no troubles, especially for large or old projects.

In this case, you should install the latest version of the driver but a specific version of the CUDA toolkit. Here’s how to do it:

First, install the latest driver as described above, if you haven’t done so already.

1
sudo apt-get install cuda   # if you haven't done so already

Then, for versions later then 12.5, there’s a meta package for the toolkit of the specific version.

1
sudo apt-get install cuda-toolkit-12-5 # Replace 12-5 with the desired version

Earlier versions can be found here.

To use the toolkit, set these environment variables:

1
2
3
export PATH="/usr/local/cuda-12.5/bin${PATH:+:${PATH}}"
export LD_LIBRARY_PATH="/usr/local/cuda-12.5/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
export CUDA_HOME="/usr/local/cuda-12.5"

Two important notes:

  • CUDA drivers are backward compatible, meaning the latest driver is compatible with the older CUDA toolkits. The CUDA Version column in nvidia-smi shows the highest CUDA version supported by the driver, not the one currently installed.
  • Two or more versions of the CUDA toolkit can coexist safely on the same system. You can switch between them by changing the PATH, LD_LIBRARY_PATH, and CUDA_HOME environment variables accordingly.

If you don’t have sudo

If you don’t have sudo privileges, you cannot modify the driver. However, if a working CUDA toolkit is not found on somewhere like /usr/local/cuda, you can still install the CUDA toolkit in your home directory via the runfile installer:

1
2
3
wget https://developer.download.nvidia.com/compute/cuda/12.2.2/local_installers/cuda_12.2.2_535.104.05_linux.run    # Find the desired version on the [CUDA Toolkit Archive](https://developer.nvidia.com/cuda-toolkit-archive)
chmod +x cuda_<version>_linux.run
./cuda_<version>_linux.run --toolkit --silent --toolkitpath=~/.cuda/cuda-<version> --no-opengl-libs

Note that the --silent flag is necessary. Otherwise the installer will ignore the other options and will fail because of the missing sudo privileges.

conda users can also install and manage CUDA toolkits in conda environments, although the writer strongly disapproves of using conda personally. uv is a better alternative for managing Python environments and fits better with the pytorch ecosystem.

Postscript

I wonder why such a simple and effective method is not the primary recommendation in the official documentation.

Postscript 2

A common mistake is to install the CUDA toolkit from the Ubuntu official repository, such as sudo apt-get install nvidia-cuda-toolkit. This can lead to a very outdated version installed.

  • Title: The best way to install CUDA on Ubuntu
  • Author: Stargazer ZJ
  • Created at : 2025-02-10 14:33:17
  • Updated at : 2025-06-28 14:19:13
  • Link: https://ji-z.net/2025/02/10/The-best-way-to-install-CUDA-on-Ubuntu/
  • License: This work is licensed under CC BY-NC-SA 4.0.