Replace Snap with Flatpak on Ubuntu

This was tested on a new install of Ubuntu 24.04.

Why?

* I'm not a fan of how Snap works, or its forced automatic updates.
* Some of the software I wanted to run (such as Bottles) is currently only available as a Flatpak.

If you don't want to remove Snap, then skip to Part 2 to install Flatpak alongside Snap.


Part 1, Kill Snap

Before removing Snap, you need to disable and remove the Firefox Hunspell service (what allows Firefox to access system dictionaries).

If you try to remove Snap without first disabling the Firefox Hunspell service, you'll get errors like these:

rm: cannot remove '/var/snap/firefox/common/host-hunspell/en_US.dic': Read-only file system
rm: cannot remove '/var/snap/firefox/common/host-hunspell/en_US.aff': Read-only file system

You can use these commands to stop and disable the Firefox Hunspell service:

sudo systemctl stop var-snap-firefox-common-host\\x2dhunspell.mount
sudo systemctl disable var-snap-firefox-common-host\\x2dhunspell.mount

(info from: https://askubuntu.com/questions/1414173/completely-remove-firefox-snap-package)

Now you can nuke Snap (and all installed snaps, including Firefox):

sudo purge snapd

Part 2, Install Flatpak

sudo apt install flatpak

Add/update the default flatpak repository:

sudo flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo

Install the Gnome Software app with the Flatpak plugin. (Using this command avoids reinstalling Snap again!)

sudo apt install --no-install-recommends gnome-software-plugin-flatpak gnome-software

(info from: https://flatpak.org/setup/Ubuntu)


Part 3, Finishing Up

Ubuntu 23.10 introduced restricted unprivileged user namespaces, which interferes with the bwrap command (Bubblewrap), which is used for namespace manipulation when the Software app launches a flatpak.
When clicking Launch in the Software app, you'll see this message:

"Sorry, something went wrong."

Viewing its details will have a message that says "Failed to sync with dbus proxy".

Checking /var/log/syslog will have a message that says "org.gnome.Software.desktop: bwrap: setting up uid map: Permission denied".

To work around this, create this file: /etc/apparmor.d/bwrap, and add the following to it:

abi <abi/4.0>,
include <tunables/global>

profile bwrap /usr/bin/bwrap flags=(unconfined) {
  userns,

  # Site-specific additions and overrides. See local/README for details.
  include if exists <local/bwrap>
}

You can then restart AppArmor to reload the change:

systemctl reload apparmor

(info from: https://etbe.coker.com.au/2024/04/24/ubuntu-24-04-bubblewrap/)

It's recommended to log out and then back in or reboot at this point to ensure the Software app works correctly with Flatpaks.


Part 4, Notes

You can now install your favorite flatpaks. I'd start with Firefox and Flatseal (an app to modify a flatpak's sandbox options, such as granting it access to outside files).

I haven't used this setup long-term enough to know how well it will work. Everything seems to work, though.