How to Solve ‘E: Could not get lock /var/lib/dpkg/lock’ Error in Ubuntu

Recently, I bumped into the error ‘Could not get lock /var/lib/dpkg/lock’. As a result, I could neither install any packages nor update the system. This error is also closely related to the ‘Could not get lock /var/lib/apt/lists/lock’ error.  Here’s some sample output on Ubuntu 20.04.

Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 3620 (apt)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock directory /var/lib/apt/lists/

apt-update-lock-error-ubuntu

This can be quite frustrating and often leaves you stranded, unable to update, upgrade or install any packages.

So, what causes this error?

As the error suggests, this error usually happens when another process is currently using the /var/lib/dpkg/lock or /var/lib/dpkg/lock file. Such happens when you have 2 or more terminals running a system update or upgrade.  It can also occur when you prematurely cancel an update/upgrade that is in progress, accidentally or otherwise. A second attempt to use apt or apt-get command will yield the error.

There’s absolutely no need to panic in case you run into this error. A couple of options are available to fix this issue. Let’s explore some of the solutions.

Solution 1)  Killing all processes which are using the APT manager

The first step in diagnosing this problem is listing the processes that are using the apt package manager. To do so, use the ps command as shown:

$ ps aux | grep - i apt

Here’s the output I got.

Process-associated-with-apt-command

To clear the error, you need to kill the processes that are associated with the apt command. You can do so by sending a SIGKILL command to shut down the process immediately. Execute the kill -9 command followed by the process ID as follows.

$ sudo kill -9 3619
$ sudo kill -9 3620

Once done, verify again if the processes have ended using the ps command. If they have all cleared, you can proceed to update the system without a problem.

Solution 2)

In some situations, the root cause could be the lock file.  The lock file blocks two or multiple processes from accessing the same data. When you run any apt or apt-get command, a lock file is usually created. However, if the latest apt command was not successfully executed (i.e terminated abruptly), the lock file persists and blocks any subsequent apt or apt-get instances.

The solution to this problem is to get rid of the apt lock file(s). And it’s quite easy. Simply run the command below:

$ sudo rm /var/lib/apt/lists/lock

If the error you are getting is the ‘Could not get lock /var/lib/dpkg/lock.’ error, delete the lock file as shown:

$ sudo rm /var/lib/dpkg/lock

Other times, you might get a /var/lib/dpkg/lock-frontend error. The lock-frontend error implies that a graphical application that used apt / dpkg is currently running. This could either be Gdebi or synaptic package manager or any other application.

The immediate remedy is to exit or close the application and give it another try. If nothing gives, simply remove the /var/lib/dpkg/lock-frontend file as shown.

$ sudo rm /var/lib/dpkg/lock-frontend

Removing the lock-frontend file might again lead to the  ‘Could not get lock /var/lib/dpkg/lock’ error, so once again, you will have to remove the lock file.

$ sudo rm /var/lib/dpkg/lock

If you happen to get an error about the apt-cache lock such as /var/cache/apt/archives/lock, proceed and remove the lock file as shown.

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

And that’s how you resolve the ‘Could not get lock /var/lib/dpkg/lock‘ and could not get lock /var/lib/apt/lists/lock errors. I’m sure that if you have come this far, you should have resolved the error by now. Let us know how it went.

5 thoughts on “How to Solve ‘E: Could not get lock /var/lib/dpkg/lock’ Error in Ubuntu”

  1. Solution 3) If you are learning Linux and not comfortable with the above commands. Simply restart and it should resolve the issues.

  2. TWIRINGIYIMANA Aimable

    I think you should wait for some time or restart your computer. I had the same problem on my ubuntu 20.04 and I waited for a few minutes then I could do my update. As you can see on your terminal it is advising you not to remove the lock file, I think you should listen to the system advise and wait.

    1. could you plz provide the solution for it.
      we are using ansible to implement UBUNTU20.04 with CIS level1 compliant.
      if possible, can you plz post the solution here.

    2. I agree that waiting a bit is advisable. My Ubuntu Desktop is running as a VM and had just been booted. That said, I received the “lock error” when attempting to install git with
      sudo apt-get install git

      I realized that the system was performing some updates so I waited a bit. About 5 minutes later, I retried the apt-get and it was successful.

      I would not be apprehensive about using:
      ps aux | grep – i apt
      to learn what processes are using the apt manager.

  3. On running sudo apt update, I received “Could not get lock /var/lib/apt/lists/lock. It is held by process 2297 (packagekitd)”.
    I simply ran sudo aptitude upgrade, end of problem.

Leave a Comment

Your email address will not be published. Required fields are marked *