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 Qt applications:
Typically this guide will take around 20 minutes and will result in a working Qt5 application in a snap. Once complete, you’ll understand how to package Qt5 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 KCalc. Don’t worry, we’ll break this down.
Snaps are defined in a single yaml file placed in the root of your project. The Kcalc example shows the entire snapcraft.yaml for an existing project. We’ll break this down.
name: kcalc version: '19.08.0' grade: stable adopt-info: kcalc confinement: strict base: core18 apps: kcalc: common-id: org.kde.kcalc.desktop command: kcalc extensions: - kde-neon plugs: - kde-frameworks-5-plug - home - opengl - network - network-bind - pulseaudio slots: session-dbus-interface: interface: dbus name: org.kde.kcalc.desktop bus: session parts: kcalc: parse-info: - usr/share/metainfo/org.kde.kcalc.appdata.xml build-snaps: - kde-frameworks-5-core18-sdk - kde-frameworks-5-core18 plugin: cmake build-packages: - libmpfr-dev - libgmp-dev - kdoctools-dev stage-packages: - libmpfr6 - libgmp10 source: https://download.kde.org/stable/applications/19.08.0/src/kcalc-19.08.0.tar.xz configflags: - "-DKDE_INSTALL_USE_QT_SYS_PATHS=ON" - "-DCMAKE_INSTALL_PREFIX=/usr" - "-DCMAKE_BUILD_TYPE=Release" - "-DENABLE_TESTING=OFF" - "-DBUILD_TESTING=OFF" - "-DKDE_SKIP_TEST_SETTINGS=ON"
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: kcalc version: '19.08.0' grade: stable adopt-info: kcalc
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.
version is a “human readable” version string. It contains no semantic meaning, its purpose is to inform users of which version of the application they are intalling.
You can also fill in the
description. However, KCalc already has this metadata defined using an AppStream metadata file
org.kde.kcalc.appdata.xml, so we don’t want to duplicate this data. We use
adopt-info to tell Snapcraft to get the metadata from the part itself. More on this later.
The 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.
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 two: the KCalc source release tarball and a number of runtime dependencies of KCalc. In other cases these can point to local directories, remote git repositories or other revision control systems.
Before building the part, the build dependencies listed as
build-snaps are installed. The CMake plugin then uses
cmake to build the part. The
kde-frameworks-5-core18-sdk snap contains most build dependencies to build Qt5 and KDE applications. However, this snap also requires some tools from the
kde-frameworks-5-core18 runtime itself.
parts: kcalc: parse-info: - usr/share/metainfo/org.kde.kcalc.appdata.xml plugin: cmake build-snaps: - kde-frameworks-5-core18-sdk - kde-frameworks-5-core18 build-packages: - libmpfr-dev - libgmp-dev - kdoctools-dev stage-packages: - libmpfr6 - libgmp10 source: https://download.kde.org/stable/applications/19.08.0/src/kcalc-19.08.0.tar.xz configflags: - "-DKDE_INSTALL_USE_QT_SYS_PATHS=ON" - "-DCMAKE_INSTALL_PREFIX=/usr" - "-DCMAKE_BUILD_TYPE=Release" - "-DENABLE_TESTING=OFF" - "-DBUILD_TESTING=OFF" - "-DKDE_SKIP_TEST_SETTINGS=ON"
stage-packages are the packages required by KCalc to run, and mirror the same packages required by the binary on a standard distribution installation.
parse-info points to the AppStream metadata file. Since we used
adopt-info: kcalc in the metadata, the AppStream file of the
kcalc part will be used to fill in the
description of this snap. See Using AppStream metadata for more information.
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
KCalc.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: kcalc: common-id: org.kde.kcalc.desktop command: kcalc extensions: - kde-neon plugs: - home - opengl - network - network-bind - pulseaudio
You can see we use the
kde-neon extension. This extension will make Qt5 and KDE libraries available to the snap at run time and it will configure the run time environment of the application so that all desktop functionality is correctly initialised.
common-id field is used to link the AppStream metadata to this application. As a result, we don’t need to manually specify the
.desktop entry file because it’s already defined in AppStream. See Using AppStream metadata for more information.
You can download the example repository with the following command:
$ git clone https://github.com/galgalesh/kcalc.git
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 kcalc_19.08.0_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 kcalc_19.08.0_amd64.snap --devmode --dangerous
You can then try it out:
$ snap run kcalc
Removing the snap is simple too:
$ sudo snap remove kcalc
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 push --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 Go snap. For a more in-depth overview of the snap building process, see Creating a snap.