dpkg: error: dpkg status database is locked by another process

Solution 1:

First run:

lsof /var/lib/dpkg/lock

Then make sure that process isn't running:

ps cax | grep PID

If it is running:

kill PID
#wait
kill -9 PID

Make sure process is done:

ps cax | grep PID

Then remove the lock file:

sudo rm /var/lib/dpkg/lock

Let dpkg fix itself:

sudo dpkg --configure -a

You should be fine afterwards :)

Solution 2:

In our case there was no running process (PID) so I hard-stopped and rebooted the VPS.

Next, there are several lock files that might need to be removed:

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

Then you can fix dpkg as suggested:

sudo apt update
sudo dpkg --configure -a

Solution 3:

One could also try:

sudo apt-get install -f

To fix any broken packages:

Fix; attempt to correct a system with broken dependencies in place. This option, when used with install/remove, can omit any packages to permit APT to deduce a likely solution. If packages are specified, these have to completely correct the problem. The option is sometimes necessary when running APT for the first time; APT itself does not allow broken package dependencies to exist on a system. It is possible that a system's dependency structure can be so corrupt as to require manual intervention (which usually means using dselect(1) or dpkg --remove to eliminate some of the offending packages). Use of this option together with -m may produce an error in some situations. Configuration Item: APT::Get::Fix-Broken.