How to Fix Could not get lock /var/lib/dpkg/lock Error on Ubuntu

Introduction

In Ubuntu, you may sometimes encounter an error when attempting to run an apt command:

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 message lets you know that dpkg, the Debian Package Manager service, is unavailable.

Use the options in this guide to resolve the Ubuntu "Could not get lock…" error.

tutorial header for Could not get lock /var/lib/dpkg/lock Error in Ubuntu

Prerequisites

  • Access to a terminal window / command line
  • A user account with sudo or root privileges

5 Options to Fix "Could not get lock" Ubuntu Error

By default, the update service launches when Ubuntu starts. The automatic updater uses dpkg to check and install updates.

The dpkg service locks itself so that two processes don’t update the content simultaneously. The service is locked to avoid potential corruptions in the system. However, it also means that the user cannot, for example, run a simple apt command.

1: Wait it Out or Reboot

Give the system up to 10 minutes after you see the error, then try your software installation again.

If you’re certain that nothing is being installed, you can reboot the machine and try again.

To adjust the settings for automatic updates:

1. Click Activites > Search.

2. Type Updates.

3. Click the icon for Software & Updates.

4. Use the check boxes and drop-down menus to select your preferred update schedule.

screenshot of preferred Ubuntu update schedule and options

Note: We recommend keeping automatic updates turned on. Optionally, you can set them to be less frequent or to notify you instead of automatic downloading.

2: Examine the Running Processes

1. If you already tried waiting and rebooting the system, and you still get the error, use the following command to see which installation services are running:

ps aux | grep -i apt

2. If there are any applications using apt, they will appear on the list. Look for an entry similar to:

/usr/lib/apt/apt.systemd.daily update
output showing applications using apt manager

3. The daily update message means your system is running normal updates. You have to wait for this process to finish, but no other action is required.

You may have another apt process that’s running:

root    2810  0.0    0.0    72948 4312  pts/0  S+    15:03  0:00   sudo apt-get remove logstash

The most important columns are the second column, which lists the PID (process ID), and the last column that displays the service that’s using apt.

4. Alternately, you may have a dpkg service that’s still running. To find it, use the following command:

ps aux | grep -i dpkg

The dpkg output will look similar to the apt output.

list of services using dpkg

3: Address the Stuck Apt Service

1. If you have a mystery apt or dpkg service running, terminate it and see if that resolves your error. Use the following command:

sudo kill 8808

2. Replace 8808 with the actual process ID (PID) from Step 2. It should complete and return to a new command prompt line. If it doesn’t, force the process to stop by adding the -9 option:

sudo kill -9 8808

This should have resolved the issue.

4: Delete Lock Files

The error message in Ubuntu may appear similar to the following:

/var/lib/dpkg/lock

/var/lib/dpkg/lock-frontend

/var/lib/apt/lists/lock

/var/cache/apt/archives/lock

These are lock files, which are created to prevent two instances of apt or dpkg from using the same files at the same time. This can happen if an installation is interrupted or did not complete. Remove the lock files at your own risk.

To delete the lock files, use the rm command:

sudo rm /var/lib/dpkg/lock

sudo rm /var/lib/apt/lists/lock

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

Removing these files should fix the error.

5: Reconfigure dpkg

1. Run the --configure command if you just deleted the lock files. Also, this command will help if you receive the following error:

“dpkg was interrupted, you must manually run ‘sudo dpkg –configure -a’ to correct the problem.”

2. Enter the command as displayed above:

sudo dpkg --configure -a

Output returns a new line and the command should resolve the issue.

Fix Could not get lock /var/lib/dpkg/lock-frontend Error

The steps for fixing this error follow the similar pattern in the methods we described above. The full error is:

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

E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

1. Do not remove the lock files immediately. Before doing so, locate and kill all processes that may be using the files.

sudo lsof /var/lib/dpkg/lock-frontend

Note: Check out our guide to learn more about lsof command in Linux.

2. The output returns a result similar to:

lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
lsof: WARNING: can't stat() fuse file system /run/user/1000/doc
      Output information may be incomplete.
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
unattende 1127 root    2oW  REG    7,2        0 165231 /var/lib/dpkg/lock-frontent

3. If you see unattended under the command column, your system is installing a security update. Wait for the process to complete.

4. Otherwise, note all process IDs and kill them with the force option:

sudo kill -9 process_id

5. Once you do that, you can try removing the lock-frontend files.

sudo rm /var/lib/dpkg/lock-frontend

This should fix the error.

6. The last thing you can try after deleting the lock files is reconfiguring dpkg as we showed in the fifth section.

sudo dpkg --configure -a

To test if these steps fixed the error, run the update command:

sudo apt update

When the process completes successfully, that shows you fixed the “E: Could not get lock /var/lib/dpkg/lock” error.

Note: The error we tackled has multiple different forms, but the methods for fixing it are similar. Some of the variations of the “Could not get lock…” error are:

E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)

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

and

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?

Conclusion

This article explains five options to fix the “E: Could not get lock /var/lib/dpkg/lock” error on Ubuntu.

One of the options above will resolve the issue and chances are that the error will resolve itself upon rebooting the system.

Another common Ubuntu error message "Sub-process /usr/bin/dpkg returned an error code (1)" could indicate a problem with the package installer.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Upgrade to Ubuntu 20.04
May 7, 2020

Ubuntu 20.04 (Focal Fossa) is the newest Ubuntu LTS release. You can upgrade to the latest release from an...
Read more
How to Grep for Multiple Strings, Patterns or Words
May 5, 2020

This tutorial lists multiple examples on how you can use the grep tool to search for multiple string patterns...
Read more
How to Kill a Process in Linux? Commands to Terminate
April 12, 2019

If a Linux process becomes unresponsive or is consuming too many resources, you may need to kill it. Most...
Read more
How to Update Linux Kernel In Ubuntu
October 22, 2018

The Linux kernel is like the central core of the operating system. It works as sort of a mediator, providing...
Read more