This brief guide helps you to how to fix "E: Could not get lock /var/lib/dpkg/lock" error on Ubuntu. This will usually occur when there is another apt operation is already running in the background.
I have a Ubuntu 18.04 LTS virtual machine which I use for testing purposes. Whenever I start this VM and try update or install any application using APT package manager, I get the following error:
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?
Or, something similar errors like below:
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?
E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable) E: Unable to lock directory /var/lib/apt/lists/
It happens every single time. I had to wait for a few minutes to perform an apt operation.
I know some process has locked the apt database while installing or removing software or updating the system in the background. I ran the "top" command to investigate the list of running processes. After looking into the output of top command, I came to know that there is a process called unattended-update, which runs apt update every time I turn my Ubuntu virtual machine on. While the apt update is running, the apt database is locked and I couldn't perform any apt operation.
Fix "E: Could not get lock /var/lib/dpkg/lock" Error On Ubuntu
The right way to solve "E: Could not get lock /var/lib/dpkg/lock
" error is to allow the currently installing or updating or uninstalling task to gracefully complete. This process will take some time (5 to 10 minutes or more) to complete depending upon the size of the update. Once that task is completed, the lock will be released automatically.
But if the process is stuck for some reason and it locked the apt database for several minutes, you have no choice but remove the lock. In that case, follow the below procedure to fix it.
First let us find out which process that owns the lock file i.e /var/lib/dpkg/lock
.
$ sudo lsof /var/lib/dpkg/lock
If the lock file is different, for example /var/lib/dpkg/lock-frontend
, you can find PID of the process that owns this lock file with command:
$ sudo lsof /var/lib/dpkg/lock-frontend
If the lock file is "/var/lib/apt/lists/lock", run:
$ sudo lsof /var/lib/apt/lists/lock
Sample output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME unattende 1548 root 6uW REG 8,2 0 1181062 /var/lib/dpkg/lock
As you can see in the above output, the PID of the process that holds the lock file is 1548.
Just kill it to release the lock using command:
$ sudo kill -9 1548
You can now safely remove the lock with commands:
$ sudo rm /var/lib/dpkg/lock
Or,
$ sudo rm /var/lib/dpkg/lock-frontend
Or,
$ sudo rm /var/lib/apt/lists/lock
Also you may need to delete the lock file in the cache directory:
$ sudo rm /var/cache/apt/archives/lock
After removing the lock, run:
$ sudo dpkg --configure -a
This should fix the problem.
This method will work just fine 99% of time. But please be mindful that if the update process is running and you killed the process in the middle of package installation, you might end up with broken system. In such cases, don't panic, just follow the below guide to fix it.
Good luck!
Is removing the lock file a correct solution?
Certainly NO! As one of our reader Mr. Guillem Jover mentioned, removing the lock files is never a correct solution, and can cause data loss or damage on the system.
You must let the currently running apt task until it completed gracefully. However, sometimes the task might be running longer than the usual, or it might be frozen.
If there ever is a need to kill any such frozen processes, run the following commands:
$ sudo fuser -vki -TERM /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend
$ sudo dpkg --configure --pending
The first command will look for processes currently holding the locks, print information about them and prompt to kill them. If graceful termination is not working, you might need to use -KILL
instead. The dpkg --configure --pending
command is needed to finish any pending configuration to let the processes get into a sane state, and to let dpkg merge any database journal updates into the main status database.
Resource:
Related Read:
5 comments
Thank you. I’m new to Linux world and you helped me a lot.
Thank you. \o/
thanks alot. you helped me out very well
This was going fine until the lock removal. :/ Removing the lock files is *never* a correct solution, and can cause data loss or damage on the system. Please see https://wiki.debian.org/Teams/Dpkg/FAQ#db-lock.
Yes, you’re totally right. I updated the guide with your inputs. Thank you.