The first important thing to do after installing a fresh Ubuntu OS is to choose the best and fastest APT archive mirror. In this brief guide, I have listed all possible ways to find best Ubuntu APT repository mirror based on download speed. The mirrors may not always be the closest to your location but offers a best possible download rate and update status.
Table of Contents
Why should I find a best APT mirror?
If your system is connected to Internet at the time of installation, the installer will automatically select a nearest mirror depending on your location and ISP. However if the Internet is not available, the Main repository will be selected. If you live outside of US, installing packages from Main repository might be painfully slow. This is why it is must to choose a best and also a nearest mirror for your Ubuntu system as described in the subsequent section.
Find Best Ubuntu APT Repository Mirror
Before moving further, it is highly recommended to backup the existing sources.list file:
$ sudo cp /etc/apt/sources.list{,.backup}
Now let us find out the best APT archive mirror. We can do this in GUI way and CLI way.
The graphical way:
If you are on Ubuntu desktop, open "Software & updates" window:
Under the Ubuntu Software tab, choose "Other" in "Download from" drop-down box and then choose "Select Best Server" option.
A series of tests will be performed to find a mirror that gives you best download speed for your location. It will take just a few seconds. Once the best mirror is found, click "Choose server" option.
Close the Software & updates window. You will be prompted to reload the software lists. Click "Reload" button to update the repository lists. Now your sources.list is automatically updated with best and fastest Ubuntu repository mirror URLs.
This is my preferred way to find a best Apt mirror for my Ubuntu desktop. Even though I live in India, it suggested me best mirrors from USA, UK and Italy countries. So, I guess your geographic location does not always give the best mirror.
The command line way:
There are many command line tools available to find the best APT mirrors based on download speed. I have tested the following tools and they are working just fine in my Ubuntu 20.04 LTS desktop.
- Apt-select
- Apt-smart
All of the aforementioned tools are written in Python. Hence they can be installed using Pip package manager. If you haven't installed Pip yet, install it using command:
$ sudo apt install python3-pip
1. Find Best Ubuntu APT Repository Mirror Using Apt-select
Apt-select is a command line tool to find a fast and up-to-date Ubuntu archive mirror. It generates sources.list file for you based on your country or based on top ranked mirrors by download rate.
Make sure you have installed Pip and run the following command to install apt-select:
$ pip3 install apt-select
Now run the following command to find top apt mirror for a given country:
$ apt-select --country IN
Or,
$ apt-select -C IN
Note: Here C is capital.
Sample output:
Getting list of mirrors...done. Testing latency to mirror(s) [7/8] 87% connection to ubuntu.mirror.snu.edu.in: timed out [8/8] 100% Getting list of launchpad URLs...done. Looking up 1 status(es) 1. ubuntu-archive.mirrors.estointernet.in Latency: 85.85 ms Org: Esto Internet Status: Up to date Speed: 10 Gbps Selecting mirror http://ubuntu-archive.mirrors.estointernet.in/ ... New config file saved to /home/ostechnix/sources.list
The above command returns only one top mirror from India and generate a new sources.list and save it in the current directory.
If no country code is provided, the US mirrors will be fetched by default.
Display top N mirrors
You can also generate sources.list from top N mirrors using -t flag. By default, it displays only one top mirror.
$ apt-select -t 3
Sample output:
WARNING: no country code provided. defaulting to US. Getting list of mirrors...done. Testing latency to mirror(s) [85/85] 100% Getting list of launchpad URLs...done. Looking up 3 status(es) [3/3] 100% 1. la-mirrors.evowise.com Latency: 40.64 ms Org: Evowise.com Status: Up to date Speed: 10 Gbps 2. mirrors.xtom.com Latency: 246.64 ms Org: xTom Status: Up to date Speed: 20 Gbps 3. mirrors.advancedhosters.com Latency: 253.92 ms Org: AdvancedHosters Status: Up to date Speed: 2 Gbps Selecting mirror http://la-mirrors.evowise.com/ubuntu/ ... New config file saved to /home/ostechnix/sources.list
Display mirrors based on status
Apt-select can display results on mirrors' status with -m flag. The choices are:
- up-to-date,
- one-day-behind,
- two-days-behind,
- one-week-behind,
- unknown.
By default, apt-select will return up-to-date mirrors.
The following commands returns mirrors that are one-week behind:
$ apt-select -m one-week-behind
We can combine multiple options and generate sources.list file. For example, the following command will return top 3 mirrors, including those last updated a week ago:
$ apt-select -c -t 3 -m one-week-behind
Rank mirrors by latency
To list top 3 mirrors by latency, excluding the statuses, use -p flag:
$ apt-select -t 3 -p
Choose mirrors from a list
This command allows you to choose one mirror from top 3 US mirrors and generates the sources.list.
$ apt-select --choose -t 5 --country US
Or shortly,
$ apt-select -c -t 5 -C US
Note: Here, the small c indicates "choose" and the capital C indicates the country code.
Choose any one mirror from the list by entering the appropriate number:
Display mirrors but don't generate sources.list file
The following command returns the top 3 mirrors, but won't generate the sources.list file:
$ apt-select -t 3 -l
To find 5 mirrors, but don't want to generate sources.list and exclude statuses, do:
$ apt-select -t 5 -p -l
Update your system's sources.list file
After generating the sources.list file as per your requirement, just replace the original one (/etc/apt/sources.list) with the new one.
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
$ sudo mv sources.list /etc/apt/
For help, run:
$ apt-select --help
Refer the Apt-select GitHub repository for more details:
2. Find Best Ubuntu APT Repository Mirror Using Apt-smart
Apt-smart is yet another command line tool written in Python. It helps you to find APT mirrors that provides best download rate for your location. It can smartly retrieve the mirrors by querying the Debian mirror list, Ubuntu mirror list and Linux mint mirror list and choose best mirror based on the country in which the user lives in. The discovered mirrors are ranked by bandwidth and their status (like up-to-date, 3-hours-behind, one-week-behind etc).
Another notable feature of Apt-smart is it will automatically switch to any other different mirrors when the current mirror is being updated. The new mirrors can be selected either automatically or manually by the user. Good thing is Apt-smart will backup the current sources.list before updating it with new mirrors.
Install Apt-smart in Ubuntu
Make sure you have installed Pip and run the following commands one by one to install Apt-smart:
$ pip3 install --user apt-smart
$ echo "export PATH=\$(python3 -c 'import site; print(site.USER_BASE + \"/bin\")'):\$PATH" >> ~/.bashrc
$ source ~/.bashrc
Display current mirror's details
To list the current main mirror configured in the /etc/apt/sources.list file and display its details in the standard output, run:
$ apt-smart --find-current-mirror
Or shortly:
$ apt-smart -f
Sample output:
http://in.archive.ubuntu.com/ubuntu
Find best mirror
To discover available mirrors, rank them and choose a best one from the list and display it in the standard output, run:
$ apt-smart --find-best-mirror
Or,
$ apt-smart -b
Sample output:
2020-07-02 07:13:41 ubuntuserver apt_smart[1613] INFO Ubuntu 20.04 (focal) is supported (based on known EOL dates).
2020-07-02 07:13:41 ubuntuserver apt_smart[1613] INFO Adding BASE_URL mirror:
2020-07-02 07:13:41 ubuntuserver apt_smart[1613] INFO http://archive.ubuntu.com/ubuntu
2020-07-02 07:13:41 ubuntuserver apt_smart.backends.ubuntu[1613] INFO Identifying fast Ubuntu mirrors using http://mirrors.ubuntu.com/mirrors.txt ..
2020-07-02 07:13:42 ubuntuserver apt_smart.backends.ubuntu[1613] INFO Discovered 8 Ubuntu mirrors in 0.8 seconds.
2020-07-02 07:13:42 ubuntuserver apt_smart[1613] INFO Checking 9 mirrors for availability and performance ..
2020-07-02 07:13:44 ubuntuserver apt_smart[1613] INFO Start retrieving :attr:`base_last_updated` using is_available
2020-07-02 07:13:44 ubuntuserver apt_smart[1613] INFO Checking 8 mirrors for Archive-Update-in-Progress marker ..
2020-07-02 07:13:52 ubuntuserver apt_smart[1613] INFO Finished checking 9 mirrors (took 10.55 seconds).
http://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu
List all mirrors based on rank
To list all available ranked mirrors in the terminal, run:
$ apt-smart --list-mirrors
Or,
$ apt-smart -l
Sample output:
Change mirrors
Once you found the best APT mirror, you can update your /etc/apt/sources.list to use the given mirror like below:
$ apt-smart --change-mirror http://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu
Or,
$ apt-smart -c http://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu
Sample output:
Automatically update mirrors
Instead of manually finding and updating the best mirror in Ubuntu, you can let Apt-smart to choose a best Apt mirror and automatically update the sources.list with new one like below:
$ apt-smart --auto-change-mirror
To get help, run:
$ apt-smart --help
For more details, refer Apt-smart GitHub Repository.
Related read: