How to fix ‘unable to acquire the dpkg frontend lock’ error in Ubuntu?

Fix dpkg frontend lock error

While updating or installing packages using apt, apt-get, aptitude, or dpkg, you might get an error saying ‘unable to acquire the dpkg frontend lock‘ or a similar error related to locks. This error is generated when the system is unable to acquire the required permissions to install or upgrade programs. In this article, we will examine the potential causes of this error and offer solutions for fixing it.

The error message looks something like this:

Could Not Acquire Lock
Could not get Lock

You might also get these error messages:

  • E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
    E: Unable to lock directory /var/lib/apt/lists/
  • 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?

It might also look like this while using the Software Center/App Store :

Screenshot From 2020 10 08 21 36 07
Could not get lock – Software Center

In this article, we cover how to resolve this error so that you can continue with your important work on your Ubuntu/Debian machine.

Why does the “unable to acquire the dpkg frontend lock” error show up?

Whenever a program like apt tries to update or install package(s), it first locks a particular file. Any other program that tries to update or install packages has to check whether this file’s lock is available or not. Only 1 program can own a lock at a given time.

It might happen that another program was updating/installing a package and then you try to sudo apt install something while the other program still owns the lock. Since apt can’t own the lock now, it will give an error saying ‘unable to acquire the dpkg frontend lock‘ or something similar.

This ‘locking mechanism‘ exists because package updates can’t be run in parallel, they could interfere with each other. Now that we understand why this error is thrown, we can move on to fixing it.

How to Fix “unable to acquire the dpkg frontend lock”

Now that we understand the reason behind the error, let’s try to fix it.

Wait it out or Reboot

Even before we start with various methods to get dpkg to work, the most obvious way to go about is to do nothing. Give the system to process all the pending subtasks it needs to perform. You liberally wait for up to 15 minutes. Even after giving sufficient time if the dpkg is still locked out, you can then go ahead and try rebooting the system.

Plan 0

This plan is named Plan 0 because it’s quite trivial and easy to overlook for a beginner. While installing a package using the apt command, you must have superuser privileges.

If you forgot to prepend your apt install with sudo, apt will yell at you saying it couldn’t open the lock file. This isn’t exactly the ‘unable to acquire the dpkg frontend lock’ error, but it is an error that can frustrate someone who has just started the Linux journey.

Step 1
Are you root?

Plan 1

This plan involves finding out the process that is running updates already and either waiting for it or gracefully killing it. You can find the running processes using the ps command.

ps aux | grep -i apt
Ps Output
ps Output

As you can see an update is already in progress. If the process that is the last column of the output of ps command is apt.systemd.daily, you should probably wait for a few minutes as this is the apt-daily.service service that runs each time the system is booted. The waiting time depends on the system.

If the last column of ps command is like the one shown in the screenshot above, you can kill it using the kill command. The PID(Process ID) can be obtained from the 2nd column of the ps command output.

sudo kill <process_id>

When apt is killed, it also releases the lock held by it.

Plan 2

If Plan 1 didn’t work for you, it might have happened because of the lock on the files. If the program owning the lock was abruptly terminated it might not have released the lock. Use the lsof command to see which process owns the lock on these files:

sudo lsof /var/lib/dpkg/lock
sudo lsof /var/lib/apt/lists/lock
sudo lsof /var/cache/apt/archives/lock

Some of the above commands might not give an output but for me lsof /var/lib/dpkg/lock gave the following output.

Find The Culprit
lsof output

Then use the PID of the command to kill the command.

sudo kill -9 <process_id>

-9 sends a SIGKILL signal to the program passed as kill command’s argument.

Then you can manually delete the lock files using the rm command.

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

Note: Manually removing lock files is not recommended. As a result Plan 2 should be used as a last resort

After deleting the locks reconfigure dpkg.

sudo dpkg --configure -a

Summary

The “unable to acquire the dpkg frontend lock” error occurs frequently when updating or installing packages on Ubuntu/Debian systems. This error occurs when a program, such as apt, is unable to acquire the necessary permissions to update or install package(s). A possible solution to this error is to delay, as the system may be processing pending subtasks. The ps command can be used to locate and terminate the process that is updating or installing packages. If that fails, you can use the lsof command to determine which process possesses the lock on the files, kill it, and manually delete the lock files with the rm command. Manually deleting lock files is not advised, and should only be done as a last resort. After removing the restrictions, use sudo dpkg –configure -a to reconfigure dpkg. It is essential to have superuser privileges when installing a package with the apt command, and failure to do so can result in errors. By following these methods, you should be able to resolve the “unable to acquire the dpkg frontend lock” error and resume your important work.

Reference

Here’s the official documentation on dpkg that I found helpful while researching for this article!