Recently, I ran into the error ‘Could not get lock /var/lib/dpkg/lock’. As a result, I might neither install any packages nor update the system. This error is additionally closely associated with the ‘Could not get lock /var/lib/apt/lists/lock’ error. Here’s some sample output on Ubuntu twenty.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/

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

So, what causes this error?

As the error suggests, this error typically happens once another method is presently mistreatment the /var/lib/dpkg/lock or /var/lib/dpkg/lock file. Such happens after you have two or additional terminals running a system update or upgrade. It may occur after you untimely cancel associate update/upgrade that’s ongoing, accidentally or otherwise. A second plan to use apt or apt-get command can yield the error.

There’s completely no have to be compelled to panic just in case you run into this error. a handful of choices square measure on the market to mend this issue. Let’s explore a number of the solutions.

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

The first step in identification this drawback is listing the processes that ar exploiting the apt package manager. To do so, use the ps command as shown:

$ ps aux | grep - i apt

Here’s the output I got.

To clear the error, you wish to kill the processes that are related to the apt command. you’ll be able to do thus by causation a SIGKILL command to clean up the method now. Execute the kill -9 command followed by the method ID as follows.

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

Once done, verify once more if the processes have concluded mistreatment the ps command. If they need all cleared, you’ll proceed to update the system withouta tangle.

Solution 2

In some thingsthe basis cause may well be the lock file. The lock file blocks 2 or multiple processes from accessing an equivalent informationafter you run any apt or apt-get command, a lock file is sometimes created. However, if the most recent apt command wasn’t with success dead (i.e terminated abruptly), the lock file persists and blocks any succeeding apt or apt-get instances.

The solution to the current downside is to urge obviate the apt lock file(s). And it’s quite simplemerely run the command below:

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

the error you’re obtaining is that 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 would possibly get a /var/lib/dpkg/lock-frontend error. The lock-frontend error implies that a graphical application that used apt / dpkg is presently running. this might either be Gdebi or synaptic package manager or the other application.

The immediate remedy is to exit or shut the application and provides it another attempt. If nothing offersmerely take away the /var/lib/dpkg/lock-frontend file as shown.

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

Removing the lock-frontend file may once more cause the ‘Could not get lock /var/lib/dpkg/lock’ error, thus another timeyou may ought to remove the lock file.

$ sudo rm /var/lib/dpkg/lock

If you happen to induce a slip concerning the apt-cache lock like /var/cache/apt/archives/lock, proceed and take away the lock file as shown.

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

And that’s however you resolve the ‘Could not get lock /var/lib/dpkg/lock‘ and will not get lock /var/lib/apt/lists/lock errors. I’m certain that if you have got come back this so muchyou ought to have resolved the error by currentlyallow us to know how it went.