What the "dpkg locked" Error Means
The dpkg locked (or Could not get lock) error occurs when the dpkg (or apt) package management system cannot access its database because another process is already using it. Typical error messages:
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (apt)
E: Could not get lock /var/lib/dpkg/lock. It is held by process 5678 (dpkg)
This error appears when attempting commands like sudo apt update, sudo apt install <package>, or sudo dpkg -i <file.deb>. It blocks any package installation, removal, or update operations until the lock is released.
Common Causes
- Parallel package manager execution: You started
aptin the terminal while simultaneously using a graphical package manager (Software Center, Synaptic) or another instance ofapt/dpkg. - Interrupted operation: A previous installation or update was forcibly stopped (e.g., via Ctrl+C, closing the terminal, or a system crash), leaving a lock-file behind.
- Background processes: Automatic updates (e.g., via
unattended-upgrades) or other system services usingdpkg/apt. - Stale lock-file: In rare cases, a lock-file might remain due to permission issues or filesystem corruption.
Solutions
Method 1: Terminate Conflicting Processes
First, identify which process holds the lock.
- Find the PID (process ID) using:
sudo lsof /var/lib/dpkg/lock-frontend sudo lsof /var/lib/dpkg/lock
If the command returns a process (e.g.,aptordpkg), note its PID. - Alternatively, use:
ps aux | grep -E 'apt|dpkg'
Look for processes likeapt-get,apt-systemd,dpkg. - Safely terminate the process:
- If the process is active (installation in progress), wait for it to finish.
- If the process is "stuck" or you're sure it's not needed, kill it:
If that doesn't work, use force kill:sudo kill <PID>sudo kill -9 <PID> - You can kill all
aptanddpkgprocesses (use caution!):sudo pkill -9 apt sudo pkill -9 dpkg
- After terminating processes, try your command again (e.g.,
sudo apt update).
Method 2: Manually Remove Lock Files
If no processes are found or the error persists, lock-files likely remain from an interrupted operation.
- Ensure no active
dpkg/aptprocesses (repeat steps from Method 1). - Remove lock files:
sudo rm /var/lib/dpkg/lock sudo rm /var/lib/dpkg/lock-frontend sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/apt/lists/lock⚠️ Important: Only remove lock files if you're certain no
dpkg/aptprocesses are running. Otherwise, you risk damaging the package database. - After removal, reconfigure
dpkgto complete any unfinished operations:sudo dpkg --configure -a - You can now use
aptordpkgagain.
Method 3: Reboot the System
If previous methods fail or you're unsure, a simple reboot often resolves the issue:
- Restart the system:
sudo reboot
During boot, all processes will terminate, and lock files will be automatically released. - After logging in, try the
aptcommand again.
Method 4: Fix Permissions (If Needed)
In rare cases, the issue may be incorrect permissions on lock files or directories.
- Check permissions on the
/var/lib/dpkg/directory:ls -ld /var/lib/dpkg/
The owner should beroot:root, permissionsdrwxr-xr-x. - If permissions are incorrect, fix them:
sudo chown root:root /var/lib/dpkg/ sudo chmod 755 /var/lib/dpkg/ - Repeat lock file removal (Method 2) and reconfiguration.
Prevention
To avoid recurring dpkg locked errors:
- Never run multiple package managers simultaneously. Close all graphical utilities (Software Center, Synaptic) before using
aptin the terminal. - Do not interrupt installation/removal operations (e.g., with Ctrl+C) unless absolutely critical. Wait for completion.
- Regularly update your system (
sudo apt update && sudo apt upgrade) to minimize unfinished operations. - Configure automatic updates (if using
unattended-upgrades) so they don't conflict with manual actions. Ensure the service runs correctly:sudo systemctl status unattended-upgrades - When using automation scripts, add lock-file checks via
flockor similar to prevent concurrent runs.
If the error occurs frequently without an obvious cause, check system logs for other issues:
sudo journalctl -xe | grep -i dpkg