How to Install CUDA on Ubuntu

GPU-accelerated computing powers machine learning training, scientific simulations, video encoding, and real-time data analysis. NVIDIA’s CUDA Toolkit provides the compiler, libraries, and debugging tools you need to build applications that harness your GPU’s parallel processing capabilities. By the end of this guide, you will have a complete CUDA development environment with access to the nvcc compiler, cuBLAS, cuFFT, and cuRAND libraries, plus profiling tools like Nsight Compute.

This guide uses NVIDIA’s official APT repository, which handles dependencies automatically and provides security updates through standard system updates. You will configure the repository, install the toolkit and drivers, verify the installation works correctly, and learn how to troubleshoot common issues.

Check GPU Compatibility Before Installation

Before installing CUDA, verify that your system has an NVIDIA GPU with compute capability 5.0 or higher. CUDA 12.x and 13.x require Maxwell architecture or newer (GTX 750 Ti and later). Run the following command to identify your GPU:

lspci | grep -i nvidia

You should see output similar to:

01:00.0 VGA compatible controller: NVIDIA Corporation GA106 [GeForce RTX 3060 Lite Hash Rate] (rev a1)

If the command returns no output, your system either lacks an NVIDIA GPU or the hardware is not detected. Check your BIOS settings and ensure the GPU is properly seated before proceeding.

Remove Existing CUDA and NVIDIA Installations

Skip this section if you have a fresh system without prior NVIDIA or CUDA installations. However, if you already have existing NVIDIA drivers on Ubuntu or CUDA packages, remove them first to prevent conflicts and installation failures.

Remove NVIDIA Packages Installed via APT

If you installed NVIDIA drivers or CUDA using the APT package manager, the following command removes all related packages cleanly. The quoted wildcard patterns prevent shell expansion and allow APT to match packages precisely:

sudo apt remove --purge "*cuda*" "*cublas*" "*cufft*" "*cufile*" "*curand*" "*cusolver*" "*cusparse*" "*gds-tools*" "*npp*" "*nvjpeg*" "nsight*" "*nvvm*" "*nvidia*"

Next, after removing the packages, clean up orphaned dependencies:

sudo apt autoremove --purge

The --purge flag removes configuration files along with the packages, ensuring a clean uninstallation.

Remove NVIDIA Drivers Installed via Runfile

If you installed NVIDIA drivers using a runfile, the uninstallation process differs from APT-based installations. Before running the uninstaller, switch to a text console (TTY), sign in, and stop the display manager:

sudo systemctl stop gdm3

Next, run the NVIDIA uninstaller:

sudo /usr/bin/nvidia-uninstall

Finally, after removal completes, reboot or restart the display manager. This script removes driver components installed through the runfile method without affecting system package management.

Never mix installation methods. Using APT removal commands on runfile installations (or vice versa) creates incomplete removals and conflicts that are difficult to resolve.

Remove CUDA Toolkit Installed via Runfile

If you installed the CUDA Toolkit using a runfile instead of the APT repository, remove it using the included uninstaller script. Replace X.Y with your installed CUDA version:

sudo /usr/local/cuda-X.Y/bin/cuda-uninstaller

For example, to remove CUDA 13.1, use /usr/local/cuda-13.1/bin/cuda-uninstaller. The script handles removal of all toolkit components installed through the runfile method.

Choose Your CUDA Installation Method

NVIDIA provides multiple installation methods with different trade-offs. Generally, the APT repository method offers the best balance of convenience and control for most users.

MethodChannelVersionUpdatesBest For
APT RepositoryNVIDIA CUDA DownloadsLatest stableAutomatic via apt upgradeMost users who want straightforward installation and updates
RunfileNVIDIA CUDA DownloadsAny versionManual reinstallationSystems without package manager access or custom installation paths
CondaAnaconda NVIDIA ChannelSelected versionsVia conda updatePython developers using conda environments

For most users, the APT repository method is recommended because it integrates with Ubuntu’s package management, handles dependencies automatically, and provides security updates through standard system updates.

This guide covers Ubuntu 22.04 LTS and 24.04 LTS. NVIDIA’s CUDA repository provides packages built specifically for these releases, and the commands work identically on both. Ubuntu 26.04 LTS is not yet supported by NVIDIA’s repository; check the NVIDIA CUDA Downloads page for updated release availability.

