Ubuntu with /var/lib/dpkg locked

Error:
“Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable)”

As soon as you have your Ubuntu 16.04 machine up provisioned by either Ansible or Vagrant, it starts a process for unattended-updates that locks the dpkg repository and any script that needs to send instructions for apt app, will fail.

This appears to be caused by a combination of an unattended-upgraded package policy set by the Debian/Ubuntu, and a change in how unattended upgrades are handled now.

In past versions of Ubuntu, unattended package upgrades were handled by creating a cron job in /etc/cron.daily responsible for running apt. With 16.04 version and the shift to systemd, unattended upgrades are handled by a systemd unit. The new systemd unit has OnCalendar set to 6am and 6pm, along with Persistent set to true. The result is that a full apt-get update && apt-get upgrade will run the first time the system boots, in order to “catch-up” with missed runs.

The idea that packages will be upgraded automatically twice a day, so what you can do is to disable the automatic apt upgrades at all.

How to disable apt automatic updates:

You have to edit this file:

 

Change the line:

 

To:

 

Edit this file:

 

Change these lines:

 

To:

 

Just remember to set the automatic updates ON again after your provision in case this machine is a server.

Leave a Reply

Your email address will not be published. Required fields are marked *