How to Fix "Failed to Fetch" Error in apt-get Update on Linux
The "Failed to fetch" error appears when apt-get cannot download package lists from repositories. This prevents you from installing or updating software. Here is how to diagnose and fix it.
Fix 1: Check Your Internet Connection
The error often means your internet connection is unstable or a repository server is down. Run ping google.com to check your connection. If that works but apt fails, the repository server may be temporarily unavailable. Wait a few hours and try again. You can also try changing your DNS to Google DNS (8.8.8.8) or Cloudflare DNS (1.1.1.1).
Fix 2: Fix GPG Key Errors
If the error mentions "GPG error" or "NO_PUBKEY", you need to add the missing key. Run sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys KEYID, replacing KEYID with the key mentioned in the error. On newer Ubuntu versions, use sudo gpg --keyserver keyserver.ubuntu.com --recv KEYID followed by gpg --export KEYID | sudo tee /usr/share/keyrings/keyname.gpg.
Fix 3: Change Repository Mirror
A slow or outdated mirror can cause fetch errors. Open Software & Updates in your system settings. Under the Ubuntu Software tab, change Download from to Main server or choose a mirror close to your location. Click Close and let it reload. This often resolves persistent fetch errors.
Fix 4: Fix SSL Certificate Errors
If the error mentions "SSL certificate", your system date may be wrong or SSL certificates are outdated. Check your date with the date command and correct it if needed. Update ca-certificates with sudo apt install --reinstall ca-certificates. For old systems, you may need to update the entire OS.