CUDA
Setup CUDA for NVIDIA GPUs.
Install drivers
From the Ubuntu repository
sudo ubuntu-drivers installFrom nvidia
Install nvidia CUDA runtime and compatible GPU driver from NVIDIA: https://developer.nvidia.com/cuda-toolkit (For Ubuntu 24.04)
Clean previous installations
sudo apt autopurge 'cuda*' 'nvidia*'Install drivers
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 && sudo apt -y install cuda-toolkit-13-0Add the CUDA compiler (nvcc) to the system PATH:
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}For WSL2, install Windows NVIDIA GPU driver first; then install the CUDA toolkit in the WSL.
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pinsudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600wget https://developer.download.nvidia.com/compute/cuda/13.0.2/local_installers/cuda-repo-wsl-ubuntu-13-0-local_13.0.2-1_amd64.debsudo dpkg -i cuda-repo-wsl-ubuntu-13-0-local_13.0.2-1_amd64.debsudo cp /var/cuda-repo-wsl-ubuntu-13-0-local/cuda-*-keyring.gpg /usr/share/keyrings/sudo apt-get updatesudo apt-get -y install cuda-toolkit-13-0Tweaks
ArchWiki: NVIDIA/Tips and tricks
Power limit
To monitor GPU power draw:
nvidia-smi -q -d POWER -l 1 | grep "Power Draw"To limit power draw to 300W, the setting will reset after reboot.
sudo nvidia-smi -pl 300To apply power draw settings at boot, make a systemd service.
[Unit]
Description=Set NVIDIA power limit on boot
[Timer]
OnBootSec=5
[Install]
WantedBy=timers.target[Unit]
Description=Set NVIDIA power limit
[Service]
Type=oneshot
ExecStart=/usr/bin/nvidia-smi -pl 300And enable nvidia-tdp.timer.
sudo systemctl enable nvidia-tdp.timerPreserve video memory after suspend
Fixes GUI corruptions after suspend and resume.
And the following file to preserve video memory after suspend.
NVreg_PreserveVideoMemoryAllocations=1
NVreg_TemporaryFilePath=/var/tmpMonitor GPU activities
nvidia-sminvtop: See nvtop
Reload nvidia GPU driver
How to reload nvidia GPU driver to fix “NVML: Driver/library version mismatch” error after installing new nvidia driver. No rebooting required. 1
The following command will switch the system into text (CLI) mode, reload the graphical driver, and then switch back to GUI mode.
sudo apt purge $DRIVER_TO_DELETE
sudo apt reinstall $DRIVER_OF_CHOICE
sudo systemctl isolate multi-user.target
lsmod | grep nvidia # - to identify the modules needing to be reloaded.
sudo modprobe --remove $ALL_MODULES_IDENTIFIED_ABOVE
sudo modprobe $ALL_MODULES_IDENTIFIED_ABOVE
sudo systemctl isolate graphical.target