Ubuntu Snaps

Types of snap

The snap packaging ecosystem consists of the following parts:

  • snap is both the command line interface and the application package format
  • snapd is the background service that manages and maintains your snaps
  • snapcraft is the command and the framework used to build your own snaps
  • Snap Store provides a place to upload your snaps, and for users to browse and install
  1. Create a checklist
    Better understand your snap’s requirements.
  2. Create a snapcraft.yaml file
    Describes your snap’s build dependencies and run-time requirements. It contains the metadata of the application.
  3. Add interfaces to your snap
    Share system resources with your snap, and from one snap to another
  4. Publish and share
    Put your snap on the Snap Store to reach an audience of millions

Difference between Snap and Apt:

Snap Vs. APT: Which One to Choose?

Choose Snap if:

  • You do not want unnecessary packages to be integrated into your system. With Snaps, the packages remain confined in the .snap package.
  • You want to make sure you have the most up-to-date version of the app. Snap updates automatically and continuously, without feature freezes for specific distributions.
  • You want to isolate an app.
  • You prefer apps to be reviewed by the distribution you are using.
  • You are concerned about conserving your storage space. Apps installed by APT share their dependencies.
  • You want apps to integrate into the GUI environment properly.

Managing updates

Snaps update automatically, and by default, the snapd daemon checks for updates 4 times a day. Each update check is called a refresh.

Manually updating snaps

Regardless of when a refresh is scheduled, an update refresh can be initiated with the snap refresh command:

Output of a snap refresh command

Snapshots

A snapshot is a copy of the user, system and configuration data stored by snapd for one or more snaps on your system. This data can be found in $HOME/snap/<snap-name> and /var/snap/<snap-name>. The snap save command creates a snapshot for all installed snaps, or if declared individually, specific snaps:

Snapcraft:

Now lets discuss snapcraft in detail. It is defined as the heart of snap ecosystem. It 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
sudo snap install snapcraft --classic

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.

  • 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 snapcraft format

The snapcraft.yaml file is the main entry point to create a snap through Snapcraft. It contains all the details the snapcraft command needs to build a snap.

name: hello
base: core18
version: '2.10'
summary: GNU Hello, the "hello world" snap
description: |
GNU hello prints a friendly greeting.
grade: stable
confinement: strict
  • name: The name of the snap.
  • base: A foundation snap that provides a run-time environment with a minimal set of libraries that are common to most applications. The template defaults to using core18, which equates to Ubuntu 18.04 LTS.
  • version: The current version of the snap. This is just a human readable string. All snap uploads will get an incremental snap revision, which is independent from this version. It’s separated so that you can upload multiple times the same snap for the same architecture with the same version. See it as a string that indicates to your user the current version, like “stable”, “2.0”, etc.
  • summary: A short, one-line summary or tag-line for your snap.
  • description: A longer description of the snap. It can span over multiple lines if prefixed with the ‘|’ character.
  • grade: Can be used by the publisher to indicate the quality confidence in the build. The store will prevent publishing ‘devel’ grade builds to the ‘stable’ channel.
  • confinement: A snap’s confinement level is the degree of isolation it has from your system, and there are three levels: strict, classic and devmode. strict snaps run in complete isolation, classic snaps have open access to system resources and devmode snaps run as strict but with open access to the system. The latter is ideal for development, but your snap will need move from devmode to be published. See Snap confinement for more details.
apps:
hello:
command: bin/hello
parts:
gnu-hello:
source: http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz
plugin: autotools

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.

$ snapcraft --debug
Support for 'multipass' needs to be set up. Would you like to do it now? [y/N]: y

Testing your snap locally

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 hello_0.1_amd64.snap --dangerous --devmode
hello 0.1 installed

Lets build a simple ubuntu snap:

We will be building a simple DOSBox snap and will break it down step-wise:

$ sudo snap install snapcraft --classic
$ mkdir -p ~/mysnaps/hello
$ cd ~/mysnaps/hello
$ snapcraft init
name: hello
base: core18
version: '2.10'
summary: GNU Hello, the "hello world" snap
description: |
GNU hello prints a friendly greeting.
grade: devel
confinement: devmode
$ snapcraft
Using 'snapcraft.yaml': Project assets will be searched for from the 'snap' directory.
Launching a VM.
[...]
Snapped hello_2.10_amd64.snap
$  sudo snap install hello_2.10_amd64.snap --devmode --dangerous
$ hello
$  sudo snap remove hello
$ snapcraft clean

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store