15

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?

27

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 >

6
  • 9
    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. Oct 16 '15 at 16:24
  • 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!" Oct 16 '15 at 16:30
  • 1
    I would agree with David here.
    – dholbach
    Oct 21 '15 at 21:09
  • 1
    The bzr part should be replaced by equivalent git commands, since the code is now on Github.
    – lapisdecor
    Apr 24 '16 at 9:36
  • 2
    Also, to do this you need to install git and gcc if you are using git and not bzr.
    – lapisdecor
    Apr 24 '16 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.