Find fastest Ubuntu mirror from cmdline

ubuntu_logo_81x81It’s always advisable to keep your Ubuntu installation up to date with the latest packages and security patches. During installation Ubuntu sets the main repository as the source of latest packages. But what if the default is too slow for you because of your geographic location? Of course, you can search for the fastest mirror from the GUI by running software-properties-gtk and select it, but here’s a cool trick to do it faster and with more control from the cmdline.

The tool we are going to use is from Debian – netselect. Variants were available on Ubuntu till Hardy but the latest releases do not have any of the netselect* utilities. It is a tiny utility with no dependencies. To proceed, download the package for your OS architecture from Debian stable. To install netselect, run:

$ sudo dpkg -i netselect_version.deb

Note that netselect chooses the fastest mirrors based on the response time, not the bandwidth available. But in most cases it should be fast enough for regular updates.

Run the following command to list the 4 fastest mirrors for you:

$ sudo netselect -v -s4 -t10 `wget -q -O- | grep -P -B8 "statusUP|statusSIX" | grep -o -P "(f|ht)tp.*\"" | tr '"\n' ' '`

Options used:

-v : verbose
-s4 : get the fastest 4 mirrors
-t10 : 10 minimum retries

wget retrieves the list of mirrors available for Ubuntu from Launchpad. The first grep gets the mirrors which are up-to-date or within a lag of 6 hours, the second grep gets the ftp/http URLs.

The command executes very fast. For me the top 4 results are:


To use these mirrors, backup your /etc/apt/sources.list, and open it in a text editor (I use vim) as sudoer. Search for uncommented lines like:

deb trusty main restricted

As I chose the first mirror, this line becomes:

deb trusty main restricted

Note that the last 2 entries in the file are usually for partner and third-party packages. You should leave them untouched.

Leave a Reply

Your email address will not be published. Required fields are marked *