How to Fix ‘Could Not Get Lock /var/cache/apt/archives/lock’ in Ubuntu

When running package management commands like sudo apt update, apt upgrade, or apt install on Ubuntu or Debian-based Linux systems, you may receive the fatal error: E: Could not get lock /var/cache/apt/archives/lock. It is held by process XXXX (apt-get) – open (11: Resource temporarily unavailable). This error occurs because Debian’s Advanced Package Tool (APT) implements exclusive mutex lockfiles to prevent concurrent operations from corrupting the local package cache database.

Why Does the APT Archive Lock Occur?

Ubuntu utilizes several background services that interact with package repositories. When one process holds the lock, all other APT calls are blocked to safeguard database integrity. Common culprits include:

  • Unattended Upgrades Running in Background: Ubuntu automatically executes unattended-upgrades or apt.systemd.daily shortly after system boot to fetch critical security patches.
  • Ubuntu Software / Snap Store Processes: Graphical software centres running silent background repository refreshes.
  • Interrupted Terminal Sessions: An earlier apt-get or dpkg operation that was forcefully terminated via terminal closure or system reboot before cleaning up its lock descriptors.
  • Orphaned Package Locks: Stale lockfiles remaining on disk with zero active processes bound to their file descriptors.

Step 1: Identify the Process Holding the Lock

Before deleting lock files, you must always verify whether a legitimate package management process is actively running to avoid corrupting your installation.

  1. Open a terminal window (Ctrl + Alt + T).
  2. Execute lsof to find the exact process ID (PID) holding the archive lock:
    sudo lsof /var/cache/apt/archives/lock
  3. Alternatively, search for running APT-related processes:
    ps aux | grep -i -E "apt|dpkg|unattended"
  4. If the output reveals that unattended-upgr or apt-get is running, wait 2 to 5 minutes for the automated routine to finish. Once completed, the lock will release automatically.

Step 2: Gracefully Terminate Hanging Package Processes

If the process has been stalled for hours or frozen due to an unresponsive network connection, terminate it cleanly.

  1. Send a graceful termination signal (SIGTERM) using the PID identified in Step 1:
    sudo kill -15 <PID>
  2. Wait 10 seconds. If the process does not terminate, enforce a forceful termination (SIGKILL):
    sudo kill -9 <PID>
  3. To terminate all stalled APT and DPKG background tasks in one command:
    sudo killall -9 apt apt-get unattended-upgrade

Step 3: Safely Remove Stale Lockfiles

Once you have confirmed that no active processes are reading or writing to the APT cache, remove the orphaned lock files.

  1. Execute the following commands in sequence to remove the package archive and frontend locks:
    sudo rm /var/cache/apt/archives/lock
    sudo rm /var/lib/apt/lists/lock
    sudo rm /var/lib/dpkg/lock
    sudo rm /var/lib/dpkg/lock-frontend
  2. These files are merely synchronization markers; removing them does not delete any downloaded deb packages.

Step 4: Reconfigure the DPKG Database and Repair Broken Packages

Forcefully terminating a package process can leave partially extracted deb packages in an unconfigured state. Rebuilding the database ensures package consistency.

  1. Reconfigure uncompleted packages:
    sudo dpkg --configure -a
  2. Clean out partially downloaded and corrupted archives from the cache:
    sudo apt clean
    sudo apt autoclean
  3. Check for and repair any broken package dependencies:
    sudo apt install -f
  4. Update your local package repositories to verify proper lock acquisition:
    sudo apt update

Step 5: Disable or Reschedule Daily Unattended Upgrades (Optional)

If automated daily upgrades frequently conflict with server maintenance windows or scheduled CI/CD deployment scripts, configure systemd timers accordingly.

  1. Edit the APT periodic update configuration file:
    sudo nano /etc/apt/apt.conf.d/20auto-upgrades
  2. Change the upgrade directive from "1" to "0" to disable automatic unattended installation:
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Unattended-Upgrade "0";
  3. Press Ctrl + O followed by Enter to save, then press Ctrl + X to exit the editor.

Get the best tech tips delivered straight to your inbox.

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