How to select the fastest apt mirror on Ubuntu Linux

The following guide will provide you with some information on how to improve Ubuntu’s repository download speed by selecting the closest, that is, possibly fastest mirror relative to your geographical location.

Country Code

The simplest approach is to make sure that your Ubuntu mirror defined within /etc/apt/sources.list includes a relevant country code appropriate to your location. For example, below you can find a official United States Ubuntu mirror as found in /etc/apt/sources.list:

deb xenial main restricted

If you are not located in United States simply overwrite the us country code with appropriate code of your country. That is, if your are located for example in Australia update your /etc/apt/sources.list file for all entries as:

deb xenial main restricted

Use mirror protocol

Using mirror protocol as part of your /etc/apt/sources.list entry will instruct apt command to fetch mirrors located within your country only. In order to use mirror protocol update all lines within /etc/apt/sources.list file from the usual eg.:

deb xenial main restricted


deb mirror:// xenial main restricted

Repeat the above for all relevant lines where appropriate. Alternatively, use sed command to automatically edit your /etc/apt/sources.list file. Update the below sed command where appropriate to fit your environment:

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

Manual apt mirror selection

The above solutions look easy and they might just work for you. However, the mirror selected by apt may not be the fastest as it can be burdened by high latency. In this case you may try to choose your mirror manually from the list of mirrors located within your country. Use wget command to retrieve the list. The below wget command will retrieve apt ubuntu mirrors related to your country. Example:

$ wget -qO -

Based on your experience select the best mirror and alter your /etc/apt/sources.list apt configuration file appropriately.

Choosing the fastest mirror with netselect

This solution is preferred, as it guarantees the fastest mirror selection. For this we are going to use netselect command. The netselect package is not available within Ubuntu’s standard repository by default, so we will need to borrow it from Debian stable repository:

$ sudo apt-get install wget
$ wget
$ sudo dpkg -i netselect_0.3.ds1-26_amd64.deb

Once you have the netselect command available on your Ubuntu system use it to locate the fastest mirror based on the lowest icmp latency. The netselect output will be relative to your location. The below example output will show top 20 apt Ubuntu mirrors ( if available ):

$ sudo netselect -s 20 -t 40 $(wget -qO -
Only found 13 hosts out of 20 requested.

Alter manually your /etc/apt/sources.list file to reflect the above netselect results or use sed command, where the lower score number on the left represents a higher mirror transfer rate. Example:

$ sudo sed -i 's/http:\/\/\/ubuntu\//http:\/\/\/archive\//' /etc/apt/sources.list

Comparing results

The following are my apt-get update command results, while located within Australia:

Fetched 23.1 MB in 20s (1148 kB/s) 

MIRROR protocol( mirror://
Fetched 23.1 MB in 4min 45s (81.0 kB/s)

Fetched 23.1 MB in 12s (1788 kB/s)

NETSTAT Auto-Selected ( ):
Fetched 23.1 MB in 6s (3544 kB/s)