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
If you ever encountered with this problem, wait for a few minutes until the currently installing or updating or uninstalling task to 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!
Related read:
3 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