It’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- https://launchpad.net/ubuntu/+archivemirrors | grep -P -B8 "statusUP|statusSIX" | grep -o -P "(f|ht)tp.*\"" | tr '"

' ' '`

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:

313 ftp://ftp.sunet.se/pub/os/Linux/distributions/ubuntu/ubuntu/ 357 http://ftp.uninett.no/ubuntu/ 360 http://mirror.amsiohosting.net/archive.ubuntu.com/ 367 http://ftp.acc.umu.se/ubuntu/

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 http://archive.ubuntu.com/ubuntu trusty main restricted

As I chose the first mirror, this line becomes:

deb ftp://ftp.sunet.se/pub/os/Linux/distributions/ubuntu/ubuntu/ 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.