NVIDIA Driver and CUDA Installation on Ubuntu Linux¶
In this article
This instructional guide details the procedure for installing NVIDIA graphics card drivers and CUDA on the subsequent operating systems: Ubuntu 22.04, Ubuntu 24.04.
Attention
For proper operation of Tesla series graphics cards (e.g., NVIDIA Tesla T4), ensure that the server's BIOS has the parameter 'above 4G decoding' or 'large/64bit BARs' or 'Above 4G MMIO BIOS assignment' enabled.
Information
We officially support Nvidia cards only on Ubuntu 22.04/24.04. To install drivers for other distributions, use the official instructions from the developers:
System Preparation¶
-
Update the system:
-
For RTX 4xxx, 5xxx series, A100, and H100 on Ubuntu 22.04, you need to update the kernel version. You can also update the kernel version for older graphics cards:
Installing Nvidia Drivers and CUDA¶
-
Install the gcc compiler, necessary for compiling CUDA:
-
Download and install drivers and CUDA. For Ubuntu 24.04, replace
ubuntu2204withubuntu2404in the path ofwget: -
Set environment variables for your frameworks and applications to detect CUDA in your
.bashrc:echo 'export PATH="/sbin:/bin:/usr/sbin:/usr/bin:${PATH}:/usr/local/cuda/bin"' >> ~/.bashrc echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64\${LD_LIBRARY_PATH:+:\${LD_LIBRARY_PATH}}' >> ~/.bashrc source ~/.bashrcAttention
You must run these commands for all users who need to use CUDA.
-
Check the installation of drivers on your video card:
You should get output similar to this:
user@48567:~$ nvidia-smi Fri May 10 15:58:17 2024 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 580.54.15 Driver Version: 580.54.15 CUDA Version: 13.0 | |-----------------------------------------+------------------------+----------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| | 0 NVIDIA RTX A4000 Off | 00000000:07:00.0 Off | Off | | 41% 31C P8 15W / 140W | 3MiB / 16376MiB | 0% Default | | | | N/A | +-----------------------------------------+------------------------+----------------------+ +-----------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=========================================================================================| | No running processes found | +-----------------------------------------------------------------------------------------+Attention
If you received a message like
modprobe: ERROR: could not insert 'nvidia': Device or resource busyduring installation, you need to remove thenouveaukernel module and enable the use ofnvidiamodules.Note
You can find the latest instructions for installing Nvidia GPU drivers on Ubuntu here.
-
Check the CUDA installation:
After a successful installation, you should get output similar to this:
Attention
If you encounter an error like Failed to initialize NVML: Driver/library version mismatch after installation, you need to re-initialize the Nvidia kernel modules by removing them and running nvidia-smi again.
Installing NVIDIA modules for Docker¶
If you're using Docker containers, don't forget to install the nvidia-docker2 package:
One-Click Installation of Drivers and CUDA¶
You can use this script for automatic installation of drivers and CUDA:
#!/bin/bash
#Check Ubuntu 25.04 and exit
if lsb_release -a | grep -q "25.04"; then
echo "Detected Ubuntu 25.04. NVIDIA do not support official CUDA for non-LTS release. Use Ubuntu 24.04 or 22.04 instead!"
exit
fi
# Update and upgrade the system using apt
sudo apt update
sudo apt upgrade -y
#Check Ubuntu 22.04 and update kernel
lsb_release=$(lsb_release -a | grep "22.04")
if [[ -n "$lsb_release" ]]; then
sudo apt install -y linux-generic-hwe-22.04
fi
# Install GCC compiler for CUDA install
sudo apt install gcc-12 g++-12
# Get the release version of Ubuntu
RELEASE_VERSION=$(lsb_release -rs | sed 's/\([0-9]\+\)\.\([0-9]\+\)/\1\2/')
# Download and install CUDA package for Ubuntu and Nvidia drivers
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${RELEASE_VERSION}/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
# Update and upgrade the system again to ensure all packages are installed correctly
sudo apt update
if lspci -nn | grep -q '10de:1b06'; then
echo "Find GTX 1080 Ti"
sudo apt purge "nvidia-*" "libnvidia-*" "cuda-*" "nvidia-driver-*" "*cudnn*" "*nsight*" -y
sudo apt remove --purge nvidia-cuda-toolkit nvidia-prime nvidia-settings -y
sudo apt autoremove -y
sudo apt --fix-broken install -y
sudo apt clean -y
sudo apt install nvidia-driver-535 -y
else
sudo apt install cuda -y
sudo apt install cuda-toolkit -y
fi
# Add PATH and LD_LIBRARY_PATH environment variables for CUDA in .bashrc file
echo 'export PATH="/sbin:/bin:/usr/sbin:/usr/bin:${PATH}:/usr/local/cuda/bin"' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64\${LD_LIBRARY_PATH:+:\${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc
#Initialize kernel modules without reboot
sudo rmmod -f nouveau
sudo nvidia-smi
nvcc -V
#Installing Docker binding for Nvidia
if command -v docker &> /dev/null; then
if lsb_release -a | grep -q "22.04"; then
echo "Detected Ubuntu 22.04. Installing nvidia-docker2..."
sudo apt install -y nvidia-docker2
sudo systemctl restart docker
fi
if lsb_release -a | grep -q "24.04"; then
echo "Detected Ubuntu 24.04. Installing nvidia-container-toolkit..."
sudo apt install -y nvidia-container-toolkit
sudo systemctl restart docker
fi
else
echo "Docker is not installed."
fi
Some of the content on this page was created or translated using AI.