What the apt-lock-failed Error Means
The error E: Could not get lock /var/lib/dpkg/lock or E: Unable to acquire the dpkg frontend lock means that the APT package manager (and its low-level component, dpkg) cannot access its critical files because they are already locked by another process.
This is a system safeguard against simultaneous modification of the package database, which could damage it. The error appears when attempting to run any command that changes the system state: apt install, apt update, apt upgrade, apt remove, dpkg -i, etc.
A typical full message:
E: Could not get lock /var/lib/dpkg/lock. It is held by process 1234 (apt)
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (apt)
Common Causes
- An active APT/dpkg process in another terminal. You started an installation/update in one terminal window and are trying to run another apt command in parallel.
- A background automatic process. Services like
unattended-upgrades(automatic updates) orapt-daily.servicemay be running in the background. - A "hung" or abnormally terminated process. A previous apt/dpkg session was interrupted (e.g., via
Ctrl+Cor a system crash), but the lock file remained. - Insufficient privileges. Attempting to run an apt command without
sudocan sometimes lead to strange access errors, though the error message is usually different. - Filesystem issues. Rarely—inode corruption or a tmpfs mount with insufficient space.
Solution 1: Simply Wait (Recommended First Step)
Most often, the lock is temporary. A background update process (especially on Ubuntu/Debian) may run for 5-15 minutes.
What to do:
- Wait 2-3 minutes.
- Try the command again.
- If that doesn't help, check what is actually running (see Solution 2).
💡 Tip: You can add a wait timeout to the command so APT itself waits for the lock to be released:
sudo apt-get -o DPkg::Lock::Timeout=60 update(waits 60 seconds).
Solution 2: Find and Kill the Process Holding the Lock
This is the primary and safest method if you are certain the background process isn't performing a critical operation.
Steps:
- Find the PID (process ID) holding the lock:
sudo lsof /var/lib/dpkg/lock
Or more comprehensively (will also show lock-frontend):sudo lsof /var/lib/dpkg/lock-frontend
Example output:COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME apt 1234 root 3uW REG 8,2 0 12345 /var/lib/dpkg/lock-frontend
Here, PID = 1234. - Assess the process. If the
COMMANDcolumn showsapt,apt-get,aptitude,dpkg, orunattended-upgr, it is indeed an APT process. - Safely terminate the process:
sudo kill -9 1234
Use-9(SIGKILL) only if a regularkill 1234(SIGTERM) fails. - Verify the process has stopped:
ps aux | grep -E "(apt|dpkg)" | grep -v grep
The output should be empty. - Remove the lock files (see Solution 3).
Solution 3: Forcefully Remove Lock Files (Only After Stopping Processes!)
IMPORTANT: Perform this step only after you have confirmed (via Solution 2) that no APT process is running. Removing a lock file while a process is active will cause serious package database corruption, requiring recovery.
Command to remove all standard APT lock files:
sudo rm -f /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock /var/cache/apt/archives/lock
The -f flag ignores non-existent files.
Solution 4: Recover dpkg State After a Crash
If the error occurred after forcibly terminating (e.g., Ctrl+C) a package installation, the dpkg database may be in a "half-configured" state.
Run:
sudo dpkg --configure -a
This command will finish configuring any packages whose installation was interrupted. After it completes successfully, normal apt commands should work.
Solution 5: Reboot the System (Last Resort)
If the previous methods fail and you cannot identify the process (e.g., in a Docker container or minimal system), the simplest solution is to reboot the machine.
sudo reboot
After reboot, all processes will be terminated and lock files cleared by the kernel. Use this method if you are unsure of your actions.
Preventing the apt-lock-failed Error
- Avoid parallel installations. Do not run
apt installin multiple terminals simultaneously. - Use a lock timeout. Add
-o DPkg::Lock::Timeout=60to commands so APT waits instead of failing.sudo apt-get -o DPkg::Lock::Timeout=60 update - Disable automatic updates (if they interfere) if you manage a server manually. For Ubuntu/Debian:
sudo systemctl stop unattended-upgrades sudo systemctl disable unattended-upgrades - Regularly check system logs for dpkg/apt errors:
sudo journalctl -u apt-daily.service -b. - Do not interrupt (
Ctrl+C)aptanddpkgprocesses unless absolutely necessary. It's better to let them finish.