How to fix the ‘could not get lock’ error in Ubuntu

This post will show you how to fix the “Could not get lock” or “unable to lock” error in Ubuntu.

Here is an example of this error when Ubuntu could not get a lock on the “/var/lib/dpkg/lock” resource:

E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)

In this example, the error states that Ubuntu is unable to lock the administration directory:

E: Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

The last example shows Ubuntu is unable to get a lock on the “/var/lib/apt/lists/” directory:

E: Unable to lock directory /var/lib/apt/lists/ 
YouTube player

What causes this error?

This error occurs when using the apt or apt-get command and:

  • it cannot get access to packages due to a broken internet connection (mid-install/update)
  • something else is installing or removing software

How to fix this error?

Step 1 – Find and stop the apt or apt-get process

Run the following command to kill all apt or apt-get processes:

sudo killall apt apt-get

This will show the process of running apt or apt-get.

Step 2 – Delete the locks.

Delete the locks if step 1 did not work:

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

What is a lock file?

Ubuntu uses lock files to prevent two or more processes from using the same data. Both apt and apt-get use lock files to prevent the processes from sharing data. Other operating systems like Windows do the same and will only allow you to add or remove a single program at a time. Lock issues occurred when the previous apt or apt-get command did not terminate properly.

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.