August 22, 2016
by Rares Aioanei

Introduction

What are snaps and why you should use them? The Linux ecosystem has suffered from an old problem since the dawn of the concept of 'distribution', and that problem is fragmentation. One of the biggest issues that cause this fragmentation is different package formats; I can't run my .debs on my Fedora system or my .rpms on my Ubuntu machine. Yes, we do have alien, which should allow the transition between the two formats, but there are two problems with this approach : there are other package formats besides rpm and deb and besides, alien doesn't always work as expected. So the issue is still there, or I should say, was there. Enter snap, the universal Linux package format, which strives to offer users and developers a single packaging format and easiness when it comes to creating new packages with the applications and libraries that are needed, ensuring that said packages are easily shareable between distributions. Dell, Samsung and the Linux Foundation are quoted as contributors, while among supported distributions are Fedora, Ubuntu, Arch or OpenSUSE.

This article will detail how to use snaps as a simple user, as well as instructions for developers/packagers on how to create snaps for others to use. The OS we're gonna use is Ubuntu 16.04, but the instructions below shouldn't be hard to adapt to other distributions.

Snaps as a simple user

This part will give you a tour of snap from a user perspective : how to install the necessary tools and how to use them for basic, day-to-day usage. First, you need to install snapcraft, a package that provide snap, the go-to tool for aforementioned day-to-day operations :
 $ sudo apt install snapcraft
That's all. Now, before we go further, let us talk about some details : first, applications offered as snaps are isolated containers, which helps a lot in terms of resource consumption as well as security matters. You will find everything installed by snap in /snap/, which in turn has its own hierarchy : /snap/bin, /snap/lib and so on. Second, you get updates automatically. Third, think of snaps as complementary to existing Linux packages, not as replacements (well, not now, anyway).

And finally, snaps are easily integrated with installed software, making use of existing libraries through interfaces. As we said in the past, there is no substitute for the manual. So, while we will give you a tour, if you want to delve deeper you will need to do some reading on your own. So let's start with basic snap operations : if you want to search for a package, do
 $ snap find hello
Here, 'hello' is the example string we're looking for. Going further with the same string, let's install something :
 $ sudo snap install hello
You can view the installed snaps with 'list' :
 $ snap list
Update with refresh, either everything you have installed or certain snaps only :
 $ sudo snap refresh
OR
 $ sudo snap refresh hello
What's somewhat new (yes, you can revert with yum for example, and not only, but that doesn't always work as wanted) is the 'revert' command :
$ sudo snap revert hello

Using snap as a developer

Getting set up

First and foremost, let's see what you need to get started in order to create snaps. The tool of the trade is called snapd, and on Ubuntu and Debian it's as simple as
$ sudo apt install snapd snapcraft
while on Fedora you can do
 $ sudo dnf copr enable zyga/snapcore
 $ sudo dnf install snapd
 $ sudo systemctl enable --now snapd.service
 $ sudo setenforce 0
 $ sudo $editor /etc/selinux/config #set SELINUX=permissive and reboot to make SELINUX settings permanent
 
and that's it. However, there's a catch. While these tools work with most major Linux distributions, snapcraft, the tool we're gonna install and use so we can learn how to create snaps, is available from the Ubuntu repositories and AUR, as far as we know. The rest of the Linux users will just have to clone the git repository (https://github.com/snapcore/snapcraft) and build it.

Taking the tour and creating a snap

Before we continue, please make note that your snaps (or existing ones) will be and are way bigger than the corresponding repository packages. Why? Because they also contain every dependency needed for the programs to work, so they are self-sustaining, in a way. Once you have snapcraft installed, it's recommended you take the tour :
 $ snapcraft tour
This command will initialize some directories and subdirectories (by default under ./snapcraft-tour) and you will get source code samples, instructions, and numbered steps which will guide you through the basics. For example, the first step is, how else?, hello world. Type
 $ cd snapcraft-tour/00-SNAPCRAFT/01-easy-start
and you will only see the .yaml file in there, which basically is the file that describes the snap. Have a look at it, you will see that it defines name, what to fetch, from where, what application will result, and so on. Afterwards, just run
 $ snapcraft
inside the above directory and it will do everything needed and give you a snap. Said snap can be installed, provided you still are in the directory we mentioned above, with
 $ sudo snap install ./hello*.snap
Take some time to play with the .yaml file - making a copy for this is always a good idea - see what happens if you change some variables there, and discover how your changes influences the creation of the snap. Remember you can always distribute your snap to your friends or create a repository for it. One last note : the 'source' section in the .yaml has a URL as a value, but you can also have a local value, e.g. ./src/my_dep/.

Last words

We barely just scratched the surface with the snaps, mind you, but there is a lot to learn and cool things to do with snaps! The neverending issue of having software only packaged as, say, RPMs and giving users headaches when needed on Debian, for example, seems to be over. Like always, we encourage you to play with snaps, create some and don't forget to share ! Have fun!