Skip to main content

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.

Comments

Popular posts from this blog

How to Fix Bluetooth not Working in Kali Linux

Hello everyone, how are you guys? Good? Cool. Quick question, Do you like our new theme? Let us know in the comments. so this article is for you if you are running Linux and your Bluetooth is not working. So I am here to help you fix that. It will be up and running in no time.

How to Flash TWRP on Samsung Galaxy Star Advance (SM-G350E)

Hi guys, I am back with another flashing guide. I had this Samsung phone lying around, so I thought about experimenting on it with custom rom. So I started searching for custom recovery, and I find out that this device is not officially supported by TWRP, but I got my hands on unofficial build of TWRP. Well I have to tell you, flashing TWRP on this device wasn't an easy task (mainly because, their isn't any simple guide available).

How to Fix Linux Boot stuck at initramfs

Last night I was using linux mint and it was working fine until I rebooted my pc and saw this And I was like What? What the hell is that? And mint wasn't booting up. So I did what we all do. Yup, Google! And I found this working solution. Now you must be wondering if there is already a solution available on the internet why the hell did you write this post? Mainly because it was all scattered around in pieces and I wanted to put it all together. Now let's fix this.