xmr-stak GPU disabled after kernel or driver updated

Sometime when the user upgrades the kernel or the graphic card driver (NVIDIA or ATI) under Ubuntu (it is essentially the same with the other distros) we may encounter errors with xmr-stak and even not to be able to use our graphic card for mining!
Here we have an UBUNTU 17.10.1 and when we updated the kernel to the latest our mining xmr-stak software stopped using the NVIDIA card:

-------------------------------------------------------------------
[2018-02-16 12:17:11] : Start mining: MONERO
WARNING: NVIDIA Insufficient driver!
WARNING: NVIDIA no device found
[2018-02-16 12:17:11] : WARNING: backend NVIDIA disabled.
[2018-02-16 12:17:11] : WARNING: UNKNOWN_ERROR when calling clGetPlatformIDs for number of platforms.
[2018-02-16 12:17:11] : WARNING: No OpenCL platform found.
[2018-02-16 12:17:11] : WARNING: No AMD OpenCL platform found. Possible driver issues or wrong vendor driver.
[2018-02-16 12:17:11] : WARNING: backend AMD disabled.
[2018-02-16 12:17:11] : Starting 1x thread, affinity: 0.
[2018-02-16 12:17:11] : hwloc: memory pinned

As you can see the NVIDIA backend is disabled! In fact the driver seemed OK and loaded and even the X was working.
So in such a situation just reinstall the NVIDIA driver and install the software dependecies for the mining xmr-software (detailed explanation here)
Here what you can do:

sudo apt-get purge nvidia-*
sudo apt-get -y install libboost-all-dev libleveldb-dev libcurl4-openssl-dev libmicrohttpd-dev libminiupnpc-dev libgmp-dev  cuda nvidia-opencl-dev libmicrohttpd-dev libssl-dev cmake build-essential
sudo reboot

And you’ll have again an usable and properly installed GPU mining system.

Here is the log of the reinstallation, check it if you think you are going in the wrong direction, when purging the driver:

