The “could not get lock /var/lib/dpkg/lock” error is pretty common in Debian/Ubuntu running in virtual machine. Existing workarounds on the Internet (i.e. kill the apt-get process, or remove the lock file) are not ideal, because they can damage your system and you will face the same error again in the future.

Fix “could not get lock /var/lib/dpkg/lock” Error Once and For All

I will tell you the real cause of this error and show you how to fix it once and for all. The error message is as follows:

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?

The error message already gives you a hint of the cause of the problem: another process is using /var/lib/dpkg/. This is because Ubuntu by default enables unattended upgrades for security update.

When your Ubuntu OS finishes booting, it automatically runs the apt-get update command. If there’s security updates available, it will install them in the background. So if you run sudo apt upgrade command at the same time, the above error is shown. Because you don’t use Ubuntu in Virtualbox or VMware workstation as often as you use Ubuntu on bare metal, your Ubuntu virtual machine rarely gets updated, so you see this error more often in virtual machine.

Whether you use Ubuntu desktop on bare metal or in virtual machine, you can disable unattended upgrades to solve this problem once and all.

Note: This error rarely happens on Ubuntu server and it’s recommended that you enable unattended upgrades on servers to install security updates automatically.

Here’s how. Open Software & Updates from your application menu.

Then click updates tab. By default, security updates are downloaded and installed automatically. You can change it to display immediately, so it won’t run sudo apt-get upgrade command in the background.

Then enter your user password for this change to take effect.

After that, reboot your computer.

Disable Unattended Upgrades From Command Line

You can also achieve the same results from the command line. In your terminal, open the /etc/apt/apt.conf.d/20auto-upgrades file with a command line text editor like nano.

sudo nano /etc/apt/apt.conf.d/20auto-upgrades

The original content is as follows:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "0";
APT::Periodic::Unattended-Upgrade "1";

To disable automatic security update, change them to

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "0";
APT::Periodic::AutocleanInterval "0";
APT::Periodic::Unattended-Upgrade "0";

To save the file in Nano text editor, press Ctrl O, then press Enter to confirm the file name. Press Ctrl X to exit. Then reboot your computer.

Update

A reader told me that after following the above instruction, the error message can still be seen if he run sudo apt update command immediately after Ubuntu finishes starting up. This is because the background “apt update” hasn’t finished yet.

If you are a person who often update packages manually, I would recommend setting the “automatically check for updates” to weekly instead of daily. To really fix the error message once and for all, set the value to Never.

When Does Ubuntu Perform Unattended Upgrades?

Ubuntu performs unattended upgrades after system boot. Also Systemd can perform unattended upgrades. The apt-daily.service is used to update package list daily and apt-daily-upgrade.service is used to download and install security updates daily. You can check their status with:

systemctl status apt-daily.service

systemctl status apt-daily-upgrade.service

apt-daily.service is controlled by apt-daily.timer. A timer is like a cron job, executing a systemd service with the same name at specified times. The apt-daily.timer‘s configuration file is /lib/systemd/system/apt-daily.timer. You can check its content with:

cat /lib/systemd/system/apt-daily.timer

Output:

[Unit]
Description=Daily apt download activities

[Timer]
OnCalendar=*-*-* 6,18:00
RandomizedDelaySec=12h
Persistent=true

[Install]
WantedBy=timers.target

The green line indicates that apt-daily.service is run at 6:00 and 18:00 everyday. RandomizedDelaySec=12h means that the action will be postponed 0 ~ 12 hours if another timer has the same start time.

apt-daily-upgrade.service is controlled by apt-daily-upgrade.timer , whose configuration file is /lib/systemd/system/apt-daily-upgrade.timer.

cat /lib/systemd/system/apt-daily-upgrade.timer

Output:

[Unit]
Description=Daily apt upgrade and clean activities
After=apt-daily.timer

[Timer]
OnCalendar=*-*-* 6:00
RandomizedDelaySec=60m
Persistent=true

[Install]
WantedBy=timers.target

As you can see, apt-daily-upgrade.service is run at 6:00 AM every day. RandomizedDelaySec=60m means that the action will be postponed 0 ~ 60 minutes if another timer has the same start time.

The two .service files and two .timer files are installed by the apt package, which also comes with the /usr/lib/apt/apt.systemd.daily shell script that is executed by apt-daily.service and apt-daily-upgrade.service.

The /usr/lib/apt/apt.systemd.daily shell script reads configurations from /etc/apt/apt.conf.d/20auto-upgrades file. So fixing the above error doesn’t require you to disable the two timers.

That’s it! I hope this article helped you fix the “could not get lock /var/lib/dpkg/lock” error on Ubuntu. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial

[Total: 6 Average: 4.3]