Issue using Ubuntu server

I have started using the Ubuntu server on PuTTY. While there was a need to update in the server; during the execution of the command sudo apt-get update I have started facing the error of lock /var/lib/apt/lists/lock -open and could not lock directory /var/lib/apt/lists. How do I fix this?
P.S.Removing this package should be the worst case scenario.
Thanks and Regards

I have managed to solve the issue; further I am sharing the fix as it is a commonly faced issue.
Basically I encountered this issue because I unknowingly/accidentally closed the Ubuntu session while it was updating and an process was under execution in the recent past. This error can also arise due to other reasons too.
For the fix, open your Ubuntu server and use the lsof command to get the process ID of the process holding the lock files. Run these commands one by one.

  1. lsof /var/lib/dpkg/lock
  2. lsof /var/lib/apt/lists/lock
  3. lsof /var/cache/apt/archives/lock
    It’s possible that the commands don’t return anything, or return just one number. If they do return at least one number, use the numbers and kill the processes like this replace the PID with the numbers you got from the above commands:
    sudo kill -9 PID

Now to safely remove the lock files, run the following commands:

  1. sudo rm /var/lib/apt/lists/lock
  2. sudo rm /var/cache/apt/archives/lock
  3. sudo rm /var/lib/dpkg/lock

After the successful execution of above three commands we have to reconfigure the packages, to do this run the following command
sudo dpkg --configure -a
Now if you run the sudo apt get command everything should work smoothly so that you can install other packages in future.

1 Like

Hi @SiddharthBhandarkar,

Can you attach the screenshot of your error or the exact error so that other people can refer this?

@rahul.singh1 Since I have fixed the issue on my system I am uploading the same error message from the internet
E: Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
This error usually comes while the upgrade command is under execution

1 Like