Skip to Content

What is a snap package in Linux?

What is a snap package in Linux?

A snap package is a software package format used in Linux operating systems to bundle applications with all their dependencies. It allows app publishers to distribute apps on multiple Linux distributions while maintaining a single build. Snap packages aim to make installing, updating, and removing applications easier for Linux users.

What is Snapcraft?

Snapcraft is the tool used to build and publish snap packages. It is designed to make the packaging process easier by automating common tasks like dependency management. Snapcraft is developed by Canonical, the company behind Ubuntu.

To create a snap package, developers simply describe their app’s components in a snapcraft.yaml file. This includes specifying the app source code location, runtime dependencies, build instructions, and metadata like app name and version. Snapcraft takes care of pulling source code, building binaries, downloading dependencies, and generating the snap package.

Once built, snap packages can be published to the Snap Store, which is a central repository of snaps similar to app stores on mobile platforms. Users can then install apps from the Snap Store on any distro that supports snaps.

Key Features of Snaps

Here are some of the key features and benefits of snap packages:

Bundling Dependencies

A major advantage of snaps is that they bundle all the dependencies an app needs to run. This means developers don’t have to worry if all the required libraries are present on the target system. The app is certain to run as intended on any Linux distro supporting snaps.

Cross-distribution Support

Snaps are designed to work across many Linux distributions including Ubuntu, Fedora, Manjaro, and more. Developers can build one snap package that can be installed on all supported distros without modification.

Automatic Updates

Snap packages feature automatic updates in the background. This means users always have the latest bug fixes and features without having to manually trigger updates.

Rollbacks

If an update causes issues, snaps make it easy to roll back to the previous working version. This provides a safety net in case an update introduces regressions.

Security

Snaps run isolated from the rest of the system by default through application confinement. This contains apps and protects the rest of the system from unexpected behavior. Snaps also support security features like HTTPS downloads.

Simplified Install/Remove

Installing and removing snap packages is designed to be quick and painless for users. A single command installs a snap along with all dependencies. Snaps can also be removed just as easily with no leftover cruft on the system.

How Snaps Work

Under the hood, snaps rely on a few key technologies to work:

SquashFS Filesystem

Snaps use SquashFS, a compressed read-only filesystem to bundle all the app files, binaries, and dependencies. This allows the entire app environment to be contained in a single package.

Snapcraft Build Tool

As mentioned earlier, Snapcraft is the official build tool for compiling source code and bundling it into a SquashFS snap package.

Snap Daemon

The snapd daemon runs on systems that support snaps and handles installing, updating, starting, and stopping snaps. It manages the snap packages on the system.

Libraries for Integration

Snaps also use supporting libraries that live outside the confinement to enable integration with the OS. This allows snaps to work like traditional apps and access resources like audio, video, home directories, etc.

Snap Store

The Snap Store provides a centralized place to publish and obtain snap packages. It provides app info, screenshots, ratings, reviews and more like traditional app stores.

Installing Snap on Linux

Installing snapd on common Linux distros is straightforward since official packages are available. Here are install instructions for some popular distros:

Ubuntu/Debian

“`
sudo apt update
sudo apt install snapd
“`

Fedora

“`
sudo dnf install snapd
“`

Arch Linux

“`
sudo pacman -S snapd
“`

Manjaro

“`
sudo pacman -S snapd
“`

Once snapd is installed, snap commands can be used. To install a snap:

“`
sudo snap install
“`

To list installed snaps:

“`
snap list
“`

And to remove a snap:

“`
sudo snap remove
“`

Creating a Snap

To create a snap package, you need to follow these steps:

  1. Install Snapcraft
  2. Create a snapcraft.yaml file to describe your snap components
  3. Run `snapcraft` to build the snap from source code
  4. Test the generated snap locally before publishing
  5. Release the snap on the Snap Store for public consumption

Here is an example snapcraft.yaml file:

“`yaml
name: my-snap-name # The name of the snap
version: ‘0.1’ # The version
summary: A short description for the snap
description: |
A longer description of what this snap does

grade: devel # Snap stability grade
confinement: devmode # Run snap in devmode

apps:
my-snap-name:
command: run-my-command

parts:
my-part:
plugin: nil
source: . # Source code location
“`

Once defined, run `snapcraft` to build the snap which will bundle up all the parts defined.

Publishing Snaps

To publish a snap for public use, you need to register, push the snap package to the Snap Store, and release it on selected channels. Here are the steps:

  1. Register for an account on snapcraft.io to access the Snap Store
  2. Build your snap locally and make sure it works as expected
  3. Run `snapcraft push ` to upload to the Snap Store
  4. On the Snap Store web interface, select channels to release the snap
  5. The snap will now be available for users to install!

Some things to note about channels – they allow publishing the same snap at different stability levels. For example, you can release to the:

  • edge channel for cutting edge builds
  • beta channel for testers
  • candidate channel for final testing
  • stable channel for general public use

This allows controlling the update flow for different user segments.

Advantages of Snaps

Here are some of the main advantages of using snap packages:

  • Automatic updates – Apps stay up-to-date without user intervention
  • Isolation – Snaps run isolated with own dependencies and don’t impact rest of system
  • Security – Strict confinement for snap apps protects system from bugs/exploits
  • Cross-distro compatibility – Snaps work on many Linux distros without changes
  • Easy rollbacks – Ability to revert snap to previous working version if update fails
  • Simplified install/remove – Snaps can be installed and removed easily with no leftovers behind
  • Works across devices – Same snap can work on desktops, servers, cloud, IoT, etc.

Disadvantages of Snaps

Some potential downsides to consider about snaps:

  • Larger app packages since dependencies bundled
  • Startup performance impact since unpacked on first run
  • Dependency conflicts between snap and distro packages
  • Limited desktop integration on some distros
  • Strict confinement limits hardware access for some apps

However, Snaps continue to improve on many of these downsides with each release.

Snaps vs Flatpak

Flatpak is an alternative packaging format and tool for distributing desktop applications on Linux. It shares some similarities with snaps:

  • Applications bundled with runtimes/dependencies
  • Sandboxed environment for isolation
  • Distribute across many Linux distros

Some differences between the two:

Snap Flatpak
Backed by Canonical Led by community
Primarily targets servers, cloud, IoT Focused on desktop apps
Uses squashfs for immutable file system Relies on OSTree for ‘atomic’ updates

In summary, snaps have broader ambitions beyond the desktop but flatpaks offer more polished desktop experience currently. The technologies continue to evolve and borrow ideas from each other.

Conclusion

Snap packages provide an efficient way to distribute apps on Linux while containing all dependencies in a single bundle. They simplify application management for users and enable developers to target multiple distros easily. With auto-updates, rollbacks, and strict isolation, they enhance reliability and security.

However, some downsides remain around performance overhead and desktop integration gaps. As snap capabilities grow, it aims to become a standardized packaging format for not just servers and cloud, but also desktops, devices, and the Internet of Things.