Ubuntu 20.04 Snap Package Management

The previous chapter explored the use of the Advanced Packaging Tool (APT) to install and update software packages on an Ubuntu system. In recent years a new package management system called Snap has been under development by the Ubuntu team at Canonical, Ltd. Although there are no official plans to replace APT entirely with Snap, the list of packages that can now be installed as “snaps” continues to grow.

The goal of this chapter is to introduce the Snap system, highlight the key advantages it has over the APT system and to outline how to use the snap command-line tool to install and manage snap -based software packages.

1.1  Managing Software with Snap

The apt tool installs software that is packaged in .deb files. A package installed using apt will often be dependent upon other packages that will also need to be installed in order to function. During an installation, apt will also download and install these additional package dependencies. Consider a graphics design app which depends on a particular imaging library. During installation, apt will install the graphics app package in addition to the package containing the library on which it depends. Now, assume that the user decides to install a different graphics tool that also relies on the same graphics library. Usually this would not be a problem since the apps will both share the same library, but problems may occur if the two apps rely on different versions of the library. Installing the second app may, therefore, stop the first app from working correctly. Another limitation of apt and .deb packages is that it is difficult to have two different versions of the same tool or app installed in parallel on a system. A user might, for example, want to keep version 1.0 of the graphics app installed while also trying out the latest beta release of the 2.0 version. After trying out version 2.0, the user may then want to remove version 1.0, leaving the new version installed, a task that would be hard to achieve using apt.

The snap system has been designed specifically to address these types of shortcomings. The snap tool installs .snap packages that contain all of the libraries and assets that are required for the software to function. This avoids the need to install any dependencies as separate, independent packages. Once a snap has been installed it is placed in a self-contained location so that no dependencies are shared with other packages. Our hypothetical graphics apps, for example, each have their own copies of the exact imaging library version used by the app developer which cannot be deleted, replaced with an incompatible version or overwritten by any other package installations.

Of course the use of snaps results in larger package files which leads to longer package download times, slower installation performance and increased disk space usage. That being said, these shortcomings are generally more than outweighed by the advantages of snap packages.

Snap also supports the concept of channels which allow app developers to publish different versions of the same app. Snap channels are the mechanism by which multiple versions of the same software are able to be installed in parallel.

1.2  Basic Snap Commands

Although many software packages are still provided in .deb format and installed using apt, the number of apps and tools now provided in snap format is increasing rapidly. In fact, all of the software listed in the Ubuntu Software tool (outlined previously in the chapter entitled “A Guided Tour of the GNOME 3 Desktop”) are packaged and installed using snap. Snap-based software may also be installed using the snap command-line tool, the basics of which will be covered in this section.

To list the snap packages that are available for a specific category of software, run a command similar to the following:

# snap find "image editor"
Name             Version       Publisher          Notes  Summary 
gimp             2.10.18       snapcrafters       -      GNU Image Manipulation Program 
paintsupreme-3d  1.0.41        braindistrict      -      PaintSupreme 3D
.
.

The above command will list all snap packages available for download and installation containing software related in some way to image editing. One such result will be the gimp image editor. Details about the gimp snap can be found as follows:

$ snap info gimp 
name:      gimp 
summary:   GNU Image Manipulation 
Program publisher: Snapcrafters 
store-url: https://snapcraft.io/gimp 
contact:   https://github.com/snapcrafters/gimp/issues 
license:   GPL-3.0+ description: |
  Whether you are a graphic designer, photographer, illustrator, or scientist, 
GIMP provides you with sophisticated tools to get your job done. You can further enhance your productivity with GIMP thanks to many customization options and 3rd party plugins.     This snap is maintained by the Snapcrafters community, and is not necessarily   endorsed or officially maintained by the upstream developers.
  
Upstream Project: https://www.gimp.org/snapcraft.yaml 
Build Definition: https://github.com/snapcrafters/gimp/blob/master/snap/snapcraft.yaml snap-id: KDHYbyuzZukmLhiogKiUksByRhXD2gYV 
channels:
   latest/stable:    2.10.18 2020-03-03 (252) 182MB   
   latest/candidate: ↑                                
   latest/beta:      ↑                                
   latest/edge:      2.11.02 2020-04-28 (265) 184MB -

