How do you select the fastest mirror from the command line?

You don't have to do any searching anymore - as ajmitch has explained, you can use deb mirror to have the best mirror picked for you automatically.

apt-get now supports a 'mirror' method that will automatically select a good mirror based on your location. Putting:

deb mirror://mirrors.ubuntu.com/mirrors.txt precise main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-updates main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-backports main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-security main restricted universe multiverse

on the top in your /etc/apt/sources.list file should be all that is needed to make it automatically pick a mirror for you based on your geographical location.

Lucid (10.04), Maverick (10.10), Natty (11.04), and Oneiric (11.10) users can replace precise with the appropriate name.


Here's one way that will always work, using good old netselect and some grep magic:

##The terminal-addict's "find best server" hack!##

  • Download and dpkg -i netselect for your architecture from the Debian website. (it's about 125 KB, no dependencies)

  • Find the fastest Ubuntu mirrors from your location, either up-to-date or at most six hours behind with this (I'll explain it below, sorry it doesn't split up nicely in Markdown)

    sudo netselect -v -s10 -t20 `wget -q -O- https://launchpad.net/ubuntu/+archivemirrors | grep -P -B8 "statusUP|statusSIX" | grep -o -P "(f|ht)tp://[^\"]*"`
    
  • netselect:

    1. -v makes it a little verbose -- you want to see progress dots and messages telling you different mirrors mapping to the same IP were merged :)
    2. -sN controls how many mirrors you want at the end (e.g. top 10 mirrors)
    3. -tN is how long each mirror is speed-tested (default is 10; the higher the number, the longer it takes but the more reliable the results.)
  • This is the backquotes stuff (don't paste, just for explanation)

    wget -q -O- https://launchpad.net/ubuntu/+archivemirrors \
    | grep -P -B8 "status(UP|SIX)" \
    | grep -o -P "(f|ht)tp://[^\"]*"
    
  1. wget pulls the latest mirror status from https://launchpad.net/ubuntu/+archivemirrors.
  2. The first grep extracts mirrors that are up-to-date or six-hours behind, along with 8 lines of previous context which includes the actual ftp/http URLs
  3. The second grep extracts these ftp/http URLs
   60 ftp://mirrors.se.eu.kernel.org/ubuntu/
   70 http://ubuntu.alex-vichev.info/
   77 http://ftp.citylink.co.nz/ubuntu/
  279 http://ubuntu.mirrors.tds.net/pub/ubuntu/
  294 http://mirror.umd.edu/ubuntu/
  332 http://mirrors.rit.edu/ubuntu/
  364 ftp://pf.archive.ubuntu.com/ubuntu/
  378 http://mirror.csclub.uwaterloo.ca/ubuntu/
  399 ftp://ubuntu.mirror.frontiernet.net/ubuntu/
  455 http://ubuntu.mirror.root.lu/ubuntu/
  
  • The "ranks" are an arbitrary metric; lower is usually better.
  • If you're wondering why the kernel.org Sweden-EU mirror and an NZ mirror are in the top three from California, well, so am I ;-) The truth is that netselect doesn't always choose the most appropriate URL to display when multiple mirrors map to a single IP; number 3 is also known as nz.archive.ubuntu.com!

Oneliner that selects the best mirror (by download speed) based on mirrors.ubuntu.com for your ip:

curl -s http://mirrors.ubuntu.com/mirrors.txt | xargs -n1 -I {} sh -c 'echo `curl -r 0-102400 -s -w %{speed_download} -o /dev/null {}/ls-lR.gz` {}' |sort -g -r |head -1| awk '{ print $2  }'