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.

Postscript

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

  • Title: The best way to install CUDA on Ubuntu
  • Author: Stargazer ZJ
  • Created at : 2025-02-10 14:33:17
  • Updated at : 2025-02-11 15:51:39
  • 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.