How to Install CUDA on Ubuntu

Tech reviewed: Deepak Prasad
Install CUDA on Ubuntu banner with NVIDIA GPU chip icon and parallel computing motif on Ubuntu orange accent

CUDA (Compute Unified Device Architecture) is NVIDIA’s parallel computing platform and API. It lets C, C++, and other supported languages offload work to the GPU—useful for machine learning, scientific computing, and video processing. On Ubuntu the install splits into two layers: a proprietary NVIDIA driver that owns the GPU, and the CUDA toolkit (nvcc, headers, libraries) from NVIDIA’s APT repository.

This guide walks through install CUDA on Ubuntu the way NVIDIA’s Linux installation guide documents today: driver first, cuda-keyring network repo (not deprecated apt-key), then cuda-toolkit, PATH setup, and verification. Commands below target Ubuntu 22.04 LTS and 24.04 LTS; adjust the repo codename for your release.

IMPORTANT
Match driver version, GPU architecture, and CUDA toolkit version using the CUDA release notes and supported GPUs list. Installing a toolkit newer than your driver supports leads to nvcc working while runtime kernels fail—or nvidia-smi showing a lower CUDA version than the toolkit you installed.

Quick command summary

Task Command
List recommended driver ubuntu-drivers devices
Auto-install driver sudo ubuntu-drivers autoinstall
Verify driver nvidia-smi
Add CUDA keyring (24.04 example) wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb then sudo dpkg -i cuda-keyring_1.1-1_all.deb
Install toolkit sudo apt update && sudo apt install -y cuda-toolkit
Add CUDA to PATH echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
Check compiler nvcc --version
Compile sample nvcc -o vector_add vector_add.cu

Prerequisites

  • Physical NVIDIA GPU (or VM with GPU passthrough). CUDA does not run on CPU-only hosts.
  • Ubuntu 22.04 LTS, 24.04 LTS, or newer with sudo access.
  • 64-bit amd64 for the commands in this article (ARM64 Jetson uses a different image—see NVIDIA’s Jetson docs).
  • Build tools: sudo apt install -y build-essential linux-headers-$(uname -r)
  • Disk space: several gigabytes under /usr/local/cuda-* plus driver packages.
  • For driver details and GUI paths, see install NVIDIA drivers on Ubuntu.

Compatibility: driver, GPU, and CUDA version

Before installing, cross-check three columns:

Check Where to look
Minimum driver for CUDA X.Y CUDA Toolkit release notes
GPU compute capability CUDA GPUs
Ubuntu + GCC support Same release notes (each CUDA version supports specific GCC ranges)

Example: a Tesla V100 (Volta) needs a CUDA release that supports sm_70+. A mobile GeForce MX150 needs a driver branch Ubuntu still ships in ubuntu-drivers devices, not a decade-old 390 series unless you deliberately pin an old toolkit.

Use lscpu and lspci | grep -i nvidia when documenting the host CPU architecture alongside GPU passthrough planning.


Step 1: Update Ubuntu and install the NVIDIA driver

Refresh package indexes:

bash
sudo apt update

Identify the driver Ubuntu recommends for your card:

bash
ubuntu-drivers devices

On a laptop with a GeForce MX150, output commonly resembles:

text
== /sys/devices/pci0000:00/0000:00:02.0 ==
modalias : pci:v000010DEd00001D10sv00001028sd000007D1bc03sc02i00
vendor   : NVIDIA Corporation
model    : GP108M [GeForce MX150]
driver   : nvidia-driver-525 - third-party free recommended
driver   : nvidia-driver-515-server - distro non-free
driver   : nvidia-driver-470 - distro non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-515 - distro non-free
driver   : nvidia-driver-450-server - distro non-free

Install the recommended branch (replace the version number with yours):

bash
sudo apt install -y nvidia-driver-525

Or let Ubuntu pick automatically:

bash
sudo ubuntu-drivers autoinstall

Reboot so the proprietary module loads:

bash
sudo reboot

Step 2: Verify the NVIDIA driver

After login, confirm the kernel module and GPU are visible:

bash
nvidia-smi

On a desktop with a GeForce RTX 3080, nvidia-smi often looks like:

text
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.37.12    Driver Version: 525.37.12    CUDA Version: 12.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce RTX 3080    Off  | 00000000:01:00.0 Off |                  N/A |
|  30%   55C    P2    70W / 320W |   5000MiB / 10000MiB |     60%      Default |
+-----------------------------------------------------------------------------+

The CUDA Version line in nvidia-smi is the maximum runtime version the driver supports, not the toolkit you have installed yet. You still need cuda-toolkit for nvcc and development headers.


Step 3: Add the official CUDA APT repository

