This error occurs when dpkg cannot remove outdated package files due to insufficient permissions. The file or parent directory ownership/permissions prevent the deletion operation needed during package installation or upgrade.
The "dpkg: error: cannot remove old file" permission denied error indicates that the dpkg package manager lacks the necessary permissions to delete existing files during a package installation, upgrade, or removal operation. This typically happens when: - The dpkg process runs without sufficient privileges (not as root) - Files or directories have restrictive permissions that prevent deletion - The parent directory permissions prevent modifications - Files have immutable attributes set by the filesystem - Incorrect ownership of files in the dpkg database or cache directories dpkg needs write access to the parent directory to remove old versions of files being upgraded or to clean up files from removed packages.
The most common cause is attempting to run apt or dpkg without root privileges. Always use sudo:
# Instead of: apt update
sudo apt update
# Instead of: apt-get install package
sudo apt-get install package
# Instead of: dpkg -i package.deb
sudo dpkg -i package.deb
# To upgrade packages
sudo apt upgrade
# To fix broken packages
sudo apt --fix-broken installRoot privileges are required because dpkg modifies system files in protected directories like /var/lib/dpkg, /var/cache/apt, and /usr.
Sometimes lock files remain from a previous interrupted operation. Remove them to allow dpkg to proceed:
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock
# Reconfigure dpkg after removing locks
sudo dpkg --configure -a
# Update package lists
sudo apt updateLock files prevent concurrent dpkg operations. Removing stale locks allows the package manager to resume normal operation.
Verify and correct permissions on critical dpkg directories:
# Check current permissions
ls -la /var/lib/dpkg
ls -la /var/cache/apt
# Fix ownership and permissions
sudo chown -R root:root /var/lib/dpkg
sudo chmod 755 /var/lib/dpkg
sudo chmod 644 /var/lib/dpkg/status
sudo chmod 644 /var/lib/dpkg/status-old
sudo chown -R root:root /var/cache/apt
sudo chmod 755 /var/cache/apt
sudo chmod 755 /var/cache/apt/archivesFiles should be owned by root and have standard Linux permissions (755 for directories, 644 for files).
Files can have the immutable attribute set, preventing deletion even by root. Check and remove it:
# Check if the problematic file has immutable attribute
sudo lsattr /path/to/problematic/file
# If the output shows 'i' flag, remove it
sudo chattr -i /path/to/problematic/file
# For directories and contents
sudo chattr -R -i /var/lib/dpkg
sudo chattr -R -i /var/cache/apt
# Retry the failed operation
sudo apt update
sudo apt --fix-broken installThe 'i' attribute makes files immutable. This is rarely set intentionally but can occur due to security tools or system configuration.
After fixing permissions, reconfigure dpkg to complete any interrupted installations:
# Configure packages that are not fully configured
sudo dpkg --configure -a
# If there are broken dependencies
sudo apt --fix-broken install
# Or try this if --fix-broken is not enough
sudo apt --fix-missing install
# Finally, run a clean update
sudo apt clean
sudo apt updateThis command processes partially installed or configured packages and resolves dependency issues.
For persistent permission issues, boot into recovery mode where the root filesystem is mounted read-write but fewer processes are running:
1. Restart your system and enter the GRUB boot menu (hold Shift during startup)
2. Select "Recovery mode" or a kernel with "(recovery mode)" in the name
3. When prompted, select "Drop to root shell prompt"
4. Run these commands in the recovery shell:
# Remount filesystem as read-write
mount -o remount,rw /
# Fix permissions
chown -R root:root /var/lib/dpkg
chmod 755 /var/lib/dpkg
# Reconfigure dpkg
dpkg --configure -a
# Exit recovery mode
exit
# Choose "Resume normal boot"Recovery mode provides a minimal environment where you can safely fix critical system issues.
SELinux and AppArmor Contexts: If your system uses SELinux or AppArmor, these security frameworks may prevent file deletion even with correct permissions. Check security logs with sudo ausearch -m avc (SELinux) or sudo grep apparmor /var/log/syslog (AppArmor).
Filesystem Issues: Use sudo mount | grep /var to verify that /var is not mounted read-only. If it is, remount with read-write permissions: sudo mount -o remount,rw /var.
Checking File Descriptor Locks: Use sudo lsof | grep /path/to/file to see if any process has the problematic file open. Close those processes before removing the file.
Disk Space and Inodes: Occasionally, permission errors mask underlying disk space or inode exhaustion. Check with df -h and df -i. If inodes are full, you may need to remove files to free space.
Multiarch Issues: On systems with multiarch support, different package architectures (amd64, i386) can have permission conflicts. Repair with sudo apt install -f or sudo apt --fix-broken install.
Complete Package Cache Cleanup: If problems persist, you can safely remove the entire apt cache and rebuild it:
sudo apt clean
sudo apt autoclean
sudo apt updateE: 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