Flatpak updates with systemd
Automatic Update
You can automate Flatpak updates without a package manager using systemd's services and timers.
Alternative package systems such as Flatpak, Snap, and AppImage are becoming increasingly popular. They offer a number of advantages, for both developers and users. For example, experimental software versions can be tested without interfering with the distribution's package management system. You can also run several versions of a software tool for comparison purposes without them interfering with each other. Distribution-agnostic Flatpaks can simplify a developer's job because software only needs to be built in a single format.
Flatpaks also present some disadvantages. In view of the low price of storage media, it is up to you whether Flatpak's greatly increased space requirement poses a disadvantage. However, the update behavior of Flatpaks and AppImages does pose a clear disadvantage. Gnome Software, Plasma Discover, and Mint Update will let you automatically update Flatpaks, but this kind of package management is not to everyone's taste. If you rely on the command line to keep your software up to date, Flatpak's update behavior leaves you completely out in the cold. In fact, you will not even be notified of Flatpaks that need to be updated.
Because the Flatpak developers have not planned any further steps for automated updates, the responsibility for updates lies with the desktop environments. For this reason, I put forth a method that lets systemd users automatically update the Flatpaks installed on their systems based on a schedule with the help of the system and session manager. This method not only relieves you of the need to update manually, but it also demonstrates how easy it is to create systemd services and matching timers.
System and User
Flatpaks can be installed in both the user and the system context. Accordingly, you need to create separate services and timers for both contexts. The /etc/systemd/
directory contains two subdirectories (among others) named user/
and system/
, which contain the service files depending on their purposes (Figure 1).
Only the system administrator can manage the system-wide systemd instance. In addition, there is one instance per user. The instance starts and stops on demand when users log in or log out. A maximum of one user instance per user runs, regardless of how often a user logs in. Only the user (or an administrator) is authorized to manage these user-specific services. The Arch Linux Wiki [1] explains this concept in more detail.
The context in which the services for updating the installed Flatpaks are created depends on whether the Flatpaks were installed in the user or system context. You can discover the details using the flatpak list
command (Figure 2), which shows all installed Flatpaks and runtimes. The right-hand Installation column shows the context in which the Flatpaks were installed. If you have both contexts, you need to create services for both the user and the system.
Quickly Installed
Listing 1 shows a systemd service for a user, which you need to store in the systemd user context as update-user-flatpaks.service
. It can be stored in two locations. If several users use the computer and you want all of them to use the service, /etc/systemd/user/
is the right choice. For single-user systems, store the service in ~/.config/system/user/
instead.
Listing 1
User-Specific Service
# update-user-flatpaks.service # in the folder /etc/systemd/user/ [Unit] Description=Update user Flatpaks [Service] Type=oneshot ExecStart=/usr/bin/flatpak --user update -y [Install] WantedBy=default.target
The easiest way to create the service file is to use the command shown in Listing 2. Enter the text from Listing 1 in your choice of editor, and then save. Follow the same steps for the system-wide variant shown in Listing 3. Name this service file update-system-flatpaks.service
and store it in /etc/system/system
.
Listing 2
Editing the File
$ sudo nano /etc/systemd/user/update-user-flatpaks.service
Listing 3
System-Wide Service
# update-systems-flatpaks.service # in the folder /etc/system/system/ [Unit] Description=Update system Flatpaks After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=/usr/bin/flatpak --system update -y [Install] WantedBy=multi-user.target
Now you need to create timers to control the services; you will find the timers in Listing 4 (user-specific) and Listing 5 (system-wide). The two timers, named update-user-flatpaks.timer
and update-system-flatpaks.timer
, are stored in /etc/systemd/user/
and /etc/systemd/system/
respectively.
Listing 4
User-Specific Timer
# update-user-flatpaks.timer # in /etc/systemd/user/ [Unit] Description=Update user Flatpaks daily [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
Listing 5
System-Wide Timer
# update-system-flatpaks.timer # in /etc/systemd/system/ [Unit] Description=Update system Flatpaks daily [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
The OnCalendar
specification in Listings 4 and 5 lets you adjust for desired update times (Figure 3). If the daily
update option you entered for OnCalendar
does not meet your needs, then you could use, for example, OnCalendar=weekly
for a weekly run.
If needed, however, you can do this in a far more targeted way. To start the update at 6:00pm on the first Saturday of each month, you would use the following entry:
OnCalendar=Sat *-*-1..7:*18:00:00
You can read all about systemd timers in the Arch Linux Wiki [2] if you are interested in a more in-depth approach.
Enabling Timers
Now it's time to enable the timers. To do this for the user, you need the command in line 1 of Listing 6. For the system-wide setup, use the command in line 2 instead. Finally, the call in line 3 tells systemd to reload all configuration files and restart all units. You also run this command after making changes to the files.
Listing 6
Timer and Systemd
01 $ sudo systemctl --user enable --now update-user-flatpaks.timer 02 $ sudo systemctl --system enable --now update-system-flatpaks.timer 03 $ sudo systemctl daemon-reload 04 $ sudo systemd-analyze calendar weekly 05 $ sudo journalctl -b | grep flatpak
The call in line 4 lets you determine when the next update will occur (Figure 4). If you get the correct output, you also know immediately that the service is set up correctly.
If you want to know after the execution date whether the setup was successful, use the command from line 5. You will see the output here, if successful; otherwise you will see flatpak update
(Figure 5).
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.