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/
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.
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.
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.
The post How to Solve ‘E: Could not get lock /var/lib/dpkg/lock’ Error in Ubuntu first appeared on Linuxtechi.