On Linux distributions with snap support, the easiest way to install snapcraft is via its snap:
$ sudo snap install snapcraft --classic
--classic argument is required because snapcraft uses classic confinement.
Snapcraft can also be installed and run on Apple’s macOS. See Install snapcraft on macOS for details.
See below for a general overview of Snapcraft’s capabilities, and see Creating a snap for a more detailed look at the process, alongside a selection of self-contained examples for some popular languages and frameworks, including Go, Python and C/C++.
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, and later releases, are designed to use bases (see Base snaps) and Multipass to both simplify the build process and 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.
However, Snapcraft offers a variety of options alongside Multipass, such as building within an ephemeral environment or with LXD. See Build options for further details.
To get started, run
snapcraft init. This creates a buildable snapcraft.yaml template within a snap sub-directory relative to your current filesystem location. If the command cannot be found, make sure
/snap/bin is on your PATH.
The typical snap build process centres on iterating over the configuration of parts, plugins and interfaces within this snapcraft.yaml file:
The following lists how you might want to approach building a new snap for your application with snapcraft.yaml:
When you are ready to test the contents of snapcraft.yaml, simply run
snapcraft --debug in the same directory where you initialised the snap.
--debug argument isn’t necessary, but it helps hugely when testing a new snapcraft.yaml.
--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 occurred.
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.
To see snapcraft build the template created by snapcraft init, simply run
$ 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
With Multipass, the default virtual machine is assigned 2 CPUs. If you have the hardware capabilities, use the following environment variables to modify CPU and memory allocation to improve performance:
$ export SNAPCRAFT_BUILD_ENVIRONMENT_CPU=8 $ export SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=16G
If you don’t have Multipass installed, snapcraft will first prompt for its automatic installation via a snap. If you can’t use Multipass, see Build options for alternatives.
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
--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
For a more comprehensive and iterative break-down of the snap building process, see Creating a snap.
ⓘ To see what’s new in each release of Snapcraft, take a look at Snapcraft release notes.
Last updated 5 days ago. Help improve this document in the forum.