root@srv2:~/xmr-stak/build/bin# apt-get purge nvidia-*
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  bbswitch-dkms cuda-command-line-tools-9-1 cuda-compiler-9-1 cuda-cublas-9-1 cuda-cublas-dev-9-1 cuda-cudart-9-1 cuda-cudart-dev-9-1 cuda-cufft-9-1 cuda-cufft-dev-9-1
  cuda-cuobjdump-9-1 cuda-cupti-9-1 cuda-curand-9-1 cuda-curand-dev-9-1 cuda-cusolver-9-1 cuda-cusolver-dev-9-1 cuda-cusparse-9-1 cuda-cusparse-dev-9-1
  cuda-documentation-9-1 cuda-driver-dev-9-1 cuda-gdb-9-1 cuda-gpu-library-advisor-9-1 cuda-libraries-9-1 cuda-libraries-dev-9-1 cuda-license-9-1 cuda-memcheck-9-1
  cuda-misc-headers-9-1 cuda-npp-9-1 cuda-npp-dev-9-1 cuda-nsight-9-1 cuda-nvcc-9-1 cuda-nvdisasm-9-1 cuda-nvgraph-9-1 cuda-nvgraph-dev-9-1 cuda-nvml-dev-9-1
  cuda-nvprof-9-1 cuda-nvprune-9-1 cuda-nvrtc-9-1 cuda-nvrtc-dev-9-1 cuda-nvtx-9-1 cuda-nvvp-9-1 cuda-samples-9-1 cuda-toolkit-9-1 cuda-tools-9-1 cuda-visual-tools-9-1
  dkms freeglut3 freeglut3-dev lib32gcc1 libc6-i386 libglu1-mesa-dev libice-dev libsm-dev libvdpau1 libxi-dev libxmu-dev libxmu-headers libxnvctrl0 libxt-dev
  linux-headers-4.13.0-21 linux-headers-4.13.0-21-generic linux-image-4.13.0-21-generic linux-image-extra-4.13.0-21-generic linux-signed-image-4.13.0-21-generic
  linux-signed-image-4.13.0-31-generic mesa-vdpau-drivers nvidia-modprobe nvidia-prime nvidia-settings screen-resolution-extra vdpau-driver-all
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  cuda* cuda-9-1* cuda-demo-suite-9-1* cuda-drivers* cuda-runtime-9-1* libcuda1-390* nvidia-390* nvidia-390-dev* nvidia-opencl-icd-390*
0 upgraded, 0 newly installed, 9 to remove and 14 not upgraded.
After this operation, 369 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 311518 files and directories currently installed.)
Removing cuda (9.1.85-1) ...
Removing cuda-9-1 (9.1.85-1) ...
Removing cuda-demo-suite-9-1 (9.1.85-1) ...
Removing cuda-runtime-9-1 (9.1.85-1) ...
Removing cuda-drivers (390.12-1) ...
Removing libcuda1-390 (390.25-0ubuntu0~gpu17.10.1) ...
Removing nvidia-opencl-icd-390 (390.25-0ubuntu0~gpu17.10.1) ...
Removing nvidia-390-dev (390.25-0ubuntu0~gpu17.10.1) ...
Removing nvidia-390 (390.30-0ubuntu1) ...
Removing all DKMS Modules
Done.
update-alternatives: using /usr/lib/nvidia-390-prime/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf (x86_64-linux-gnu_gl_conf) in auto mode
update-alternatives: using /usr/lib/nvidia-390-prime/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf (x86_64-linux-gnu_egl_conf) in auto mode
update-alternatives: using /usr/lib/nvidia-390-prime/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_GL.conf (i386-linux-gnu_gl_conf) in auto mode
update-alternatives: using /usr/lib/nvidia-390-prime/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_EGL.conf (i386-linux-gnu_egl_conf) in auto mode
update-alternatives: using /usr/lib/x86_64-linux-gnu/mesa/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf (x86_64-linux-gnu_gl_conf) in auto mode
update-alternatives: using /usr/lib/x86_64-linux-gnu/mesa-egl/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf (x86_64-linux-gnu_egl_conf) in auto mode
INFO:Disable nvidia-390
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/put_your_quirks_here
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/dell_latitude
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/lenovo_thinkpad
update-initramfs: deferring update (trigger activated)
Processing triggers for initramfs-tools (0.125ubuntu12) ...
update-initramfs: Generating /boot/initrd.img-4.15.3-041503-generic
cryptsetup: WARNING: target cryptswap1 has a random key, skipped
Processing triggers for libc-bin (2.26-0ubuntu2.1) ...
Processing triggers for man-db (2.7.6.1-2) ...
(Reading database ... 310876 files and directories currently installed.)
Purging configuration files for nvidia-opencl-icd-390 (390.25-0ubuntu0~gpu17.10.1) ...
Purging configuration files for nvidia-390 (390.30-0ubuntu1) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for initramfs-tools (0.125ubuntu12) ...
update-initramfs: Generating /boot/initrd.img-4.15.3-041503-generic
cryptsetup: WARNING: target cryptswap1 has a random key, skipped
root@srv2:~/xmr-stak/build/bin# apt-get -y install libboost-all-dev libleveldb-dev libcurl4-openssl-dev libmicrohttpd-dev libminiupnpc-dev libgmp-dev  cuda nvidia-opencl-dev libmicrohttpd-dev libssl-dev cmake build-essential
Reading package lists... Done
Building dependency tree       
Reading state information... Done
build-essential is already the newest version (12.4ubuntu1).
cmake is already the newest version (3.9.1-1).
libgmp-dev is already the newest version (2:6.1.2+dfsg-1).
libleveldb-dev is already the newest version (1.20-1).
libboost-all-dev is already the newest version (1.62.0.1).
libmicrohttpd-dev is already the newest version (0.9.55-1).
nvidia-opencl-dev is already the newest version (8.0.61-1).
libcurl4-openssl-dev is already the newest version (7.55.1-1ubuntu2.3).
libminiupnpc-dev is already the newest version (1.9.20140610-4ubuntu1.1).
libssl-dev is already the newest version (1.0.2g-1ubuntu13.3).
The following packages were automatically installed and are no longer required:
  linux-headers-4.13.0-21 linux-headers-4.13.0-21-generic linux-image-4.13.0-21-generic linux-image-extra-4.13.0-21-generic linux-signed-image-4.13.0-21-generic
  linux-signed-image-4.13.0-31-generic
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
  cuda cuda-9-1 cuda-demo-suite-9-1 cuda-drivers cuda-runtime-9-1 libcuda1-390 nvidia-390 nvidia-390-dev nvidia-opencl-icd-390
