Snapcraft bundles necessary libraries required by the application, and can configure the environment for confinement of applications for end user peace of mind. Developers can ensure their application is delivered pre-packaged with libraries which will not be replaced or superseded by a distribution vendor.
Here are some snap advantages that will benefit many GTK applications:
Typically this guide will take around 20 minutes and will result in a working GTK application in a snap. Once complete, you’ll understand how to package GTK3 applications as snaps and deliver them to millions of Linux users. After making the snap available in the store, you’ll get access to installation metrics and tools to directly manage the delivery of updates to Linux users.
Snaps are defined in a single YAML file placed in the root folder of your project. The following example shows the entire snapcraft.yaml file for Foliate. Don’t worry, we’ll break this down.
Snaps are defined in a single yaml file placed in the root of your project. The Foliate example shows the entire snapcraft.yaml for an existing project. We’ll break this down.
name: foliate grade: stable adopt-info: foliate license: GPL-3.0+ base: core18 confinement: strict apps: foliate: command: usr/bin/com.github.johnfactotum.Foliate extensions: [gnome-3-28] plugs: - home slots: - dbus-daemon common-id: com.github.johnfactotum.Foliate parts: foliate: plugin: meson source: https://github.com/johnfactotum/foliate.git source-branch: 1.x meson-parameters: [--prefix=/snap/foliate/current/usr] build-packages: - libgjs-dev - gettext organize: snap/foliate/current/usr: usr parse-info: [usr/share/metainfo/com.github.johnfactotum.Foliate.appdata.xml] slots: dbus-daemon: interface: dbus bus: session name: com.github.johnfactotum.Foliate
snapcraft.yaml file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project README.md. This data is used in the presentation of your app in the Snap Store.
name: foliate grade: stable adopt-info: foliate license: GPL-3.0+
name must be unique in the Snap Store. Valid snap names consist of lower-case alphanumeric characters and hyphens. They cannot be all numbers and they also cannot start or end with a hyphen.
You can also fill in
icon. However, Foliate already has this metadata defined using an AppStream metadata file
com.github.johnfactotum.Foliate.appdata.xml, so we don’t want to duplicate this data. We use
adopt-info to tell Snapcraft to get the metadata from the
foliate part. More on this later.
base keyword defines a special kind of snap that provides a run-time environment with a minimal set of libraries that are common to most applications. They’re transparent to users, but they need to be considered, and specified, when building a snap.
To get started, we won’t confine this application. Unconfined applications, specified with
devmode, can only be released to the hidden “edge” channel where you and other developers can install them. After you get the snap working in
devmode confinement, you can switch to strict mode and figure out which interfaces (plugs) the snap uses.
Apps are the commands and services exposed to end users. If your command name matches the snap
name, users will be able run the command directly. If the names differ, then apps are prefixed with the snap
foliate.command-name, for example). This is to avoid conflicting with apps defined by other installed snaps.
If you don’t want your command prefixed you can request an alias for it on the Snapcraft forum. These are set up automatically when your snap is installed from the Snap Store.
apps: foliate: command: usr/bin/com.github.johnfactotum.Foliate extensions: [gnome-3-28] plugs: - home slots: - dbus-daemon common-id: com.github.johnfactotum.Foliate
This application uses the
gnome-3-28 extension. This will make GTK and Gnome libraries available to the snap at runtime and it will configure the runtime environment of the application so that all desktop functionality is correctly initialised.
common-id property is used to link this application to the AppStream metadata specified further down below. This will cause this
app to use the
.desktop launcher specified in the AppStream file.
Parts define how to build your app. Parts can be anything: programs, libraries, or other assets needed to create and run your application. In this case we have one: the Foliate source release tarball. In other cases these can point to local directories, remote git repositories or other revision control systems.
Before building the part, the dependencies listed as
build-packages are installed. The Meson plugin then uses
ninja build and
ninja install to build the part.
parts: foliate: plugin: meson source: https://github.com/johnfactotum/foliate.git source-branch: 1.x meson-parameters: [--prefix=/snap/foliate/current/usr] build-packages: - libgjs-dev - gettext organize: snap/foliate/current/usr: usr parse-info: [usr/share/metainfo/com.github.johnfactotum.Foliate.appdata.xml]
stage-packages are the packages required by Foliate to run, and mirror the same packages required by the binary on a standard distribution installation. Note that we did not add GTK3 to the stage-packages. This is because the GTK3 runtime is already added by the
gnome-3-28 extension extension.
parse-info points to the AppStream metadata file. Since we used
adopt-info: foliate in the top-level metadata, the AppStream file of the
foliate part will be used to fill in the
icon of this snap. See Using AppStream metadata for more information.
Many GTK3 applications require access to DBus in order to run correctly. However, snap blocks this access by default so you need to explicitly define that this application is allowed access to dbus.
slots: dbus-daemon: interface: dbus bus: session name: com.github.johnfactotum.Foliate
You can download the example repository with the following command:
$ git clone https://github.com/snapcrafters/foliate
After you’ve created the snapcraft.yaml, you can build the snap by simply executing the snapcraft command in the project directory:
$ snapcraft Using 'snapcraft.yaml': Project assets will be searched for from the 'snap' directory. Launching a VM. [...] Snapped foliate_amd64.snap
The resulting snap can be installed locally. This requires the
--dangerous flag because the snap is not signed by the Snap Store. The
--devmode flag acknowledges that you are installing an unconfined application:
$ sudo snap install foliate_amd64.snap --devmode --dangerous
You can then try it out:
Removing the snap is simple too:
$ sudo snap remove foliate
You can clean up the build environment with the following command:
$ snapcraft clean
By default, when you make a change to snapcraft.yaml, snapcraft only builds the parts that have changed. Cleaning a build, however, forces your snap to be rebuilt in a clean environment and will take longer.
To share your snaps you need to publish them in the Snap Store. First, create an account on the dashboard. Here you can customise how your snaps are presented, review your uploads and control publishing.
You’ll need to choose a unique “developer namespace” as part of the account creation process. This name will be visible by users and associated with your published snaps.
Make sure the
snapcraft command is authenticated using the email address attached to your Snap Store account:
$ snapcraft login
You can publish your own version of a snap, provided you do so under a name you have rights to. You can register a name on dashboard.snapcraft.io, or by running the following command:
$ snapcraft register mysnap
Be sure to update the
name: in your
snapcraft.yaml to match this registered name, then run
Use snapcraft to push the snap to the Snap Store.
$ snapcraft upload --release=edge mysnap_*.snap
If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo and turn on automatic builds so any further commits automatically get released to edge, without requiring you to manually build locally.
Congratulations! You’ve just built and published your first GTK 3 snap. For a more in-depth overview of the snap building process, see Creating a snap.
Last updated 2 months ago. Help improve this document in the forum.