Infinite login loop after attempted runfile installation of Cuda 8.0 toolkit on Ubuntu 14.04

I recently was stuck in an infinite login loop after attempting to install Cuda tookit 8.0 using the runfile method on a linux PC (Ubuntu 14.04) I wanted to share what I learned and how I recovered from it. A lot of posts suggested that you needed to reinstall desktop or even the OS to fix it. I avoided that, but it took a day and half of searching and trying different suggestions. I appreciate any comments on what I might have done wrong initially to cause this to happen and how to fix if it happens again. I have some guesses below.

Some details:
$ lspci | grep -i nvidia
4b:00.0 VGA compatible controller: NVIDIA Corporation GP102 [TITAN X] (rev a1)
4b:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)

$ uname -m && cat /etc/*release
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION=“Ubuntu 14.04.5 LTS”
NAME=“Ubuntu”
VERSION=“14.04.5 LTS, Trusty Tahr”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=“Ubuntu 14.04.5 LTS”
VERSION_ID=“14.04”

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4

$ uname -r
4.4.0-31-generic

I ran the following:
sudo apt-get install linux-headers-$(uname -r)

I first attempted a deb install and that seemed to work until I tested the deviceQuery code which resulted in FAIL. At that point, I somehow convinced myself from reading other posts that I should have done the runfile install instead. Here is a place where I may have gone wrong. I may not have completely uninstalled the deb package before doing the runfile install, or not done it properly. I recall that I did run sudo apt-get --purge remove cuda but now I am not so sure. At any rate, I then followed the install instructions for the runfile installation. I made the mistake of saying yes to the openGL and the X config options. When I rebooted I was then in an infinite login loop. The desktop would come up and it would show my name and a guest login option. When I typed my password the screen would go black and then come right back with the same prompt for password again.

I saw many posts on similar problems, some were fixed by .Xauthority permission or xorg.conf. Others by simply purging nvidia and re-installing drivers. NONE of this worked for me. I ran the --uninstall option on the runfile and tried to re-install driver by runfile again and not choosing openGL and X options and then tried deb install again after that, everything resulted in same infinite login.

Finally I came across the following post and there was an answer at the bottom that was not upvoted. I tried it along with a deb driver install and I got my system back. The post that I followed to get things to work is below, HOWEVER I used the answer by user Ashu at the bottom of the post which has you interrupt the boot process and type noveau.modeset=0 in the GRUB menu after the linux line.

http://askubuntu.com/questions/693454/infinite-login-loop-with-nvidia-proprietary-graphics

The only thing I did differently was to use
$ sudo ubuntu-drivers devices
to get a list of nvidia drivers. The recommended one from this list was nvidia-375. I installed that one with sudo apt-get install nvidia-375 instead of the driver mentioned in the post. This got my normal desktop back and got me out of the infinite login loop. Hope this helps someone else.

Then I did a complete uninstall of the runfile install including removing the cuda-8.0 directory manually. Then did a deb install. After deb install, I still had to install the nvidia-375 driver using sudo apt-get install nvidia-375 in order to get the deviceQuery sample code to PASS. It failed immediately after following the deb install instructions at http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#axzz4VWRXgvDF

If anyone can explain why I had so much trouble in the first place, I would love to hear it. Or if I really needed nvidia-375 instead of nvidia-367 to make all this work with TITAN X (Pascal).

The reason the infinite login loop occurs is discussed here:

https://devtalk.nvidia.com/default/topic/878117/cuda-setup-and-installation/-solved-titan-x-for-cuda-7-5-login-loop-error-ubuntu-14-04-/

The core problem is that the nvidia driver (runfile) installer is corrupting the display driver stack assuming your display driver stack is not using the NVIDIA GPU for display.

The fix is to tell the runfile installer not to install the OpenGL ibraries that are corrupting the (non-NVIDIA) X driver stack. (Note that if your display is hosted by the NVIDIA GPU, you will almost certainly want these additional libraries installed.)

The deb install method generally avoids this hazard through package dependencies.

This topic has already been covered in a number of forum posts. Also the concept that runfile install methods and package manager install methods should not be mixed on the same system has been covered in many forum posts, and is covered in the CUDA linux install guide as well.

I did read your former post on the infinite loop before I posted this. It said that I should re-install the OS. I did not want to have to do that. I found another way and posted that here. Sorry if I am out of line posting this, I am new to the forum. I will defer to your experience. I also followed the CUDA linux guide or at least attempted to.