The following packages will be upgraded:
  nvidia-modprobe nvidia-settings
2 upgraded, 9 newly installed, 0 to remove and 12 not upgraded.
Need to get 3948 kB/88,1 MB of archives.
After this operation, 369 MB of additional disk space will be used.
Get:1 http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64  cuda-runtime-9-1 9.1.85-1 [2534 B]
Get:2 http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64  cuda-demo-suite-9-1 9.1.85-1 [3943 kB]
Get:3 http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64  cuda-9-1 9.1.85-1 [2560 B]
Fetched 3948 kB in 3s (1003 kB/s)  
Selecting previously unselected package nvidia-390.
(Reading database ... 310871 files and directories currently installed.)
Preparing to unpack .../00-nvidia-390_390.30-0ubuntu1_amd64.deb ...
Unpacking nvidia-390 (390.30-0ubuntu1) ...
Selecting previously unselected package nvidia-390-dev.
Preparing to unpack .../01-nvidia-390-dev_390.30-0ubuntu1_amd64.deb ...
Unpacking nvidia-390-dev (390.30-0ubuntu1) ...
Selecting previously unselected package libcuda1-390.
Preparing to unpack .../02-libcuda1-390_390.30-0ubuntu1_amd64.deb ...
Unpacking libcuda1-390 (390.30-0ubuntu1) ...
Preparing to unpack .../03-nvidia-modprobe_390.30-0ubuntu1_amd64.deb ...
Unpacking nvidia-modprobe (390.30-0ubuntu1) over (390.12-0ubuntu1) ...
Preparing to unpack .../04-nvidia-settings_390.30-0ubuntu1_amd64.deb ...
Unpacking nvidia-settings (390.30-0ubuntu1) over (390.25-0ubuntu0~gpu17.10.1) ...
Selecting previously unselected package nvidia-opencl-icd-390.
Preparing to unpack .../05-nvidia-opencl-icd-390_390.30-0ubuntu1_amd64.deb ...
Unpacking nvidia-opencl-icd-390 (390.30-0ubuntu1) ...
Selecting previously unselected package cuda-drivers.
Preparing to unpack .../06-cuda-drivers_390.30-1_amd64.deb ...
Unpacking cuda-drivers (390.30-1) ...
Selecting previously unselected package cuda-runtime-9-1.
Preparing to unpack .../07-cuda-runtime-9-1_9.1.85-1_amd64.deb ...
Unpacking cuda-runtime-9-1 (9.1.85-1) ...
Selecting previously unselected package cuda-demo-suite-9-1.
Preparing to unpack .../08-cuda-demo-suite-9-1_9.1.85-1_amd64.deb ...
Unpacking cuda-demo-suite-9-1 (9.1.85-1) ...
Selecting previously unselected package cuda-9-1.
Preparing to unpack .../09-cuda-9-1_9.1.85-1_amd64.deb ...
Unpacking cuda-9-1 (9.1.85-1) ...
Selecting previously unselected package cuda.
Preparing to unpack .../10-cuda_9.1.85-1_amd64.deb ...
Unpacking cuda (9.1.85-1) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3) ...
Setting up nvidia-390 (390.30-0ubuntu1) ...
update-alternatives: using /usr/lib/nvidia-390/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf (x86_64-linux-gnu_gl_conf) in auto mode
update-alternatives: warning: skip creation of /usr/share/grub-gfxpayload-lists/blacklist/10_proprietary-graphics-drivers because associated file /usr/share/nvidia-390/nvidia-390.grub-gfxpayload (of link group x86_64-linux-gnu_gl_conf) doesn't exist
update-alternatives: using /usr/lib/nvidia-390/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf (x86_64-linux-gnu_egl_conf) in auto mode
update-alternatives: using /usr/lib/nvidia-390/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_GL.conf (i386-linux-gnu_gl_conf) in auto mode
update-alternatives: using /usr/lib/nvidia-390/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_EGL.conf (i386-linux-gnu_egl_conf) in auto mode
update-alternatives: using /usr/share/nvidia-390/glamor.conf to provide /usr/share/X11/xorg.conf.d/glamoregl.conf (glamor_conf) in auto mode
dpkg: error: version '-' has bad syntax: revision number is empty
dpkg: error: version '-' has bad syntax: revision number is empty
dpkg: error: version '-' has bad syntax: revision number is empty
dpkg: error: version '-' has bad syntax: revision number is empty
dpkg: error: version '-' has bad syntax: revision number is empty
update-initramfs: deferring update (trigger activated)

