Home / News / Avoid Pacman Headaches on Arch Linux by Automating Mirror List Updates

Avoid Pacman Headaches on Arch Linux by Automating Mirror List Updates

160
The Arch Linux mirror list generator web page

Key Takeaways

  • Mirrors are servers that hold software packages in Linux repositories. Keeping your mirror list up-to-date is crucial for application installations and system updates.
  • Updating the Arch mirror list manually involves generating a list of mirrors in your region, pasting it into a file, and saving it for pacman to use.
  • Reflector is a utility that generates mirror lists and updates the mirrorlist file. It can be used on the command line or as a service with customizable options.



Mirrors are servers that replicate a Linux distribution’s repositories. Arch Linux has many mirrors situated around the globe. We show you two ways to select which mirrors your Arch Linux computer uses.


Why Mirrors Matter

All the software packages available to users of a Linux distribution are held in repositories. Repositories are simply internet-accessible servers. When you install an application, your package manager has to connect to a repository so that it can retrieve the installation files.


Like all cloud-based resources, repositories face challenges with bandwidth and availability. Too many connections and network traffic and the server can become bogged down and sluggish. Hardware failures or scheduled maintenance can take a repository offline.


Distributions use a network of copycat repositories located around the world. These allow faster connections to users by providing repositories in their regions, instead of forcing everyone to connect to the main repository.


It’s important to make sure the list of mirrors your computer uses is up-to-date because application installations and system updates depend on them.

Updating the Arch Mirror List Manually

By default, Arch doesn’t automatically update the mirrors list. It creates a mirror list at install time, but unless you take action yourself, that list is never changed.


Updating the list manually works, but it’s not convenient. Automating the process is the best solution. But you can, if you want, update your mirror list by hand.


The place to start is the Arch Linux Pacman Mirrorlist Generator. Our objective is to get a list of the mirrors in your region and some from other regions for redundancy.



I’m selecting “United Kingdom” from the scrolling list and “HTTPS” and “IPV4” from the checkboxes. I’ve selected the “Use Mirror Status” checkbox, so only active mirrors are included in the results.


To see the results, click the “Generate List” button.


The matching mirrors are listed. Copy this text and paste it into your editor. Note that all lines start with a hash character “#”, meaning they are treated as comments. To activate a mirror, remove the hash from the start of its line.



You can repeat this process for other regions, pasting the results to your editor each time. I also selected mirrors in Germany and Sweden. That way, should the UK mirrors be down or inaccessible, pacman will try to use the mirrors from Sweden and Germany.


pacman reads its mirrors from a file called “/etc/pacman.d/mirrorlist.” You need to edit that file and replace its contents with the new list.

sudo gedit /etc/pacman.d/mirrorlist 


Replace “gedit” with your favorite editor. Copy and paste the list you’ve just created into the mirrorlist file, replacing the original contents. Save the file and close your editor.


pacman will now use our new list.

What Is Reflector, and What Does It Do?

Reflector is a utility for generating mirror lists, and optionally updating the mirrorlist file. You can use it on the command line, or as a service.


You can pass parameters to it to choose the regions you want to use mirrors from, and you can have the results ranked by, say, download speed.


In Arch Linux, Reflector isn’t installed by default, but in other Arch-based distributions, it might be.

Installing Reflector on Arch Linux

Installing Reflector is simple, as long as you have a working mirror list. If you don’t, pacman won’t work. If that’s the case, you’ll have to go through the steps above to manually create a working mirror list.


The pacman command is:

sudo pacman -S reflector 

Using Reflector on the Command Line

Using Reflector on the command line can overwrite your existing mirror list, so if you want to preserve your existing mirror list as a backup, make a copy of it before you start.

sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/old-mirrorlist 


This example doesn’t overwrite your mirror list.

reflector  


The options we used were:


  • verbose: Give a more detailed output, where possible.
  • ipv4: Select mirrors that support the IPv4 protocol.
  • protocol: Select mirrors that support the specified protocol, such as HTTP, HTTPS, or FTP.
  • score: Select a number of mirrors that have the best scores. Each mirror gets a score, with lower scores being better than higher scores. This is calculated from connection delay times, average connection duration, and the percentage of successfully completed test connections. Note that the “10” in the command line refers to how many best-scored mirrors we want returned. It doesn’t refer to the score itself.
  • sort: Sorts the results. We have opted to sort by download rate. pacman tries the mirrors in the mirror list from top to bottom, until it finds one that is working, so it makes sense to have the fastest mirror first in the list.


Reflector supports a lot of command-line options. You won’t find them on Reflector’s man page though, you need to use its help option:

reflector  


As we’ve seen, omitting the –save option lets you dry-run Reflector commands without putting your existing mirror list at risk. Let’s include the –save option so that we update our mirrorlist file. You need to use sudo when using this option.

sudo reflector  


Our new options are:


  • country: Select the regions we wish to include mirrors from. You can use country codes or country names.
  • latest: We want to use the 20 most recently updated mirrors.
  • download-timeout: Sets the duration in seconds before Reflector considers a repository offline.
  • save: The file the results should be written to. The default location on Arch is “/etc/pacman.d/mirrorlist.”


You can view your mirror list using the cat or less commands.

less /etc/pacman.d/mirrorlist 


Reflector writes a timestamped header, so you can see when the last update happened.


Using the Reflector Service

Reflector provides a service and a timer. If you enable and start the reflector.service, it’ll update your mirror list whenever you boot your computer. The downside is slower boot times.


A better solution is to enable and start the reflector.timer instead. It’ll run the reflector.service once a week for you.


sudo systemctl enable reflector.timer


sudo systemctl start reflector.timer



To edit the Reflector configuration file, use your favorite editor in this command:

sudo gedit /etc/xdg/reflector/reflector.conf 


You can see that the command-line options are listed on separate lines.



You can change their values or add in the ones you want to use.


To edit the configuration file for the timer, use:

sudo gedit /usr/lib/systemd/system/reflector.timer 


We’ve described how to configure timers in-depth in another article.

Use the Timer and Update When You Want

You can manually update your mirror list at any time by running Reflector on the command line. If you turn the command into an alias or a Bash shell function, you won’t need to remember all the parameters.

Comments