Tuesday, August 26, 2025

Installing Nvidia drivers and toolkit

 

#!/bin/bash



### steps ####

# verify the system has a cuda-capable gpu

# download and install the nvidia cuda toolkit and cudnn

# setup environmental variables

# verify the installation

###



### to verify your gpu is cuda enable check

lspci | grep -i nvidia



### If you have previous installation remove it first.

sudo apt purge nvidia* -y

sudo apt remove nvidia-* -y

sudo rm /etc/apt/sources.list.d/cuda*

sudo apt autoremove -y && sudo apt autoclean -y

sudo rm -rf /usr/local/cuda*



# system update

sudo apt update && sudo apt upgrade -y


#autoinstall the latest driver, although it is deprecated

sudo ubuntu-drivers autoinstall


# reboot

sudo reboot now


#this two lines might be optional but not sure yet

curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-cuda-keyring.gpg


echo "deb [signed-by=/usr/share/keyrings/nvidia-cuda-keyring.gpg] https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /" | sudo tee /etc/apt/sources.list.d/cuda-repository.list


#sudo apt update



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-get update

sudo apt-get -y install cuda-toolkit-12-9 #sometimes you can just do sudo apt-get -y install cuda but sometimes you have to be specific

Nvcc #you should see an error message