How to Install CUDA on Ubuntu 24.04 and 26.04 (Driver, Toolkit, Test)

Linux system administrator installing CUDA toolkit and NVIDIA drivers on Ubuntu server in a modern data center
How to Install CUDA on Ubuntu 24.04 and 26.04 (Driver, Toolkit, Test) 3

To install cuda ubuntu systems correctly, you need to register the official NVIDIA network repository via cuda-keyring, install the compatible proprietary NVIDIA display driver, install the CUDA Toolkit developer packages, and configure environment paths in your shell profile. On modern long-term support releases like Ubuntu 24.04 LTS and Ubuntu 26.04 LTS, using the official NVIDIA repository ensures reliable DKMS kernel module builds, automated updates, and complete compatibility with modern AI frameworks like PyTorch and TensorFlow. This guide provides complete step-by-step instructions from hardware pre-checks to runtime verification and troubleshooting common driver conflicts.

Understanding the NVIDIA CUDA Stack on Linux

Before installing packages, distinguish between the three primary layers of the NVIDIA compute ecosystem on Linux:

  • NVIDIA Display Driver: The kernel-level module (built via DKMS) and user-space libraries (libcuda.so) that interface directly with the physical GPU hardware, managing power, memory, and compute channels.
  • CUDA Toolkit: The developer toolchain containing the NVIDIA CUDA Compiler (nvcc), headers, and accelerated math libraries (cuBLAS, cuFFT, cuRAND) required to build and execute GPU kernels.
  • Ubuntu Universe Package (nvidia-cuda-toolkit): Canonical’s distribution build. While easy to install, it remains frozen on older CUDA releases and lags significantly behind NVIDIA’s active upstream releases.

The following table summarizes the responsibilities and primary verification command for each layer:

Layer Main Function Source Verification Command
NVIDIA Driver Kernel interface & hardware control NVIDIA official repo / Ubuntu restricted nvidia-smi
CUDA Toolkit Compiler (nvcc), headers, math libraries NVIDIA official network repository nvcc --version
Container Toolkit Docker & container GPU passthrough NVIDIA container repository nvidia-ctk --version

Prerequisites and Pre-Installation Checks

Run these verification checks on your Ubuntu system before modifying repositories or installing packages to prevent conflicts.

1. Verify GPU Hardware and System Architecture

Confirm that your machine detects a supported NVIDIA PCI device:

lspci | grep -i nvidia

Verify that your system runs a supported 64-bit architecture and inspect your exact Ubuntu release:

uname -m && cat /etc/os-release

2. Install Required Kernel Headers and Build Tools

The NVIDIA proprietary driver requires kernel headers and build utilities to compile kernel modules via DKMS during installation and future kernel updates:

sudo apt update
sudo apt install -y build-essential linux-headers-$(uname -r) dkms

3. Remove Conflicting Drivers and Stale Repositories

If you previously installed drivers using manual runfiles (.run) or third-party PPAs, purge them to avoid library conflicts:

sudo apt purge -y "*nvidia*" "*cuda*"
sudo apt autoremove -y

Step-by-Step Installation: How to Install CUDA on Ubuntu

NVIDIA’s official network repository is the recommended installation path. It ensures automated GPG key management, clean dependency resolution, and updates via standard apt commands.

Step 1: Download and Register the Official NVIDIA Keyring

Download and install the repository keyring package to configure NVIDIA’s GPG key and repository list for Ubuntu 24.04 LTS:

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 update

Step 2: Install the NVIDIA Driver

Install the proprietary driver branch and reboot your system to load the newly compiled kernel modules into memory:

sudo apt install -y cuda-drivers
sudo reboot

Rebooting ensures that the open-source Nouveau driver is completely unloaded and the NVIDIA proprietary kernel module takes ownership of the GPU.

Step 3: Install the CUDA Toolkit

Once your machine reboots with the active NVIDIA driver, install the CUDA Toolkit package. Installing the meta-package cuda-toolkit provides the latest compiler toolchain, runtime, and math development libraries:

sudo apt update
sudo apt install -y cuda-toolkit

If your project or framework specifically requires a pinned version (such as CUDA 12.8), install that versioned package directly:

sudo apt install -y cuda-toolkit-12-8

