Snapcraft overview


#1

Snapcraft is a powerful and easy to use command line tool for building snaps. It helps you to:

  • build and then publish your snaps on the Snap store
  • use channels, tracks and branches to finely control updates and releases
  • build and debug snaps within a confined environment
  • update and iterate over new builds without rebuilding the environment
  • test and share your snaps locally

On Linux distributions with snap support, the easiest way to install snapcraft is via its snap:

$ sudo snap install snapcraft --classic

The --classic argument is required because snapcraft requires classic confinement.

Snapcraft can also be installed and run on Apple’s macOS. See Install snapcraft on macOS for details.

Working with snapcraft

At the heart of the snapcraft build process is a file called snapcraft.yaml. This file describes a snap’s build dependencies and run-time requirements, it integrates remote repositories and extensions, and runs custom scripts and hooks for better integration with CI systems.

Snapcraft 3.0, the current stable release, is designed to use bases (see below) and Multipass to both simplify the build process and to to confine the build environment within a virtual machine. Confining the build in this way isolates potentially conflicting libraries and other files from your host system, and vice-versa.

Alternatively, if it better suits your processes, building directly on your system, or with either LXD or Docker, is also supported.

Creating snapcraft.yaml

To get started, run snapcraft init. This creates a buildable snapcraft.yaml template within a snap sub-directory relative to your current filesystem location.

The typical snap build process centres on iterating over the configuration of parts, plugins and interfaces within this snapcraft.yaml file:

  • parts are the raw building blocks of a snap, used to collect and build binaries and their dependencies.
  • plugins are used within parts to better integrate projects using languages and framework.
  • interfaces enable resources from one snap to be shared with another, and with the host system.

The following lists how you might want to approach building a new snap for your application with snapcraft.yaml:

  1. describe your application with top-level metadata
  2. use parts metadata to import and build your application and its dependencies
    • incorporate plugins within parts to easily integrate applications using specific languages and frameworks, or work with binary files directly. You can also write your own plugin.
    • use plugin metadata to locate your project, or sync with a remote repository
    • set build dependencies, if required, and any run-time dependencies
  3. add interface metadata to connect external system resources to your application

ⓘ See Creating a snap for a selection of self-contained examples for some popular languages and frameworks, including Go, Python and C/C++.

Bases

In addition to the above, you can also specify a base snap within snapcraft.yaml. This is a special kind of snap that provides a minimal set of libraries common to most applications. A base snap mounts itself as the root filesystem within your snap so that when your application runs, the base’s library paths are searched directly after the paths for your specific snap.

To use a base, add the base: keyword followed by the base name to snapcraft.yaml. For example, to specify Core 18, a snap based on the Ubuntu 18.04 LTS and the current standard base for snap building, add the following:

base: core18

Building your snap

When you are ready to test the contents of snapcraft.yaml, simply run snapcraft --debug in the same directory where you initialised the snap,

The --debug argument isn’t necessary, but it helps hugely when testing a new snapcraft.yaml.

With --debug, if snapcraft encounters an error it will automatically open a shell within your snap’s virtual environment. You can then explore the build issue directly, working on your project within the parts directory, or the files being staged within prime, depending on the build stage when the error occured.

Critically, you can update snapcraft.yaml outside of the build environment and run snapcraft within the build environment to incorporate any external changes and continue with the build. If there are no further errors, your snap will be built.

ⓘ See Debugging building snaps for common problems and their solutions.

To see snapcraft in action, the template snapcraft.yaml created with snapcraft init will build with no further requirements, creating a snap called my-snap-name:

$ snapcraft --debug
Launching a VM.
Launched: snapcraft-my-snap-name [...]
Pulling my-part
Building my-part
Staging my-part
Priming my-part
Snapping 'my-snap-name' |
Snapped my-snap-name_0.1_amd64.snap

If you don’t have Multipass installed, snapcraft will first prompt for its automatic installation via a snap.

The build process will proceed through the Snapcraft lifecycle, installing and building your project’s dependencies, as described by your snapcraft.yaml. The time this takes will depend on the complexity of your project and the capabilities of your system.

After a snap has been built, it can be installed locally with the --dangerous and --devmode flags, enabling your unsigned and unconfined snap to be installed:

$ sudo snap install my-snap-name_0.1_amd64.snap --dangerous --devmode
my-snap-name 0.1 installed

See Releasing your app to discover how to share your own snaps with the snap community.


The docs roadmap
Snap Documentation
Snapcraft filesets
Install snapcraft on macOS
#2

minor grammer correction: change you to your


#3

Blockquote use parts metadata to import and build your application and its dependencies
incorporate plugins within parts to easily integrate applications using specific languages and frameworks, or work with binary files directly. You can also You can also write your own plugin.

Please kindly remove extraneous “You can also” in the above section 2.


#4

Good spot - thank you!