Ubuntu apt-get update Hash Sum Mismatch on macOS: Caching Error Resolution
Fix 'Hash Sum mismatch' errors for apt-get update on Ubuntu VMs/containers running on macOS, often due to filesystem or network caching issues.
Fix 'Hash Sum mismatch' errors for apt-get update on Ubuntu VMs/containers running on macOS, often due to filesystem or network caching issues.
Running sudo apt-get update is a routine operation for any Ubuntu user, especially in development and hosting environments. However, when working with Ubuntu virtual machines or Docker containers on a macOS host, you might occasionally encounter a frustrating "Hash Sum mismatch" error. This issue indicates that the downloaded package index files are corrupted or incomplete, preventing apt from reliably determining available package versions. While often transient, in a macOS local setup, it frequently points to deeper caching or filesystem synchronization problems between the host and the guest.
Symptom & Error Signature
When you execute sudo apt-get update, instead of a clean refresh of package lists, you'll observe output similar to the following, highlighting the "Hash Sum mismatch" error for one or more repository files:
$ sudo apt-get update
Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [117 kB]
...
Err:11 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
Hash Sum mismatch
Hashes of expected file:
- Filesize: 123456
- SHA256: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
- SHA1: 456789abcdef0123456789abcdef0123456789abcdef
- MD5Sum: 89abcdef0123456789abcdef01234567
Actual download was:
- Filesize: 123450
- SHA256: cdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789ab
- SHA1: abcd0123456789abcdef0123456789abcdef
- MD5Sum: ef0123456789abcdef0123456789ab
E: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-updates/main/binary-amd64/Packages.gz Hash Sum mismatch
E: Some index files failed to download. They have been ignored, or old ones used instead.
The key indicator is the Hash Sum mismatch line followed by discrepancies between expected and actual checksums (SHA256, SHA1, MD5Sum) and often filesize.
Root Cause Analysis
A "Hash Sum mismatch" error fundamentally means that the data downloaded by apt does not match the expected checksum provided by the repository's Release file. This indicates data integrity compromise. While generic network issues can cause this, in a macOS local environment running Ubuntu guests, several specific factors often contribute:
- Virtualization Layer Caching: Tools like Docker Desktop, VirtualBox, Vagrant, and multipass often employ various caching mechanisms (network, disk I/O, shared folders) to improve performance. These caches can sometimes become stale or corrupted, serving outdated or partial data to the Ubuntu guest.
- macOS Network Stack & DNS Caching: The macOS operating system itself can aggressively cache DNS resolutions and network responses. If the IP address of an
aptrepository mirror changes or becomes outdated in the macOS cache, it might lead to attempts to download from an incorrect or stale source. - Local Proxy/VPN Interference: If you are using a local HTTP proxy, a corporate VPN, or even certain network security software on your macOS host, they can intercept, modify, or cache network traffic, inadvertently corrupting
apt's index file downloads. - Incomplete Downloads: Network instability, even over local Wi-Fi, can lead to partial downloads of index files. The checksum mismatch occurs because
aptreceives an incomplete file. - Filesystem Sync Issues (Less Common for
aptbut possible): Whileaptprimarily writes to the guest's filesystem, issues with host-guest filesystem synchronization (e.g., in shared folders) or underlying disk I/O problems can theoretically lead to corrupted cached files on the guest. - Corrupted Local
aptCache: Theaptsystem maintains its own local cache of package lists (/var/lib/apt/lists/) and downloaded packages (/var/cache/apt/archives/). These files can become corrupted due to unexpected shutdowns, disk errors, or previous incomplete downloads.
Step-by-Step Resolution
Follow these steps meticulously to troubleshoot and resolve the "Hash Sum mismatch" error. Start from the simplest solutions and progress to more involved ones.
1. Clear the apt Cache and Old Lists (Within Ubuntu Guest)
This is the most common fix, especially if the issue is a transient network glitch or a corrupted local cache.
# Clear the apt cache of downloaded .deb files
sudo apt-get clean
# Remove all existing package list files. This forces apt to download fresh ones.
# > [!WARNING] This command effectively deletes your local package list index.
# > Running `apt-get update` immediately afterwards is crucial.
sudo rm -rf /var/lib/apt/lists/*
# Now, try updating again
sudo apt-get update
2. Verify and Adjust Repository Sources (Within Ubuntu Guest)
Ensure your sources.list files are correctly configured and not pointing to problematic mirrors or outdated entries.
# Display the main sources list file
cat /etc/apt/sources.list
# List additional repository files (e.g., PPAs, Docker, Nginx repos)
ls /etc/apt/sources.list.d/
- Review for problems: Look for commented-out lines (
#), duplicate entries, or non-standard repositories that might be unstable. - Default mirrors: If you are using custom mirrors, try temporarily switching back to the default Ubuntu mirrors (e.g.,
http://archive.ubuntu.com/ubuntu/) or a closer official mirror. - HTTPS: While
httpis common forapt, usinghttpscan sometimes be more reliable depending on your network conditions, assuming the repository supports it.
Ensure all repository URLs correctly specify your Ubuntu release (e.g.,
jammyfor Ubuntu 22.04 LTS,focalfor 20.04 LTS). Mismatches will cause significant issues.
3. Flush macOS DNS Cache and Network Reset (On macOS Host)
Sometimes the issue stems from stale DNS entries or network artifacts on the macOS host itself, affecting how your VM/container resolves repository hostnames.
# Flush macOS DNS cache
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
- Restart Network Interface/VPN: If you're using a VPN, try disabling and re-enabling it. If on Wi-Fi, consider restarting your Wi-Fi router or switching to a wired connection if possible.
4. Restart Virtualization/Container Environment
The caching layers of your virtualization software or container runtime can get into a bad state. A restart can clear these intermediate caches.
- For Docker Containers:
docker restart <container_name_or_id> # or if using docker-compose docker-compose restart <service_name> - For VirtualBox/multipass/Vagrant VMs:
# For multipass multipass stop <vm_name> multipass start <vm_name> # For Vagrant (which typically uses VirtualBox or other providers) vagrant reload
After restarting, try sudo apt-get update again inside the guest.
5. Check for Proxy Configuration Issues (Within Ubuntu Guest & macOS Host)
If you're behind a corporate proxy or have one configured locally, it could be interfering.
# Check for apt proxy configuration files
cat /etc/apt/apt.conf
ls /etc/apt/apt.conf.d/
Look for lines like Acquire::http::Proxy "http://proxy.example.com:8080/";.
- Temporarily Bypass/Verify: If a proxy is configured, try temporarily disabling it or verifying its health. If you rely on it, ensure its configuration is correct and it's not experiencing issues.
- macOS System Proxy: Also check your macOS System Settings -> Network -> Proxies. Ensure no unexpected proxy settings are active that could affect your VMs/containers.
Modifying proxy settings can impact network connectivity for other applications. Ensure you understand your network configuration before making changes.
6. Increase APT Timeout (Within Ubuntu Guest)
On flaky networks or with slow mirrors, apt might time out prematurely. Increasing the timeout can sometimes help, allowing more time for downloads.
# Create a new apt configuration file or edit an existing one
sudo nano /etc/apt/apt.conf.d/99timeout
Add the following lines:
Acquire::http::Timeout "300";
Acquire::ftp::Timeout "300";
Save the file and try sudo apt-get update again. A timeout of 300 seconds (5 minutes) is generally sufficient.
7. Verify Disk Space (Within Ubuntu Guest)
While less common for Hash Sum mismatch specifically, insufficient disk space can lead to incomplete file writes, resulting in corrupted files.
# Check disk space usage on the root filesystem
df -h /
Ensure there's at least a few gigabytes of free space available. If space is critically low, clear old logs, temporary files, or remove unnecessary packages.
8. Flush Systemd DNS Cache (Within Ubuntu Guest, if applicable)
If your Ubuntu guest is using systemd-resolved for DNS management, its cache might also be a culprit.
# Restart the systemd-resolved service
sudo systemctl restart systemd-resolved
# Flush its internal caches
sudo resolvectl flush-caches
9. Consider a Fresh Environment (Last Resort)
If all the above steps fail, the underlying environment (VM image, Docker base image, or virtualization setup) might have deeper corruption or misconfiguration. As a last resort, consider:
- Docker: Rebuilding your container image (
docker build --no-cache ...) or pulling a fresh base image (docker pull ubuntu:latest). - Virtual Machines: Creating a new VM instance from a clean image or re-provisioning with Vagrant/multipass.
Before resorting to a fresh environment, ensure you have backups of any critical data, configurations, or application code. Document your setup process thoroughly (e.g., in a Dockerfile or provisioning script) to minimize future headaches.
Our Production Verification Guarantee
Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.