How to rollback pacman updates in Arch Linux

Arch Linux is often praised for it’s bleeding edge software and rolling release model. We discuss these features more in depth in our article comparing Arch Linux and Manjaro. In addition to this praise, Arch Linux also has a reputation for being unstable. This reputation stems from the sometimes unpredictable nature of bleeding edge software. The latest software from upstream developers may contain bugs that were not apparent during initial testing. As a result, the risk is always present that updating with the package manager, pacman, may bring about unexpected results. These may include a specific piece of software no longer working properly (or at all) or even multiple applications or Desktop Environments no longer working as expected.

Backing up your GNU/Linux system regularly is the best safeguard to mitigate the headache this could cause. There is no shortage of backup solutions for Linux; some of the backup options include dd, BackupPC, rsync, Fsarchiver , rsnapshot. If you have a regular backup solution in place then it can be comforting to know that you could restore from a backup in the event that a pacman update caused issues, but it would be ideal if you didn’t have to. In this article we will show you how to roll back pacman updates in Arch Linux. There are two ways to accomplish this. One way is via the pacman cache; the other is by using the Arch Linux Archive. We will discuss both methods.

In this tutorial you will learn:

  • How to rollback Arch Linux updates using the pacman cache
  • How to rollback Arch Linux updates using the Arch Linux Archive
How to rollback pacman updates in Arch Linux

How to rollback pacman updates in Arch Linux

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Arch Linux
Software pacman
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

How to rollback Arch Linux updates using the pacman cache

Every time that you install or update a package pacman saves this package in /var/cache/pacman/pkg/. pacman also allows you to manually upgrade a package via the -U flag. As it turns out, this upgrade method can also be used to install an older version of the package, effectively downgrading the package.

Downgrading a single package

Suppose you just updated your system via the $ sudo pacman -Syu command and then you open up firefox to browse the web, only to realize that it now crashes whereas it never had before. You can easily downgrade firefox to the previous version that you had installed using the pacman cache.

First, examine the firefox packages listed in /var/cache/pacman/pkg/

$ ls /var/cache/pacman/pkg/firefox*

You should see the latest version you just updated to and any previous versions you had installed. In our example, this is the output we receive.

/var/cache/pacman/pkg/firefox-81.0.1-1-x86_64.pkg.tar.zst
/var/cache/pacman/pkg/firefox-83.0-2-x86_64.pkg.tar.zst

We know that we upgraded from version 81.0.1-1 to 83.0-2 and suspect that it may have caused a problem. To test this theory, simply rollback to the version 81.0.1-1 using the pacman cache with the following command.

$ sudo pacman -U /var/cache/pacman/pkg/firefox-81.0.1-1-x86_64.pkg.tar.zst


You will see the following output.

loading packages...
warning: downgrading package firefox (83.0-2 => 81.0.1-1)
resolving dependencies...
looking for conflicting packages...

Packages (1) firefox-81.0.1-1

Total Installed Size:  206.76 MiB
Net Upgrade Size:       -0.13 MiB

:: Proceed with installation? [Y/n]

Once you type y to continue you will have the previously installed version of firefox installed and can open it to see if the issue has been resolved.

Downgrading multiple packages

Using the pacman cache, you can downgrade multiple packages by providing them as separate arguments to pacman -U.
For example

$ sudo pacman -U /var/cache/pacman/pkg/firefox-81.0.1-1-x86_64.pkg.tar.zst /var/cache/pacman/pkg/alsa-lib-1.2.3.2-1-x86_64.pkg.tar.zst /var/cache/pacman/pkg/adwaita-icon-theme-3.38.0-1-any.pkg.tar.zst

When downgrading multiple packages it can be beneficial to move to the /var/cache/pacman/pkg directory so that you don’t need to type in the full path of each package that you want to downgrade.

$ cd /var/cache/pacman/pkg
$ sudo pacman -U firefox-81.0.1-1-x86_64.pkg.tar.zst alsa-lib-1.2.3.2-1-x86_64.pkg.tar.zst adwaita-icon-theme-3.38.0-1-any.pkg.tar.zst

Most likely, you are beginning to think that downgrading a large number of packages this way would be tedious to say the least. You may find yourself in situations where you suspect that 20 different packages could potentially be causing a problem or you have no idea what packages are causing a problem and you want to downgrade all of them. These scenarios may be better suited to using the Arch Linux Archive, but before we discuss that let’s examine some examples of situations where we could hack together a solution to use the pacman cache for this purpose, without having to manually type each package name.

Downgrading multiple related packages with a similar naming scheme

Suppose you just powered up your machine after not using it for a few weeks. You proceed to update your system via the $ sudo pacman -Syu command and you notice that all of your qt based applications are not functioning correctly, but all other apps are. Because many of the foundational qt packages and libraries have qt in the filename and it is very unlikely that any non-qt packages have this in the filename, it is possible to rollback just the qt updates from the last system upgrade.

Whenever you perform an update using pacman a record of exactly what you upgraded and when is kept in /var/log/pacman.log. An example line follows below.

[2020-11-30T05:39:16-0500] [ALPM] upgraded qt5-xmlpatterns (5.15.1-1 -> 5.15.2-1)


