Recently, I encountered "Could not get lock /var/lib/dpkg/lock" error, I can neither install any packages nor update the system. This error is also closely related to the "Could not get lock /var/lib/apt/lists/lock" error. Below is some sample output on Ubuntu 20.04.
Reading package lists… Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 3620 (apt)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock directory /var/lib/apt/lists/
This can be very frustrating and often leaves you stuck unable to update, upgrade or install any packages.
*what is causing this error*
As the error suggests, another process is using the /var/lib/dpkg/lock or /var/lib/dpkg/lock file. This happens when you have 2 or more endpoints running a system update or upgrade. It can also happen when you cancel or otherwise update/upgrade prematurely.
If you encounter this error, there is absolutely no need to panic, let's explore some solutions.
*(1) Close all processes that are using the APT manager*
List processes using the apt package manager
$ ps aux | grep - i apt
Kill the process that is using the apt package manager
$ sudo kill -9 3619
$ sudo kill -9 3620
<