The apt package manager cannot acquire a lock on the /var/lib/apt/lists/ directory, usually because another apt process is running or a previous operation didn't complete properly. This prevents you from running package manager commands.
When you run apt commands like `apt update`, `apt install`, or `apt upgrade`, the package manager creates lock files in /var/lib/apt/lists/ to prevent multiple processes from corrupting the package database simultaneously. If a lock file already exists and the process that created it is still running (or didn't clean up properly), apt will refuse to proceed with new operations. This error is a safety mechanism that protects your system from broken package states. However, it can occur when: - An automatic background update is still running (apt.systemd.daily) - A previous apt command was interrupted or crashed - Another package manager (like Ubuntu Software Center) is actively running - Lock files weren't properly released due to a system issue
First, check if the system's automatic update daemon is running. It runs periodically and will release the lock when finished.
ps aux | grep aptIf you see apt.systemd.daily in the output, the automatic update is running. Simply wait a few minutes for it to complete, then try your command again.
If another apt process is stuck or not responding, use fuser to safely terminate it. This is the recommended approach as it's more controlled than killing processes directly.
sudo fuser -vik -TERM /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lockThis command will:
- Identify which processes are holding these lock files
- Prompt you to confirm termination
- Send a TERM signal (graceful shutdown) to those processes
After this completes, try your apt command again.
If the graceful termination doesn't work, use the -KILL signal for a forceful shutdown:
sudo fuser -vik -KILL /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lockAfter terminating the processes, reconfigure the package system:
sudo dpkg --configure -a
sudo apt updateThe first command reconfigures any interrupted package operations.
If the above approaches don't work and you're confident no apt process is running, you can manually remove lock files:
sudo rm /var/lib/apt/lists/lock
sudo rm /var/lib/dpkg/lock
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/cache/apt/archives/lockAfter removing the locks, reconfigure the package system:
sudo dpkg --configure -a
sudo apt updateNote: Only do this as a last resort when you're certain no package manager process is running, as manually deleting lock files can lead to package database corruption if processes are still active.
If lock files persist even after checking for running processes, a system restart will clear them:
sudo rebootAfter reboot, the lock files will be cleaned up and you can run apt commands normally. This is a safe recovery option when lock files are truly stale.
Lock files exist for a reason: they prevent simultaneous access to the package database that could corrupt your system. Never blindly delete them without understanding the consequences. The fuser approach is preferred because it safely terminates the problematic processes while respecting process shutdown procedures. If you frequently encounter this error on servers, consider implementing systemd-hold or apt-listchanges to prevent automatic updates at inconvenient times. On systems with SELinux, ensure proper context is set on /var/lib/apt/ and /var/lib/dpkg/ directories after any permission issues.
dpkg: serious warning: files list file for package 'package-name' contains empty filename
How to fix "files list file contains empty filename" in APT
E: Sub-process /usr/bin/dpkg returned an error code (2)
How to fix "Sub-process /usr/bin/dpkg returned an error code (2)" in APT
dpkg-divert: error: rename involves overwriting 'path' with different file
How to fix dpkg-divert rename conflicts in APT
E: Sub-process /usr/bin/dpkg returned an error code (1) during kernel installation
How to fix "dpkg returned an error code (1)" in APT kernel installation
dpkg: dependency problems prevent configuration of triggers
dpkg: dependency problems prevent configuration of triggers in apt