Could not get lock

Linux SystemAPT ErrorCommonLast updated: June 29, 2026Tested on:Ubuntu 22.04 LTSBash Shell v5.1June 2026

This error occurs when another process is running package management tasks and holds the system database locks (/var/lib/dpkg/lock).

Could not get lock Quick Fix⏱️ Est. Fix Time: 3 minutes

Usually happens because:

  • Background security upgrades are running automatically
  • Previous package installation task crashed, leaving stale locks
  • Running concurrent APT/dpkg sessions in separate shell panels

🔍 Quick Checklist:

What is Could not get lock?

The 'Could not get lock' error is thrown by the APT package manager when another application is actively reading or writing to the Debian package database (/var/lib/dpkg/). To protect the database from corruption, APT creates file locks (like lock-frontend or lock). If an automated background service (like unattended-upgrades) is active, or a previous installation command was interrupted or frozen, new APT commands are blocked until the lock is released.

Common Causes

  • Unattended upgrades running in background: The system is automatically updating security packages in the background.
  • Interrupted package installation command: A previous package installation task crashed or was forcefully closed before releasing locks.
  • Simultaneous manual package updates: Running APT commands concurrently in separate terminal panels.
CauseFrequency
Unattended upgrades background process running⭐⭐⭐⭐⭐
Interrupted or frozen package management process⭐⭐⭐⭐
Concurrent manual apt/dpkg command sessions⭐⭐

Common Mistakes

  • Force-killing background packages managers during critical writes, causing database corruption that blocks future installs.
  • Deleting locks files blindly when an installation command is actively downloading files in another terminal session.

How to Fix

1Wait for background tasks to finish: Allow active background installers (unattended-upgrades) to release locks naturally.
2Locate and terminate active lock processes: Find the holding process PID using 'lsof' and terminate it safely.
3Remove stale lock files: If a crash occurred, manually delete the stale lock file to unlock database access.

Linux Operations & Verification

Locate which process ID (PID) is currently locking the package database files.

Finding the Lock Owner Example
# 1. Inspect lock ownership
$ sudo lsof /var/lib/dpkg/lock-frontend
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
apt-get 12345 root    4u  WREG    8,1        0 395123 /var/lib/dpkg/lock-frontend

# 2. Safely wait or terminate the PID if frozen
$ sudo kill -15 12345

Platform Specific Fixes

Disable automatic security background upgrades to prevent unexpected lock errors.

Linux Config
# Edit configuration file:
# sudo nano /etc/apt/apt.conf.d/20auto-upgrades
# Update settings to disable:
# APT::Periodic::Update-Package-Lists "0";
# APT::Periodic::Unattended-Upgrade "0";

Best Practices

  • Verify system update schedules using log reviews in `/var/log/unattended-upgrades/` folders.
  • Adopt package management checks inside automated deployment scripts to ensure lock availability before running installs.

Frequently Asked Questions (FAQ)

Q: What is the lock-frontend in APT?

It is a lock file used to prevent multiple package management applications from modifying the system package list at the same time.

Q: Is it safe to delete the lock files?

Only if you are 100% sure no install command is running. Deleting lock files while a package installation is active will corrupt your package database, requiring recovery steps.

Q: How do I find out which process is holding the lock?

Run 'sudo lsof /var/lib/dpkg/lock-frontend' or 'sudo fuser /var/lib/dpkg/lock-frontend' to list the active process ID (PID) holding the lock.

Q: How do I disable automatic unattended upgrades?

Edit '/etc/apt/apt.conf.d/20auto-upgrades' and set 'APT::Periodic::Unattended-Upgrade' to '0'.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error