This error occurs when the apt package manager cannot acquire the dpkg lock file because another package management operation is already in progress. The system's package manager can only run one operation at a time, and concurrent attempts will fail with this lock error.
The 'Could not get lock' error indicates that the dpkg package management system is busy performing another operation. When apt or apt-get attempts to install, update, or remove packages, it creates a lock file at /var/lib/dpkg/lock-frontend to ensure only one package operation runs at a time. This prevents data corruption and conflicts. If you attempt to run an apt command while another is in progress (or if a previous operation didn't release the lock), apt rejects the request with error code 11 (Resource temporarily unavailable), which means the resource is currently locked.
The most common cause is an unattended upgrade running in the background. Wait 5-15 minutes for the operation to finish naturally. You can check if upgrades are running:
sudo lsof /var/lib/dpkg/lock-frontendIf you see 'unattended-upg' in the output, an automatic upgrade is running. Let it finish before proceeding.
Check which process is holding the lock using lsof or ps:
# Check which process holds the lock
sudo lsof /var/lib/dpkg/lock-frontend
# Or search for apt/dpkg processes
ps aux | grep -E 'apt|dpkg'Note the PID and command. If it's 'unattended-upgr', wait for it to finish. If it's another process that shouldn't be running, proceed to the next step.
If the lock is held by a process that's stuck or shouldn't be running (not unattended-upgrades), you can terminate it:
# Replace PID with the actual process ID from step 2
sudo kill -9 PIDWait a few seconds after killing the process, then try your apt command again. Do NOT kill unattended-upgrades unless you're certain updates are truly stuck.
If no processes are using dpkg but the lock file persists, you can manually remove it:
# Remove lock files
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
sudo rm /var/lib/dpkg/lock-frontend
# Reconfigure dpkg
sudo dpkg --configure -aAfter removing lock files, reconfigure dpkg and try your apt command again. This should only be done if other methods fail, as it risks package inconsistency if done incorrectly.
If the lock remains stuck after all steps, reboot the system:
sudo rebootAfter reboot, the lock files will be cleared and background upgrades will resume cleanly. This is the safest option if you're unsure about killing processes.
On Ubuntu systems, unattended-upgrades runs automatically by default, typically scheduling updates for early morning hours. If you frequently encounter this error, consider disabling unattended upgrades (sudo apt remove unattended-upgrades) or configuring its schedule in /etc/apt/apt.conf.d/50unattended-upgrades. The dpkg lock mechanism is crucial for system stability—never force remove lock files if a package manager process is actively running. On Debian systems, the timing of APT-acquire and unattended-upgrades can conflict; consider installing needrestart to handle automatic daemon restart after updates without locking apt. When scripting apt operations in automation (Docker, CI/CD, etc.), always wait for or kill existing apt processes before starting your own to avoid lock contention.
E: Could not connect to proxy server
Could not connect to proxy server
E: Package 'package:i386' has no installation candidate
How to fix "Package package:i386 has no installation candidate" in apt
E: The value 'value' is invalid for APT::Default-Release
How to fix invalid APT::Default-Release value in APT
dpkg: error: unable to create new file 'path': Permission denied
How to fix dpkg permission denied errors in APT
subprocess installed post-removal script returned error exit status 1
How to fix "subprocess installed post-removal script returned error exit status 1" in APT