Easily reproduce Flatpak app installs on any system

I am a huge fan of Flatpak applications on Linux. I like how they work. I like how easy they are to install. I like how you can control their permissions with such granularity. Etc.

Well now, I have yet another reason to love Flatpaks: easy installation reproducibility. Let me show you what I mean.

Let's say you have been using Flatpaks for a while, and you have all the apps you could want installed on your system.

Then, for whatever reason, you have to set up an OS on a new machine. For example, it could be that a new version of Ubuntu is about to drop, and you want to install it from scratch on your laptop.

Well, now you can easily install all the Flatpaks that you use and love on that new OS with just a few commands.

First, on your current machine, we want to list out all the installed Flatpaks. But, we don't want to just do it with a flatpak list, because that gives us too much information.

All we want right now is a list of the Application IDs. We can do that with the following command:

flatpak list --app --columns application

Now, this is great, and we can just manually copy and paste this list into a text file if we want.

Instead, what we are going to do is take that output and redirect it to a file with the output redirection operator. We are going to call that output something like “flatpaks.txt” or something else unimportant, because we need to rename the file in the next step.

flatpak list --app --columns application > flatpaks.txt

Great! Now, we have a text file of every Flatpak we currently have installed on our system. Next, we need to format it, so we can easily input the contents of the text file into the command line for install at a later time.

We can easily format this file appropriately with the next command and output the contents into a new text file. In my experience, if we try to overwrite the contents of the first file, the command won't work. Here's how to do it:

cat flatpaks.txt | tr '\n' ' ' | sed 's/ $/\n/' > flatpakinstalls.txt

Now, this is technically a few commands, and when broken down they simply say “take this file's contents, replace the new lines with spaces, remove the trailing space at the end of the line, and write this new output to a new file.” Regular expressions are wild.

The last thing you'll want to do is delete the original file, since now you have two:

rm flatpaks.txt

With that, you have a clean text file containing all the Flatpaks installed on your system that you can reference on any other machine. But, we can actually do this a little better.

With all of that out of the way, we can take that three or four-step process and boil it down to one simple copy pasta command thanks to the and operator, &&:

flatpak list --app --columns application > flatpaks.txt && cat flatpaks.txt | tr '\n' ' ' | sed 's/ $/\n/' > flatpakinstalls.txt && rm flatpaks.txt

That's it! Easy as pie.

Just keep this file backed up and safe. Then, after you are done installing your new OS, you'll need to make sure Flatpak is set up and ready by following the official Flatpak documentation.

After that, you can move this text file over to that machine, copy the contents, and paste them after the flatpak install command to quickly and effortlessly reproduce the Flatpak setup you had on your old installation.

Hope you find this as helpful as I will.

Tags: #Tutorials Comments: Discuss...