Search This Blog

Saturday 23 January 2021

dpkg: package is in a very inconsistent state

Somehow the docker-ce package on my Ubuntu16.04 machine got out of shape.  I could not update it, I could not delete it.  Running this and other package management related commands

sudo dpkg --configure -a

resulted in:

.....

dpkg: error processing package docker-ce (--purge): package is in a very bad inconsistent state; you should reinstall it before attempting a removal Errors were encountered while processing: docker-ce .....

 Below are the steps that helped me solve the problem:

A) If you get an error message about not being able to acquire a lock, run the following commads to find, which processes are holding the particular lock(s):

sudo lsof /var/lib/dpkg/lock-frontend
sudo lsof /var/lib/dpkg/lock
sudo lsof /var/lib/apt/lists/lock
sudo lsof /var/cache/apt/archives/lock

Then kill the process(es) with

sudo kill -9 <process_id>

and remove the associated lock(s)

sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

If no processes are listed, just remove the lock(s).

B) Find the package (docker-ce) location using

ls -l /var/lib/dpkg/info | grep docker-ce

and do a backup of the package files

sudo mv /var/lib/dpkg/info/docker-ce.* /tmp/

          

C) Cleanup the inconsistency

sudo dpkg --remove --force-remove-reinstreq docker-ce  

D) Reinstall docker-ce

sudo dpkg --configure -a
sudo apt-get install docker-ce


Links

https://itsfoss.com/could-not-get-lock-error/

https://stackoverflow.com/questions/48431372/removing-broken-packages-in-ubuntu

https://stackoverflow.com/questions/55700980/cant-uninstall-docker-ce-and-cant-install-any-new-programs

https://askubuntu.com/questions/979293/docker-ce-post-installation-subprocess-never-finishes