Retrieve Latest Mirror List Using Reflector In Arch Linux

Maintaining up-to-date mirror list in your Arch Linux gives some benefits. If you use updated mirrorlist, you could easily avoid slow download rate, and timed-out error messages while installing, and updating packages. This tutorial explains what is Reflector and how to retrieve latest mirror list in Arch Linux using Reflector.

What is Reflector?

Reflector is a simple, Python 3 module and script that checks and retrieves the most up-to-date mirrorlist sorted by speed from Mirror Status page, and saves them in /etc/pacman.d/mirrorlist file in your Arch Linux system.

Generally the /etc/pacman.d/mirrorlist file will containe one mirror server per country. However, there should be more than just one server per country, but the mirror list file will usually have only one.

So, how do you add or retrieve the latest and update-to-date mirror list of your choice in Arch Linux? This is where Reflector comes in help.

Reflector will retrieve and filter a list of the latest as well as fastest Arch Linux mirrors based on the speed, country, and age (last server synchronization).

Reflector is free and open source Python script, which works just fine on Arch Linux and its derivatives such as EndeavourOS and Manjaro Linux.

1. Install Reflector in Arch Linux, EndeavourOS, Manjaro

To install Reflector in Arch Linux and its variants such as EndeavourOS and Manjaro, run the following command from your Terminal:

$ sudo pacman -S reflector rsync curl

The above command will install Reflector along with its dependencies rsync and curl packages.

2. Retrieve latest mirror list using Reflector in Arch Linux

2.1. Backup current mirror list

Before running Reflector, you must backup your default mirror list file. Because, Reflector will overwrite it and replace the existing mirror list with a new one.

To backup the current mirror list, run:

$ sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak

2.2. Retrieve latest N mirrors and sort by download rate

Let us retrieve top 20 latest mirrors and sort the mirror list by their download rate, and save them in /etc/pacman.d/mirrorlist file using command:

$ sudo reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist

Please note that this will replace the existing mirrors with new mirrors.

Let us break down the above command and see what each option does.

  • --latest 20 – fetch the most recently synchronized servers
  • --protocol https – retrieve https enabled mirrors
  • --sort rate – sort mirrorlist based on the download rate
  • --save /etc/pacman.d/mirrorlist

You can verify it by listing the content of /etc/pacman.d/mirrorlist file.

$ cat /etc/pacman.d/mirrorlist

Sample output:

 ############################################################
 ########### Arch Linux mirrorlist generated by Reflector  ###########
 ############################################################
 With: reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
 When: 2021-06-10 09:51:51 UTC
 From: https://www.archlinux.org/mirrors/status/json/
 Retrieved:  2021-06-10 09:49:34 UTC
 Last Check: 2021-06-10 09:15:02 UTC
 Server = https://mirror.osbeck.com/archlinux/$repo/os/$arch
 Server = https://asia.mirror.pkgbuild.com/$repo/os/$arch
 Server = https://archlinux.thaller.ws/$repo/os/$arch
 Server = https://arch.mirror.constant.com/$repo/os/$arch
 Server = https://mirror.f4st.host/archlinux/$repo/os/$arch
 Server = https://phinau.de/arch/$repo/os/$arch
 Server = https://mirrors.neusoft.edu.cn/archlinux/$repo/os/$arch
 Server = https://mirror.pseudoform.org/$repo/os/$arch
 Server = https://arch.mirror.square-r00t.net/$repo/os/$arch
 Server = https://mirror.pkgbuild.com/$repo/os/$arch
 Server = https://archlinux.uk.mirror.allworldit.com/archlinux/$repo/os/$arch
 Server = https://archlinux.za.mirror.allworldit.com/archlinux/$repo/os/$arch
 Server = https://mirror.telepoint.bg/archlinux/$repo/os/$arch
 Server = https://mirror.lty.me/archlinux/$repo/os/$arch
 Server = https://mirror.chaoticum.net/arch/$repo/os/$arch
 Server = https://mirror.efect.ro/archlinux/$repo/os/$arch
 Server = https://archmirror.it/repos/$repo/os/$arch
 Server = https://archlinux.mailtunnel.eu/$repo/os/$arch
 Server = https://archlinux.qontinuum.space:4443/$repo/os/$arch
 Server = https://ftp.halifax.rwth-aachen.de/archlinux/$repo/os/$arch

Now, you can install, update, and upgrade packages from the most update-to-date mirrors.

$ sudo pacman -Syu
$ sudo pacman -S <package_name>

2.3. Retrieve latest N mirrors and sort by age

To retrieve the top mirrors and sort them by their age i.e last server synchronization, run:

$ sudo reflector --latest 20 --protocol https --sort age --save /etc/pacman.d/mirrorlist

2.4. Retrieve country-specific mirror list

You can also restrict mirrors to selected countries. For example, if you live India, you might want to update the mirrorlist with the servers only from India.

To list all available Arch Linux mirrors world-wide, run:

$ reflector --list-countries

Sample output:

Country Code Count
---------------------- ---- -----
Australia AU 19
Austria AT 6
Bangladesh BD 1
Belarus BY 4
Belgium BE 3
Bosnia and Herzegovina BA 2
Brazil BR 11
Bulgaria BG 11
Canada CA 20
Chile CL 8
[...]

As you see in the above output, reflector lists the available mirrors in the world along with country code and the number of mirrors in each country.

As of writing this guide, the most number of Arch Linux mirrors are located in USA (134 mirrors), Germany (114), and Netherlands (51).

To retrieve a mirror from a specific country, you can use the country name or country code, or a mix of both.

The following command will retrieve top 5 mirrors from India, sort them by the download rate, and save them to the mirrorlist file.

