Snapcraft can be used to package and distribute Python applications in a way that enables convenient installation by users.
The process of creating a snap for a C or C++ application builds on standard
make, making it possible to adapt
or integrate an application’s existing packaging into the snap building process.
Snaps are defined in a single
snapcraft.yaml file placed in a
snap folder at the root of your project. This YAML file describes
the application, its dependencies and how it should be built.
The following example shows the entire
snapcraft.yaml file for an
existing project, DOSBox:
snapcraft.yaml for DOSBox
name: dosbox version: "0.74-svn" summary: DOS emulator description: | DOSBox is a x86 emulator with Tandy/Hercules/ CGA/EGA/VGA/SVGA graphics sound and DOS. It's been designed to run old DOS games under platforms that don't support it. base: core18 confinement: devmode parts: dosbox: plugin: autotools source-type: tar source: http://source.dosbox.com/dosboxsvn.tgz build-packages: - g++ - make - libsdl1.2-dev - libpng-dev - libsdl-net1.2-dev - libsdl-sound1.2-dev - libasound2-dev stage-packages: - freeglut3 - libsdl-sound1.2 - libsdl-net1.2 - libxcursor1 - libxi6 - libxinerama1 - libxrandr2 - libxrender1 - libopenal1 - libsndio6.1 - libspeex1 - libvorbisfile3 - libwayland-client0 - libwayland-cursor0 - libwayland-egl1-mesa - libxkbcommon0 - libglu1-mesa - libasound2 - libasyncns0 - libbsd0 - libcaca0 - libdbus-1-3 - libflac8 - libgcc1 - libgcrypt20 - libgl1 - libglvnd0 - libglx0 - libgpg-error0 - liblz4-1 - liblzma5 - libncursesw5 - libogg0 - libpng16-16 - libpulse0 - libsdl1.2debian - libslang2 - libsndfile1 - libstdc++6 - libsystemd0 - libtinfo5 - libvorbis0a - libvorbisenc2 - libwrap0 - libx11-6 - libxau6 - libxcb1 - libxdmcp6 - libxext6 - zlib1g apps: dosbox: command: dosbox environment: "LD_LIBRARY_PATH": "$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio" "DISABLE_WAYLAND": "1"
This extensive description is broken down in the following sections.
snapcraft.yaml file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project
file. This data is used in the presentation of your app in the Snap Store.
name: dosbox version: "0.74-svn" summary: DOS emulator description: | DOSBox is a x86 emulator with Tandy/Hercules/ CGA/EGA/VGA/SVGA graphics sound and DOS. It's been designed to run old DOS games under platforms that don't support it.
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.
git for the version, the current git tag or commit will be used as the version string. Versions carry no semantic meaning in snaps.
summary can not exceed 79 characters. You can use the pipe symbol ‘|’ in the
description key to declare a multi-line description.
The base keyword declares which base snap to use with the project. A base snap is a special kind of snap that provides a run-time environment alongside a minimal set of libraries that are common to most applications.
Snaps are containerised to ensure more predictable application behaviour and greater security. Unlike other container systems, the shape of this confinement can be changed through a set of interfaces. These are declarations that tell the system to give permission for specific tasks, such as accessing a webcam or binding to a network port.
The next section describes the level of confinement applied to the running application:
It is best to start creating a snap with a confinement level that provides
warnings for confinement issues instead of strictly applying confinement.
This is done by specifying the
devmode (developer mode) confinement value.
When a snap is in devmode, runtime confinement violations will be allowed but
reported. These can be reviewed by running
Because devmode is only intended for development, snaps must be set to strict confinement before they can be published as “stable” in the Snap Store. Once an application is working well in devmode, you can review confinement violations, add appropriate interfaces, and switch to strict confinement.
The above example will also work if you change the confinement from
strict, as you would before a release.
Parts define what sources are needed to build your application. Parts can be anything: programs, libraries, or other needed assets, but for this example, we only need to use one part: the DOSBox source release tarball.
plugin keyword is used to select a language or technology-specific
plugin that knows how to perform the build steps for the project. In this
example, the autotools plugin is used to automate the build, using the standard
make tools to build the part.
parts: dosbox: plugin: autotools source-type: tar source: http://source.dosbox.com/dosboxsvn.tgz build-packages: - g++ - make - libsdl1.2-dev - libpng-dev - libsdl-net1.2-dev - libsdl-sound1.2-dev - libasound2-dev
Before building the part, the packages listed in the
need to be installed in the build environment. These are the tools and libraries
that are used during the build process.
There is also a large
stage-packages: - freeglut3 - libsdl-sound1.2 - libsdl-net1.2 [...] - libxext6 - zlib1g
These are packages containing libraries are resources that DOSBox needs to run. They are very similar to those that would be listed as run-time dependencies on a standard distribution package.
For more details on autotools-specific metadata, see The autotools plugin.
Apps are the commands you want to expose to users, and also the names of any
background services the application provides. Each key under
apps is the command name that should be made available on users’ systems.
command specifies the path to the binary to be run. This is resolved relative to the root of the snap contents.
apps: dosbox: command: bin/dosbox environment: "LD_LIBRARY_PATH": "$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio" "DISABLE_WAYLAND": "1"
If the command name matches the name of the snap specified in the top-level
name keyword (see the Metadata section above), the binary file will be given
the same name as the snap, as in this example.
If the names differ, the binary file name will be prefixed with the snap name
to avoid naming conflicts between installed snaps. An example of this would be
We also make two adjustments to the run-time environment for DOSBox: the first to work around a PulseAudio issue and the second to disable Wayland. These kinds of requirements are usually figured out through trial and error after an initial build.
If your application is intended to run as a service you simply add the line
daemon: simple after the command keyword. This will automatically keep the service running on install, update, and reboot.
Building the snap
You can download the example repository with the following command:
$ git clone https://github.com/snapcraft-docs/dosbox
After you’ve created the
snapcraft.yaml file (which already exists
in the above repository), 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 dosbox_0.74-svn_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 dosbox_*.snap --devmode --dangerous
You can then try it out:
Removing the snap is simple too:
$ sudo snap remove dosbox
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
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 C/C++ snap. For a more in-depth overview of the snap building process, see Creating a snap.
Last updated a month ago.