What is Snapcraft?

6,273

Snapcraft is a set of tools bundled under the snapcraft command to easily create (craft) packages for multiple Linux distributions. These .snap packages generally contain self-contained apps, provide secure isolation and are installable either from the Ubuntu Software Store or manually via the snap install <snap-name>.snap command.

In a nutshell, as a developer you would write code using your regular tools, and let Snapcraft take care of assembling it for distribution. Snapcraft also excels in enabling developers port their existing apps to any snap-enabled Linux platform.

Snapcraft:

  • Is intelligent: it fetches, builds and assembles diverse pieces of software (parts) from remote sources into a final .snap package, which contains all dependencies it needs to function
  • Needs a recipe: it relies on a snapcraft.yaml file that specifies the parts and plugins required to create the package
  • Is extensible with plugins. While it ships with a set of the most common build system plugins to cater for a wide range of app builds, it can be easily extended with new ones.

The 3-minute Snapcraft tour

Install Snapcraft on Ubuntu

You will need Ubuntu 16.04 LTS to use Snapcraft. Open a terminal with Ctrl+Alt+t and simply install Snapcraft with this command:

sudo apt install snapcraft
sudo apt install build-essential  # Optional, but useful for different builds

If you are using another distro, check out the alternative installation instructions >

Test-drive Snapcraft

The following example crafts a package that contains a service that allows you to paste and share. Once finished, you can install it manually on your snappy device for testing purposes or upload it to the Store for other users.

First of all we open a terminal and download the example from the examples repository:

sudo apt install git
git clone https://github.com/ubuntu-core/snapcraft.git
cd snapcraft/demos/gopaste

Notice the snapcraft.yaml file in that directory, which specifies a service and the parts required to assemble the final .snap. You can optionally examine it with a text editor.

Now run the snapcraft command on the terminal. This will cause all snapcraft subcommands to run in sequence to build the parts and put the results in the final .snap package. During development, you would normally run the steps separately until you are confident that the whole build and assembly works.

$ snapcraft 
Pulling gopaste 
env GOPATH=/tmp/snapcraft/examples/gopaste/parts/gopaste/build go get -t -d github.com/wisnij/gopaste/gopasted
Building gopaste 
env GOPATH=/tmp/snapcraft/examples/gopaste/parts/gopaste/build go build github.com/wisnij/gopaste/gopasted
env GOPATH=/tmp/snapcraft/examples/gopaste/parts/gopaste/build go install github.com/wisnij/gopaste/gopasted
env GOPATH=/tmp/snapcraft/examples/gopaste/parts/gopaste/build cp -a /tmp/snapcraft/examples/gopaste/parts/gopaste/build/bin /tmp/snapcraft/examples/gopaste/parts/gopaste/install
Staging gopaste 
Snapping gopaste 
Generated 'gopaste_1.0_amd64.snap' snap

On the output of the command you can see the steps snapcraft runs for you:

  1. Pull: it pulls the code from the required gopaste part from a remote Github repo
  2. Build: it builds gopaste locally
  3. Stage: after the build, the parts are put into a single directory tree, the "staging area"
  4. Snap: the final .snap package is created from the assembled parts in the staging area

Notes:

  • You'll find the final snap file as gopaste_1.0_amd64.snap (notice in my case I built it on my amd64 desktop, e.g. Raspberry Pi 2 packages would have the _armhf architecture suffix).
  • You can also run each command individually: snapcraft pull, snapcraft build, snapcraft stage or snapcraft snap
  • Use snapcraft -h for a quick overview of all commands available.

And that's it for a quick glimpse of what Snapcraft can do! Learn more about Snapcraft >

Share:
6,273

Related videos on Youtube

David Planella
Author by

David Planella

I work at GitLab as Director of Community Relations. Before, I worked for Canonical as the former Ubuntu Community Team Manager. As an Open Source contributor, I am mostly involved in app development and localization: I'm the developer of Qreator, former lead the Ubuntu Catalan Translators team and also a GNOME translator. In the past I've contributed to other projects, such as Debian or Mozilla. Blog Google+ Twitter

Updated on September 18, 2022

Comments

  • David Planella
    David Planella over 1 year

    I've been trying snappy Ubuntu core on a Raspberry Pi 2 device, and I've heard I can use Snapcraft to do development on it. Now I hear that snaps are available on the desktop and server for Ubuntu 16.04 and other distros.

    What is Snapcraft and how can I use it?

  • David Planella
    David Planella over 8 years
    I appreciate the comment, but while I'm not one of the snapcraft developers, I'm also not sure how explaining what a tool to create Ubuntu snaps is and how to use it, on an Ubuntu site, could ever constitute spam.
  • Ismael Miguel
    Ismael Miguel over 8 years
    The wording on the list of features triggered that on me. And also "And that's it for a quick glimpse of what Snapcraft can do!"
  • dholbach
    dholbach over 8 years
    I would agree with David here.
  • lapisdecor
    lapisdecor about 8 years
    The bzr part should be replaced by equivalent git commands, since the code is now on Github.
  • lapisdecor
    lapisdecor about 8 years
    Also, to do this you need to install git and gcc if you are using git and not bzr.
  • Attila Szeremi
    Attila Szeremi over 5 years
    Question: how is this able to get around the typical hurdle when sharing Linux applications that you normally have to compile an application for each Linux distro? I believe because usually glibc is the library that people don't like to statically compile into their applications for some reason.