Install Prerequisites for CUDA

First, update your package index and install the packages required for downloading keys, building kernel modules, and managing repositories:

sudo apt update
sudo apt install build-essential gcc linux-headers-$(uname -r) ca-certificates software-properties-common dkms curl wget -y

This command installs several key components: GCC and build-essential provide the compiler toolchain for building CUDA programs, kernel headers allow DKMS to compile driver modules for your running kernel, and DKMS manages kernel modules so they persist across kernel updates. The remaining packages handle secure downloads and repository management.

Configure NVIDIA CUDA Repository

NVIDIA distributes CUDA through its own APT repository. The cuda-keyring package handles GPG key management and repository configuration automatically, while the pin file ensures CUDA packages from NVIDIA take priority over Ubuntu’s default packages.

Download and Install CUDA Keyring

Next, download and install the cuda-keyring package for your Ubuntu version. This package configures the NVIDIA repository and manages GPG keys for secure package verification.

Ubuntu 24.04 LTS (Noble Numbat):

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

Ubuntu 22.04 LTS (Jammy Jellyfish):

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb

Add Repository Pin File

After installing the keyring, download the pin file for your version. This file ensures APT prioritizes CUDA packages from NVIDIA’s repository over any conflicting packages from Ubuntu’s default repositories.

Ubuntu 24.04 LTS:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-ubuntu2404.pin
sudo mv cuda-ubuntu2404.pin /etc/apt/preferences.d/cuda-repository-pin-600

Ubuntu 22.04 LTS:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600

Refresh Package Cache

After adding the repository, update the package cache so APT recognizes the new CUDA packages:

sudo apt update

Verify the NVIDIA repository appears in the output. You should see a line referencing developer.download.nvidia.com in the package list updates.

Install CUDA Toolkit and GPU Drivers

CUDA installation separates into toolkit components (compiler, libraries, development tools) and GPU drivers (kernel modules for hardware communication). Therefore, understanding the available packages helps you choose the right installation for your needs.

Available CUDA Packages

NVIDIA provides several package options with distinct purposes:

cuda-toolkit

  • Installs CUDA SDK, libraries, compilers, and development tools
  • Does not install or modify GPU drivers
  • Recommended when you already have working NVIDIA drivers

cuda

  • Meta-package that installs full CUDA Toolkit plus NVIDIA drivers
  • Includes desktop components for display output
  • Suitable for workstations with NVIDIA GPUs driving displays

nvidia-open

  • Open-source kernel module for Turing and newer GPUs (RTX 20 series and later)
  • Better compatibility with modern kernel features
  • Tracks the newest supported open driver automatically

cuda-drivers

  • Installs proprietary NVIDIA GPU drivers
  • Supports all NVIDIA GPU architectures including older cards
  • Required for Maxwell and Pascal GPUs (GTX 900/1000 series)

Check Available Package Versions

Before installing, review the available CUDA Toolkit and driver versions:

apt search cuda-toolkit | grep -E "^cuda-toolkit"

You should see output listing available toolkit versions:

cuda-toolkit/unknown 13.1.0-1 amd64
  CUDA Toolkit meta-package

cuda-toolkit-12/unknown 12.9.1-1 amd64
  CUDA Toolkit 12 meta-package

cuda-toolkit-13/unknown 13.1.0-1 amd64
  CUDA Toolkit 13 meta-package

cuda-toolkit-13-1/unknown 13.1.0-1 amd64
  CUDA Toolkit 13.1 meta-package

Similarly, check available driver versions:

apt search nvidia-driver | grep -E "^nvidia-driver-[0-9]+"

Install CUDA with Open Kernel Module (Turing and Newer GPUs)

For RTX 20 series and newer GPUs (Turing, Ampere, Ada Lovelace, Hopper, Blackwell architectures), install the toolkit with the open-source kernel module. This option provides better integration with modern kernel features and represents NVIDIA’s future driver direction:

sudo apt install cuda-toolkit nvidia-open

The nvidia-open meta-package automatically tracks the newest supported open driver version. If you need a specific driver branch for compatibility testing, install it explicitly (for example, nvidia-open-580).

Install CUDA with Proprietary Drivers (All GPU Architectures)

Alternatively, for older GPUs (Maxwell, Pascal) or when the open kernel module causes issues, install the toolkit with proprietary drivers:

