Qt5 and KDE Frameworks Applications

Why are snaps good for Qt5 and KDE Frameworks applications?

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:

  • Snaps are easy to discover and install Millions of users can browse and install snaps graphically in the Ubuntu Software Center, the Snap Store or from the command-line.
  • Snaps install and run the same across Linux They bundle the latest version of Qt5 and KDE Frameworks, along with all of your app’s dependencies, be they binaries or system libraries.
  • You control the release schedule You decide when a new version of your application is released without having to wait for distributions to catch up.
  • Snaps automatically update to the latest version Four times a day, users’ systems will check for new versions and upgrade in the background.
  • Upgrades are safe If your app fails to upgrade, users automatically roll back to the previous revision.

Build a snap in 20 minutes

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.

ⓘ For a brief overview of the snap creation process, including how to install snapcraft and how it’s used, see Snapcraft overview. For a more comprehensive breakdown of the steps involved, take a look at Creating a snap.

Getting started

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.

KCalc Snap

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.

snapcraft.yaml for KCalc
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"

Metadata

The 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

The 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.

The 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 installing.

You can also fill in the title, summary and 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.

Base

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.

base: core18

core18 is the current standard base for snap building and is based on Ubuntu 18.04 LTS.

Security model

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.

confinement: devmode

Parts

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-packages and 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 title, summary and description of this snap. See Using AppStream metadata for more information.

Apps

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 name (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.

The 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.

Building the snap

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

:warning: The extension used in this example currently only works on amd64 systems. Other architectures like arm are not supported.

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.

Publishing your snap

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

Reserve a name for your snap

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 snapcraft again.

Upload your snap

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 Go snap. For a more in-depth overview of the snap building process, see Creating a snap.

2 Likes

This links to the GTK extension instead.

Fixed (with a huge thanks to @galgalesh for adding this docs page).

Thanks! Just a heads up that this page will still change a lot. Using Krita as an example wasn’t such a good idea because it can take up to an hour to actually build the Krita snap. I’m currently trying out some smaller KDE applications to find a good example.

Edit: I used kcalc now, this is a non-trivial example but it still builds relatively quickly.

An important omission in this thread of discussion is that it doesn’t work for snaps that use classic confinement.

1 Like

Should add a warning that this solution currently only works for amd64 architecture, for compatibility with other architectures one should look [Deprecated] Desktop App support - Qt5 - doc - snapcraft.io

1 Like

I wasn’t aware of this. I maintain an application which uses the kde-neon extension. It builds successfully on Snapcraft IO on other architectures, e.g. ARM, but I can only test the amd64 build. Does this mean it builds but would not run?

This point seems to be conflicting with the current behavior:

screenshot

Apologies for the ignorance, the answer at the time is “Yes”, however it seems that kde-frameworks-5-qt-5-15-3-core20 as added ARM64 support: https://snapcraft.io/kde-frameworks-5-qt-5-15-3-core20

1 Like

Thanks, I removed it!

1 Like

So I am trying to run this with “snapcraft --destructive-mode” and it does not work I get this error:

    Skipping pull kcalc (already ran)
    Skipping pull kde-neon-extension (already ran)
    Building kcalc 
    + snapcraftctl build
    cmake /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/src -DCMAKE_INSTALL_PREFIX= -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 '-DCMAKE_FIND_ROOT_PATH=/snap/kde-frameworks-5-core18-sdk/current;/snap/kde-frameworks-5-core18/current'
    -- The C compiler identification is GNU 9.4.0
    -- The CXX compiler identification is GNU 9.4.0
    -- Check for working C compiler: /usr/bin/cc
    CMake Error: Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_b918c/fast && 
    -- Check for working C compiler: /usr/bin/cc -- broken
    CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:60 (message):
      The C compiler

        "/usr/bin/cc"

      is not able to compile a simple test program.

      It fails with the following output:

        Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp
        
        Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_b918c/fast && Segmentation fault
        Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_b918c/fast && 
        

      

      CMake will not be able to correctly generate this project.
    Call Stack (most recent call first):
      CMakeLists.txt:8 (project)


    -- Configuring incomplete, errors occurred!
    See also "/home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeOutput.log".
    See also "/home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeError.log".
    Failed to run "cmake /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/src -DCMAKE_INSTALL_PREFIX= -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 '-DCMAKE_FIND_ROOT_PATH=/snap/kde-frameworks-5-core18-sdk/current;/snap/kde-frameworks-5-core18/current'" for 'kcalc': Exited with code 1.
    Verify that the part is using the correct parameters and try again.

The last bit of the CMakeOutput.log:

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: /usr/bin/c++ 
Build flags: 
Id flags:  

The output was:
0


Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "a.out"

The CXX compiler identification is GNU, found in "/home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/3.16.3/CompilerIdCXX/a.out"

Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: /usr/bin/cc 
Build flags: 
Id flags:  

The output was:
0


Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "a.out"

The C compiler identification is GNU, found in "/home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/3.16.3/CompilerIdC/a.out"

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: /usr/bin/c++ 
Build flags: 
Id flags:  

The output was:
0


Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "a.out"

The CXX compiler identification is GNU, found in "/home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/3.16.3/CompilerIdCXX/a.out"

The output of the CMakeError.log:

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_d5b66/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_d5b66/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_3db26/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_3db26/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_e1dfc/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_e1dfc/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_f5ded/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_f5ded/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_11270/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_11270/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_6f832/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_6f832/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_28a1e/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_28a1e/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_58171/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_58171/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_8f86d/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_8f86d/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_e9338/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_e9338/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_337c9/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_337c9/fast && 


Determining if the C compiler works failed with the following output:
Change Dir: /home/ubuntu/Desktop/qtexamplesnap/parts/kcalc/build/CMakeFiles/CMakeTmp

Run Build Command(s):/snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_b918c/fast && Segmentation fault
Generator: execution of make failed. Make command was: /snap/kde-frameworks-5-core18-sdk/current/usr/bin/make cmTC_b918c/fast &&

This page should link to https://snapcraft.io/docs/kde-neon-extension or it should be updated to core22.