Locked out of XWindows - Login Loop after driver upgrade - Ubuntu 16.04

I am currently experiencing a login loop after upgrading from manual install of nVidia 361 to package install of 367. I have tried downgrading to 364 and back to 361 (package install this time) to no avail, the errors appear to be the same.

My .xsession-errors contains the following:

Failed to connect to bus: No such file or directory
X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  154 (GLX)
  Minor opcode of failed request:  3 (X_GLXCreateContext)
  Value in failed request:  0x0
  Serial number of failed request:  32
  Current serial number in output stream:  33
openConnection: connect: No such file or directory
cannot connect to brltty at :0

I have additional logs to provide, however I don’t see how I can attach them to this thread. I’ll paste what I can here:

I found this in my Xorg log file

[ 2312.921302] NVRM: Your system is not currently configured to drive a VGA console
[ 2312.921304] NVRM: on the primary VGA device. The NVIDIA Linux graphics driver
[ 2312.921305] NVRM: requires the use of a text-mode VGA console. Use of other console
[ 2312.921305] NVRM: drivers including, but not limited to, vesafb, may result in
               NVRM: make sure that this kernel module and all NVIDIA driver

And further down I found:

[  2850.609] (II) NOUVEAU driver Date:   Tue Dec 8 15:52:25 2015 +1000
[  2850.609] (II) NOUVEAU driver for NVIDIA chipset families :
[  2850.706] (EE) [drm] Failed to open DRM device for (null): -22
[  2850.706] (EE) [drm] Failed to open DRM device for (null): -22
[  2850.706] (EE) [drm] Failed to open DRM device for pci:0000:02:00.0: -22
[  2850.706] (EE) No devices detected.
[  2850.706] (II) Applying OutputClass "nvidia" to /dev/dri/card0
[  2850.706] 	loading driver: nvidia
[  2850.706] (==) Matched nvidia as autoconfigured driver 0
[  2850.706] (==) Matched nvidia as autoconfigured driver 1
[  2850.706] (==) Matched nouveau as autoconfigured driver 2
[  2850.706] (==) Matched nvidia as autoconfigured driver 3
[  2850.706] (==) Matched nouveau as autoconfigured driver 4
[  2850.706] (==) Matched vesa as autoconfigured driver 7
[  2850.706] (II) LoadModule: "nvidia"
[  2850.706] (II) Loading /usr/lib/xorg/modules/drivers/nvidia_drv.so
[  2850.708] (II) Module nvidia: vendor="NVIDIA Corporation"
[  2850.708] (II) LoadModule: "nouveau"
[  2850.708] (II) Loading /usr/lib/xorg/modules/drivers/nouveau_drv.so
[  2850.708] (II) Module nouveau: vendor="X.Org Foundation"
[  2850.708] (II) UnloadModule: "nouveau"
[  2850.708] (II) Unloading nouveau
[  2850.708] (II) Failed to load module "nouveau" (already loaded, 21886)
[  2850.709] (II) LoadModule: "vesa"
[  2850.709] (II) Loading /usr/lib/xorg/modules/drivers/vesa_drv.so
[  2850.709] (II) Module vesa: vendor="X.Org Foundation"
[  2850.709] (II) NOUVEAU driver Date:   Tue Dec 8 15:52:25 2015 +1000
[  2850.709] (II) NOUVEAU driver for NVIDIA chipset families :
[  2850.709] (II) NVIDIA dlloader X Driver  364.19  Tue Apr 19 13:48:55 PDT 2016
[  2850.709] (II) NVIDIA Unified Driver for all Supported NVIDIA GPUs
[  2850.709] (II) VESA: driver for VESA chipsets: vesa
[  2850.709] (WW) xf86OpenConsole: setpgid failed: Operation not permitted
[  2850.709] (WW) xf86OpenConsole: setsid failed: Operation not permitted
[  2850.709] (EE) [drm] Failed to open DRM device for (null): -22
[  2850.709] (EE) [drm] Failed to open DRM device for (null): -22
[  2850.709] (EE) [drm] Failed to open DRM device for (null): -22
[  2850.709] (EE) [drm] Failed to open DRM device for pci:0000:02:00.0: -22
[  2850.709] (EE) [drm] Failed to open DRM device for pci:0000:02:00.0: -22

I have disabled secure boot in the BIOS.

Full details based on the Ubuntu graphics driver troubleshooting steps are here: https://answers.launchpad.net/ubuntu/+question/300410

I resolved the issue. Although I attempted a manual uninstall of the manually installed 361 driver, it failed to clean-up all the shared objects and so what I was experiencing was essentially a library problem.

After purging all NVIDIA packages using apt, I resolved this as follows:

  1. Locate all the 361 driver shared object files using:
    locate so.361.45 | egrep -vi "trash|steam|^/home"
    
  2. Remove the suspicious shared object files (some contain the word nvidia while others contain just nv)
  3. Run ldconfig
  4. Run the following command:
    ldd $( which glxinfo )
    

    Inspect the output and ensure that none of the entries point to any stale NVIDIA 361.45 shared objects

  5. Reboot
  6. Install the NVIDIA 364 driver via apt
  7. Reboot