A modprobe blacklist file has been created at /etc/modprobe.d to prevent Nouveau from loading. This can be reverted by deleting /etc/modprobe.d/nvidia-graphics-drivers.conf.
A new initrd image has also been created. To revert, please replace /boot/initrd-4.15.3-041503-generic with /boot/initrd-$(uname -r)-backup.

*****************************************************************************
*** Reboot your computer and verify that the NVIDIA graphics driver can   ***
*** be loaded.                                                            ***
*****************************************************************************

INFO:Enable nvidia-390
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/put_your_quirks_here
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/dell_latitude
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/lenovo_thinkpad
Adding system user `nvidia-persistenced' (UID 123) ...
Adding new group `nvidia-persistenced' (GID 129) ...
Adding new user `nvidia-persistenced' (UID 123) with group `nvidia-persistenced' ...
Not creating home directory `/'.
Loading new nvidia-390-390.30 DKMS files...
Building for 4.15.3-041503-generic
Building for architecture x86_64
Building initial module for 4.15.3-041503-generic
Done.

nvidia_390:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.15.3-041503-generic/updates/dkms/

nvidia_390_modeset.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.15.3-041503-generic/updates/dkms/

nvidia_390_drm.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.15.3-041503-generic/updates/dkms/

nvidia_390_uvm.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.15.3-041503-generic/updates/dkms/

depmod...

DKMS: install completed.
Setting up nvidia-390-dev (390.30-0ubuntu1) ...
Setting up nvidia-settings (390.30-0ubuntu1) ...
Processing triggers for libc-bin (2.26-0ubuntu2.1) ...
Setting up nvidia-modprobe (390.30-0ubuntu1) ...
Processing triggers for man-db (2.7.6.1-2) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu5) ...
Setting up nvidia-opencl-icd-390 (390.30-0ubuntu1) ...
Setting up libcuda1-390 (390.30-0ubuntu1) ...
Setting up cuda-drivers (390.30-1) ...
Setting up cuda-runtime-9-1 (9.1.85-1) ...
Setting up cuda-demo-suite-9-1 (9.1.85-1) ...
Setting up cuda-9-1 (9.1.85-1) ...
Setting up cuda (9.1.85-1) ...
Processing triggers for initramfs-tools (0.125ubuntu12) ...
update-initramfs: Generating /boot/initrd.img-4.15.3-041503-generic
cryptsetup: WARNING: target cryptswap1 has a random key, skipped
Processing triggers for shim-signed (1.33.1~17.10.1+13-0ubuntu2) ...
Secure Boot not enabled on this system.
Processing triggers for libc-bin (2.26-0ubuntu2.1) ...
root@srv2:~/xmr-stak/build/bin#