theme: ricetheme
New palette
New palette

Install CUDA on Ubuntu: Complete Step-by-Step Guide

Install CUDA on Ubuntu in the right order: set up NVIDIA drivers, the toolkit, Docker GPU containers, then verify your setup and fix common errors.

Terminal shows CUDA installation steps with checkmarks and a GPU icon connected by a glowing line.

tl;dr the short answer

CUDA is NVIDIA’s platform for running code on a GPU, and installing it on Ubuntu means setting up a compatible driver and, when needed, the toolkit for building CUDA programs. Install and verify the driver first, add the toolkit and environment paths next, then configure the NVIDIA Container Toolkit if using Docker; keep versions aligned and add libraries only when a project needs them. Checking the driver, compiler, and GPU access catches problems early and leaves you with a dependable setup for development or GPU applications.

Installing CUDA on Ubuntu can feel like assembling furniture with the instructions shuffled. CUDA, NVIDIA’s platform for running code on a graphics processing unit (GPU), has several moving parts, and one wrong step can leave you staring at an error.

The good news is that it’s far more manageable once you know the right order. This guide walks you through the full path: the driver, the toolkit, your environment, GPU containers, verification, and fixes for when things go sideways.

We’ll also set up your shell and editor for writing CUDA code.

Get Ubuntu Ready to Install CUDA

Before you touch a driver, get the base system clean.

Set up the OS and core packages

  • Supported Ubuntu versions: confirm your release is supported for your CUDA version.
  • Update and upgrade: run sudo apt update && sudo apt upgrade, then reboot if the kernel changed.
  • Build tools and kernel headers: install build-essential and linux-headers-$(uname -r), matching the kernel you’re running.
  • DKMS (Dynamic Kernel Module Support) and dependencies: install dkms so the NVIDIA kernel module rebuilds on kernel updates. Don’t skip it.

What a cloud or server needs first

Renting a remote machine, like an Amazon Elastic Compute Cloud (EC2) instance with a graphics processing unit (GPU)? Check these before you launch.

A grad student sharing a lab server can add Secure Shell (SSH) keys once, so everyone logs in with their own key.

  • EC2 GPU instance quotas: check your account’s quota and request an increase before you need one.
  • Minimum storage size: size the root volume for where your data is going, not where it starts.
  • Network and SSH setup: allow SSH only from addresses you trust, and load your public key at launch.
  • Shared lab server config: agree on one driver and toolkit version so everyone’s code builds the same way.

Skip manual setup with prebuilt images

Prebuilt images, known as AMIs (Amazon Machine Images), get you to a working CUDA box faster.

  • Deep Learning AMIs: AMIs with GPU software already installed.
  • Pre-built container AMIs: AMIs that come with a container runtime ready to use.
  • Use Docker for CUDA dev: run your code in a CUDA container so the toolkit lives in the image, not on your host. Your host still needs its NVIDIA driver.

Get the NVIDIA Driver Installed

Your CUDA setup is only as solid as the driver under your graphics processor (GPU), so we’ll get this step right first. Before installing, disable the open-source nouveau driver, usually by blocking it from loading, since it can clash with NVIDIA’s.

Add the NVIDIA package repository

NVIDIA’s network repo is the easiest way in: install the cuda-keyring package, which adds NVIDIA’s signing key and repo entry. The setup detects your Ubuntu release and architecture, then picks the matching repo for you.

If you’re in China, use the China region mirror instead of the default.

A flat illustration shows a terminal command installing CUDA, with an automatic keyring detection matching a repository card while showing an alternate China mirror path.

Which driver install method to choose

Ubuntu offers several routes:

  • NVIDIA repo driver. Comes from NVIDIA’s own repository, alongside the toolkit.
  • Ubuntu repo pre-compiled driver. Prebuilt by Ubuntu, so nothing compiles locally, but it may lag behind NVIDIA.
  • Graphics-drivers PPA. A Personal Package Archive (PPA) with newer drivers. It’s third-party, so you’re trusting its maintainers.
  • GUI Additional Drivers tool. A point-and-click app in Ubuntu’s settings that picks a driver for you.
  • Specific driver branch. Pins one driver version family when your software needs a known-good release.

Think of a developer who needs CUDA before a deadline. The GUI tool gets a working driver in a few clicks.

