Ubuntu

How to Solve ‘E: Could not get lock /var/lib/dpkg/lock’ Error in Ubuntu

How to Solve ‘E: Could not get lock /var/lib/dpkg/lock’ Error in Ubuntu

If you are a regular Ubuntu/ Debian user, you might have, at some point in time, come across the error: ‘Could not get lock /var/lib/dpkg/lock’. This happens to be related to the error ‘Could not get lock /var/lib/apt/lists/lock’. Below is a screenshot displaying such an error.

This is a non-fatal error and is usually not a major cause of concern. It can easily be resolved as we shall later demonstrate. But what causes it in the first place? Let’s find out.

Causes of the error

The common cause of this error is when a process is using the APT package manager to update/upgrade/install software or perform some package management on the system. When this happens, the process locks the dpkg file using a lock file such that another process doesn’t alter the data which is likely to lead to errors and possible corruption of crucial files in the system.

Whenever you encounter this error, there a high chance that there is another simultaneous process running APT. This could be a process running concurrently on another terminal. The error can also occur due to an interrupted update or upgrade process which was prematurely terminated by pressing CTRL + C on the terminal or accidental closure of the terminal window.

How to resolve the Could not get lock /var/lib/dpkg/lock error

Here are a few tips that can help remediate the error and help you get back to using the APT package manager.

Wait for the process to finish

If you have another terminal session where the APT package manager is being used by a command such as updating or upgrading the system or installing an application, just allow the operation to finish successfully. After completion, you can then run the command you want and perform any other operation using APT.

Terminate processes using the APT package manager

If you interrupted a process using APT such as canceling an upgrade or an update of the package index, first identify the processes using APT using the ps command shown:

$ ps aux | grep -i apt

From the output, we can see that APT is used by two processes started by root. The processes bear the PIDs 3994 and 3999. Once you have identified the processes using APT, the next course of action is to kill or terminate the processes.

To achieve this, use the kill command as follows.

$ kill -9 PID

From the output, we will kill the processes bearing the PIDs of 3994 and 3999 as follows:

$ kill -9 3994
$ kill -9 3999

The -9 flag triggers a SIGKILL signal which terminates a process immediately without allowing it to exit gracefully.

A much simpler way is to use the killall command as shown.

$ killall apt apt-get

Once you have killed the problematic processes, you can now proceed to use the APT package manager in your command.

Remove the lock files

The other fix you can apply is to remove the lock files. As earlier discussed, the lock files prevent the access of data by two different processes. To get rid of the lock files, run the following commands.

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

And finally, reconfigure the packages.

$ sudo dpkg --configure -a

Conclusion

Any of these solutions will help you navigate around this error and let you use the APT package manager for other operations. Do let us know what worked for you.

Similar Posts