Error install nvidia-387: Error! Bad return status for module build on kernel:

I'm trying to install nvidia-387 on my fresh Ubuntu 16.04.

Once I launch sudo apt-get install nvidia-387 I always end up in the following outputs:

Building initial module for 4.13.0-26-generic
Error! Bad return status for module build on kernel: 4.13.0-26-generic (x86_64)

During my searches I found lot of issues pointing to a wrong gcc version but after several attempts I wasn't able to find a way to go: following this discussion I tried with gcc 4.8/4-9/5.4/6 without any luck.

Any help?

Thank you very much.


Solution 1:

Looks like the issue is cause by some shuffling of linux headers in some recent linux kernel updates.

Relevant discussion: https://devtalk.nvidia.com/default/topic/1028016/linux/patch-for-compiling-v384-98-modules-with-linux-v4-14-9-/

I had the exact same issue and I fixed it by applying the patch suggested by user PGNd in the discussion above.

Step by step instructions for manual edit of the nvidia src files on Ubuntu 16.04:

  1. sudo apt-get install nvidia-387 (this will fail)
  2. sudo nano /usr/src/nvidia-387-387.26/nvidia-uvm/uvm8_va_block.c (you should replace nvidia-387-387.26 with the version you are trying to install.)
  3. Add the line #include <linux/sched/task_stack.h> before the other include statements at the beginning of the file
  4. Run sudo dpkg-reconfigure nvidia-387 to rebuild the nvidia driver
  5. Reboot

This should work as a temporary workaround until nvidia releases a fix. I was able to install cuda-9.1 after doing this.