Table of Contents
In this article, we will see how to solve "Could not get lock /var/lib/dpkg/lock-frontend (unattended-upgr)"
error on a Ubuntu/Debian based Linux systems. It is not very uncommon that one fine day you tried to run some command and all you got is "Could not get lock /var/lib/dpkg/lock-frontend (unattended-upgr)"
error on the output. This error usually occurs when you just started your system to perform certain task which requires you to run apt command.
This error can be sometimes very annoying especially when you have to perform certain urgent task. In my case, this error occurred when I was trying to remove tree utility from my Ubuntu 20.04 LTS System using sudo apt remove tree command. I am sure many of you sooner or later will encounter this error as well. Hence before solving this error, I decided to write an article about this so that it will help you folks as well.
Solved "Could not get lock /var/lib/dpkg/lock-frontend (unattended-upgr)"
Also Read: Solved "Error while dialing dial unix /var/run/dockershim.sock"
As I was explaining when I tried to remove tree
utility from my system by using sudo apt remove tree
command then I noticed below error on the output.
cyberithub@ubuntu:~$ sudo apt remove tree [sudo] password for cyberithub: ^Citing for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 3279 (unattended-upgr)... 19s
While the above error could occur due to many reasons but in my case it was due to start of the unattended upgrade in my Ubuntu 20.04 LTS
system. To further check and verify the error, I tried to check all the processes running for apt command.
cyberithub@ubuntu:~$ ps aux | grep apt
root 3227 0.0 0.0 2616 460 ? Ss 20:05 0:00 /bin/sh /usr/lib/apt/apt.systemd.daily install
root 3231 0.0 0.0 2616 1880 ? S 20:05 0:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held install
root 5488 63.0 4.3 100824 87712 ? RN 20:11 0:00 /usr/bin/python3 /usr/lib/update-notifier/apt-check --human-readable
cyberit+ 5492 0.0 0.0 8908 720 pts/1 S+ 20:11 0:00 grep --color=auto apt
As you can see from the above output, there is a scheduled unattended upgrade started running. This you can also verify by checking the configuration set under /etc/apt/apt.conf.d/20auto-upgrades
file using cat /etc/apt/apt.conf.d/20auto-upgrades
command as shown below.
cyberithub@ubuntu:~$ cat /etc/apt/apt.conf.d/20auto-upgrades APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "1";
As you can see from above output, both unattended upgrade and update package list are set to 1
which means it will check for available updates from the backend after you start your system and then automatically start updating your system packages. If this is indeed the case with you, then to solve this problem you can follow any of the below solutions depending on your condition and requirements.
Solution 1: Wait for Upgrade to Finish
The first solution is to do nothing and wait patiently for your unattended upgrade to finish. It should not take much time to finish depending on your system configuration and network bandwidth. This solution worked for me. In about few mins when I checked again apt
process using same command then I found that the unattended upgrade is not running anymore.
cyberithub@ubuntu:~$ ps aux | grep apt
cyberit+ 10405 1.0 0.0 8908 716 pts/1 S+ 22:12 0:00 grep --color=auto apt
Now when I again tried to remove tree utility by using sudo apt remove tree
command then this time it worked fine as expected and the package got removed.
cyberithub@ubuntu:~$ sudo apt remove tree [sudo] password for cyberithub: Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: tree 0 upgraded, 0 newly installed, 1 to remove and 3 not upgraded. After this operation, 115 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 211974 files and directories currently installed.) Removing tree (1.8.0-1) ... Processing triggers for man-db (2.9.1-1) ...
Solution 2: Kill the Process
The other solution that I wouldn't recommend until it's an emergency is to forcefully kill the unattended upgrade by using kill -9 <PID>
command where PID is the Process ID of apt
process running for unattended upgrade. For example, in our case it would be kill -9 3227
command as shown below.
cyberithub@ubuntu:~$ kill -9 3227
Solution 3: Delete Lock File
This solution is basically valid for those cases where after running apt command, you close the terminal. In that case, you might not have any apt process running but still you won't able to run the apt
command. It is simply because there is a lock file created which prevents any further apt process to start until the previous open one is ended. So to deal with this problem, you need to identify all the lock file created using below lsof
commands and then manually delete all those files.
cyberithub@ubuntu:~$ sudo lsof /var/lib/dpkg/lock cyberithub@ubuntu:~$ sudo lsof /var/lib/apt/lists/lock cyberithub@ubuntu:~$ sudo lsof /var/cache/apt/archives/lock
If you see any process running then you can delete those processes by using kill -9 <PID>
command where PID
is the Process ID of all the apt processes. Finally you can delete below lock files using rm command as shown below.
cyberithub@ubuntu:~$ sudo rm /var/lib/apt/lists/lock cyberithub@ubuntu:~$ sudo rm /var/cache/apt/archives/lock cyberithub@ubuntu:~$ sudo rm /var/lib/dpkg/lock
Now you need to reconfigure dpkg again by using sudo dpkg --configure -a
command as shown below.
cyberithub@ubuntu:~$ sudo dpkg --configure -a
Solution 4: Permanently Remove Unattended Upgrade
If you looking for any permanent solution then all you can do is remove unattended-upgrades from your system using sudo apt remove unattended-upgrades
command as shown below.
cyberithub@ubuntu:~$ sudo apt remove unattended-upgrades Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: unattended-upgrades 0 upgraded, 0 newly installed, 1 to remove and 20 not upgraded. After this operation, 451 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 211967 files and directories currently installed.) Removing unattended-upgrades (2.3ubuntu0.3) ... Processing triggers for man-db (2.9.1-1) ...
Then run sudo apt update && sudo apt upgrade
command to update the system about the latest changes done.
cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
Get:1 https://dl.winehq.org/wine-builds/ubuntu focal InRelease [8,041 B]
Get:2 https://dl.google.com/linux/chrome/deb stable InRelease [1,825 B]
Hit:3 http://ppa.launchpad.net/flatpak/stable/ubuntu focal InRelease
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:5 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:7 https://download.sublimetext.com apt/stable/ InRelease
Get:8 https://dl.winehq.org/wine-builds/ubuntu focal/main i386 Packages [404 kB]
Get:9 https://dl.winehq.org/wine-builds/ubuntu focal/main amd64 Packages [403 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:11 https://dl.google.com/linux/chrome/deb stable/main amd64 Packages [1,077 B]
Get:12 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [59.7 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,529 kB]
.........................................................
You can check How to Enable or Disable Automatic Updates on Ubuntu/Debian Linux article to know more about enabling or disabling automatic updates on Ubuntu/Debian based linux systems. Hope above given solutions would be enough to solve your error as well. Please let me know your feedback in the comment box.
Tried to buy you a coffe, but not working. Thanks anyway
Hi ,
This post solved my problem
so very cool and useful
Thanks a lot