How to reinstall the NVIDIA driver after a Linux kernel upgrade

How to reinstall the NVIDIA driver after a Linux kernel upgrade

In my NVIDIA card-attached PC, the NVIDIA graphic driver always fails to start every time the Linux kernel is upgraded.

Whenever I upgrade the kernel version, after a PC restart, the second display cannot connect. I tried running nvidia-smi, it shows an error message

NVIDIA-SMI has failed because it couldn’t communicate with the NVIDIA driver.
Make sure that the latest NVIDIA driver is installed and running.

The fact is that when the kernel is upgraded, the NVIDIA driver needs to be re-compiled/reinstalled to be able to be used. Although NVIDIA does support DKMS (Dynamic Kernel Module Support), which means it is automatically rebuilt after a kernel upgrade, my NVIDIA driver continuously fails after any kernel upgrade.

The culprit is that after a kernel upgrade, my ubuntu does not install the associated generic linux headers, which causes the NVIDIA driver to fail to be rebuilt. To install the current generic header, you can use the following command

sudo apt install -y linux-headers-$(uname -r)

Next, there will be several ways to reinstall the NVIDIA driver after the Linux headers are installed

Method 1 (tested): UI only. Require PC restart

  • Press the window/super key, search for Software & Updates, and open it.
  • Navigate to the "Additional Drivers" tab
  • Select any other driver except the latest one. Click "Apply Changes"
  • Select the latest driver. Click "Apply Changes"
  • Restart your PC
Software & Updates

Method 2 (not tested): No PC restart required. A logout is required.

Note: I just invented this method while writing this post. I have not tested it yet because I cannot reproduce the case without a reasonable amount of effort. I will try this method next time.

Like method 1, except in the last step, instead of restarting your PC, you need to log out the current user. Press Ctrl-Alt-3 to switch to another console, login to that console, then run the following commands

sudo systemctl restart gdm3

Press Ctrl-Alt-1 to switch back to gnome desktop, and the second display should work.

Method 3 (tested): No PC restart required. A logout is required.

  • Download the latest NVIDIA driver of your graphic card from here.
  • Logout the current user
  • Press Ctrl-Alt-3 to switch to another console, login to that console
  • Run following commands: sudo init 3 && sudo systemctl stop gdm3
  • Install the driver downloaded in the previous step. Ignore the warning about the unsupported kernel version to continue the installation.
  • Run this command to restore the gdm daemon: sudo init 5 && sudo systemctl start gdm3
  • Press Ctrl-Alt-1 to switch back to the gnome-desktop. Now the second display should work.

After one of the above method, to check if the Nvidia driver works, you can use nvidia-smi

$ nvidia-smi
Sat Jun 27 10:14:32 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.100      Driver Version: 440.100      CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1060    Off  | 00000000:01:00.0  On |                  N/A |
| N/A   62C    P8     8W /  N/A |    406MiB /  6069MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1409      G   /usr/lib/xorg/Xorg                            59MiB |
|    0      2301      G   /usr/lib/xorg/Xorg                           234MiB |
|    0      2482      G   /usr/bin/gnome-shell                          74MiB |
|    0      2512      G   /opt/teamviewer/tv_bin/TeamViewer              1MiB |
|    0      2856      G   ...AAAAAAAAAAAACAAAAAAAAAA= --shared-files    20MiB |
|    0      3327      G   ...ps/IDEA-U/ch-0/201.7846.76/jbr/bin/java     2MiB |
+-----------------------------------------------------------------------------+

Happy coding!

Buy Me A Coffee