Install Nvidia drivers on Ubuntu.

Author
By Darío Rivera
Posted On in Ubuntu

Check if you have the video driver loaded in the kernel. For this, assuming you have an Nvidia card, check if you have this folder:

ls /lib/modules/6.2.14-060214-generic/kernel/drivers/video/

If not, you must add the Nvidia repo. Currently Nvidia supports up to version Ubuntu 22.04 as it is the current LTS version. If at the time of viewing this video the LTS version is different you must change this value in the UBUNTU_VERSION variable of the following script.

export UBUNTU_VERSION=ubuntu2204
export BASE_URL=https://developer.download.nvidia.com/compute/cuda/repos
wget $BASE_URL/$UBUNTU_VERSION/x86_64/cuda-keyring_1.0-1_all.deb

With this, we will have the package to add the GPG public key of the CUDA repository. To install this package we will use dpkg.

dpkg -i cuda-keyring_1.0-1_all.deb

We update the package list.

apt-get update

Once this is done we can install the necessary drivers.

apt-get -y install cuda-drivers
sudo grub-mkconfig -o /boot/grub/grub.cfg
update-grub

https://docs.nvidia.com/datacenter/tesla/tesla-installation-notes/index.html#ubuntu-lts

We add the third-party libraries recommended by Nvidia.

sudo apt-get install g++ freeglut3-dev build-essential libx11-dev \
    libxmu-dev libxi-dev libglu1-mesa-dev libfreeimage-dev libglfw3-dev

Standard method

sudo ubuntu-drivers install
apt install ubuntu-desktop
update-initramfs -u -k all
sudo grub-mkconfig -o /boot/grub/grub.cfg
update-grub

Recent kernels

You must be cautious when installing a recent Linux kernel, which can be done manually or with some non-LTS system update, as the driver may not be available for that recent kernel version.

Before checking if there is a version of the Nvidia driver for your kernel version, you must find out which version of the kernel you are using. For this you can quickly use the following command.

root@ubuntu:/# uname -r
6.2.0-20-generic

You must be careful if you are using a Live USB as this command may show the version of the kernel of the Live USB even if you are using chroot. To be sure you can see the version linked to the kernel like this.

root@ubuntu:/# ls -gG /boot/initrd.img
lrwxrwxrwx 1 32 May 10 20:31 /boot/initrd.img -> initrd.img-6.2.14-060214-generic

In this case, as you can see, the version of the kernel being used is the latest to date which is v6.2.14. However, the command uname -r shows the version of my live USB because I'm using chroot.

Once this is done we can check if there is a version of the Nvidia drivers for the specific version we are using. You can list the available versions with the following command:

apt search nvidia | grep "nvidia-driver"

If you do not find a driver for your kernel version it means that Nvidia does not support that kernel version. This can cause errors when booting the system. The most recommended thing in this case is not to use the Nvidia driver or to use the latest version of the kernel that Nvidia is supporting. In my case it is v5.30. Below is part of the output of the previous command.

nvidia-driver-515/unknown 515.105.01-0ubuntu1 amd64
nvidia-driver-515-open/lunar 515.105.01-0ubuntu1 amd64
nvidia-driver-515-server/lunar 515.105.01-0ubuntu1 amd64
nvidia-driver-520/unknown 520.61.05-0ubuntu1 amd64
nvidia-driver-520-open/lunar 525.105.17-0ubuntu1 amd64
nvidia-driver-525/unknown 525.105.17-0ubuntu1 amd64
nvidia-driver-525-open/lunar 525.105.17-0ubuntu1 amd64
nvidia-driver-525-server/lunar 525.105.17-0ubuntu1 amd64
nvidia-driver-530/unknown,now 530.30.02-0ubuntu1 amd64

However, for that driver version I would need a kernel version that is not available in the repositories.

root@ubuntu:/# apt search linux-image-5
...
linux-image-5.15.0-60-generic/now 5.15.0-60.66 amd64
  (none)
...

linux-image-5.19.0-40-generic/now 5.19.0-40.41 amd64
  Signed kernel image generic

Downloading a kernel version that is not in the repos is not a big deal, but I don't want to add more complexity than this article requires so I'll go for v5.15.0 since I have both the driver and the linux image available in the repos.

Once the version you want to install has been identified, you can run the following command which will install the kernel (if you don't already have it installed) and the appropriate driver.

apt install linux-image-5.15.0-60-generic nvidia-driver-515

You should keep in mind that if this change involves installing an old version of the kernel that is not recommended for your operating system it may be better to try another method.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.