Brief : Article covers details about the error Could not get lock /var/lib/dpkg/lock while installing an application.

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

Somehow I messed up my Ubuntu 16.04 LTS in VirtualBox and decided to set up a new machine from scratch. After some 30 min, I logged into freshly installed Ubuntu 18.04 VM. Yay! Now, lets install GNOME Tweaks to modify some of the default settings to my preference.

Read : Install Ubuntu 20.04

I ran into a problem though when I fired up a terminal (Ctrl + Alt + T) and tried installing it. Ubuntu presented me with the error “Could not get lock /var/lib/dpkg/lock …”.

Could not get lock /var/lib/dpkg/lock in Ubuntu

ambarish@ubuntu18-04:~$ sudo apt-get install gnome-tweaks-tool
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily inavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

Out of curiosity, I tried the graphical way to install Arc theme using Apt and saw a message “Waiting for unattended-upgr to exit”. On a positive side, that’s not as vague as what Terminal is saying.

Software Center

Since I have installed a fresh copy, Ubuntu is trying to update all the packages and system updates for the first time and the the unattended-upgr process is holding a lock on /var/lib/dpkg.

Lets check the exact process which has this lock.

ps aux | grep -i apt

Process details

Output clearly shows the process apt.systemd.daily update is the culprit. For now, if you are in similar situation, I will suggest to let it finish.

The unattended upgrades packages are used to automatically install updated packages. You can either configure it to update everything or restrict it to just security updates.

The settings for unattended upgrade is listed under /etc/apt/apt.conf.d/20auto-upgrades.

Let the unattended-upgrades finish before you install anything. It should not take much time and is critical in keeping your system up to date. Once it’s finished you would be able to use dpkg/installer.

Don’t want to wait for it to finish? You can kill the process as you have the process details by using below command. Use it at your risk, I will never interrupt an already running installation / upgrade in any case.

kill -9 [processId]

After some 15min, I am not seeing any more Could not get lock /var/lib/dpkg/lock error. But, I want to keep the control for Updates / Upgrades with me and don’t wanna face same thing once a week!

If you want to disable automatic updates, open Software Updater and click on Settings.

Software & Updates

You can choose your preferences for updates from here. My preferences include Checking for automatically updates daily but instead of download and automatic installation, I prefer it to be displayed and let me choose whether I want to download and install at that point of time.

It’s important that you keep the Important security updates (bionic-security) checked to receive any critical bug fixes and security updates!