The snap find command can also be used to find a specific package by name, together with other packages that provide similar features. Searching for the VLC media player app, for example, also lists similar software packages:

# snap find vlc
Name            Version                 Publisher  Notes  Summary
vlc             3.0.10                  videolan   -      The ultimate media player
mjpg-streamer   2.0                     ogra       -      UVC webcam streaming tool
audio-recorder  3.0.5+rev1432+pkg-7b07  brlin      -      A free audio-recorder for Linux (EXTREMELY BUGGY)
tundra          0.1.0                   m4tx       -      MyAnimeList scrobbler
dav1d           0.6.0                   videolan   -      AV1 decoder from VideoLAN
peerflix        v0.39.0+git1.df28e20    pmagill    -      Streaming torrent client for Node.js

The snap list command-line option can be used to obtain a list of snap packages that are already installed on a system:

$ snap list
Name                 Version                     Rev   Tracking         Publisher   Notes
canonical-livepatch  9.5.5                       95    latest/stable    canonical  
core                 16-2.44.3                   9066  latest/stable    canonical  core
core18               20200427                    1754  latest/stable    canonical  base
gnome-3-28-1804      3.28.0-16-g27c9498.27c9498  116   latest/stable    canonical 
.
.

To install a snap package (for example to install the Remmina remote desktop tool), run the snap command with the install option followed by the name of the package to be installed:

$ snap install remmina

To remove a snap package, simply specify the package name when running snap with the remove option:

# snap remove remmina

1.3  Working with Snap Channels

If no channel is specified when performing an installation, snap will default to the stable channel. This ensures that the latest reliable version of the software is installed. To perform the installation from a different channel, begin by identifying the channels that are currently available for the required package using the snap info option:

# snap info remmina
name:      remmina
summary:   Remote Desktop Client
.
.
channels:
  latest/stable:    v1.4.3+git13.688f5f75 2020-04-20 (4139) 37MB -
  latest/candidate: ↑                                            
  latest/beta:      ↑                                            
  latest/edge:      v1.4.3+git27.1bd753df 2020-05-01 (4150) 37MB -

From the above output we can see that while the stable version of the Remmina app is v1.4.3+git13.688f5f75 a more recent version is available in the edge channel.

Of course the candidate, beta and edge channels provide access to the software in increasingly unstable forms (referred to as risk level), but if you would like to try out an early access version of upcoming features of a package, install from a higher risk channel. For example:

# snap install --channel=edge remmina

The channel selection may also be abbreviated to –stable, –candidate, –beta or –edge, for example:

# snap install --edge remmina

If the package is already installed, the risk level can be changed using the switch option:

# snap switch channel=edge remmina

This will change the channel that snap is tracking for the specified package. The current channel being tracked for a package can be identified using the snap info command:

# snap info remmina 
name:      remmina 
. 
.
tracking:     latest/edge 
. 
.

Simply running a snap switch command will not immediately refresh the package to use the new channel. To understand how this works it will help to explore the snap refresh schedule.

1.4  Snap Refresh Schedule

The snap system includes a background service named snapd which is responsible for refreshing installed snaps based on the channels that they are tracking. By default, snapd performs refresh operations at regular intervals (typically four times a day). To identify when the last refresh was performed and the next is due, run the following command:

# snap refresh --time
timer: 00:00~24:00/4
last: today at 07:23 EDT
next: today at 14:25 EDT

The above output also includes timer information which indicates that the refresh will be performed four times within each 24 hour period:

.
.
timer: 00:00~24:00/4
.
.

The snap command can also be used to force a refresh of all installed snap packages as follows:

# snap refresh

Alternatively, to refresh a specific package:

# snap refresh remmina

To switch a package to a different channel without having to wait for the next snapd service refresh, simply run the snap refresh command as follows, specifying the target channel:

# snap refresh remmina --channel=edge