$ sudo reflector --verbose --country 'India' -l 5 --sort rate --save /etc/pacman.d/mirrorlist

The following is the brief summary of what these flags in the above command will do.

  • –verbose – prints more information than normal.
  • –country India – my preferred mirror location.
  • –l 5 – retrieves top five mirrors of India.
  • –sort rate – sorts by download rate.
  • –save – save the downloaded mirrorlist to /etc/pacman-d/mirrorlist.

Let us verify the updated mirror list using command:

$ cat /etc/pacman.d/mirrorlist

Sample output:

################################################################################
################# Arch Linux mirrorlist generated by Reflector #########################
################################################################################ # With: reflector --verbose --country India -l 5 --sort rate --save /etc/pacman.d/mirrorlist
# When: 2021-06-10 10:27:06 UTC
# From: https://www.archlinux.org/mirrors/status/json/
# Retrieved:  2021-06-10 10:26:49 UTC
# Last Check: 2021-06-10 09:15:02 UTC

Server = http://mirrors.piconets.webwerks.in/archlinux-mirror/$repo/os/$arch
Server = https://mirrors.piconets.webwerks.in/archlinux-mirror/$repo/os/$arch
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch

Unfortunately, there are only three Arch Linux mirrors available in India.

2.5. Retrieve mirror list from multiple countries

You can also specify multiple countries with comma-separated like below.

$ sudo reflector --country 'United States,India' -l 10 --age 12 --protocol https --sort rate --save /etc/pacman.d/mirrorlist 

This command will select the top 10 HTTPS mirrors synchronized within the last 12 hours and located in either United States or India, sort them by download speed, and overwrite the file /etc/pacman.d/mirrorlist file.

2.6. Retrieve country-sorted mirror list

To get all country-sorted list run the following command:

$ sudo curl -o /etc/pacman.d/mirrorlist https://archlinux.org/mirrorlist/all/

After retrieving the country-sorted mirrors, edit your /etc/pacman.d/mirrorlist file:

$ sudo nano /etc/pacman.d/mirrorlist

Uncomment the mirror URL of your choice to enable it.

For example, I enabled the mirrors from India. So, I removed the (#) symbol in-front of each URL.

[...]
India
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
Server = http://mirrors.piconets.webwerks.in/archlinux-mirror/$repo/os/$arch
Server = https://mirrors.piconets.webwerks.in/archlinux-mirror/$repo/os/$arch
[...]

Save and close the file. Update repository lists using command:

$ sudo pacman -Syu

2.7. Select fastest mirrors

Reflector can retrieve a fastest mirror for your Arch Linux.

The following command retrieves the 5 fastest 10 HTTPS mirrors located in United States, sorts them by download speed, and overwrites the file /etc/pacman.d/mirrorlist file.

$ sudo reflector -f 5 --country 'United States' --protocol https --sort rate --save /etc/pacman.d/mirrorlist 

2.8. Specify protocol

You can select the up-to-date mirrors that supports a specific type of protocol, for example https, http, ftp etc.

For example, this command will select 5 most up-to-date mirrors from United States that support HTTP and override /etc/pacman.d/mirrorlist file:

$ sudo reflector --latest 5 --protocol http --country 'United States' --save /etc/pacman.d/mirrorlist

The other supported options are --isos which will only return mirrors that host ISOs, --ipv4 that will only return mirrors that support IPv4, and --ipv6 only returns mirrors that support IPv6.

3. Update Arch Linux mirror list automatically on boot

Reflector includes systemd service and timer units that can be used to automatically update Pacman’s mirrorlist.

3.1. Enable and start reflector systemd service

Starting from version 2020.8, Reflector ships with reflector.service. To update your Arch Linux mirror list automatically on every reboot, simply enable and start this service using commands:

$ sudo systemctl enable reflector.service
$ sudo systemctl start reflector.service

The reflector.service will run reflector with the parameters specified in /etc/xdg/reflector/reflector.conf file.

The default settings are just enough for most of the time. Here is the contents of reflector.conf file:

$ cat /etc/xdg/reflector/reflector.conf

Sample output:

#
# Empty lines and lines beginning with "#" are ignored.  All other lines should
# contain valid reflector command-line arguments. The lines are parsed with
# Python's shlex modules so standard shell syntax should work. All arguments are
# collected into a single argument list.
#
# See "reflector --help" for details. # Recommended Options # Set the output path where the mirrorlist will be saved (--save).
--save /etc/pacman.d/mirrorlist # Select the transfer protocol (--protocol).
--protocol https # Select the country (--country).
# Consult the list of available countries with "reflector --list-countries" and
# select the countries nearest to you or the ones that you trust. For example:
# --country France,Germany # Use only the  most recently synchronized mirrors (--latest).
--latest 5 # Sort the mirrors by synchronization time (--sort).
--sort age

You can also edit this file and change the parameters as per your requirements. This file accepts all of the reflector command-line arguments.

You can group the arguments together on a single line or split across many lines, with optional blank lines and comment lines beginning with #.

3.2. Enable and start reflector systemd timer

Reflector ships another systemd unit named reflector.timer, which starts the reflector.service weekly.

To enable and start it automatically on system boot, run the following commands one by one:

$ sudo systemctl enable reflector.timer
$ sudo systemctl start reflector.timer

By default, it will start reflector.service once in a week. If you don’t want to wait for the scheduled time, you can start the reflector.service to immediately update the pacman’s mirror list:

$ sudo systemctl start reflector.service

Conclusion

In this guide, we learned what is Reflector, how to retrive up-to-date Pacman mirror list for your Arch Linux system using Reflector script and finally how to update Arch Linux mirror list automatically.

Resource:

Related read: