But what if there was a package format that could be used on any distribution? That’s where Snapcraft comes in. In this tutorial, you will learn how to install and use Snapcraft to create and install snap packages in Linux.

What Is a Snap Package?

A snap package is a self-contained application package that includes all the necessary dependencies and libraries, making it easy to install and update applications without having to worry about dependency issues.

Why Use Snap Packages?

There are several advantages to using snap packages:

Snaps are easy to install and update. You can install a snap package with a single command, and snaps are automatically updated in the background.Snaps are safe and secure. Since all the dependencies are included in the snap package, there is no risk of a dependency conflict. Also, snaps are isolated from the rest of the system, so they cannot access your data or other applications on your system.

You can install your favorite app on any Linux distribution that supports snaps.

Installing Snapd in Linux

Snapd is a daemon that enables the installation and use of snaps and needs to be installed before you can use Snapcraft. When you install Snapd, it also installs a command line interface (CLI) tool called snap. You can use this tool to manage your snaps. To install Snapd on Ubuntu, open a terminal and enter the following command, entering your user password when prompted. On CentOS, you’ll need to enable the EPEL repository before you can install Snapd. To do this, first enter the following command in your terminal: Then, install Snapd with the below command. Enter the password for the sudo user when prompted. In Fedora, install with the command: In Arch Linux, install snapd from AUR. Check out these AUR helpers to help you install third-party packages easily. Once the installation completes, run the below command to enable the snapd.socket systemd unit. This ensures that the Snapd daemon starts automatically when your system boots up. Create a symbolic link between “/var/lib/snapd/snap” and “/snap” to enable the classic snap support. Now that you have installed the Snapd on your Linux system, check the version of the Snapd with the below command. You will see an output similar to the following. You can also check the status of the Snapd service with the followng command.

Installing Snapcraft

Before you can create snap packages, you need to install Snapcraft, the tool used for building snap packages. To install Snapcraft on Linux, run the following command. The classic flag tells snap to use the classic confinement mode. This flag is required since Snapcraft doesn’t support the newer, more restrictive confinement mode yet. Verify that Snapcraft is installed by checking the version number.

Using Snapcraft to Build a Snap Package

Now for the interesting part: using Snapcraft to create a snap package for an application. In this tutorial, we are creating a basic hello-world snap, a simple snap that prints “Hello, world!” when you run it. After you complete this tutorial, apply the same process to create snaps for your own applications. For the sake of simplicity, the steps below will guide you through the process of creating a snap in Ubuntu. The same steps can be applied on other Linux distros as well.

Starting a Project for Your Snap

First create a project directory for your snap. It will be the working directory for your project and helps you keep your project files organized. The -p flag tells the mkdir command to create any parent directories that don’t already exist. In this case, the mysnaps directory will be created if it doesn’t exist. You can put any future snaps inside this directory. You will see an output similar to the following.

Adding Top-Level Metadata

Snapcraft provides many metadata that you can use to describe your snap package. In this tutorial, we add some basic info that is required for every snap. Here we changed the info of our snap. The core18 base tells Snapcraft that you want to build a snap based on Ubuntu Core 18. The confinement: devmode metadata tells Snapcraft that you want to build a snap that is not confined, which is useful for development and testing purposes.

Exposing Your Application

In this case, we only have one app, hello. The command line tells Snapcraft which binary to run when the snap is installed.

Adding a Part

This tells Snapcraft which software you want to include in your snap package. To build this hello-world snap, you need to download the source code of GNU Hello, then use the autotool plugin to build the application from source.

Building the Snap Package

Now that you have defined your snap, it’s time to build it. To build a snap package, run the following command. You will be asked to install “multipass” if you don’t have it on your system. Snapcraft uses multipass to create an isolated environment for building snaps within a virtual machine. Type y and press Enter to continue. The building process may take a while, depending on your Internet connection and computer specs. Once the building process is finished, you will see something similar to the following output. In the end, you will find a “hello_2.10_amd64.snap” in the project directory.

Testing the Snap Package

To test whether your snap package is working, run the following command: The –devmode flag tells snap that you want to install the snap in devmode, which is useful for testing purposes. Next, run the following command to run your hello-world application. You will see the following output, which indicates that your hello-world snap is working as expected. To see the version of your hello-world application, run the following command. If you encounter an issue while testing the snap, get more information by using the –debug flag and running the following command. Image credit: Freepik. All screenshots by Nicholas Xuan Nguyen.