Linux

How to fix Unable to lock the administration directory /var/lib/dpkg/

This post will discuss what this error means and the various methods and techniques we can use to resolve it.
Captain Salem 3 min read
How to fix Unable to lock the administration directory /var/lib/dpkg/

Package management is the underlying foundation of a functional Linux distribution. Package managers such as apt, dpkg, yum, Pacman, etc. allow you to install, uninstall, update, and reconfigure all the packages in your system.

However, when working with the apt package manager, you might encounter the error message:

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

What causes the could not get a lock error when running the apt or dpkg command?

Let us start with the basics and describe this error and its causes. This error occurs when the dpkg service is unavailable.

If another process runs the apt or the dpkg command, it locks the Debian Package Manager until that process is complete. This occurs when installing, uninstalling, or updating packages. This feature prevents multiple processes from making modifications in the system simultaneously, which can lead to package conflicts or break the system entirely.

Let us now see some basic techniques of how to resolve this issue.

Method 1 - Check Running Processes

As mentioned, this error occurs when another process uses the DPKG utility. You can examine the running processes for apt as:

ps aux | grep -i apt

The command returns an output as:

root      1064  0.0  0.0   4644   772 ?        Ss   19:08   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily update
root      1084  0.0  0.0   4644  1676 ?        S    19:08   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held update
_apt      2836  0.8  0.1  96912  9432 ?        S    19:09   0:03 /usr/lib/apt/methods/http
ubuntu  6172  0.0  0.0  21532  1152 pts/1    S+   19:16   0:00 grep --color=auto -i apt

The command shows the running process. In the example above, the systemd.daily.update message indicates that the system is checking and installing system updates. We recommend waiting for the process to finish.

You can also check for running dpkg commands as:

ps aux | grep -i dpkg

You should get. a similar output with the processes running dpkg.

Method 2 - Kill the Running Process

Sometimes, the apt/dpkg process may run for a suspiciously long time. You can terminate the running process with the kill command in such a case.

sudo kill <process_id>

You can get the process id by running the. ps command as demonstrated previously. Once terminated, you can check if the process has exited:

ps aux | grep apt

If the process is still running, you can force terminate by running the command:

sudo kill -9 <process_id>

You can also use the process name:

sudo killall apt apt-get

Method 3 - Removing the Lock Files

In extreme cases, the error may persist even after terminating the running processes. In such as case, you can result to removing the lock files.

As mentioned, the lock files prevent multiple apt or dpkg processes from running simultaneously. However, if a running process is corrupted or terminates prematurely, it can leave the lock files running. This makes the system 'think' there is an instance of an apt or dpkg process running.

To solve for that, run the rm command on the lock files as:

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

Finally, reconfigure dpkg with the command:

sudo dpkg --configure -a

Method 4 - Restart the System

The simplest and most common method is to reboot the system. This should force all the apt and dpkg processes to terminate and close gracefully.

Conclusion

In this post, we discussed resolving the 'unable to lock the administration directory /var/lib/dpkg' error in Ubuntu and Debian-based systems.

We hope this tutorial helped resolve your issue. If so, leave a comment below to let us know which method worked for you.

You can also reach out to us if you are unable to fix it.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.