How to select the fastest apt mirror on Ubuntu Linux

The purpose of this tutorial is to 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.

In this tutorial you will learn:

  • How to select fastest mirror via country code
  • How to select faster mirror with mirror protocol
  • How to perform manual mirror selection
  • How to use netselect to detmine best mirror
  • How to configure a different mirror in GNOME and KDE Plasma GUI
How to select the fastest apt mirror on Ubuntu Linux
How to select the fastest apt mirror on Ubuntu Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Select the fastest mirror step by step instructions



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


There are multiple ways to do this, all covered below. You should read through each one and determine which method sounds most convenient for you and fits your environment the best.

Country Code

  1. 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 http://us.archive.ubuntu.com/ubuntu/ focal main restricted
    
  2. 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 http://au.archive.ubuntu.com/ubuntu/ focal main restricted
    

Use mirror protocol

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

    To:

    deb mirror://mirrors.ubuntu.com/mirrors.txt focal main restricted
    
  2. 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 - mirrors.ubuntu.com/mirrors.txt

http://mirror.netspace.net.au/pub/ubuntu/
http://mirror.internode.on.net/pub/ubuntu/ubuntu/
http://mirror.overthewire.com.au/ubuntu/
http://mirror.aarnet.edu.au/pub/ubuntu/archive/
http://mirror.tcc.wa.edu.au/ubuntu/
http://ubuntu.mirror.serversaustralia.com.au/ubuntu/
http://ftp.iinet.net.au/pub/ubuntu/
http://ubuntu.mirror.digitalpacific.com.au/archive/
http://mirror.waia.asn.au/ubuntu/
http://ubuntu.uberglobalmirror.com/archive/
http://mirror.as24220.net/pub/ubuntu/
http://mirror.as24220.net/pub/ubuntu-archive/

You can see a list of available Ubuntu mirrors on the official archive mirrors website. Find your country, click on some of the servers, and perform a ping or traceroute test to see how speedy the connection is.

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.

  1. 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 install wget
    $ wget http://ftp.us.debian.org/debian/pool/main/n/netselect/netselect_0.3.ds1-29_amd64.deb
    $ sudo dpkg -i netselect_0.3.ds1-29_amd64.deb
    
  2. 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 - mirrors.ubuntu.com/mirrors.txt)
    
      134 https://mirror.us.leaseweb.net/ubuntu/
      142 http://mirrors.advancedhosters.com/ubuntu/
      147 http://ubuntu.osuosl.org/ubuntu/
      188 http://ubuntu.osuosl.org/ubuntu/
      195 http://mirror.us-ny2.kamatera.com/ubuntu/
      199 http://mirror.pit.teraswitch.com/ubuntu/
      211 https://nyc.mirrors.clouvider.net/ubuntu/
      213 http://mirror.brightridge.com/ubuntuarchive/
      213 http://mirror.math.princeton.edu/pub/ubuntu/
      220 http://ubuntu.mirror.constant.com/
      220 https://mirrors.bloomu.edu/ubuntu/
      225 http://ubuntu.mirror.frontiernet.net/ubuntu/
      227 http://www.club.cc.cmu.edu/pub/ubuntu/
      231 http://mirrors.tripadvisor.com/ubuntu/
      237 http://mirror.team-cymru.com/ubuntu/
      246 http://mirrors.gigenet.com/ubuntuarchive/
      246 http://mirror.siena.edu/ubuntu/
      250 http://us.mirror.nsec.pt/ubuntu/
      252 http://archive.linux.duke.edu/ubuntu/
      252 https://mirror.jacksontechnical.com/ubuntu/
    
    Using netselect command to determine the best Ubuntu mirror for us
    Using netselect command to determine the best Ubuntu mirror for us
  3. 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:\/\/us.archive.ubuntu.com\/ubuntu\//http:\/\/ubuntu.uberglobalmirror.com\/archive\//' /etc/apt/sources.list
    

Comparing Results

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

US MIRROR ( http://us.archive.ubuntu.com/ubuntu ):
Fetched 23.1 MB in 20s (1148 kB/s) 

MIRROR protocol( mirror://mirrors.ubuntu.com/mirrors.txt):
Fetched 23.1 MB in 4min 45s (81.0 kB/s)

AU MIRROR ( http://au.archive.ubuntu.com/ubuntu ):
Fetched 23.1 MB in 12s (1788 kB/s)

NETSTAT Auto-Selected ( http://ubuntu.uberglobalmirror.com/archive ):
Fetched 23.1 MB in 6s (3544 kB/s)

Change mirror via GUI

If you prefer to use the GUI over command line, and have either the GNOME or KDE desktop envrionment installed, you can use the following sections below to change your Ubuntu downlod mirror.

GNOME



  1. Open the Software & Updates application, then click on the “Download From” drop down box.
    Open the download from box inside the Software & Updates application
    Open the download from box inside the Software & Updates application
  2. Manually scroll through the list and select your desired mirror, or alternatively just click the “Select best server” button to let Ubuntu do the work for you.
    Manually selecting download mirror via GNOME gui
    Manually selecting download mirror via GNOME gui

KDE Plasma

  1. Start by opening the Discover Software Center, then click on Settings and finally the “Software Sources” button.
    Open the Software Sources menu inside of the Discover Software Center application
    Open the Software Sources menu inside of the Discover Software Center application
  2. You will be required to enter your root password, and then you can expand the Download From menu and click on Other.
    Opening the download from box in KDE settings
    Opening the download from box in KDE settings
  3. Manually scroll through the list and select your desired mirror, or alternatively just click the “Select best server” button to let Ubuntu do the work for you.
    Manually selecting download mirror via KDE Plasma gui
    Manually selecting download mirror via KDE Plasma gui

Closing Thoughts



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


In this tutorial, we saw several different methods to choose the fastest download mirror on Ubuntu Linux. We recommend the netselect method as it will give concise results and is very quick after you get the program installed. If you don’t like to fiddle with the command line, you can always let Ubuntu do the job for you and find the best server via GUI.

You usually won’t need to change your download server manually. But it can be beneficial when traveling or if a certain server suddenly becomes slow. Hopefully this will speed up your downloads for you.