sudo apt install cuda-toolkit cuda-drivers

This command installs the proprietary NVIDIA kernel module, which supports all GPU architectures from Maxwell onwards.

Install Specific Driver Version

When you need a particular driver version for compatibility or testing, specify both packages explicitly:

sudo apt install cuda-toolkit nvidia-driver-570

Replace 570 with your required version. Run the search command shown earlier to see all available driver versions in the repository.

Optional: Install GPUDirect Storage

GPUDirect Storage (GDS) enables direct data paths between GPU memory and NVMe storage, bypassing system memory for improved I/O performance in data-intensive workloads. Install GDS only after completing the CUDA Toolkit and driver installation:

sudo apt install nvidia-gds

Install nvidia-gds only after the driver installation completes. The nvidia-fs kernel module builds against the current driver, and installing out of order can cause application hangs.

Reboot to Load Kernel Modules

After installation completes, reboot your system to load the new NVIDIA kernel modules:

sudo reboot

A reboot is required because the NVIDIA driver installs kernel modules that cannot load while older modules (or the nouveau driver) remain in use. After the system restarts, continue with verification.

Verify CUDA Installation

After rebooting, verify that both the NVIDIA drivers and CUDA Toolkit installed correctly. These checks confirm that the kernel modules loaded successfully and the compiler is accessible.

Check NVIDIA Driver Status

First, run the nvidia-smi command to display GPU information and confirm the driver loaded successfully:

nvidia-smi

You should see output showing your GPU model, driver version, and CUDA version:

+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.xx.xx    Driver Version: 580.xx.xx    CUDA Version: 13.1               |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 3060       Off | 00000000:01:00.0  Off |                  N/A |
|  0%   35C    P8              12W / 170W |       1MiB / 12288MiB |      0%      Default |
+-----------------------------------------+------------------------+----------------------+

If you see “NVIDIA-SMI has failed” or “No devices were found,” the driver did not load correctly. Check the troubleshooting section below.

Check CUDA Compiler Version

Next, verify the CUDA compiler is accessible and displays the expected version:

nvcc --version

Expected output:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on ...
Cuda compilation tools, release 13.1, V13.1.xxx
Build cuda_13.1.r13.1/compiler.xxxxxxxx_0

However, if the command is not found, the CUDA binaries are not in your PATH. For APT installations, they should be available automatically. If using a runfile installation, see the environment variables section below.

Environment Variables for Runfile Installations

If you installed CUDA using the APT repository method (recommended), environment variables are configured automatically and you can skip this section.

However, for runfile installations, manually configure PATH and LD_LIBRARY_PATH by adding these lines to your ~/.bashrc:

export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Finally, reload the configuration:

source ~/.bashrc

Test CUDA with Sample Programs

NVIDIA provides sample programs demonstrating CUDA capabilities. To build these samples, you need Git installed on your Ubuntu system. Clone the samples repository and build the device query sample to verify your installation works correctly:

git clone https://github.com/nvidia/cuda-samples.git
cd cuda-samples/Samples/1_Utilities/deviceQuery
make

The compilation process takes a few seconds. Once complete, run the compiled sample:

./deviceQuery

Successful output ends with:

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 13.1, CUDA Runtime Version = 13.1, NumDevs = 1
Result = PASS

Once the result shows PASS, your CUDA installation is working correctly and ready for development.

Switch Between Kernel Module Types

After installation, you can switch between open and proprietary kernel modules without reinstalling the entire toolkit. This flexibility lets you test compatibility or recover from driver issues.

To switch to the open-source kernel module, run:

sudo apt install nvidia-open

To switch back to the proprietary kernel module:

sudo apt install nvidia-driver-580

Replace the driver version number with your preferred branch. After switching, reboot to load the new kernel module.

Update CUDA Toolkit

NVIDIA regularly releases CUDA updates with performance improvements, bug fixes, and new features. Since you configured the NVIDIA repository, updates arrive through standard APT commands:

sudo apt update
sudo apt upgrade

Major version upgrades (for example, from 12.x to 13.x) may require reviewing NVIDIA’s release notes for breaking changes that could affect your applications.

Troubleshoot Common CUDA Issues

Driver Fails to Load After Reboot

If nvidia-smi fails after installation, first check whether the kernel module loaded:

lsmod | grep nvidia

