Could Not Get Lock
Linux Linux
Severity: MinorWhat Does This Error Mean?
The 'could not get lock' error in Linux means the package manager (apt, dpkg) cannot run because another package manager process is already running and has locked the database. This prevents two package manager instances from modifying the package list at the same time — which could corrupt the system. The lock is usually held by an automatic background update or a previous command that was interrupted.
Affected Models
- Ubuntu
- Debian
- Linux Mint
- Raspberry Pi OS
- Pop!_OS
Common Causes
- An automatic background update (unattended-upgrades) is currently running
- You have another terminal window open running apt or dpkg
- A previous apt command was interrupted and left a stale lock file behind
- The Software Center or Update Manager graphical application is open and running
- A system update was interrupted by a crash or power loss and left the lock file intact
How to Fix It
-
Wait and try again. The most common cause is a background auto-update that will finish in a few minutes. Wait 5–10 minutes and retry your command.
On Ubuntu and Debian, unattended-upgrades runs automatically in the background. It holds the lock briefly while updating. Patience is often the only fix needed.
-
Find and identify what holds the lock. Type: sudo lsof /var/lib/dpkg/lock-frontend — this shows which process is holding the lock. Note the process ID (PID) shown.
If lsof shows an active process, that process is legitimately using the package manager. Wait for it to finish rather than killing it.
-
Kill the blocking process if it is stuck. If the process from Step 2 has been running for over 30 minutes with no progress, kill it: sudo kill -9 [PID] — replace [PID] with the process ID found above.
Only kill the process if you are sure it is stuck, not just slow. Killing an active apt operation can leave packages in a partially installed state.
-
Remove stale lock files if no process holds the lock. If Step 2 showed no active process but the lock still exists, remove the stale locks: sudo rm /var/lib/dpkg/lock-frontend — sudo rm /var/lib/dpkg/lock — sudo rm /var/cache/apt/archives/lock
Only do this if lsof confirmed no process is holding the lock. Removing an active lock can corrupt the package database.
-
Run dpkg to fix any interrupted installations. After removing stale locks, type: sudo dpkg --configure -a — this completes any package installations that were interrupted.
If a previous apt operation was killed mid-installation, dpkg --configure -a recovers the package database to a consistent state.
When to Call a Professional
This error is always fixable without professional help. Never force-remove the lock file without following the steps below — deleting an active lock can corrupt your package database.
Frequently Asked Questions
Is it safe to delete the lock file?
Only if no process is currently holding the lock. Use lsof to verify before deleting. If a process is actively using the lock and you delete it, two package manager instances can run simultaneously and corrupt the dpkg database. A corrupted dpkg database is difficult to repair and can break the ability to install or remove any software.
This happens every time I try to install something. How do I stop it?
If the lock is being held by unattended-upgrades (Ubuntu's automatic updater), you can disable or schedule it differently. To stop it from running while you are working: sudo systemctl stop unattended-upgrades — or configure it to run at a specific time by editing /etc/apt/apt.conf.d/20auto-upgrades
After fixing the lock, apt says packages are in a broken state. What do I do?
If an interrupted apt operation left packages in a broken state, run: sudo apt --fix-broken install — this attempts to complete broken installations and resolve dependency problems. If that does not work, try: sudo dpkg --configure -a followed by: sudo apt update — and then: sudo apt upgrade