Learn how to solve the cli error of could not get lock after trying to install a new package.

How to solve Kali Linux CLI error: Could not get lock /var/lib/dpkg/lock – open (9: Resource temporarily unavailable)

Repository errors are a common issue on debian based systems. In Kali Linux you will face the mentioned exception when trying to install some packages with apt-get:

E: Could not get lock /var/lib/apt/lists/lock – open (9: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/ 
E: Could not get lock /var/lib/dpkg/lock – open (9: Resource temporarily unavailable) 
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

The issue is basically caused by the lock files. Lock files are used to prevent two or more processes from using the same data. When apt or apt-commands are run, it creates lock files in a few places. When the previous apt command was not terminated properly, the lock files were not deleted and hence they prevent any new instances of apt/apt-get commands.

Solution

To fix the problem, all you need to do is to remove the lock files of dpkg. Execute the following commands in the following order:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

After removing the locks, run the following command to reconfigure dpkg:

sudo dpkg --configure -a

Now if you try to run the command that was causing the trouble, you should be able to execute it without any problem.

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors