Although a Linux distribution might choose to install the software however it sees fit, more often than not, they can, at a minimum, install it from their official repositories. These repositories can be maintained directly by the distribution and/or based on others, as happens with Debian->Ubuntu->Linux Mint, for example. In these cases, the package manager is APT, and sometimes we can see the message "could not lock /var/lib/dpkg/lock" in the terminal or even in a tool with a graphical interface.
This article will try to shed some light. about what it is and how the error can be solved that causes us to see the message "/var/lib/dpkg/lock could not be locked", but I can tell you that there is a very simple solution that can fix this and many other problems, whether it is in a Linux-based distribution or any other other operating system, including mobile.
Table of Contents
What does the error "could not lock /var/lib/dpkg/lock" mean?
In general, when we see the error "could not lock /var/lib/dpkg/lock", the terminal or the software that shows it is indicating that another APT process is running and uses the same database. which we are trying to access. In other words, the new APT process tries to lock the /var/lib/dpkg/lock file, he can't get it because he's busy and inform us of it.
The most common is that another APT process, such as the update, is already running and has locked the lock file, so it cannot take the first step, which is precisely to lock said file.
How to fix the error
The first thing we have to do, especially if we don't like using the terminal, is check if there is any software application running. For example, in Ubuntu we can see if Ubuntu Software (fork of GNOME Software) is doing something, and also Software Update, the application that is in charge, as its name suggests, of updating the software. If we do not see any of those applications open, we can also start the system monitor and look for them, to see if they are running in the background.
Our applications that we have to look for will depend on the distribution that we are using, since Ubuntu Software is not in Kubuntu, nor is Discover in the main version of Debian. Each distribution manages the software with the tools it chooses to add, and they can block the GNOME Software archive, Discover, any other software store, or the update tool.
Whether they are in the foreground or in the background, one option is have patience and wait to see if the job you are doing is finished. Sometimes we see an error message and we want it to be fixed instantly, when the solution is to wait a moment.
A reboot or logoff should suffice
Many years ago, a friend was always asking me how to fix his crashes in Windows. There came a time when he almost didn't even ask, and he recommended Reiniciar because it was almost always the best solution for him. In the case of the error "could not lock /var/lib/dpkg/lock" is also a possible solution. When starting from scratch, you may start with the file locked by the update tool, but it will unlock it soon, when you verify that there is nothing new to install or there is, but you have already informed us about it.
Sure, it's not the most elegant solution, but it's the least expensive and may be the best and fastest if you don't have any reason to keep you logged in.
When the message "failed to lock /var/lib/dpkg/lock" does not go away
When we have restarted or waited for a while and we continue to see the message, what is happening to us is not something usual. The blocking has been "hung" or, worth the redundancy, blocked, so it is already worth pulling the terminal.
- The first thing we will do is open a terminal and identify the running APT process with this command:
sudo lsof /var/lib/dpkg/lock
- With the process identified, we kill it with this command, replacing PID with the number of the process that we will have found out with the command from step 1:
sudo kill PID
- Finally, we try again to carry out the APT management that the error was returning. It should allow us to move on.
If this does not solve it, we can do something more drastic, so you will have to take precautions or take extra steps. This is doing it "the hard way": removing the file /var/lib/dpkg/lock. The steps to follow to do it safely would be these:
- We make a backup of the file /var/lib/dpkg/lock. As what we will do is copy it, we can use the file manager, since super user permissions are not required. From the terminal it could be done with cp /var/lib/dpkg/lock new-path, changing "new-path" to the path in which we want to make the backup copy.
- We delete the file. For this it will be necessary to pull privileges. If our file manager allows us, we can write, for example, "sudo nautilus" without the quotes to remove it with a graphical interface, but I think it will be faster and easier to open the terminal and write:
sudo rm /var/lib/dpkg/lock
- With the file deleted we can try again what we were not allowed. if we write sudo apt update and we don't see the error, the bug is fixed.
Reboot and patience are usually best
Although there are ways to fix it, such as those explained in the previous point, the error "could not lock /var/lib/dpkg/lock" it's nothing really serious and it can be fixed by waiting for a while or by rebooting. It's something that usually fixes itself, and if it doesn't, a reboot is the least aggressive and most effective. Now, this is Linux, and everything or almost everything has a solution from the terminal, not to mention that we can "kill" whatever we want.
For whatever reason, I hope that what is stated here has been able to help you, either to make the message disappear with a more aggressive process or for you to have a little patience.
A comment, leave yours
This command works for me (tested on Xubuntu 18.04 LTS):
sudo fuser -vki /var/lib/dpkg/lock
It is also valid for other locked files, such as /var/lib/dpkg/lock-frontend or /var/lib/apt/lists/lock.