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

Introduction

The error Could not get lock /var/lib/dpkg/lock is a frequent Ubuntu error. It occurs when the system tries to execute several commands that need access to the same file, usually the /var/lib/dpkg/lock file.

The /var/lib/dpkg/lock file plays a crucial role in Ubuntu's apt package management system and is responsible for installing, upgrading, and removing packages on the operating system. If this file is locked or inaccessible, it impedes essential updates and installations, creating the error.

Use this guide to resolve the Ubuntu Could not get lock /var/lib/dpkg/lock error.

How to Fix `Could not get lock /var/lib/dpkg/lock' Error Error On Ubuntu

Prerequisites

  • Access to the terminal.
  • A user account with sudo or root privileges.
  • Ubuntu system (this tutorial uses Ubuntu 22.04).

What Causes Could not get lock Ubuntu Error

The Could not get lock /var/lib/dpkg/lock error occurs when two system processes attempt to access the /var/lib/dpkg/lock file simultaneously.

The error has several different forms, but the most common one is:

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?
Could not get lock /var/lib/dpkg/lock error message

Another error form that sometimes appears in newer Ubuntu versions is:

Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by the process 5017 (apt)
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend error in newer Ubuntu versions

The error also appears in this form:

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?
Could not get lock var/lib/dpkg/lock-frontend - open error message

Common error causes include:

  • Corrupted package manager files. Corruption of critical files within the package management system leads to the inability to access or modify these files. This happens due to software issues, file system problems, or other errors.
  • Simultaneous package manager operations. When multiple package managers (such as apt, apt-get, or dpkg) attempt to run simultaneously, they conflict with each other for access to the same lock file. Users trigger this error when trying to install or update several packages at the same time. The package management system locks files to prevent conflicts during these operations.
  • Background processes using the package manager. Conflicts arise if a background process uses a package manager while a user attempts to run it from the terminal. Background processes include automated updates or other system tasks.
  • Incomplete or interrupted operations. The error occurs if a previous installation or update process was interrupted or not completed successfully. This happens due to power loss, system shutdowns, or manual process cancellation.

How to Fix Could not get lock Ubuntu Error

The Could not get lock /var/lib/dpkg/lock error is closely linked to Ubuntu's update mechanism, specifically when using the dpkg or apt service for installing or updating packages. When Ubuntu starts, the update service initiates by default, and it employs the package manager to check for and install updates.

The service implements a self-locking mechanism (lock files) to ensure the two processes do not attempt to update the system content simultaneously. The error arises when the service cannot obtain the necessary lock to access critical files.

Encountering the error hinders crucial operations like installing or updating packages, potentially leaving the system with outdated software or security vulnerabilities. The following text presents several ways to fix the Could not get lock error.

Method 1: Check Running Applications

Check if another program is currently performing a system update or installing a new application. Specifically, investigate applications like Software Updater or any package managers to ensure they are not actively running any update or installation processes.

Additionally, review all open terminal windows to confirm no ongoing updates or installations are taking place. If such operations exist, wait for them to complete before proceeding.

Method 2: Wait it Out

Sometimes, the error is a transient issue that resolves itself with time. Wait up to 10 minutes after encountering the error before attempting the software installation again.

This period allows any ongoing background processes related to package management to be completed, freeing up the lock and facilitating the installation experience.

Method 3: Reboot

If the error persists, a straightforward solution is to reboot the machine. Restarting the system clears any lingering lock issues and provides a clean slate for subsequent installation attempts.

To restart Ubuntu, run the following command:

reboot
terminal output for reboot

The command has no output, but it restarts the machine. After rebooting, try the software installation again to check if the Could not get lock /var/lib/dpkg/lock error has been resolved.

Method 4: Clear Cache

Sometimes, the error message stems from interrupted installations or update processes. When a package is being installed or updated, the package management system downloads the necessary files and stores them in a cache before applying the changes to the system.

If an installation or update process is interrupted, it leaves behind incomplete or corrupted cache files. Therefore, one way to fix the Could not get lock /var/lib/dpkg/lock error is to clear old cache files.

To accomplish this, enter:

sudo apt clean
sudo apt clean terminal output

The command has no output, but the process removes all files stored in the cache directory at /var/cache/apt/archives/. This step mitigates potential conflicts caused by incomplete or disrupted package management operations and enhances the likelihood of resolving the error.

Method 5: Check for Software Updates

When trying to fix the error, address the possibility the issue is related to outdated or vulnerable software. This is a proactive way to ensure the system is running the latest and most secure installed software components versions.

Run the following command to ensure the repository has the latest software available:

sudo apt update
sudo apt update terminal output

Next, upgrade the software to the latest versions with:

sudo apt upgrade
sudo apt upgrade terminal output

Method 6: Address Automatic Updates

Ubuntu automatic updates incorporate the latest software enhancements and security patches. However, these automated processes occasionally clash with manual package management tasks, resulting in the Could not get lock error. To mitigate such conflicts, adjust automatic updates.

The steps to modify update settings are:

1. Click Show Applications (the nine-dot icon) in the bottom-left corner.

Clicking on Show Applications

2. Go to Search.

3. Type Updates.

4. Open the Software & Updates application.

Open Software & Updates application

5. Navigate to the Updates tab.

Navigate to the Updates tab

6. Customize the settings for automatic updates. Specify the update frequency and whether to install security updates automatically. There is an option to turn them off temporarily to check whether they are the ones causing the error.

However, enabling automatic security updates is generally recommended for maintaining system security. This ensures that critical security patches are applied without manual intervention. Optimally, set them to be less frequent or to notify instead of automatically downloading.

Method 7: Examine Running Processes

If a prior update or installation process is still in progress, it might retain control of the lock, causing the Could not get lock /var/lib/dpkg/lock error. With this method, users can pinpoint the exact processes contributing to the lock conflict.

Take the following steps to examine the running processes:

1. Check apt running processes. Use the following command to list the running processes associated with apt:

ps aux | grep -i apt
List running processes apt terminal output

If any applications are using apt, they appear on the list. The output shows several processes related to the apt command, specifically an ongoing upgrade operation. Users sometimes also encounter daily update messages. This means the system is running normal updates.

Daily updates terminal output

In both cases, wait for this process to finish or terminate unwanted processes.

2. Check dpkg services. Examining ongoing dpkg processes is crucial in preventing conflicts between simultaneous operations that result in the Could not get lock /var/lib/dpkg/lock error. This error commonly arises when conflicting processes attempt to access the Debian Package Manager (dpkg) concurrently.

To check for running dpkg processes, run:

ps aux | grep -i dpkg
list running dpkg processes terminal output

The dpkg output prints running processes.

Method 8: Check for Running Package Managers

Same as the error message, lock files come in several forms:

  • /var/lib/dpkg/lock
  • /var/lib/dpkg/lock-frontend
  • /var/lib/apt/lists/lock
  • /var/cache/apt/archives/lock

These files are created to prevent two instances of apt or dpkg from using the same files at the same time. To check files currently open by apt or dpkg, use the lsof command. The syntax is:

sudo lsof [lock_file_name]

For instance, check whether the /var/lib/dpkg/lock file is open with:

sudo lsof /var/lib/dpkg/lock
lsof terminal output

The command has no output, meaning that the file is not open. If the file is used by one of the services, the output returns the process ID (PID). In that case, address the service as described in the next method.

Method 9: Address the Stuck Service

If an unwanted apt or dpkg service is running, terminate it using the PID and see if it resolves the error. Use the following command:

sudo kill [PID]

For example, run the following to determine the PID:

ps aux | grep -i dpkg

In this example, the dpkg-query -l process is running. The PID is in the second column, and the service name is in the last column.

dpkg PID

To terminate the process, enter:

sudo kill 2636
sudo kill 2636 terminal output

The command has no output. To verify the process terminated, run the following command again:

ps aux | grep -i dpkg
ps aux | grep -i dpkg terminal output

The output doesn't list the process anymore. If the service is still running, force the process to stop by adding the -9 option:

sudo kill -9 [PID]
sudo kill 2636 -9 terminal output

The command has no output but should have resolved the issue.

Method 10: Delete Lock Files

If everything else fails, delete the lock files. To accomplish this, use the rm command:

For instance, delete the /var/lib/dpkg/lock file with:

sudo rm /var/lib/dpkg/lock
Deleting lock file via sudo rm /var/lib/dpkg/lock

Remove the /var/lib/apt/lists/lock file with the same command:

sudo rm /var/lib/apt/lists/lock
Removing the /var/lib/apt/lists/lock file

The rm command also works on /var/lib/dpkg/lock-frontend lock file. Run the following:

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

Lastly, delete the /var/cache/apt/archives/lock with:

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

The commands have no output, but removing these files fixes the error.

After removing lock files, fix broken packages because it helps complete interrupted installations, resolves dependencies, and ensures the package management system is in a coherent and functional state. Doing so reduces the likelihood of encountering the Could not get lock /var/lib/dpkg/lock error.

To accomplish that, run the following command:

sudo dpkg --configure -a
sudo dpkg --configure -a terminal output

Conclusion

After reading this article, you know how to fix the E: Could not get lock /var/lib/dpkg/lock error on Ubuntu using several different methods.

Next, read about another common Ubuntu error message, Sub-process /usr/bin/dpkg returned an error code (1), which could indicate a problem with the package installer.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
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
December 13, 2023

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
December 7, 2023

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