Home Ubuntu How To Fix “E: Could not get lock /var/lib/dpkg/lock” Error On Ubuntu

How To Fix “E: Could not get lock /var/lib/dpkg/lock” Error On Ubuntu

By sk
Published: Last Updated on 26.6K views

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
Could not get lock ubuntu

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

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?

Of course not! 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:

You May Also Like

5 comments

Mustafa February 24, 2020 - 2:50 pm

Thank you. I’m new to Linux world and you helped me a lot.

Reply
Naval June 20, 2020 - 9:43 pm

Thank you. \o/

Reply
arman July 28, 2020 - 6:06 pm

thanks alot. you helped me out very well

Reply
Guillem Jover November 22, 2022 - 11:13 pm

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.

Reply
sk November 23, 2022 - 12:27 pm

Yes, you’re totally right. I updated the guide with your inputs. Thank you.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More