Are you trying to use the best Ubuntu APT Repository Mirror?
This guide will help you.
The most important thing to do after installing a new Ubuntu operating system is to choose the best and fastest APT file mirror.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to manage their Ubuntu servers.
In this context, we shall look into how to choose the best APT mirror and add the it to the system.
If our system is connected to the Internet at the time of installation, the installer will automatically select the nearest mirror depending on our location and ISP.
However, if the Internet is not available, the Main repository will be selected. If we live outside of the US, installing packages from the Main repository might be painfully slow.
This is why it is a must to choose the best and also the nearest mirror for our Ubuntu system.
To begin, we need to take the backup of the existing sources.list file using the following command:
$ sudo cp /etc/apt/sources.list{,.backup}
We can find the best mirror using GUI and CLI.
1. First, open the "Software & updates" window.
2. Then choose "Other" in the "Download from" drop-down box and then choose the "Select Best Server" option.
3. Once the best mirror is found, click the "Choose Server" option.
4. After that Close the Software & Updates window.
5. And click the "Reload" button to update the repository lists.
Now our sources.list is automatically updated with the best and fastest Ubuntu repository mirror URLs.
There are many command-line tools available to find the best APT mirrors based on download speed.
We will use two of the following tools:
1. Apt-select
2. Apt-smart
Since the tools are written in Python we will be using Pip package manager.
If we have not installed Pip yet, install it using the following command:
$ sudo apt install python3-pip
Apt-select is a command-line tool to find a fast and up-to-date Ubuntu archive mirror. It generates sources.list file for each country or depending upon top-ranked mirrors by download rate.
First, we can run the following command to find the top apt-mirror for a given country:
$ apt-select --country IN
or
$ apt-select -C IN
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/ibmimedia/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.
To Display top N mirrors:
We 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/ibmimedia/sources.list
To Display mirrors based on status:
Apt-select can display results on mirror's status with -m flag.
The choices are:
i. up-to-date
ii. one-day-behind
iii. two-days-behind
iv. one-week-behind
v. unknown
By default, apt-select will return up-to-date mirrors.
The following commands return 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 the top 3 mirrors, including those last updated a week ago:
$ apt-select -c -t 3 -m one-week-behind
To Rank mirrors by latency:
To list the top 3 mirrors by latency, excluding the statuses, use the following command with -p flag:
$ apt-select -t 3 -p
To Choose mirrors from a list:
The following command allows to choose one mirror from the top 3 US mirrors and generates the sources.list:
$ apt-select --choose -t 5 --country US
or
$ apt-select -c -t 5 -C US
To Update our system’s sources.list file:
After generating the sources.list file as per our requirement, we can replace the original one (/etc/apt/sources.list) with the new one.
We can do this using the following commands:
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
$ sudo mv sources.list /etc/apt/
Apt-smart is yet another command-line tool written in Python. It helps to find APT mirrors that provide the best download rate for our location.
Furthermore, it discovers mirrors by bandwidth and their status. Another notable feature of Apt-smart is it will automatically switch to any other different mirrors when the current mirror is being updated.
To Install Apt-smart in Ubuntu:
First, we have to ensure that Pip package manager is available:
After that 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
To Display current mirror's details:
To list the current main mirror in the /etc/apt/sources.list file and display its details in the standard output, we can run the following command:
$ apt-smart --find-current-mirror
or
$ apt-smart -f
To Find best mirror:
We can discover available mirrors, rank them and choose the best one from the list and display it in the standard output.
Using the following commands we can do this:
$ apt-smart --find-best-mirror
or
$ apt-smart -b
Sample output:
2021-04-02 07:13:41 ubuntuserver apt_smart[1613] INFO Ubuntu 20.04 (focal) is supported (based on known EOL dates).
2021-04-02 07:13:41 ubuntuserver apt_smart[1613] INFO Adding BASE_URL mirror:
2021-04-02 07:13:41 ubuntuserver apt_smart[1613] INFO http://archive.ubuntu.com/ubuntu
2021-04-02 07:13:41 ubuntuserver apt_smart.backends.ubuntu[1613] INFO Identifying fast Ubuntu mirrors using http://mirrors.ubuntu.com/mirrors.txt ..
2021-04-02 07:13:42 ubuntuserver apt_smart.backends.ubuntu[1613] INFO Discovered 8 Ubuntu mirrors in 0.8 seconds.
2021-04-02 07:13:42 ubuntuserver apt_smart[1613] INFO Checking 9 mirrors for availability and performance ..
2021-04-02 07:13:44 ubuntuserver apt_smart[1613] INFO Start retrieving :attr:`base_last_updated` using is_available
2021-04-02 07:13:44 ubuntuserver apt_smart[1613] INFO Checking 8 mirrors for Archive-Update-in-Progress marker ..
2021-04-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
To List all mirrors based on rank:
We can use the following commands for this:
$ apt-smart --list-mirrors
or
$ apt-smart -l
To Change mirrors:
Once we found the best APT mirror, we can update our /etc/apt/sources.list to use the given mirror.
We can do this using the following command:
$ apt-smart --change-mirror http://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu
$ apt-smart -c http://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu
To Automatically update mirrors:
Instead of manually finding and updating the best mirror in Ubuntu, we can let Apt-smart choose the best Apt mirror and automatically update the sources.list with a new one.
We can do this using the following command:
$ apt-smart --auto-change-mirror
This article covers methods to find the best APT mirror on the Ubuntu server.
To 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.
To 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
To List all mirrors based on rank:
To list all available ranked mirrors in the terminal, run:
$ apt-smart --list-mirrors
Or,
$ apt-smart -l
To 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