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

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.