How to install NVIDIA Driver, and CUDA on Ubuntu

Install NVIDIA Driver

Disable Nouveau which conflicts with NVIDIA drivers, and may affect the GPU running reliability, so it should be disabled before using NVIDIA GPUs.

sudo vi /etc/modprobe.d/blacklist-nouveau.conf

Create the file and include these lines.

blacklist nouveau
options nouveau modeset=0

Execute it

sudo update-initramfs -u

Confirm the installed GPU model

test@ubuntu:~$ lspci | grep -i nvidia
00:06.0 VGA compatible controller: NVIDIA Corporation GP106 [GeForce GTX 1060 3GB] (rev a1)
00:07.0 Audio device: NVIDIA Corporation GP106 High Definition Audio Controller (rev a1)

Remove installed nvidia and cuda drivers

sudo apt --purge remove nvidia-*
sudo apt --purge remove cuda-*
sudo apt autoremove

Confirm the recommended driver

test@ubuntu:~$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:06.0 ==
modalias : pci:v000010DEd00001C02sv00001043sd000085EDbc03sc00i00
vendor   : NVIDIA Corporation
model    : GP106 [GeForce GTX 1060 3GB]
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-390 - distro non-free
driver   : nvidia-driver-418-server - distro non-free
driver   : nvidia-driver-450-server - distro non-free
driver   : nvidia-driver-535 - distro non-free
driver   : nvidia-driver-470 - distro non-free
driver   : nvidia-driver-545 - distro non-free
driver   : nvidia-driver-535-server - distro non-free
driver   : nvidia-driver-550 - distro non-free recommended
driver   : xserver-xorg-video-nouveau - distro free builtin

Install driver

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-driver-550

Reboot

sudo reboot

Confirm

test@ubuntu:~$ nvidia-smi 
Fri Sep 13 16:08:22 2024       
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.107.02             Driver Version: 550.107.02     CUDA Version: 12.4     |
|-----------------------------------------+------------------------+----------------------+
| 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 GeForce GTX 1060 3GB    Off |   00000000:00:06.0 Off |                  N/A |
| 25%   39C    P8              9W /  120W |       2MiB /   3072MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
                                                                                         
+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+

Install CUDA Toolkit

Open the page, and get the latest download instructions.

https://docs.nvidia.com/cuda/wsl-user-guide/index.html#cuda-support-for-wsl-2:~:text=Option%201%3A%20Installation%20of%20Linux%20×86%20CUDA%20Toolkit%20using%20WSL%2DUbuntu%20Package%20%2D%20Recommended

Current instructions is here.

sudo apt-key del 7fa2af80

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.6.1/local_installers/cuda-repo-wsl-ubuntu-12-6-local_12.6.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-6-local_12.6.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-6