E: Sub-process /usr/bin/dpkg returned an error code (1) won't leave me alone :'( [duplicate]

I've had Nvidia drivers work successfully before.

I switched to Nouveau for a little while since a game wasn't working properly, then switched back to Nvidia only to be notified of an error.

I have uninstalled and re-installed the Nvidia driver multiple times now but keep getting the same errors.

diversion of /usr/lib/x86_64-linux-gnu/libGL.so.1 to /usr/lib/x86_64-linux-gnu/libGL.so.1.distrib by nvidia-340
dpkg-divert: error: mismatch on package
  when removing 'diversion of /usr/lib/x86_64-linux-gnu/libGL.so.1 by libnvidia-gl-390'
  found 'diversion of /usr/lib/x86_64-linux-gnu/libGL.so.1 to /usr/lib/x86_64-linux-gnu/libGL.so.1.distrib by nvidia-340'
dpkg: error processing archive /tmp/apt-dpkg-install-JJFsm3/13-libnvidia-gl-390_390.48-0ubuntu3_amd64.deb (--unpack):
new libnvidia-gl-390:amd64 package pre-installation script subprocess returned error exit status 2

Errors were encountered while processing:
 /tmp/apt-dpkg-install-JJFsm3/13-libnvidia-gl-390_390.48-0ubuntu3_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Installation method is sudo ubuntu-drivers autoinstall


Running sudo dpkg-divert --remove "/usr/lib/x86_64-linux-gnu/libGL.so.1" resolved my issue.

All thanks to @Terrance !


I had a raft of these problem with nvidia-340 diverts in Ubuntu 19.10; it seems to be a known bug with Ubuntu. package libnvidia-gl-390 (not installed) failed to install/upgrade: new libnvidia-gl-390:amd64 package pre-installation script subprocess returned error exit status 2 (NOT FIXED)

The workaround I did was #5, Yuri's improvement of Alexandre's suggestion

Try

# for FILE in $(dpkg-divert --list | grep nvidia-340 | awk '{print $3}'); do echo $FILE; done

first and choose the correct number for substring {print $3}

For example for Russian locale it will be $2:

# for FILE in $(dpkg-divert --list | grep nvidia-340 | awk '{print $2}'); do dpkg-divert --remove $FILE; done