The snap system also has a set of four properties that may be modified to adjust the refresh schedule used by snapd:

  • refresh.timer: Stores the current refresh schedule and frequency.
  • refresh.hold: Used to delay refresh operations until the specified day and time (in RFC 3339 format).
  • refresh.metered: Pauses refresh operations when network access is via a metered connection (such as a mobile data connection).
  • refresh.retain: Used to configure the number of revisions of each snap installation that are to be retained.

For example, to schedule the refresh to occur on weekdays between 1:00am and 2:00am:

# snap set system refresh.timer=mon-fri,1:00-2:00

Similarly, the following command will configure refreshes twice every day to occur between the hours of 6:00am and 7:00am, and 10:00pm and 11:00pm:

snap set system refresh.timer=6:00-7:00,22:00-23:00

A full explanation of the timer format and syntax can be found online at the following URL:

https://snapcraft.io/docs/timer-string-format

After making a change to the timer, be sure to check the settings as follows:

# snap refresh --time
timer: mon-fri,1:00-2:00
last: today at 07:23 EDT
next: tomorrow at 01:00 EDT

To pause refreshes, the date and time at which refreshing is to resume must be specified using the RFC 3339 format, details of which can be found at the following URL:

https://tools.ietf.org/html/rfc3339

In summary, the date and time should use the following format:

YYYY-MM-DDTHH:MM.SS<UTC offset>

For example, to specify a hold until October 12, 2020 at 3:20am for a system located in New York, the date and time would be formatted as follows:

2020-10-12T03:20:50.0-05:00

Note that since New York uses Eastern Standard Time (EST) it has a -5 hour offset from Coordinated Universal Time (UTC-5:00). Having formatted the date and time, the following command would be used to set the hold:

# snap set system refresh.hold="2020-10-12T03:20:50.0-05:00"

To check the current hold setting, use snap with the system get option:

# snap get system refresh.hold
2020-10-12T03:20:50.0-04:00

To remove the hold, simply assign a null value to the property:

# snap set system refresh.hold=null

The refresh.retain property can be set to any value between 0 and 20, for example:

# snap set system refresh.retain=10

Finally, to pause refresh updates while the system is on a metered connection, set the refresh. metered property to hold as follows:

# snap set system refresh.metered=hold

As with the hold property, disable this setting by assigning a null value to the property:

# snap set system refresh.metered=null

1.5  Snap Services

It is worth noting that some snap packages include their own services which run in the background when the package is installed (much like the systemd services described in the chapter entitled “Managing Ubuntu systemd Units”). To obtain a list of snap services that are currently running on a system, execute the snap command with the services option:

# snap services
Service                                   Startup  Current  Notes
canonical-livepatch.canonical-livepatchd  enabled  active   -

The above output indicated that the LivePatch snap service is currently enabled and active. To stop or stop a service the following snap commands can be used:

# snap start canonical-livepatch.canonical-livepatch
# snap stop canonical-livepatch.canonical-livepatch

Similarly the snap enable and disable options may to used to control whether or not a service starts automatically on system startup:

# snap enable canonical-livepatch.canonical-livepatch
# snap disable canonical-livepatch.canonical-livepatch

If the snap service generates a log file, that file can be viewed as follows:

# snap logs canonical-livepatch
2020-05-06T13:21:58Z canonical-livepatch[763]: No payload available.
2020-05-06T13:21:58Z canonical-livepatch[763]: during refresh: cannot check: No machine-token. Please run 'canonical-livepatch enable'!
.
.

It is also still possible to manage snap services using the systemctl command. This usually involves prefixing the service name with “snap.”. For example:

# systemctl status snap.canonical-livepatch.canonical-livepatchd

1.6  Summary

Until recently, all Ubuntu software packages were stored in .deb files and installed using the Advanced Packaging Tool (APT). An increasing number of packages are now available for installation using Snap, a package management system developed by Canonical, Ltd. Unlike apt packages, snap bundles all of the dependencies for a package into a single .snap file. This ensures that the software package is self-contained with its own copy of all of the libraries and assets needed to run. This avoids the potential conflicts of packages relying on different versions of the same shared assets and libraries. The Snap system also allows different versions of the same packages to be installed in parallel. All of the software listed in the Ubuntu Software tool are supplied as snap packages. In addition, snap can be used to install, remove and manage packages from the command-line.