If you are a frequent user of Ubuntu, 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'.
Basically, this is a non-fatal error and is usually not a major cause of concern.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform fixes to related Ubuntu errors.
In this context, we shall look into how to tackle this Ubuntu Linux system error.
Causes of Ubuntu "E: Could not get lock /var/lib/dpkg/lock" 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 fix Ubuntu "E: Could not get lock /var/lib/dpkg/lock" error ?
You can apply the tips below to resolve this error and also help to get back to using the APT package manager.
1. 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.
2. 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, you will see that APT is used by two processes started by root. The processes might bear the PIDs such as 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.
3. Remove the lock files
The other fix you can apply is to remove the lock files. As earlier mentioned, 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
[Need assistance in fixing Linux system processes locked files issues? We can help you. ]