If no nvidia modules appear in the output, the nouveau driver may be blocking NVIDIA. Check for nouveau:

lsmod | grep nouveau

If nouveau appears in the output, blacklist it by creating a configuration file and updating the initramfs:

echo "blacklist nouveau" | sudo tee /etc/modprobe.d/blacklist-nouveau.conf
echo "options nouveau modeset=0" | sudo tee -a /etc/modprobe.d/blacklist-nouveau.conf
sudo update-initramfs -u
sudo reboot

Secure Boot Blocks Unsigned Modules

On systems with Secure Boot enabled, unsigned kernel modules cannot load. Check if Secure Boot is active:

mokutil --sb-state

If Secure Boot is enabled, you have two options: disable it in BIOS/UEFI settings, or sign the NVIDIA kernel modules using Machine Owner Key (MOK). For development workstations, disabling Secure Boot is often the simpler solution.

DKMS Module Build Fails

If the NVIDIA module fails to build during installation, check DKMS status:

dkms status

Then look for errors and check the build log:

cat /var/lib/dkms/nvidia/*/build/make.log

Common causes include missing kernel headers. Install them with:

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

After installing headers, rebuild the module:

sudo dkms autoinstall

Unable to Locate Package Errors

If APT cannot find CUDA packages, the repository may not be configured correctly. Verify the repository is present:

apt-cache policy cuda-toolkit

A working repository shows version candidates from the NVIDIA URL:

cuda-toolkit:
  Installed: (none)
  Candidate: 13.1.0-1
  Version table:
     13.1.0-1 600
        600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64  Packages

If no candidate appears, reinstall the cuda-keyring package and update:

sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update

CUDA Keyring Download Fails

If the wget command fails to download the cuda-keyring package, the URL may have changed since this guide was written. Visit the NVIDIA CUDA Downloads page, select your Ubuntu version, and copy the current keyring package URL. NVIDIA occasionally updates the keyring version number as they issue repository maintenance updates.

Remove CUDA Toolkit

The following commands remove CUDA packages, NVIDIA drivers, and associated configuration. Make sure you want to completely remove your GPU development environment before proceeding.

To remove the CUDA Toolkit and NVIDIA drivers installed via APT:

sudo apt remove --purge "*cuda*" "*nvidia*" cuda-keyring
sudo apt autoremove --purge

Next, remove the repository pin file and any leftover repository configuration files:

sudo rm -f /etc/apt/preferences.d/cuda-repository-pin-600
sudo rm -f /etc/apt/sources.list.d/cuda*.list

Then refresh the package cache:

sudo apt update

Finally, verify removal completed successfully:

apt-cache policy cuda-toolkit

The output should show no installation candidate:

N: Unable to locate package cuda-toolkit

Next Steps with CUDA on Ubuntu

Your Ubuntu system now runs a complete CUDA development environment with the nvcc compiler, GPU-accelerated libraries, and profiling tools. The APT repository configuration ensures you receive CUDA updates, security patches, and new features as NVIDIA releases them. To continue learning, explore the CUDA samples repository for working examples covering parallel algorithms, memory optimization, and library usage, or consult the NVIDIA CUDA documentation for API references and programming guides. If you plan to run CUDA workloads in containers, see how to install Docker on Ubuntu and configure the NVIDIA Container Toolkit.

4 thoughts on “How to Install CUDA on Ubuntu”

  1. I got this error when trying to install GROMACS 2025 on my ubuntu 24.04

    CMake Error at /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
    Could NOT find CUDAToolkit: Found unsuitable version “12.0.140”, but
    required is at least “12.1” (found /usr/include)
    Call Stack (most recent call first):
    /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:598 (_FPHSA_FAILURE_MESSAGE)
    /usr/share/cmake-3.28/Modules/FindCUDAToolkit.cmake:1009 (find_package_handle_standard_args)
    cmake/gmxManageCuda.cmake:45 (find_package)
    CMakeLists.txt:726 (include)

    When I ran nvidia-smi, it showed 12.8. So I installed the CUDAToolKit-12.8 but it is not showing when I run the cmake command

    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2025 NVIDIA Corporation
    Built on Fri_Feb_21_20:23:50_PST_2025
    Cuda compilation tools, release 12.8, V12.8.93
    Build cuda_12.8.r12.8/compiler.35583870_0

    Please how can I solve this Cmake error

    Reply
    • Thanks for reporting this Charles. The error shows CMake is detecting CUDA 12.0.140 in /usr/include instead of your installed 12.8 version, even though nvcc confirms 12.8 is present and nvidia-smi reports the correct driver API version.

      CMake’s FindCUDAToolkit module searches multiple paths and stops at the first match, which in your case is an older CUDA installation or leftover headers in /usr/include. Run this command to list all installed CUDA packages:

      dpkg -l | grep cuda

      If you see cuda-toolkit-12-0 or similar older packages, remove them with:

      sudo apt remove cuda-toolkit-12-0

      After cleanup, explicitly tell CMake where to find CUDA 12.8 by setting the toolkit root when running cmake:

      cmake -DCUDAToolkit_ROOT=/usr/local/cuda-12.8 ..

      Or export the CUDA path before building:

      export CUDA_PATH=/usr/local/cuda-12.8

      Verify the symlink at /usr/local/cuda points to cuda-12.8:

      ls -l /usr/local/cuda

      Let me know if the conflict persists after removing the old version.

      Reply
  2. Did on 22.04:
    sudo apt autoremove cuda* nvidia* –purge

    sudo apt install build-essential gcc dirmngr ca-certificates software-properties-common apt-transport-https dkms curl -y

    curl -fSsL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub | sudo gpg –dearmor | sudo tee /usr/share/keyrings/nvidia-drivers.gpg > /dev/null 2>&1

    echo ‘deb [signed-by=/usr/share/keyrings/nvidia-drivers.gpg] https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /’ | sudo tee /etc/apt/sources.list.d/nvidia-drivers.list

    sudo apt install nvidia-driver-535 cuda-drivers-535 cuda

    But get:
    ~$ sudo apt install nvidia-driver-535 cuda-drivers-535 cuda
    Reading package lists… Done
    Building dependency tree… Done
    Reading state information… Done
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    cuda-drivers-535 : Depends: nvidia-kernel-source-535 (>= 535.183.06) but it is not installable or
    nvidia-kernel-open-535 (>= 535.183.06) but it is not installable
    nvidia-dkms-535 : Depends: nvidia-kernel-source-535 but it is not installable or
    nvidia-kernel-open-535 but it is not installable
    nvidia-driver-535 : Depends: nvidia-kernel-source-535 (= 535.183.06-0ubuntu1) but it is not installable or
    nvidia-kernel-open-535 (= 535.183.06-0ubuntu1) but it is not installable
    nvidia-driver-560-open : Depends: libnvidia-gl-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: nvidia-dkms-560-open (= 560.35.03) but it is not installable
    Depends: nvidia-kernel-common-560 (= 560.35.03) but it is not installable
    Depends: libnvidia-compute-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: libnvidia-extra-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: nvidia-compute-utils-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: libnvidia-decode-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: libnvidia-encode-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: nvidia-utils-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: xserver-xorg-video-nvidia-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: libnvidia-cfg1-560 (= 560.35.03-0ubuntu1) but it is not installable
    Depends: libnvidia-fbc1-560 (= 560.35.03-0ubuntu1) but it is not installable
    Recommends: libnvidia-compute-560:i386 (= 560.35.03-0ubuntu1)
    Recommends: libnvidia-decode-560:i386 (= 560.35.03-0ubuntu1)
    Recommends: libnvidia-encode-560:i386 (= 560.35.03-0ubuntu1)
    Recommends: libnvidia-fbc1-560:i386 (= 560.35.03-0ubuntu1)
    Recommends: libnvidia-gl-560:i386 (= 560.35.03-0ubuntu1)
    E: Unable to correct problems, you have held broken packages.

    Reply
    • It seems that the packages for the NVIDIA 535 drivers may not be available in your current repository. To verify this, you can try searching for the packages with the following commands:

      apt-cache search nvidia-kernel-source

      or

      apt-cache search nvidia-kernel-source-535

      This will show you if the necessary packages are available. If nothing comes up, it may indicate that the 535 driver isn’t present in the current repository configuration.

      As far as I know, the NVIDIA 535 drivers are not set to reach End-of-Life (EOL) on Linux until June 2026, so they should still be supported. If the packages aren’t available, it could be an issue with the repository or a temporary glitch.

      Let me know what you find, and we can troubleshoot further if needed!

      Reply

Leave a Comment