Can't login into desktop environment after apt upgrade && reboot: on entering password the screen flickers into a black screen and returns back to the login screen. Login via the terminal (Ctrl+Alt+F1) works fine.

/var/log/Xorg.0.log says:

(EE) NVIDIA: Failed to initialize the NVIDIA kernel module. Please see the
(EE) NVIDIA: system's kernel log for additional error messages and
(EE) NVIDIA: consult the NVIDIA README for details.
(EE) No devices detected.

$ dmesg says:

nvidia: version magic '4.4.0-116-generic SMP mod_unload modversions ' should be '4.4.0-116-generic SMP mod_unload modversions retpoline '

Trying to load the nvidia driver manually fails:

$ sudo modprobe nvidia
modprobe: ERROR: could not insert 'nvidia': Exec format error

Related: VirtualBox not starting after kernel upgrade

  • @Jeremy it is a different issue. Read the answer
    – jfs
    Apr 23 '18 at 11:24

The issue is with gcc version that doesn't support retpoline (What is a retpoline and how does it work?). See Ubuntu bug: 4.4.0-116 Kernel update on 2/21 breaks Nvidia drivers (on 14.04 and 16.04).

In my case, purging ppa:ubuntu-toolchain-r/test to install the default gcc version and rebuilding with DKMS the nvidia module (by reinstalling 4.4.0-116 kernel) fixes the problem. See instructions posted by @cjjefcoat on the bug tracker.

Restore default gcc by purging ppa:ubuntu-toolchain-r/test's version:

$ sudo apt-get install ppa-purge
$ sudo ppa-purge ppa:ubuntu-toolchain-r/test

gcc version (on Ubuntu 16.04) with retpoline support:

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609

Reinstall kernel:

$ sudo apt-get purge linux-headers-4.4.0-116 linux-headers-4.4.0-116-generic linux-image-4.4.0-116-generic linux-image-extra-4.4.0-116-generic linux-signed-image-4.4.0-116-generic
$ sudo apt-get install linux-generic linux-signed-generic

Check nvidia module:

$ modinfo nvidia_xxx -k 4.4.0-116-generic | grep vermagic
vermagic:       4.4.0-116-generic SMP mod_unload modversions retpoline 

replace _xxx with your version -- just press TAB after modinfo nvidia

retpoline should be in the output.

After that reboot completed successfully.

If you have compatible gcc version already, you could rebuild nvidia module using dkms command without reinstalling the kernel:

# dkms remove nvidia-xxx/yyy.zzz -k 4.4.0-116-generic
# dkms install nvidia-xxx/yyy.zzz -k 4.4.0-116-generic

I've decided to reinstall the kernel instead to update all modules that were re-built with DKMS using a wrong gcc version.

  • I am sure I have the same problem but I don't have "ppa:ubuntu-toolchain-r/test" It seems my default version of gcc is Ubuntu 5.4.1-2ubuntu1~16.04. Any advice? Feb 28 '18 at 21:44
  • @OleksiiRudenko /usr/bin/gcc points to gcc-5 on my machine (16.04). apt policy gcc-5 shows 5.4.0-... (not 5.4.1) from expected repositories.
    – jfs
    Feb 28 '18 at 22:18
  • I have figured it out. So I have already had the ppa:ubuntu-toolchain-r/test removed. So in order to purge packages, I had to add it first ppa:ubuntu-toolchain-r/test. The rest of steps worked as expected plus I had to reinstall nvidia driver: sudo apt-get install --reinstall nvidia-384 Mar 1 '18 at 6:48

I don't know whether Ask Ubuntu is a correct place for that shit, but — as I need new g++ and still periodically update kernel — I've written a bash-script that (1) purges ppa:ubuntu-toolchain-r/test, (2) rebuilds all DKMS-modules for chosen kernels, (3) installs g++-7 back — per this answer.

The script is provided "as is", without warranties of any kind.
Please, don't use it unless you understand the meaning of every line.
It's intended for saving time when doing things you are able to do manually (not for doing "magic" you don't understand).

The script:

#!/bin/bash -e

for list in /etc/apt/sources.list.d/ubuntu-toolchain-r*.list; do
    sudo cp -a "$list" "$list.backup"
    echo "Backed up $list to $list.backup"
sudo ppa-purge ppa:ubuntu-toolchain-r/test

readarray -t kernels < <(ls -1 /lib/modules)
echo "Kernels: ${kernels[*]}"
for kernel in "${kernels[@]}"; do
    dkms_modules=($(sudo dkms status -k "$kernel" | sed -r 's#^([^,]+), ([^,]+), .*$#\1/\2#'))
    while true; do
        read -p "Reinstall DKMS-modules (${dkms_modules[*]}) on kernel $kernel? [Y/n] " choice
        if [ "${choice^^}" = N ]; then continue 2; fi
        if [ "${choice^^}" = Y ] || [ -z "$choice" ]; then break; fi
        echo "Expected 'y', 'n' or '', but got '$choice'"
    for dkms_module in "${dkms_modules[@]}"; do
        sudo dkms remove -k "$kernel" "$dkms_module"
        sudo dkms install -k "$kernel" "$dkms_module"
    for module in /lib/modules/"$kernel"/updates/dkms/*.ko; do
        vermagic="$(modinfo -F vermagic $module)"
        echo -n "Vermagic for $(basename ${module%.ko}): $vermagic -- "
        fgrep -q retpoline <<<"$vermagic" && echo ok || echo "'retpoline' is missing!!!"

for list in /etc/apt/sources.list.d/ubuntu-toolchain-r*.list; do
    sudo mv "$list.backup" "$list"
    echo "Restored $list from $list.backup"
sudo apt update
sudo apt install g++-7
sudo apt dist-upgrade

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.