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.
- Inspect your active terminal prompt. If you ran
apt updatewithout prefixing superuser credentials, prependsudo:sudo apt update - Enter your user account password when prompted.
- In the majority of standard desktop workflows, invoking
sudoimmediately 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.
- 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). - Inspect the process name. Frequent holders include
unattended-upgradeorapt.systemd.daily. - 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.
- Terminate the stalled process using its specific PID identified from the step above:
sudo kill -15 <PID> - 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.
- Remove the stale lock file:
sudo rm -f /var/lib/apt/lists/locksudo rm -f /var/cache/apt/archives/locksudo rm -f /var/lib/dpkg/lock* - Reconfigure any half-installed deb packages:
sudo dpkg --configure -a - Refresh your repository lists cleanly:
sudo apt update