How to fix lock for apt in Linux

Hi guys, So last night I was working with my Linux and then I realized that there are updates available for it, so I ran the command apt upgrade which surprisingly showed an error something like "could not get lock /var/lib/dpkg/lock", and then I started searching for a solution and the solution I found is the one I am sharing with you here. Source for the solution is Ask ubuntu.
Terminal

Why this happened?

This could happen if there was an apt process running and halted in the middle,  which left the apt in a locked condition. By the way a lock is a file that is acquired every time you run an apt command so that no other process can use it or interrupt it while apt is manipulating the packages which can leave them in a corrupt state if interrupted.

So we have two options here, Let's start with the first...

Kill The Process

This is probably the safest option, but make sure there is no package manipulation process running in the middle, if there is please wait for it to complete, but if it is stuck then you have no choice but to kill the process.

Open your terminal and run this command:
ps aux | grep apt
this will show you all the running process that contains the word apt. You need to kill the rouge process that is running:
kill processnumber 
 change processnumber with the actual process number. If this does not work try:
kill -9 processnumber
Terminal apt

Delete the Lock

This should be used as last resort. If you use this carelessly you can end up with a broken system.

You can delete the lock file with the following command:
sudo rm /var/lib/apt/lists/lock
You may also need to delete the lock file in the cache directory
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
sudo dpkg --configure -a
Hopefull it should work after this...

And that's it guys. Hope you likes it. And please like us and share us on social media. Thank you.

No comments:

Post a Comment

Popular Posts