Snap is a technology developed by Canonical to make it easier to install software in the Ubuntu distribution. It is a relatively recent development. The development of Snap took place over several years, but only by the release of Ubuntu 16.04 did it appear in a state in which it is ready for use by the masses.
The main advantage of Snap is the ability to install programs without dependencies. All the necessary libraries are already in the package with the program and nothing is required from the system to run it. When updating the user has to update one snap package without making changes to the system, and in case of failures and errors can roll back the package to the previous version. On the web you can often come across debates
among users about whether snap technology is really good or should be abandoned in favor of deb
packages. But time and testing will tell. Next in this article we will look at the process of creating snap
packages. Snap packages are a bit easier to create than the same deb
packages.
To create snap
packages we will use a special tool called Snapcraft. This is the official tool for creating snap packages, which allows the developer to package programs in order to build the necessary files throughout the system. Snapcraft allows you to not only copy files, but also download the source code and build the program.
Next, let's see how to create snap
packages using the Samplenote application as an example.
First install Snapcraft. To get the latest version add a PPA to our system:
$ sudo add-apt-repository ppa:snappy-dev/tools
Now update the package list and install the program:
$ sudo apt-get update
$ sudo apt-get install snapcraft
When the installation of Snapcraft is complete, you can move on to get the source code of the program. First, download the sources from GitHub and unzip them:
$ wget https://github.com/Automattic/simplenote-electron/releases/download/v1.0.1/Simplenote-linux-x64.1.0.1.tar.gz
$ tar xvzf Simplenote-linux-x64.1.0.1.tar.gz
Since the program does not need to be compiled our task is a little easier. Go to the folder with the program and initialize the Snapcraft build environment there:
$ cd Simplenote-linux-x64
$ snapcraft init
After executing the last command, the snapcraft.yml
file will appear in the folder. By editing the file, let's customize the process of creating our snap package. We will need a file with the following contents:
name: simplenote
version: 1.0.1
summary: The simplest way to keep notes.
description: The simplest way to keep notes. Light, clean, and free.
apps:
simplenote:
command: usr/bin/wrapper
plugs:
- unity7
- opengl
- network
parts:
simplenote:
plugin: copy
stage-packages:
- libnss3
- fontconfig-config
- gnome-themes-standard
files:
Simplenote: Simplenote
wrapper: usr/bin/wrapper
icudtl.dat: icudtl.dat
snapshot_blob.bin: snapshot_blob.bin
natives_blob.bin: natives_blob.bin
resources*: resources
libnode.so: usr/lib/x86_64-linux-gnu/libnode.so
libffmpeg.so: usr/lib/x86_64-linux-gnu/libffmpeg.so
The first four lines are information about the package itself, the name of the program, the version and a short and full description.
Command - this is the command to start the program, further we will use the script warpper
, because we need to specify additional environment variables.
Plugs
- these are the interfaces that can be used by the program. The point is that snap programs work in an isolated environment, from where they can not get access to hardware and personal data. For our program to work properly it needs to give access to opengl
, unity7
and network
interface.
You can see the available interfaces and the programs that use them by executing the command:
$ snap interfaces
In the parts
section you need to specify the program files. Specifically, what snapcraft
needs to do when creating a package. Since we do not need to compile anything, we use the plugin copy, to copy files. The files that need to be copied are listed in the files
section. There are not only executables, but also all the necessary libraries, so the program will get all the components it needs.
The format of the entry is as follows:
system_file: snap_package_file_path
There is one non-standard entry:
resources*: resources
The wildcard character *
allows you to copy the entire directory. To avoid copying all the files from the system, there is an option in the site-packages section to tell the program which packages should be installed in the snap package, in our case it is libnss3
, fontconfig-config-config
and gnome-themes-standard
.
To see which libraries the program uses, you can use the command:
ldd Simplenote | grep Simplenote
libnode.so => /home/sergiy/test/Simplenote/Simplenote-linux-x64/./libnode.so (0x00007f05fb477000)
libffmpeg.so => /home/sergiy/test/Simplenote/Simplenote-linux-x64/./libffmpeg.so (0x00007f05f61ec000)
It is important that these libraries are shipped with the program, and therefore we need to prescribe them in the snapcraft.yaml
configuration file. All other libraries are available on the system and will be detected by snapcraft
automatically.
Next, let's create the wrapper
file:
!/bin/sh
export FONTCONFIG_PATH=$SNAP/etc/fonts
export FONTCONFIG_FILE=$SNAP/etc/fonts/fonts.conf
export XDG_DATA_HOME=$SNAP/usr/share
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu/
exec "$SNAP/Simplenote" "$@"
This is simple here - a Bash
script. Set the paths for the fonts, and tell the program where to look for the libraries so that libnote.
so
and libffmpeg.so
are successfully found. And the last line starts simplenote
for execution.
The next thing left to do is to make this file executable:
$ chmod +x wrapper
To start building the package, run:
$ snapcraft
The build may take some time, and once the build is complete you can install the package:
$ sudo snap install simplenote_1.0.1_amd64.snap
You can use the usual command to run the program:
$ simplenote
Now the program is installed. You can use the exact same method of creating snap
packages to package your applications.