Matching driver and CUDA versions

A runtime vs driver mismatch means your CUDA runtime expects a newer driver than the one you have. The goal isn’t the newest driver. It’s the one that matches. Match first, upgrade later.

  • CUDA compatibility package. Lets a newer toolkit run on an older driver in cases NVIDIA supports.
  • Forward compatibility on datacenter GPUs. Server cards get support GeForce cards lack, so check NVIDIA’s notes first.
  • Tesla vs GeForce drivers. Tesla is NVIDIA’s datacenter line, GeForce the consumer line, and each has its own driver.

Open kernel modules and driver types

NVIDIA now offers open kernel modules, an open-source option beside the proprietary ones. If Secure Boot is on, kernel modules must be signed before they’ll load.

During install you’ll set a one-time password, then enroll the key at the Machine Owner Key (MOK) prompt on reboot.

  • Compute-only vs desktop. Compute-only drivers skip display features, which suits servers with no monitor. Desktop drivers include graphics for your monitor.
  • Tesla vs GRID drivers. GRID shares one GPU across virtual machines, so it’s not the pick for plain CUDA work.

Add the CUDA Toolkit to Your System

The toolkit holds the compilers and libraries you need to build CUDA code. You can install it two ways, and we’ll walk through both.

Install with the package manager

Install the cuda-toolkit meta package with apt, or pick a version-specific package when your project needs an exact release. Keep the driver and toolkit separate: the driver runs your GPU, while the toolkit builds code for it.

The toolkit is optional for running apps. A designer who only runs a GPU-accelerated editor can skip it and keep only the driver.

Installing from a .run file

Download the .run file for your release and run it as root. Then work through its prompts:

  • Driver: Decline the bundled driver if you already have one.
  • Toolkit and samples: Keep the toolkit selected. The samples are optional.
  • Symlink: The installer points /usr/local/cuda at your version, giving tools one stable path.
  • cuBLAS patch: Apply it after the base install if your release includes one.

Versions can coexist, and repointing the symlink picks the active one. Remove a version the same way you installed it.

Diagram showing how a symlink points to the active CUDA version, allowing tools to remain unchanged while the target is updated.

Set your PATH and library paths

PATH tells your shell where to look for commands, and LD_LIBRARY_PATH tells programs where to find shared libraries:

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

Put them in ~/.bashrc for one user, or in a script under /etc/profile.d/ for every user. Or, for libraries, list the lib64 path in a file under /etc/ld.so.conf.d/ and run sudo ldconfig to skip the variable.

Zsh takes the same export lines in ~/.zshrc. Fish uses set -gx PATH /usr/local/cuda/bin $PATH and a matching LD_LIBRARY_PATH line instead.

A developer who opens new terminal tabs all day never retypes these lines. Set them once.

Run GPU Containers with Docker

Containers keep your CUDA work separate from the rest of your machine. Here’s how to set up Docker, let it reach your graphics processing unit (GPU), and use it as a dev environment.

Install Docker and manage access

You can install Docker with the convenience script or the docker.io package from Ubuntu’s repositories. Pick one and stick with it.

Docker’s daemon runs as root, so every command normally needs sudo. Adding your user to the docker group removes that step, but it grants broad control over your machine, so only add users you trust.

Rootless Docker skips that group entirely by running the daemon under your own user account instead of root.

Say you’re a grad student sharing a lab workstation with a labmate: rootless Docker lets you run containers without asking anyone for root. Your containers, your permissions.

Wire the container toolkit into Docker

Docker knows how to run containers, but not how to hand them your GPU. That job belongs to the NVIDIA Container Toolkit. Four steps get it working:

  1. Install the nvidia-container-toolkit package.
  2. Run nvidia-ctk runtime configure to register it with Docker.
  3. Restart Docker so it loads the new configuration.
  4. If containers won’t start over a cgroup driver mismatch (cgroups are Linux’s resource-control system), apply the cgroupfs driver fix and restart Docker again.

Use containers as your CUDA environment

Once Docker can see the GPU, a container becomes your full CUDA development environment. Your host only needs the NVIDIA driver, since the toolkit and libraries live inside the image.

After a fresh install, pulling the image back down gets you writing code again.

Extra NVIDIA Libraries and Tools

