How To Fix `Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)` Errors

E: Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

I've been getting a lot of "Could not get lock /var/lib/dpkg/lock" errors when"Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)" trying to install or upgrade packages from the command line on Ubuntu virtual machines lately, so I thought I'd make a post about how you can get rid of such issues.

This is the complete error message:

E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

[[Edit]] For newer Ubuntu releases, this message has changed, and it now shows which process is holding the "/var/lib/dpkg/lock-frontend", like this:

Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 251486 (apt)

Just like the message says, this could happen if another process is using /var/lib/dpkg/lock. So the first thing to do if you encounter this error is to make sure you close package managers such as Synaptic, etc. Also check if you have other open terminals that are currently running an install / upgrade procedure and wait for those processes to finish. If you're using a newer Ubuntu, the message itself will tell you which process is holding the "/var/lib/dpkg/lock" / "/var/lib/dpkg/lock-frontend".

If no processes are using /var/lib/dpkg/lock, the next step is to... wait. In some cases, this is enough to fix such "Could not get lock /var/lib/dpkg/lock" and "Could not get lock /var/lib/dpkg/lock-frontend" errors.

Another potential way to get around this issue is to reboot the system and see if this still occurs.

There are cases though in which the solutions mentioned above may not be enough. For such cases, here's what you can do. 

Only use this if nothing else worked! Using the commands below may result in broken packages / corruption. Use them at your own risk!

If nothing else worked (from my experience, this usually happens if the system was forcefully shut down or rebooted while installing or upgrading packages, e.g. due to a power outage), you can remove the apt lock / lock-frontend file and see if that fixes the issue on your Ubuntu / Debian / Linux Mint (and any system that uses APT) system:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/lib/apt/lists/lock-frontend

If you're still getting errors about either the apt cache lock (/var/cache/apt/archives/lock) or the dpkg lock (/var/lib/dpkg/lock), you can remove them:

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

A package reconfiguration may also be needed after this, as well as fixing any potentially broken packages:

sudo dpkg --configure -a
sudo apt install -f

In some rare cases you may see an error like the one below, after trying to run sudo dpkg --configure -a:

$ sudo dpkg --configure -a
dpkg: error: parsing file '/var/lib/dpkg/updates/0004' near line 0:
newline in field name '#padding'

In such cases, remove the offending file, then run the sudo dpkg --configure -a command again. In my example above, the file is /var/lib/dpkg/updates/0004 (this may be different in your case!) so to remove it and reconfigure dpkg, one would need to use:

sudo rm /var/lib/dpkg/updates/0004
sudo dpkg --configure -a

Hopefully after running these commands you should stop getting the "Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)" and "Could not get lock /var/lib/dpkg/lock-frontend - open (11 Resource temporarily unavailable)" errors.

Edit: one major reason for this error reoccurring seems to be the fact that Ubuntu enables unattended updates by default, and either an upgrade is currently in progress when you're seeing this error (in which case you should wait until the upgrade is performed successfully!), or an upgrade failed, in which case you're left with this error until you fix it. You could disable automatic (unattended) upgrades, see: How To Stop Installing Updates Automatically On Ubuntu Or Debian (Unattended Upgrades).