How to Fix ‘Could Not Get Lock /var/lib/apt/lists/lock’ in Ubuntu

Running a routine package update on Ubuntu or Debian using sudo apt update only to encounter the error “Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)” or “Unable to lock directory /var/lib/apt/lists/” prevents you from installing or upgrading software. In Debian-based operating systems, APT employs advisory file locks to enforce mutual exclusion. Because multiple processes cannot safely modify the package cache and dependency graph simultaneously without causing database corruption, APT places a file lock whenever a transaction begins. When an automated background update daemon (such as unattended-upgrades) is currently refreshing package lists, or when a previous terminal session was terminated abruptly while holding the lock descriptor, subsequent APT commands are rejected. This comprehensive technical guide explains how Ubuntu manages package locks and details safe methods to resolve the lock conflict without damaging system repositories.

Why Does the /var/lib/apt/lists/lock Error Occur?

File locking ensures repository database integrity. The error occurs under two primary circumstances:

  • Background Automated Update Daemons: Ubuntu automatically executes apt.systemd.daily and unattended-upgrades in the background shortly after system boot. While these daemons are polling repositories, manual APT commands are locked out.
  • Orphaned or Abruptly Terminated Processes: Closing a terminal window, pressing Ctrl + Z, or suffering a system power failure while apt, apt-get, or dpkg was running leaves stale file descriptors on disk.
  • Concurrent Package Management Frontends: Running graphical software centres (such as Ubuntu Software Centre or Synaptic) simultaneously alongside terminal commands.
  • Exhausted Inodes or Read-Only Filesystem: Filesystem anomalies preventing APT from verifying lock ownership flags.

Method 1: Identify and Wait for Active Background Package Processes

Before forcefully deleting lock files, verify whether Ubuntu is legitimately installing essential security updates in the background.

  1. Open your Terminal (or press Ctrl + Alt + T).
  2. Run lsof or fuser to determine which process currently holds the lock:
    sudo lsof /var/lib/apt/lists/lock
  3. Alternatively, search for running apt processes using ps:
    ps aux | grep -E "[a]pt|[d]pkg|[u]nattended"
  4. If you see processes such as unattended-upgrade or apt.systemd.daily, the system is actively applying automated security patches. Wait 2 to 3 minutes for the process to complete naturally, then run sudo apt update.

Method 2: Gracefully Terminate Hanging Package Processes

If a package manager process has hung indefinitely or was paused by an abandoned terminal session, terminating the process releases the lock safely.

  1. Use the Process ID (PID) identified in Method 1 to terminate the specific process gracefully:
    sudo kill -SIGTERM PID
    (Replace PID with the actual numeric process identifier, e.g., sudo kill -SIGTERM 3421).
  2. Wait 10 seconds. If the process does not terminate, force closure:
    sudo kill -SIGKILL PID
  3. Alternatively, terminate all orphaned apt instances:
    sudo killall apt apt-get 2>/dev/null

Method 3: Remove Stale Lock Files Safely

If no processes are active but the lock file persists due to an abrupt system crash, manually removing the lock descriptors restores functionality.

  1. Confirm that no APT or DPKG processes are running:
    sudo fuser -v /var/lib/apt/lists/lock
  2. If the output returns blank, remove the lists lock file:
    sudo rm -f /var/lib/apt/lists/lock
  3. For thoroughness, remove related cache and dpkg locks if present:
    sudo rm -f /var/cache/apt/archives/lock
    sudo rm -f /var/lib/dpkg/lock
    sudo rm -f /var/lib/dpkg/lock-frontend

Method 4: Reconfigure Unfinished Packages

When an APT process is interrupted, packages may remain in an unconfigured half-installed state.

  1. Run dpkg’s automated package reconfiguration command:
    sudo dpkg --configure -a
  2. Fix any broken dependencies across installed software:
    sudo apt-get install -f

Method 5: Rebuild the APT Lists Directory

If the lists directory itself contains truncated repository metadata, rebuilding the directory downloads clean package indexes.

  1. Clear out damaged repository index lists:
    sudo rm -rf /var/lib/apt/lists/*
    sudo mkdir -p /var/lib/apt/lists/partial
  2. Execute a complete package index refresh:
    sudo apt-get clean
    sudo apt-get update
  3. The package manager will cleanly reconstruct the entire repository index without errors.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.