This error occurs when the package manager (apt or dpkg) is being used by another process, preventing new installations or updates. The system uses lock files to prevent simultaneous access that could corrupt the package database.
The dpkg package manager uses lock files to ensure only one process accesses the package database at a time. When you try to run apt-get, apt, or dpkg while another process is using these tools, the new process gets locked out and cannot proceed. This commonly happens because: - The Ubuntu/Debian system is running automatic package updates in the background - Another terminal window or process is running apt/apt-get commands - A previous apt process crashed and left the system in a locked state - A package manager tool is hanging or stuck The lock files are located at /var/lib/dpkg/lock, /var/lib/apt/lists/lock, and /var/cache/apt/archives/lock to prevent database corruption.
The simplest and most reliable solution is to wait for whatever process holds the lock to complete. Ubuntu typically runs automatic updates in the background.
Run this command to check if there are any apt/dpkg processes still running:
ps aux | grep -i apt
ps aux | grep -i dpkgIf you see processes listed, wait 10-15 minutes for them to complete. Check again with the same commands until they're gone, then try your apt command again.
If waiting doesn't work or you want to see exactly what's holding the lock, use the lsof command to identify the process ID:
sudo lsof /var/lib/dpkg/lock
sudo lsof /var/lib/apt/lists/lock
sudo lsof /var/cache/apt/archives/lockThis will show you the process ID (PID) of any process currently holding each lock file. Note these PIDs for the next step.
If you've confirmed a process is stuck and won't finish, use the fuser command to safely terminate it:
sudo fuser -vki -TERM /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lockThis command will prompt you to confirm termination. The -TERM flag sends a graceful termination signal (safer than kill -9).
After terminating any processes, reconfigure dpkg to finalize any pending operations:
sudo dpkg --configure --pendingThis ensures any interrupted package configuration is completed and the database is consistent.
For better resilience in scripts or automated environments, you can tell apt to wait for the lock with a timeout:
sudo apt-get -o DPkg::Lock::Timeout=300 update
sudo apt-get -o DPkg::Lock::Timeout=300 install package-nameThe timeout value is in seconds (300 = 5 minutes). This tells apt to wait up to 5 minutes for the lock to be released before giving up, rather than failing immediately.
Why removing lock files is dangerous: Many online guides suggest deleting the lock files directly (/var/lib/dpkg/lock, /var/lib/apt/lists/lock, /var/cache/apt/archives/lock). The Debian team strongly opposes this approach because lock files exist to prevent simultaneous access that would corrupt the package manager database and potentially damage your entire system. Only remove lock files as an absolute last resort, and always run sudo dpkg --configure -a immediately afterward to restore consistency.
On systems with full /var partition: If disk space is critically low in /var/lib, the lock mechanism may fail. Check disk usage with df -h /var and clear package cache if needed: sudo apt-get clean
In container/CI environments: Docker and CI systems sometimes inherit lock files from previous runs. Always clean lock files in container entry points before running apt: rm -f /var/lib/apt/lists/lock /var/cache/apt/archives/lock /var/lib/dpkg/lock*
E: Could not connect to proxy server
Could not connect to proxy server
E: Package 'package:i386' has no installation candidate
How to fix "Package package:i386 has no installation candidate" in apt
E: The value 'value' is invalid for APT::Default-Release
How to fix invalid APT::Default-Release value in APT
dpkg: error: unable to create new file 'path': Permission denied
How to fix dpkg permission denied errors in APT
subprocess installed post-removal script returned error exit status 1
How to fix "subprocess installed post-removal script returned error exit status 1" in APT