Once the toolkit runs, add-ons make it practical. Install them after CUDA works, not before, and skip the data center half unless you run server hardware.

Add cuDNN, NCCL, and other CUDA-X libraries

Say you’re a grad student training a small model on one workstation. You’ll probably want cuDNN, NVIDIA’s deep neural network library. Skip each library until your project actually imports it.

NCCL (NVIDIA Collective Communications Library) speeds up data sharing between GPUs, so it matters once you use more than one. CUDA-X is NVIDIA’s umbrella name for its CUDA-based domain libraries, so you’ll only install the ones your project calls.

Data center GPU tools and requirements

Built for servers and clusters, these tools matter less on a desktop:

  • DCGM (Data Center GPU Manager) monitors GPU health and usage across machines.
  • GPUDirect Storage moves data between storage and GPU memory with fewer detours through the CPU.
  • GDRCopy is a small library for fast copies between CPU and GPU memory.
  • Fabric Manager coordinates GPUs linked by NVLink, NVIDIA’s high-speed connection between GPUs.

The P6 requirement deserves a close look. On P6 systems, Fabric Manager needs NVLSM (the NVLink subnet manager) as an extra dependency.

A commit to the siderolabs extensions repository adding Fabric Manager support for Blackwell baseboards notes that “On NVIDIA DGX-B200, HGX-B200, and HGX-B100 systems and later, the FabricManager package needs an additional NVLSM dependency for proper operation.”

Picture a sysadmin rolling out a new DGX-B200 node. Checking for NVLSM before setup begins means no surprises halfway through. Do that first.

A sysadmin in a server room checks NVLSM status on a terminal while GPU nodes with glowing links stand nearby.

Check Your Setup and Fix Problems

Mismatched versions are a common culprit when CUDA misbehaves, so let’s verify before you write any code.

Confirm the driver and CUDA versions

Start with nvidia-smi, the NVIDIA System Management Interface tool that ships with your driver. It shows the driver version and the CUDA version that driver supports. Then run nvcc -V for the version of nvcc, the NVIDIA CUDA compiler in your toolkit.

The driver runs your GPU, but the toolkit builds your code, so both numbers matter.

Reboot before you verify, or you may chase stale results.

Test the GPU from inside a container

Say you’re a homelabber who runs CUDA jobs in Docker. Run docker run --gpus all <cuda-image> nvidia-smi to confirm the container sees the GPU, then check that the table matches your host.

For a stronger test, run the deviceQuery container sample, which confirms CUDA itself can reach the hardware.

Build and run the CUDA samples

Build the samples with make, then run deviceQuery and look for Result = PASS at the bottom. PASS means CUDA can reach your GPU, not just that the files compiled.

A version mismatch error means your driver and toolkit are out of step, so recheck both versions above.

With PASS in hand, tell your editor that .cu files are CUDA. Many editors treat them as plain C++ by default, so map *.cu to the CUDA filetype in your config.

Keep versions locked and fix errors

Picture a Linux user whose Friday system upgrade leaves Monday’s build broken because the driver changed under the toolkit. Locking before an upgrade costs one command. Debugging afterward costs a weekend.

Four fixes and habits help you stay out of that spot:

  • Block unattended driver upgrades. Automatic updates can swap the driver out from under your toolkit, so exclude driver packages in your unattended-upgrades config.
  • Version locking. Hold the driver and toolkit at tested versions with apt-mark hold.
  • NVML version mismatch. NVML (the NVIDIA Management Library that nvidia-smi uses to read the driver) reports this when its library and the loaded driver disagree. Compare both versions, then reboot and re-run the checks.
  • cloud-init troubleshooting. On a cloud VM, check /var/log/cloud-init-output.log for the step that failed.

Working through the driver, toolkit, environment, containers, and checks in order gives you a CUDA setup you can rely on. Each step builds on the one before it, so verifying as you go keeps small mistakes from turning into mystery errors later.

Take the time to get the foundation right, and every project you’ll build on top of it will feel smoother. Now go put that GPU to work.

References

  1. feat: fabricmanager support Blackwell baseboards (DGX/HGX B100/B200/B300) · 9d782c5 — github.com (2025)

Sources

  1. feat: fabricmanager support Blackwell baseboards (DGX/HGX B100/B200/B300) · 9d782c5 · siderolabs/extensions, github.com,