In this article, we cover what happens in the background when we get the error: Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process.
It mainly comes up when we try to install/update a package while another process is already in the midst of installing/updating packages.
So, it waits for the cache lock. If multiple processes access the same resource then, it would lead to unpredictable outcomes. File locking in Linux restricts multiple processes from accessing and modifying the same file. Only one process gets the right to access it at a time.
[Fixed] Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend
- Ideally, one should wait for the currently running process to finish installing/updating packages. Otherwise, it may lead to broken packages. And, we need to configure them later. Not something we should do unless absolutely required.
- It could also be due to an instance of a package manager like Synaptic running in the background. Though it may or may not be installing a package. Still, we would get the error. So, if it is just there idling then close it first.
- One more reason could be automatic updates. The automatic updates ensure that our system is updated as and when package updates are available. Though we can set the frequency. But, if there is an automatic update process running in the background then that would also lead to the error.
If there is an error due to any of the above conditions then, it is better to wait for the process to exit on its own. We shouldn’t interfere in between. Though there are ways that can help us identify and terminate the update process. But, that is something we don’t advise.
Run the following command to see the instance of apt:
ps aux | grep -i apt
From here, we can get the Process ID and that will help us terminate the process:
sudo kill -9 <Process_ID>
We reiterate that it is better to wait for the process to exit on its own.
But, at times the above-mentioned steps won’t get you anywhere. Things have gone out of hand and we need to utilize another instruction set. For that, use lsof command.
sudo lsof /var/lib/dpkg/lock-frontend
Analyze the output here. If you think, it will be resolved soon then wait. Otherwise, note down the Process ID:
sudo kill -9 <Process_ID>
And, remove the lock file:
sudo rm /var/lib/dpkg/lock-frontend
Lastly, in order for things to work:
sudo dpkg --configure -a
Run the following command to check if everything is in order:
sudo apt update
In conclusion, we have covered here how to get rid of error: Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend.