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 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?

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 and wait for those processes to finish.

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" 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, you can remove the apt lock 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

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)" error.

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)


In some situations you might see a similar error, but for which says lock-frontend instead of just lock:

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

This dpkg lock-frontend error means a graphical application that uses dpkg / apt, is currently running, like Synaptic package manager, Gdebi or some other. The solution in this case is to close the application and try again. In 99% of cases this should solve the issue. If it persists, try to reboot the system, and only as a last resort remove the /var/lib/dpkg/lock-frontend file.

If you do remove the /var/lib/dpkg/lock-frontend file, you'll then most probably run into the Could not get lock /var/lib/dpkg/lock error mentioned in the beginning of the article, so you'll have to start from the top of this article with the instructions to fix this.