NVIDIA’s current network repository method uses the cuda-keyring package—avoid legacy apt-key adv and hard-coded ubuntu2004 entries on modern Ubuntu.

Pick the distro tag from the CUDA download page (Linux → x86_64 → Ubuntu → your version → deb (network)).

Ubuntu 24.04 LTS (ubuntu2404):

bash
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

Ubuntu 22.04 LTS (ubuntu2204):

bash
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
sudo apt update
NOTE
Older tutorials used add-apt-repository plus apt-key adv against ubuntu2004 repos on 18.04/20.04. That pattern is obsolete and the wrong codename on 22.04+. Use cuda-keyring and the matching ubuntu2204 / ubuntu2404 path from the installation guide.

Local repository alternative

If you downloaded the large local .deb repo file from NVIDIA instead:

bash
sudo dpkg -i cuda-repo-ubuntu2404-*-local_*.deb
sudo cp /var/cuda-repo-ubuntu2404-*-local/cuda-*-keyring.gpg /usr/share/keyrings/
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
sudo apt update

Step 4: Install the CUDA toolkit

Install the toolkit package (driver already handled in Step 1):

bash
sudo apt install -y cuda-toolkit

NVIDIA documents cuda-toolkit and apt install as separate commands—do not chain them with && on the same line in fragile automation if your mirror prompts for input; run update, then install.

The default install location is /usr/local/cuda-<version>/ with a symlink at /usr/local/cuda. A reboot is sometimes recommended after major upgrades; for a first install on a stable driver it is optional if nvidia-smi already works.

HINT
The cuda meta-package can install NVIDIA’s bundled driver and conflict with nvidia-driver-* from Ubuntu. Prefer cuda-toolkit when you already installed the distro driver in Step 1.

Step 5: Set environment variables

Add CUDA binaries to your PATH (per post-installation actions):

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

For all users on a shared server, use /etc/profile.d/cuda.sh with the same export line.

Debian packages usually register libraries with ldconfig; if a .run installer was used instead, you may also need:

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

See .bashrc vs .bash_profile if login shells do not read ~/.bashrc.


Step 6: Verify CUDA installation

Check the compiler:

bash
nvcc --version

Example output shape:

text
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Jul_11_02:20:44_PDT_2023
Cuda compilation tools, release 12.0, V12.0.140

Your exact release and build strings depend on the toolkit version APT installed.

Confirm the driver still answers:

bash
nvidia-smi

Step 7: Compile and run a sample CUDA program

Minimal vector addition

Create vector_add.cu:

cpp
#include <cuda_runtime.h>
#include <stdio.h>

__global__ void add(int n, float *x, float *y) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) y[i] = x[i] + y[i];
}

int main() {
    int n = 1 << 20;
    size_t bytes = n * sizeof(float);
    float *h_x = (float *)malloc(bytes);
    float *h_y = (float *)malloc(bytes);
    for (int i = 0; i < n; i++) { h_x[i] = 1.0f; h_y[i] = 2.0f; }

    float *d_x, *d_y;
    cudaMalloc(&d_x, bytes);
    cudaMalloc(&d_y, bytes);
    cudaMemcpy(d_x, h_x, bytes, cudaMemcpyHostToDevice);
    cudaMemcpy(d_y, h_y, bytes, cudaMemcpyHostToDevice);

    int threads = 256;
    int blocks = (n + threads - 1) / threads;
    add<<<blocks, threads>>>(n, d_x, d_y);
    cudaMemcpy(h_y, d_y, bytes, cudaMemcpyDeviceToHost);

    printf("Sample result y[0] = %f (expect 3.0)\n", h_y[0]);

    cudaFree(d_x); cudaFree(d_y);
    free(h_x); free(h_y);
    return 0;
}

Compile and run:

bash
nvcc -o vector_add vector_add.cu
./vector_add

Expected line:

text
Sample result y[0] = 3.000000 (expect 3.0)

Official deviceQuery sample

NVIDIA moved samples to github.com/NVIDIA/cuda-samples. Clone the repo, build deviceQuery, and run it—the output should name your GPU and report Result = PASS when the driver and toolkit align.


Optional: scheduled checks and upgrades

  • Upgrade only the toolkit: sudo apt update && sudo apt install --only-upgrade cuda-toolkit
  • Pin a specific major version via the CUDA toolkit archive if frameworks (TensorFlow, PyTorch) require an older CUDA line.
  • Pair host scans with rkhunter / rootkit checks on internet-facing GPU nodes—orthogonal to CUDA, but part of a sensible server baseline.

Uninstall

Remove APT-managed toolkit and keyring:

