Fix “Unable to lock the administration directory (/var/lib/dpkg/)” in Ubuntu

While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or its derivatives such as Linux Mint (which I actually use as my primary operating system for doing daily work), you might have encountered the error – “unable to lock the administration directory (/var/lib/dpkg/) is another process using it” on the command line.

This error can be so annoying especially for new Linux (Ubuntu) users who may not know exactly the cause of the error.

Below is an example, showing the lock file error in Ubuntu 16.10:

[email protected]:~$ sudo apt install neofetch
[sudo] password for tecmint:
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?

The output below is another possible instance of the same error:

E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/ 
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?

How can you solve the above error in case you bump into it in the future? There are several ways of dealing with this error(s), but in this guide, we will go through the two easiest and probably the most effective ways of solving it.

1. Find and Kill all apt-get or apt Processes

Run the command below to generate a list of all processes whose name comprises of the word apt, you will get a list inclusive of all apt or apt-get processes by using ps and grep commands together with a pipeline.

$ ps -A | grep apt
Find apt and apt-get Processes

Find apt and apt-get Processes

For each apt-get or apt process that you can see in the output of the command above, kill each process using the command below.

The process ID (PID) is found in the first column from the screenshot above.

$ sudo kill -9 processnumber
OR
$ sudo kill -SIGKILL processnumber

For instance, in the command below where 9 is the signal number for the SIGKILL signal, will kill the first apt process:

$ sudo kill -9 13431
OR
$ sudo kill -SIGKILL 13431

2. Delete the lock Files

A lock file simply prevents access to another file(s) or some data on your Linux system, this concept is present in Windows and other operating systems as well.

Once you run an apt-get or apt command, a lock file is created under the any of these directories /var/lib/apt/lists/, /var/lib/dpkg/ and /var/cache/apt/archives/.

This helps to avoid the apt-get or apt process that is already running from being interrupted by either a user or other system processes that would need to work with files being used by apt-get or apt. When the process has finished executing, the lock file is then deleted.

Important: In case a lock is still exiting in the two directories above with no noticeable apt-get or apt process running, this may mean the process was held for one reason or the other, therefore you need to delete the lock files in order to clear the error.

First execute the command below to remove the lock file in the /var/lib/dpkg/ directory:

$ sudo rm /var/lib/dpkg/lock

Afterwards force package(s) to reconfigure like so:

$ sudo dpkg --configure -a

Alternatively, delete the lock files in the /var/lib/apt/lists/ and cache directory as below:

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

Next, update your packages sources list as follows:

$ sudo apt update
OR
$ sudo apt-get update

In conclusion, we have walked through two important methods to deal with a common problem faced by Ubuntu (and its derivatives) users, while running apt-get or apt as well as aptitude commands.

Do you have any other reliable methods to share meant for deal with this common error? Then get in touch with us via the feedback form below.

In addition, you may as well want to learn how to find and kill running processes and read through a simple guide to kill, pkill and killall commands to terminate a process in Linux.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

90 Responses

  1. Kalyan says:

    Thanks, It worked for me

  2. jason chu says:

    Great post, thanks!

    Ran into issue

    fatal: [192.168.56.xxx]: FAILED! => {“changed”: false, “msg”: “‘/usr/bin/aptitude safe-upgrade’ failed: E: Could not open lock file /var/lib/dpkg/lock – open (13: Permission denied)\nE: Unable to lock the administration directory (/var/lib/dpkg/), are you root?\n”, “rc”: 255, “stdout”: “Reading package lists…\nBuilding dependency tree…\nReading state information…\nInitializing package states…\n”, “stdout_lines”: [“Reading package lists…”, “Building dependency tree…”, “Reading state information…”, “Initializing package states…”]}

    fatal: [192.168.56.xxx]: FAILED! => {“changed”: false, “msg”: “‘/usr/bin/aptitude safe-upgrade’ failed: E: Could not open lock file /var/lib/dpkg/lock – open (13: Permission denied)\nE: Unable to lock the administration directory (/var/lib/dpkg/), are you root?\n”, “rc”: 255, “stdout”: “Reading package lists…\nBuilding dependency tree…\nReading state information…\nInitializing package states…\n”, “stdout_lines”: [“Reading package lists…”, “Building dependency tree…”, “Reading state information…”, “Initializing package states…”]}

  3. tony says:

    thank you !!!

  4. dEV says:

    Thank you very much, I’m new to Ubuntu and almost about break the monitor.. You save it

  5. jean mouawad says:

    the only useful document on the web thank you very much

  6. Prasanth says:

    Thalaiva …Super..!

  7. kk says:

    Thanks a ton, it worked without errors for me..

  8. Dhanush Acharya says:

    thanks a lot, this worked !!!

Got something to say? Join the discussion.

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.