Debian 9: Avoid "Could not get lock /var/lib/dpkg/lock" on boot

This article will describe a way to avoid "Could not get lock /var/lib/dpkg/lock" on boot (and periodically).

1 Could not get lock /var/lib/dpkg/lock

This error will be caused because other process running dpkg operation like apt command.

$ sudo apt install -y <package>
<snip>
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource
temporarily unavailable)

2 apt-daily and unattended-upgrades

apt-daily and apt-daily-upgrades runs "apt update" and unattended-upgrade on boot (and periodically)

apt-daily.service Run "apt update" and "unattended-upgrade –download-only" on boot
apt-daily.timer Run "apt update" and "unattended-upgrade –download-only" periodically
apt-daily-upgrades.service Run "unattended-upgrade" on boot
apt-daily.timer Run "unattended-upgrade" periodically

unattended-upgrade downloads packages to be updated and update these packages.

unattended-upgrade –download-only Download packages to be updated
unattended-upgrade Update packages
unattended-upgrades.service Kill unattended-update process on shutdown

These are for automated package update. These are useful for desktop and server. But these will lock /var/lib/dpkg/lock.

There is some cases that locking /var/lib/dpkg/lock automatically will be problem. For example, when you try to clone base virtual machine and install mail server to it on boot, locked /var/lib/dpkg/lock will prevent installation mail server.

3 Avoid "Could not get lock /var/lib/dpkg/lock"

So stop apt-daily and apt-daily-upgrades will avoid "Could not get lock /var/lib/dpkg/lock".

$ sudo systemctl mask apt-daily.service
$ sudo systemctl disable apt-daily.timer
$ sudo systemctl mask apt-daily-upgrade.service
$ sudo systemctl disable apt-daily-upgrade.timer