Ubuntu

Guide to Change Ubuntu Mirror – Instant Commands for Each Mirror

I used to get really frustrated every time I ran apt update or apt upgrade on my Ubuntu server. The process would hang for minutes, and downloads felt painfully slow. After some digging, I realized the default Ubuntu repositories were pointing to servers far away from my location. That’s when I discovered the magic of choosing a closer mirror—and it made a huge difference! In this guide, I’ll share several easy ways to find and apply the fastest Ubuntu mirror for your region, whether you’re in the US, Europe, Asia, or anywhere else.

By default, Ubuntu uses international repositories (archive.ubuntu.com). To improve download speeds, you should select a mirror geographically close to your server location. Below are backup steps and ready-to-use commands for various regions worldwide.


1. Backup the Original Configuration (Mandatory)

Run the appropriate command for your Ubuntu release.

Ubuntu 18.04 / 20.04 / 22.04

cd /etc/apt/ && cp sources.list sources.list.bak

Ubuntu 24.04 and later

cd /etc/apt/sources.list.d/ && cp ubuntu.sources ubuntu.sources.bak

2. Quick Methods to Find and Apply the Fastest Mirror

Method A: Simple Country Code Replacement

Replace the country code in the default mirror URL with your own. The format is http://XX.archive.ubuntu.com/ubuntu/ where XX is your country code .

Examples:

United States

# Ubuntu 18-22
sed -i 's/us.archive.ubuntu.com/us.archive.ubuntu.com/g' /etc/apt/sources.list
# Ubuntu 24+
sed -i 's/us.archive.ubuntu.com/us.archive.ubuntu.com/g' /etc/apt/sources.list.d/ubuntu.sources

Germany (Europe)

# Ubuntu 18-22
sed -i 's/archive.ubuntu.com/de.archive.ubuntu.com/g' /etc/apt/sources.list
# Ubuntu 24+
sed -i 's/archive.ubuntu.com/de.archive.ubuntu.com/g' /etc/apt/sources.list.d/ubuntu.sources

Australia

# Ubuntu 18-22
sed -i 's/archive.ubuntu.com/au.archive.ubuntu.com/g' /etc/apt/sources.list
# Ubuntu 24+
sed -i 's/archive.ubuntu.com/au.archive.ubuntu.com/g' /etc/apt/sources.list.d/ubuntu.sources

Singapore (Southeast Asia)

# Ubuntu 18-22
sed -i 's/archive.ubuntu.com/sg.archive.ubuntu.com/g' /etc/apt/sources.list
# Ubuntu 24+
sed -i 's/archive.ubuntu.com/sg.archive.ubuntu.com/g' /etc/apt/sources.list.d/ubuntu.sources

Japan

# Ubuntu 18-22
sed -i 's/archive.ubuntu.com/jp.archive.ubuntu.com/g' /etc/apt/sources.list
# Ubuntu 24+
sed -i 's/archive.ubuntu.com/jp.archive.ubuntu.com/g' /etc/apt/sources.list.d/ubuntu.sources

Method B: Use Mirror Protocol (Automatically Selects Country Mirror)

This method lets Ubuntu automatically select a mirror in your country .

Ubuntu 18-22

sed -i -e 's/http:\/\/.*archive.ubuntu.com\/ubuntu\//mirror:\/\/mirrors.ubuntu.com\/mirrors.txt/' /etc/apt/sources.list

Ubuntu 24+

# For Ubuntu 24+, manual editing is recommended as the sources format is different
# You'll need to replace the URIs line in ubuntu.sources with:
# URIs: mirror://mirrors.ubuntu.com/mirrors.txt

Method C: Using mirrorselect Tool (Recommended)

mirrorselect tests TCP latency and actual download speeds to find the fastest mirror .

Install mirrorselect

sudo snap install mirrorselect

Auto-select fastest mirror for your location

sudo mirrorselect

Select fastest HTTPS mirror in the US

sudo mirrorselect --protocol https --country US

Select fastest mirror in Europe (Germany example)

sudo mirrorselect --country DE

Select fastest mirror in Asia (Singapore)

sudo mirrorselect --country SG

Test top 10 mirrors with custom timeout

sudo mirrorselect --max 10 --timeout 800

Method D: Using faster-ubuntu-mirror Script

This bash script automatically tests and selects the fastest mirrors based on country codes .

Download and prepare the script

wget https://raw.githubusercontent.com/ezhkov-ph/faster-ubuntu-mirror/main/run.sh
chmod +x run.sh
sudo apt install wget bc  # Install dependencies

Auto-select fastest mirror (recommended)

sudo ./run.sh -a

Select fastest mirror from specific countries

United States only:

sudo ./run.sh -c US -a

Europe (Germany and France):

sudo ./run.sh -c DE FR -a

Asia Pacific (Singapore, Japan, Australia):

sudo ./run.sh -c SG JP AU -a

Test with larger file size for accuracy:

sudo ./run.sh -c US -s 500 -a

Select top 3 fastest mirrors from a region:

sudo ./run.sh -c DE -n 3 -a

Method E: Using mircheck (Python Tool)

Simple Python tool to find fastest mirrors .

Install

pip install mircheck

Find fastest mirror for Ubuntu 22.04 (Jammy) in the US

mircheck jammy us

Find fastest mirror in Germany and save to file

mircheck jammy de -o sources.list

3. Verify and Update

After applying any method above, update your package lists:

sudo apt update

Check download speeds with:

sudo apt upgrade --download-only  # Tests download speed without installing

4. List of Common Country Codes

Country/RegionCode
United StatesUS
CanadaCA
United KingdomGB
GermanyDE
FranceFR
NetherlandsNL
AustraliaAU
JapanJP
SingaporeSG
South KoreaKR
IndiaIN
BrazilBR

5. Restore Backup if Needed

Ubuntu 18-22

cp /etc/apt/sources.list.bak /etc/apt/sources.list

Ubuntu 24+

cp /etc/apt/sources.list.d/ubuntu.sources.bak /etc/apt/sources.list.d/ubuntu.sources

Choose the method that best suits your needs. For most users, Method C (mirrorselect) or Method D (faster-ubuntu-mirror) provide the best automated results. For simple manual selection, use Method A with your country code.

Share


Categories