This error occurs when apt lacks write permissions to its cache directory, typically caused by read-only filesystem, permission issues, or running apt without sudo. The fix involves checking filesystem status, using sudo, or clearing lock files.
The "Unable to write to /var/cache/apt/" error indicates that the apt package manager cannot create, modify, or access files in the /var/cache/apt/ directory. This directory is essential for apt to store downloaded packages, metadata, and lock files. When apt cannot write to this location, it cannot proceed with package updates or installations. The error typically stems from three sources: insufficient user permissions (running apt without sudo), a read-only filesystem state, or competing apt processes holding lock files.
The most common cause is running apt without administrative privileges. Always prefix apt commands with sudo:
sudo apt update
sudo apt install <package-name>Apt requires root permissions to write to system directories like /var/cache/apt/.
If using sudo does not resolve the issue, check if another package manager instance is currently running:
ps aux | grep -i aptIf you see an apt or apt-get process, wait for it to complete. You can also check if the Software Update manager is open and close it before trying again.
If no apt process is running but lock files remain, remove them:
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lockThen reconfigure the package database:
sudo dpkg --configure -aIf the error message includes "Read-only file system", run a filesystem check:
sudo touch /var/cache/apt/test-fileIf this fails with read-only error, your filesystem needs repair. Reboot into recovery mode or use a live USB, then run:
sudo fsck -y /dev/sdXnReplace /dev/sdXn with your actual partition. After repair, reboot and verify apt works.
If your cache is corrupted from an interrupted operation:
sudo rm -rf /var/lib/apt/lists/*
sudo mkdir -p /var/lib/apt/lists/partial
sudo apt updateThis removes all cached package lists and rebuilds them from repositories.
If issues persist, verify the cache directory ownership:
ls -la /var/cache/apt/The directory should be owned by root. If not, fix permissions:
sudo chown -R root:root /var/cache/apt/
sudo chmod -R 755 /var/cache/apt/Then retry your apt command.
In containerized environments (Docker), this error commonly occurs when running apt without sudo inside the container, even though root is the default user. Explicitly use RUN apt-get (not apt) in Dockerfiles and ensure the base image has apt installed. In WSL (Windows Subsystem for Linux), the error can occur after ungraceful shutdown; run wsl --shutdown on Windows, then reopen WSL. On systems with automounted read-only /var/cache partitions (rare), check /etc/fstab for ro flags and remount with sudo mount -o remount,rw /var/cache. If you frequently encounter this, consider using apt-cacher-ng to cache packages locally on your network.
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