The apt package manager is the foundation of software management in Debian, Ubuntu, and most of their derivatives. When apt stops working, the system can no longer install applications or receive security patches. The good news: most apt errors resolve in two or three commands once we understand what actually broke. Below we cover typical symptoms with step-by-step recovery for each case and a universal fallback procedure.
Where to start troubleshooting
We always read the full error message before applying any fix. Apt and dpkg usually state exactly what they dislike: a package name, a repository URL, a specific file. Running random commands from the internet without understanding the cause tends to make things worse.
Basic diagnostic commands to run first:
- sudo apt update surfaces current errors when reaching the repositories.
- sudo apt check exposes broken dependencies among installed packages.
- sudo dpkg --audit lists packages stuck in an intermediate state.
- dpkg -l | grep -E '^iF|^iU|^rc' shows packages with unusual status flags.
Operation logs live in /var/log/apt/history.log and /var/log/dpkg.log. They tell us when and on which package things went wrong.
Common apt errors and how to fix them
Lock file blocking
Message: E: Could not get lock /var/lib/dpkg/lock-frontend. Apt cannot start because the resource is busy.
Typical causes: a parallel apt in another terminal, the background unattended-upgrades service, or a previous process that died from an SSH disconnect or a reboot.
- Check active processes: ps aux | grep -E 'apt|dpkg'.
- If the processes are alive, wait. Unattended-upgrades finishes on its own.
- If nothing is running, remove the lock files: sudo rm /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/cache/apt/archives/lock /var/lib/apt/lists/lock.
- Finalize configuration: sudo dpkg --configure -a.
Never delete lock files while apt is still running: this corrupts the package database and creates much worse problems than the original lock.
dpkg was interrupted
Message: E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a'. A previous operation did not finish, and apt refuses to continue until we fix it.
This usually follows a hard reboot during installation, the OOM killer firing, an SSH drop mid-update, or sudden power loss.
- Run sudo dpkg --configure -a. In most cases this is enough.
- On dependency errors: sudo apt --fix-broken install, then dpkg --configure -a
- For a specific package: sudo apt install --reinstall package_name.
- If the status file itself is corrupted, restore it from backup: sudo cp /var/lib/dpkg/status-old /var/lib/dpkg/status. Dpkg creates this backup automatically on every operation.
GPG repository key errors
Messages: NO_PUBKEY, The following signatures couldn't be verified, Key is stored in legacy trusted.gpg keyring. Apt does not trust the repository because it cannot verify the signature.
Important note: Ubuntu 24.04 and Debian 12 removed the apt-key command, and the /etc/apt/trusted.gpg store is no longer read by default. Older tutorials you might find online simply will not work here.
The modern workflow for adding a key:
- Create the directory: sudo mkdir -p /etc/apt/keyrings.
- Download the key and convert it to binary format in a single pipe:
curl -fsSL https://example.com/gpg.key | \
sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg- In the source file, explicitly reference the key via signed-by:
deb [signed-by=/etc/apt/keyrings/example.gpg] https://example.com/ubuntu noble
main- Verify: sudo apt update.
Keys distributed in .asc format go into /etc/apt/keyrings/ as is, without conversion. Legacy keys from the old trusted.gpg store can be exported with gpg --export and moved to the new directory the same way.
Hash Sum mismatch and mirror issues
Message: W: Failed to fetch ... Hash Sum mismatch. The checksum of the downloaded file does not match the one listed in the signed Release file.
Causes: a stale cache, a desynced CDN, a download that was cut off, or a mirror temporarily serving broken data.
- Clear the cache: sudo rm -rf /var/lib/apt/lists/* and sudo apt clean.
- Retry sudo apt update.
- If the issue persists, force IPv4 through /etc/apt/apt.conf.d/99force-ipv4 with Acquire::ForceIPv4 "true";.
- If the problem lasts for hours, a specific mirror is broken. Switch to another one.
When a default mirror consistently misbehaves, switching to a geographically closer one usually resolves the issue. For servers in the US, mirrors like us.archive.ubuntu.com or mirror.us.leaseweb.net are solid replacements. A one-line switch looks like this:
sudo sed -i 's|archive.ubuntu.com|us.archive.ubuntu.com|g' /etc/apt/sources.list
sudo apt updateOn Serverspace VPS the network is tuned for low-latency package delivery out of the box, so errors of this kind are less frequent on a freshly deployed instance.
404 Not Found and unreachable repositories
Symptom: 404 Not Found during apt update, or The repository ... no longer has a Release file.
First cause: the distribution reached end of life. Its packages move to old-releases.ubuntu.com and standard mirrors stop serving them. Update the address:
sudo sed -i 's|archive.ubuntu.com|old-releases.ubuntu.com|g; \
s|security.ubuntu.com|old-releases.ubuntu.com|g' /etc/apt/sources.listAfter this fix, plan an upgrade to a supported LTS release, since no security patches are issued for EOL versions anymore.
Second cause: a removed or relocated PPA. Find the file in /etc/apt/sources.list.d/ and either delete it or comment out the lines.
Third cause: a typo in the URL or release codename. Track it down with: grep -r "broken_url" /etc/apt/sources.list /etc/apt/sources.list.d/.
Unmet dependencies and held broken packages
Messages: The following packages have unmet dependencies, E: Unable to correct problems, you have held broken packages.
Causes: a PPA conflicts with official repositories, sources.list mixes release codenames (for example, noble and jammy), or a package has been pinned with apt-mark hold.
- List held packages: apt-mark showhold. Release them if needed: sudo apt-mark unhold package_name.
- Inspect package sources: apt-cache policy package_name.
- Standard fix: sudo apt --fix-broken install.
- If apt cannot resolve the conflict, try aptitude: sudo apt install aptitude, then sudo aptitude install package. Aptitude offers several resolution options to choose from.
- If a third-party PPA is the culprit, disable it and repeat the procedure.
Commands like dpkg --force-depends and dpkg --force-all are reserved for last resort: they skip dependency checks and leave the system in an inconsistent state.
/boot partition full and no space left on device
Symptoms: E: gzip: stdout: No space left on device, mkinitramfs failure. Almost always triggered by a kernel install.
On a typical VPS, /boot is small (500 MB or 1 GB), while each kernel with its initrd image occupies 100 to 200 MB. After a year of regular updates, the partition fills up.
- Check usage: df -h /boot and list installed kernels: dpkg -l | grep linux-image.
- Identify the running kernel: uname -r. Never remove this one under any circumstances.
- Simple path: sudo apt autoremove --purge.
- If /boot is packed so tight that autoremove cannot even stage temp files, manually delete one old initrd (never the current one), then sudo apt -f install, then autoremove.
- Update the bootloader: sudo update-grub.
For prevention, uncomment Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; in /etc/apt/apt.conf.d/50unattended-upgrades.
Universal recovery procedure when nothing helps
When the cause is unclear or a targeted fix failed, we run a standard chain from safe to aggressive:
- sudo rm -rf /var/lib/apt/lists/* and sudo apt clean wipe cached metadata.
- sudo apt update rebuilds the index.
- sudo dpkg --configure -a finalizes any pending operations.
- sudo apt --fix-broken install repairs dependencies.
- sudo apt full-upgrade brings the system to a consistent state.
- sudo apt autoremove --purge clears leftovers.
- If the chain still fails, repeat steps 3 through 5 using aptitude.
Before running destructive commands in production, take a disk snapshot. On Serverspace VPS snapshots are created from the control panel in a few clicks, so a botched experiment can be rolled back to a working state in under a minute.
Quick diagnostic table for apt errors
A short reference covering the most common messages and first recovery steps.
| Error message | Likely cause | First action |
|---|---|---|
| Could not get lock /var/lib/dpkg/lock | Parallel apt or a crashed process | Check ps aux, remove lock files if no process is running |
| dpkg was interrupted | Interrupted install or upgrade | sudo dpkg --configure -a |
| NO_PUBKEY | Missing repository key | Add the key to /etc/apt/keyrings/ with signed-by |
| Hash Sum mismatch | Stale cache or desynced mirror | Clear /var/lib/apt/lists/ and rerun update |
| 404 Not Found | EOL release, dead PPA, or URL typo | Switch to old-releases or remove the source |
| Unmet dependencies | Repo conflict or held package | apt --fix-broken install, aptitude if needed |
| No space left on device | /boot filled with old kernels | apt autoremove --purge, then update-grub |
| sub-process /usr/bin/dpkg returned an error code (1) | Damaged package or broken dpkg database | dpkg --configure -a, then reinstall the package |
How to prevent apt issues in the future
Most breakages are prevented by careful operation. A few simple rules:
- Update regularly. Run sudo apt update && sudo apt upgrade at least once a week.
- Minimize third-party sources. Every PPA is a potential point of failure.
- Take a snapshot before any significant change: kernel upgrade, LTS jump, installation of large stacks like Plesk or cPanel.
- Automate old kernel cleanup to avoid /boot overflow.
- Never mix release codenames in sources.list.
Conclusion
Apt errors only look scary until the first successful repair. Almost all of them map to a handful of typical scenarios: an interrupted process, a key problem, a stale cache, a full /boot, or repository conflicts. The approach is always the same: read the error message first, then apply a targeted fix starting from the safest action. The table above covers the majority of situations encountered while operating VPS instances on Debian and Ubuntu in everyday work.