bash
sudo apt purge -y cuda-toolkit 'cuda-*'
sudo apt autoremove -y

Runfile installs provide /usr/local/cuda-*/bin/cuda-uninstaller. Remove /usr/local/cuda-* directories only when no other NVIDIA stack depends on them.


Troubleshooting

Symptom Likely cause Fix
nvidia-smi: command not found Driver not installed or wrong GPU Re-run Step 1; confirm lspci | grep -i nvidia
NVIDIA-SMI has failed after reboot Nouveau still bound, or Secure Boot blocking module Blacklist nouveau; sign module or disable Secure Boot for proprietary driver
nvcc: command not found PATH not set Step 5; open a new shell
Driver CUDA version < toolkit Toolkit too new for driver Upgrade nvidia-driver-* or install older toolkit from archive
apt install cuda breaks desktop Meta-package replaced Ubuntu driver Purge cuda driver component; use cuda-toolkit + ubuntu-drivers driver
Install loop asks to reboot forever Mixed runfile + apt driver (common on 18.04-era guides) Pick one channel: Ubuntu driver + cuda-keyring + cuda-toolkit
Sample builds, runtime fails VM without GPU Use hardware passthrough or cloud GPU instance

References


Summary

Install CUDA on Ubuntu by separating concerns: install the proprietary NVIDIA driver (ubuntu-drivers devicesnvidia-driver-* or ubuntu-drivers autoinstall), confirm nvidia-smi, add NVIDIA’s cuda-keyring for ubuntu2204 or ubuntu2404, then sudo apt install cuda-toolkit. Export /usr/local/cuda/bin on PATH, run nvcc --version, and compile a small kernel or deviceQuery from the official samples.

Use the cuda-keyring network repo—not deprecated apt-key lines aimed at ubuntu2004. Keep driver, GPU architecture, and toolkit versions inside the same release-notes matrix so runtime and compiler stay aligned.

Frequently Asked Questions

1. How do I install CUDA on Ubuntu?

Install the recommended nvidia-driver-* package with ubuntu-drivers, reboot, confirm nvidia-smi, add NVIDIA cuda-keyring for your Ubuntu codename (ubuntu2204 or ubuntu2404), run sudo apt install cuda-toolkit, add /usr/local/cuda/bin to PATH, then run nvcc --version.

2. Do I need to install the NVIDIA driver before CUDA?

Yes for GPU compute. The CUDA toolkit expects a working proprietary driver on bare metal. Install ubuntu-drivers autoinstall or the recommended nvidia-driver-XXX from ubuntu-drivers devices, reboot, and verify nvidia-smi before installing cuda-toolkit from the NVIDIA network repository.

3. What is the difference between cuda and cuda-toolkit packages?

On current NVIDIA APT repos, cuda-toolkit installs the compiler and libraries under /usr/local/cuda-VERSION. The cuda meta-package can pull a bundled driver stack that conflicts with Ubuntu-packaged drivers—most desktop and server setups should install the Ubuntu driver first, then cuda-toolkit only.

4. Which Ubuntu CUDA repository codename should I use?

Match your LTS release: ubuntu2204 for 22.04, ubuntu2404 for 24.04. NVIDIA also publishes ubuntu2604 for 26.04. Pick the row in the CUDA download selector or the compatibility table in the official Linux installation guide—do not reuse ubuntu2004 pins on 22.04 or newer.

5. How do I verify CUDA is installed correctly?

Run nvidia-smi for driver and GPU visibility, nvcc --version for the toolkit compiler, and build deviceQuery from the official cuda-samples GitHub repository. deviceQuery should list your GPU name and compute capability when the driver and toolkit match.

6. Why does nvcc command not found after apt install?

Add export PATH=/usr/local/cuda/bin${PATH:+:${PATH}} to ~/.bashrc or /etc/profile.d/cuda.sh, then open a new shell. The default symlink /usr/local/cuda points at the latest toolkit directory NVIDIA installed.

7. Can I install CUDA in a VM without a GPU?

CUDA needs an NVIDIA GPU visible to the guest—bare metal or PCIe passthrough/vGPU. Without a GPU, nvidia-smi and CUDA samples cannot run; install only when hardware is present.

8. How do I uninstall CUDA from Ubuntu?

Run sudo apt purge cuda-toolkit cuda-keyring and remove /usr/local/cuda-* if needed. Use /usr/local/cuda-VERSION/bin/cuda-uninstaller for runfile installs. Purge conflicting nvidia-driver packages only when you intend to remove the driver stack entirely.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, …

  • Red Hat Certified System Administrator in Red Hat OpenStack
  • Certified Kubernetes Application Developer (CKAD)
  • Red Hat Certified Specialist in Ansible Automation
  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security