Step 4: Configure Shell Environment Variables

The compiler and shared libraries install under /usr/local/cuda. Because Ubuntu does not include this directory in standard search paths, you must export them in your shell configuration:

echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc

For system-wide availability across all server users, place the configuration in /etc/profile.d/cuda.sh:

sudo tee /etc/profile.d/cuda.sh << 'EOF'
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
EOF
sudo chmod +x /etc/profile.d/cuda.sh

Verifying the CUDA Installation

Verify your installation through three targeted diagnostic checks:

1. Check Driver Communication (nvidia-smi)

Verify that the kernel driver communicates with your GPU:

nvidia-smi

The output confirms your GPU hardware, driver version, VRAM capacity, and maximum supported CUDA API level.

2. Verify Compiler Toolchain (nvcc)

Confirm that the CUDA compiler is available in your shell PATH:

nvcc --version

This command reports the exact release of the installed development toolkit and compiler build number.

3. Execute a Functional Compute Test with PyTorch

Verify actual memory allocation and compute execution by running a quick Python check:

python3 -c "import torch; print(f'CUDA available: {torch.cuda.is_available()} | Device: {torch.cuda.get_device_name(0)} | VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB')"

When this returns CUDA available: True, your compute stack is fully ready for production. From here, you can power demanding workloads: deploy high-throughput LLM serving via vLLM with Docker Compose, run private local language models using Ollama with NVIDIA GPU acceleration, or enable high-performance video encoding with Jellyfin NVIDIA hardware transcoding.

Troubleshooting Common CUDA Installation Errors

1. Secure Boot Blocking Kernel Modules

Symptom: nvidia-smi fails with NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver, and dmesg indicates module loading denial.

Fix: Disable Secure Boot in your UEFI settings or enroll a Machine Owner Key (MOK) during driver installation. If prompted during apt install, set a temporary password. On the subsequent reboot, navigate the blue Shim management screen, select Enroll MOK, enter your password, and reboot.

2. Command ‘nvcc’ Not Found

Symptom: nvidia-smi works, but typing nvcc prompts you to run apt install nvidia-cuda-toolkit.

Fix: Do not install the suggested package, as it will overwrite your NVIDIA repository installation with outdated distro packages. Check that the binary exists at /usr/local/cuda/bin/nvcc and source your updated profile:

export PATH=/usr/local/cuda/bin:$PATH
source ~/.bashrc

3. Open-Source Nouveau Driver Conflicts

Symptom: System freezes during boot or desktop display manager crashes caused by driver conflicts.

Fix: Explicitly blacklist Nouveau in modprobe and update initramfs:

sudo tee /etc/modprobe.d/blacklist-nouveau.conf << 'EOF'
blacklist nouveau
options nouveau modeset=0
EOF
sudo update-initramfs -u
sudo reboot

Frequently Asked Questions

What is the difference between apt install nvidia-cuda-toolkit and official CUDA Toolkit?

The nvidia-cuda-toolkit package in Ubuntu’s universe repository provides a quick installation, but it remains frozen on older CUDA releases throughout the lifecycle of that Ubuntu release. The official cuda-toolkit from NVIDIA’s repository provides the newest releases, compiler optimizations, bug fixes, and support for the latest GPU architectures.

Can I install CUDA on Ubuntu Server without a graphical desktop interface?

Yes. Installing CUDA on headless Ubuntu Server is standard for machine learning clusters and cloud servers. Installing cuda-drivers delivers the kernel modules and compute libraries without installing desktop environments, display managers (GDM/LightDM), or X11/Wayland display servers.

How do I switch between multiple CUDA versions on the same Ubuntu system?

Each version installs into /usr/local/cuda-12.X. To switch default versions, update the symbolic link at /usr/local/cuda (e.g., sudo ln -sfn /usr/local/cuda-12.4 /usr/local/cuda) or configure the PATH and LD_LIBRARY_PATH environment variables inside your specific project virtual environment.

Summary

Installing CUDA on Ubuntu through the official NVIDIA network repository provides a solid, maintainable foundation for GPU-accelerated computing. By matching drivers with your active kernel headers, installing the official toolkit, and properly exporting shell paths, you ensure maximum stability and compute performance across your AI, data science, and container workloads.