We can use this log to help us systematically downgrade only the qt packages by looking for the lines in the pacman.log that show packages containing qt in the filename which were upgraded today and performing some text manipulation on them and then providing them as arguments to pacman -U in a for loop. The example below assumes that the current date is November 30th 2020. Adjust the date accordingly if you would like to make use of it.

$ grep -a qt /var/log/pacman.log | grep 2020-11-30 > /tmp/qtupdates.txt
$ awk '{print $4}' /tmp/qtupdates.txt > /tmp/lines1;awk '{print $5}' /tmp/qtupdates.txt | sed 's/(/-/g' > /tmp/lines2
$ paste /tmp/lines1 /tmp/lines2 > /tmp/lines
$ tr -d "[:blank:]" < /tmp/lines > /tmp/packages
$ cd /var/cache/pacman/pkg/
$ for i in $(cat /tmp/packages); do sudo pacman --noconfirm -U "$i"*; done

Downgrading all upgraded packages

Suppose you just powered up your machine after not using it for a few weeks. You proceed to update your system via the $ sudo pacman -Syu command and then you notice that your entire system feels abnormally slow. You have no idea what package update could be causing this to happen and you want to temporarily roll back all 512 of them to the previous version that you had installed.

This actually doesn’t require any more effort than the previous example. You perform the same steps except that you use the pacman.log file to get the names off all packages that were upgraded today not just ones that fit a particular naming scheme. This will work for any number of packages, essentially functioning as a big undo button for pacman. The example below assumes that the current date is November 30th 2020. Adjust the date accordingly if you would like to make use of it.

$ grep -a upgraded /var/log/pacman.log| grep 2020-11-30 > /tmp/lastupdates.txt                                                              
$ awk '{print $4}' /tmp/lastupdates.txt > /tmp/lines1;awk '{print $5}' /tmp/lastupdates.txt | sed 's/(/-/g' > /tmp/lines2
$ paste /tmp/lines1 /tmp/lines2 > /tmp/lines
$ tr -d "[:blank:]" < /tmp/lines > /tmp/packages
$ cd /var/cache/pacman/pkg/
$ for i in $(cat /tmp/packages); do sudo pacman --noconfirm -U "$i"*; done

How to rollback Arch Linux updates using the Arch Linux Archive

the Arch Linux Archive is an official archive which contains snapshots of previous versions of packages. These are saved in the packages directory of the site. There are two potential benefits to using the Arch Linux Archive over the pacman cache. First, the pacman cache takes up space on your local storage. As a result, some people will choose to regularly clear their cache. Second, the Archive makes available all previous versions of a package, not just the ones that you had previously installed.

Downgrading a single package

To downgrade a package to a previous version, first you find it in the archive and then use pacman to downgrade to that specific version. If we reexamine our previous example of downgrading Firefox, we are no longer limited to downgrading from version 83.0-2 to 81.0.1-1. As you can see from the firefox archive, we also have the option to downgrade to the following intermediate versions: 81.0.2-1, 82.0-1, 82.0.2-1, 82.0.3-1, 83.0-1.

To downgrade to Firefox version 83.0-1 using the Arch Linux Archive enter the following command.

$ sudo pacman -U https://archive.archlinux.org/packages/f/firefox/firefox-83.0-1-x86_64.pkg.tar.zst


Downgrade all packages to a previous date

Another option that you have available when using the Arch Linux Archive is to restore all of the packages on your system to the “current” version that existed in the repositories at a specific date in time. This may be useful if you are encountering issues with your Arch system now, but you remember that a few weeks ago you weren’t having those issues. In the following example, we will downgrade all packages to November 10th 2020.
To downgrade the packages we will have to let pacman know that we want to use the archive repo by editing /etc/pacman.d/mirrorlist.
Before making any changes to the file, it is recommended to back it up so that we can easily revert back to the current mirrorlist file when we would like to.

$ sudo cp /etc/pacman.d/mirrorlist{,.bak}

Next, edit your /etc/pacman.d/mirrorlist file using your editor of choice:

$ sudo vim /etc/pacman.d/mirrorlist

and change the content to the following.

### Arch Linux repository mirrorlist
### Created to downgrade to Nov 10th 2020
Server=https://archive.archlinux.org/repos/2020/10/11/$repo/os/$arch

Now to finalize downgrading the packages enter the following command.

$ sudo pacman -Syyuu

Conclusion

In this article we examined two methods to rollback or downgrade packages in Arch Linux, the pacman cache and the Arch Linux Archive. They are both powerful methods that have their own advantages. Let’s briefly reflect on those advantages as we conclude this article.

The main benefit of using the pacman cache is that it does not require an internet connection or downloading of packages as it is stored locally. As a result, the downgrading process is faster. Another benefit is that the pacman cache contains versions of packages that you have installed on your system before, therefore you most likely have an idea of how well they were working. A third benefit is that you can downgrade multiple packages without having to edit any configuration files.

The main benefit of using the Arch Linux Archive is that you can downgrade to any version of a package regardless of whether you had ever previously installed it. Another benefit is that it does not take up local storage, so if you are running low on storage and would like to clear your pacman cache you know there is another viable option for downgrading. A third benefit is that you can easily downgrade every single package on your system just by editing a single configuration file.



Comments and Discussions
Linux Forum