How to fix "could not open lock file /var/lib/dpkg/lock-frontend" error [update 2022]

If you use Linux then the "could not open lock file /var/lib/dpkg/lock-frontend" error is very common and this post will help you to solve this issue. I will tell you the right procedure to fix it. So that you can avoid any dpkg database or filesystem corruption problem.

Before deep dive let's find out the reason for this dpkg lock issue -

Why I get  "Could not open lock file /var/lib/dpkg/lock-frontend" issue ?

Following may be the reasons:

  1. You don't have sudo rights and trying to run sudo update command
  2. Any other process is holding dpkg database lock
  3. May be your file system is corrupted, Read only or using noexec in fstab file
  4. Software center (GUI) is open

How to fix Why I get  "Could not open lock file /var/lib/dpkg/lock-frontend" error ?

Following methods can fix this /var/lib/dpkg/lock-frontend issue -

  1. Make sure you have sudo or root rights to run updates.
  2. Let the other process complete, which is holding dpkg database lock.
  3. Find the frontend and quit it gracefully
  4. Check out cron jobs or systemd timers calling dpkg database or frontend, disable them
  5. Kill hang or frozen process, if soft quit is not working
  6. Reboot system
  7. Removing the lock file may be a solution but not recommended

Let's deep dive and see all the solutions and similar errors you may face -

Method1 - Appropriate sudo or root rights to fix "Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)" error

If you are trying to install any application or running updates in Ubuntu and getting "E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)" error. It means you don't have permission to run the "apt" command.

fix  Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied) error
$ sudo apt update
Always use sudo as a prefix against commands to avoid  "Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)" error

Method2- Let the other process complete, which is holding dpkg database lock

Whenever you get a "Could not open lock file /var/lib/dpkg/lock-frontend" error while installing any package in Ubuntu. It happens due to the dpkg database lock. To fix this issue, the best solution is to wait and let the other process complete and the dpkg database will get released.

Nowadays, apt is pretty smart, it automatically shows you the PID and process name, which is having a lock as shown in the image.

In my case, the process with PID 13172 (aptd) was holding dpkg lock and resulting in /var/lib/dpkg/lock-frontend error while installing another package. When I checked in details, automatic software updates were running using the software centre in Ubuntu.

could not open lock file /var/lib/dpkg/lock-frontend

In case you don't get this message due to any reason. You can run the following command to get the PID to know, which process has locked the dpkg database.

$ ps aux | grep -i apt
find process holding dpkg lock

Method3 - Find the frontend process and quit it gracefully to release dpkg lock

Sometimes waiting for another process to release dpkg doesn't work if the process is not completed due to some dependencies. So to fix the /var/lib/dpkg/lock-frontend issue, you need to find the process and quit or close it gracefully. It may be a cron job or systemd timers calling the dpkg database.

Run the following command to look and print the process holding dpkg lock and prompt to terminate it gracefully.

$ sudo fuser -vki -TERM /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend

If you will refer to the below-mentioned image, It asked me to "terminate /var/lib/dpkg/lock process 4096" gracefully by prompting y/N options.

fuser command to release dpkg frontend lock gracefully

Finish any pending configuration to let the processes get into a sane state. Also, let dpkg merge any database journal updates into the main status database by running the following command.

sudo dpkg --configure --pending
dpkg --configure --pending command to merge database updates

Method4 - Kill hang or frozen process, if soft quit is not working to fix "Could not open lock file /var/lib/dpkg/lock-frontend"

In case graceful or soft quit doesn't work, you can forcefully kill the process holding the dpkg lock. Run the following command to locate and kill the process holding dpkg lock.

$ ps aux | grep -i apt
Locate process resulting could not open lock file /var/lib/dpkg/lock-frontend error

To kill process run any of the following commands -

$ sudo kill -9 {Process ID}

For example, in my case, PID is 3118, so I will run

$ sudo kill -9 3118

You can also kill all the processes related to apt and apt-get process using killall command.

sudo killall apt apt-get

Method5 - Reboot system if everything else fails

If you have performed all the methods and still you are getting Could not open lock file /var/lib/dpkg/lock-frontend" error. Try rebooting your Linux system.

There is one more solution to find out the dpkg lock file and delete it. But this solution is not officially recommended and is not a correct solution. Many posts talk about this solution but being a differentiator, I would recommend not to follow this method. I have added this solution to bring awareness to this solution.

The presence of the file does not mean that the lock is currently being held, and removing them can most probably cause dpkg database or filesystem corruption.

If there is ever a need to override the locks, then it is always preferable to kill an active running dpkg over ever removing the lock files, as dpkg is supposed to be resilient against the abrupt system or process crashes or termination, and any issues stemming from those would be considered serious problems in need of fixing.

Summary

I hope you are able to solve the "Could not open lock file /var/lib/dpkg/lock-frontend" issue by following this post. Let me know, which method helped you to get this issue solved.

In case you still face any issues, leave a comment, I will try my best to help you.