How to fix: “Could not get lock /var/lib/dpkg/lock”

One of the common issues that most probably every Linux user has faced is to get the following message:

root@cb:~# apt-get upgrade
E: Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

Okey, let’s check in what files are in: /var/lib/dpkg

root@cb:/var/lib/dpkg# ls
alternatives available cmethopt diversions info lock parts statoverride status status-old tmp.ci triggers updates

Most often cause for this issue is that some other process is currently using the Advanced Package Tool, or apt. For this reason, lock is placed while apt/apt-get process is not yet completed, and once it is finished – the lock is removed.

So let check the apt processes which are currently running:

root@cb:/var/lib/dpkg# ps aux | grep apt
root 1946 0.0 0.1 4628 724 ? Ss 15:18 0:00 /bin/sh /usr/lib/apt/apt.systemd.daily install
root 1974 0.0 0.3 4628 1588 ? S 15:18 0:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held install
root 20944 2.4 5.2 45848 25084 pts/1 Ds+ 15:26 0:00 /usr/bin/dpkg –status-fd 10 –no-triggers –unpack –auto-deconfigure –recursive /tmp/apt-dpkg-install-iAN9eb
root 20973 0.0 0.2 13136 1120 pts/0 S+ 15:27 0:00 grep –color=auto apt

Okey, so we will kill the processes stated above (we use SIGKILL signal, so they terminate immediately):

root@cb:/var/lib/dpkg# kill -9 1946
root@cb:/var/lib/dpkg# kill -9 1974
root@cb:/var/lib/dpkg# kill -9 20944

Let’s give another try to install newer versions of the packages we have:

root@cb:/var/lib/dpkg# apt-get upgrade
E: dpkg was interrupted, you must manually run ‘dpkg –configure -a’ to correct the problem.

Follow the instructions provided in the output:

root@cb:/var/lib/dpkg# dpkg –configure -a
dpkg: dependency problems prevent configuration of linux-generic:
linux-generic depends on linux-headers-generic (= 4.15.0.36.38); however:
Version of linux-headers-generic on system is 4.15.0.32.34.

Okey, once again we try to upgrade the packages:

root@cb:/var/lib/dpkg# apt-get upgrade
Reading package lists… Done
Building dependency tree
Reading state information… Done
You might want to run ‘apt –fix-broken install’ to correct these.
The following packages have unmet dependencies:
linux-generic : Depends: linux-headers-generic (= 4.15.0.36.38) but 4.15.0.32.34 is installed
E: Unmet dependencies. Try ‘apt –fix-broken install’ with no packages (or specify a solution).

The output above states that there are “unmet dependencies”, which may be caused of broken package database or some package may be installed incorrectly.

root@cb:/var/lib/dpkg# apt –fix-broken install
Reading package lists… Done
Building dependency tree
Reading state information… Done
Correcting dependencies… Done
The following packages were automatically installed and are no longer required:
linux-headers-4.15.0-20 linux-headers-4.15.0-20-generic linux-image-4.15.0-20-generic linux-modules-4.15.0-20-generic linux-modules-extra-4.15.0-20-generic
Use ‘apt autoremove’ to remove them.
The following additional packages will be installed:
linux-headers-4.15.0-36 linux-headers-4.15.0-36-generic linux-headers-generic
The following NEW packages will be installed:
linux-headers-4.15.0-36-generic

Perfect, now let’s run the upgrade command:

root@cb:/var/lib/dpkg# apt-get upgrade
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
The following packages were automatically installed and are no longer required:
linux-headers-4.15.0-20 linux-headers-4.15.0-20-generic linux-image-4.15.0-20-generic linux-modules-4.15.0-20-generic linux-modules-extra-4.15.0-20-generic
Use ‘apt autoremove’ to remove them.
The following packages will be upgraded:
apport base-files bind9-host cloud-initramfs-copymods cloud-initramfs-dyn-netconf console-setup console-setup-linux cryptsetup cryptsetup-bin dnsutils gcc-8-base
grub-common grub-pc grub-pc-bin grub2-common initramfs-tools initramfs-tools-bin initramfs-tools-core keyboard-configuration libbind9-160 libcryptsetup12 libdns1100
libgcc1 libirs160 libisc169 libisccc160 libisccfg160 liblwres160 libplymouth4 libpython3.6 libpython3.6-minimal libpython3.6-stdlib libstdc++6 open-iscsi
open-vm-tools overlayroot plymouth plymouth-theme-ubuntu-text python3-apport python3-distupgrade python3-problem-report python3-software-properties
python3-update-manager python3.6 python3.6-minimal software-properties-common ubuntu-release-upgrader-core update-manager-core
48 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Setting up python3-software-properties (0.96.24.32.5) …
Setting up initramfs-tools-bin (0.130ubuntu3.5) …
Processing triggers for dbus (1.12.2-1ubuntu1) …
Setting up console-setup-linux (1.178ubuntu2.7) …

There are some packages which are no longer required so we remove them:

root@cb:/var/lib/dpkg# apt autoremove
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages will be REMOVED:
linux-headers-4.15.0-20 linux-headers-4.15.0-20-generic linux-image-4.15.0-20-generic linux-modules-4.15.0-20-generic linux-modules-extra-4.15.0-20-generic
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 335 MB disk space will be freed.

Great, you are ready to proceed.