Install NVIDIA kernel module for a non running upgraded linux kernel

In the /usr/share/doc/NVIDIA_GLX-1.0/README.txt documentation, Chapter 8. Common Problems, there is a Q&A that goes like this:

Q. I just upgraded my kernel, and now the NVIDIA kernel module will not load.

A. The kernel interface layer of the NVIDIA kernel module must be compiled
   specifically for the configuration and version of your kernel. If you
   upgrade your kernel, then the simplest solution is to reinstall the driver.

   ADVANCED: You can install the NVIDIA kernel module for a non running kernel
   (for example: in the situation where you just built and installed a new
   kernel, but have not rebooted yet) with a command line such as this:
   
       # sh NVIDIA-Linux-x86_64-375.39.run --kernel-name='KERNEL_NAME'

Where 'KERNEL_NAME' is what 'uname -r' would report if the target kernel
   were running.

My question is:
In this scenario, can the command
sudo sh NVIDIA-Linux-x86_64-375.39.run --kernel-name=‘KERNEL_NAME’ be executed from a X-window terminal as is? Or, must it be executed in tty1 after doing sudo systemctl stop lightdm.service?

sh NVIDIA-Linux-x86_64-375.39.run --kernel-name='KERNEL_NAME' --kernel-module-only

Might wanna use --no-x-check as well - I’m not sure.

Why not easy use dkms?

dkms install -m nvidia -v 375.39 -k 'KERNEL_NAME'

@birdie and @towo, do I run the commands you had suggested in standard X window terminals or do I need to run them in the tty mode?

The dkms command can be runned in a xterm.
But for that dkms command you would have choosen dkms for module building in the installer.

@towo Can you elaborate?

During my original install, I was not able to install using dkms. That is why I did my install w/o dkms.
-> Would you like to register the kernel module sources with DKMS? This will allow DKMS to automatically build a new module, if you install a different kernel later. (Answer: No)

If your answer was “no” during the installer, then you can’t use dkms for rebuilding the modules.
I’ve never seen a linux system, where dkms has not worked, besides fedore, they have no dkms afair.

I have a slight twist on this question. As I was not in control of the initial installs the dkms solution does not appear to be an option. The problem with the “sh NVIDIA-Linux-x86_64-375.39.run --kernel-name=‘KERNEL_NAME’ --kernel-module-only” option in my case is that I would like to upgrade my driver version as well. But the --kernel-module-only option requires my running nvidia module to be the same as the new installer. And not specifying the --kernel-module-only flag leads to complaints that the current kernel module is still loaded in the running kernel even though I was not trying to load into the current kernel. So, is there no way to update the driver version at the same time as installing into a new kernel?