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

Running package maintenance commands on an Ubuntu desktop or remote cloud server is an everyday task. However, executing apt update or apt-get upgrade only to be greeted by the terminal error ‘E: Could not open lock file /var/lib/apt/lists/lock – open (13: Permission denied)’ or ‘E: Unable to lock directory /var/lib/apt/lists/’ halts administrative work immediately.

This error manifests under two distinct conditions: running the command without superuser administrative privileges (Error 13: Permission denied), or competing background processes (such as automated daily unattended upgrades) retaining an active exclusive lock on the package database. Following this guide will resolve both scenarios cleanly.

Execute the Command with Elevated Superuser Privileges

The package cache directories in /var/lib/apt/ are owned strictly by the root administrative account. Running apt commands as a standard unprivileged user results directly in the permission denied error.

  1. Inspect your active terminal prompt. If you ran apt update without prefixing superuser credentials, prepend sudo:
    sudo apt update
  2. Enter your user account password when prompted.
  3. In the majority of standard desktop workflows, invoking sudo immediately grants write permissions to the lock directory, allowing package index downloads to proceed.

Identify and Wait for Competing APT Background Processes

If you invoked sudo yet still receive a locking error (e.g., ‘Could not get lock /var/lib/apt/lists/lock. It is held by process…’), an automated maintenance utility is currently running in the background.

  1. Query the system to identify the process ID currently holding the lock:
    sudo lsof /var/lib/apt/lists/lock
    (Alternatively, run: ps aux | grep -i apt).
  2. Inspect the process name. Frequent holders include unattended-upgrade or apt.systemd.daily.
  3. If automated security updates are running, the recommended and safest action is to wait two to three minutes for the background patch cycle to conclude naturally, avoiding package database corruption.

Safely Terminate Hung Lock Processes

If a previous package installation hung due to a power interruption or terminal closure, the locking process may remain orphaned in a zombie state.

  1. Terminate the stalled process using its specific PID identified from the step above:
    sudo kill -15 <PID>
  2. If the process fails to terminate gracefully after thirty seconds, issue:
    sudo kill -9 <PID>

Clean Stale Lock Files and Reconfigure Repositories

Once you have verified that no active APT processes are running, delete any orphaned lock tokens and repair partial installations.

  1. Remove the stale lock file:
    sudo rm -f /var/lib/apt/lists/lock
    sudo rm -f /var/cache/apt/archives/lock
    sudo rm -f /var/lib/dpkg/lock*
  2. Reconfigure any half-installed deb packages:
    sudo dpkg --configure -a
  3. Refresh your repository lists cleanly:
    sudo apt update

Get the best tech tips delivered straight to your inbox.

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