How to Install and Use Snapcraft on Linux

Snapcraft Linux Featured

Have you ever wondered how some Linux distributions make it so easy to install software while others can be quite difficult? The difference is in the package format that they use. Some distributions use .deb files while others use .rpm.

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.

sudo apt update -y  && sudo apt install snapd -y
Snapcraft Install Snapd Ubuntu

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:

sudo dnf install epel-release -y && sudo dnf update -y
Snapcraft Enable Epel Repository

Then, install Snapd with the below command. Enter the password for the sudo user when prompted.

sudo dnf install snapd -y
Snapscrat Install Snapd Centos

In Fedora, install with the command:

sudo dnf install snapd

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.

sudo systemctl enable --now snapd.socket

Create a symbolic link between “/var/lib/snapd/snap” and “/snap” to enable the classic snap support.

sudo ln -s /var/lib/snapd/snap /snap

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.

snap --version
Snapcraft Snapd Version

You can also check the status of the Snapd service with the followng command.

sudo systemctl status snapd
Snapcraft Status Snapd Ubuntu 1

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.

sudo snap install snapcraft --classic
Snapcraft Install Snapcraft

Verify that Snapcraft is installed by checking the version number.

snapcraft --version
Snapscraft 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.

  1. Create a project directory named “hello” with the following command:
mkdir -p ~/mysnaps/hello

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.

  1. Move into the newly created project directory and initialize the project with the init command to create a file named “snapcraft.yaml” in your project directory. You can use the “snapcraft.yaml” file to configure your snap later.
cd ~/mysnaps/hello
snapcraft init
Snapcraft Initialize Project 1
  1. Check the structure of your project with the tree command, which prints a graphical representation of your project directory.
tree -a

You will see an output similar to the following.

Check The Structure Of Your Project

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.

  1. Open the “snapcraft.yaml” file in your favorite text editor. We used nano in this tutorial.
sudo nano snapcraft.yaml
  1. Delete the placeholder lines and replace them with the following content.
name: hello
base: core18
version: '2.10'
summary: GNU Hello, the "hello world" snap
description: |
  GNU hello prints a friendly greeting.
grade: devel
confinement: devmode

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

  1. Add the following lines after the confinement field in your “snapcraft.yaml” file. Here we specify the application we want to package.
apps:
 hello:
  command: bin/hello

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

  1. In a new line, add the following parts command:
parts:
  gnu-hello:
    source: http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz
    plugin: autotools

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.

  1. At this point, your snapcraft.yaml file will look like the one below. Save and close the snapcraft.yaml file before you move on to the next step.
Snapcraft.yaml File

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.

snapcraft

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.

Snapcraft Multipass

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.

Snapcraft Build

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:

sudo snap install --devmode hello_2.10_amd64.snap

The --devmode flag tells snap that you want to install the snap in devmode, which is useful for testing purposes.

Snapcraft Install Snap

Next, run the following command to run your hello-world application.

hello

You will see the following output, which indicates that your hello-world snap is working as expected.

Snapcraft Run Your Hello World Application

To see the version of your hello-world application, run the following command.

hello --version
Snapcraft Hello World Application Version

If you encounter an issue while testing the snap, get more information by using the --debug flag and running the following command.

snapcraft --debug
Snapcraft Debug

Frequently Asked Questions

Is Snapcraft safe?

Absolutely! Snapcraft is the official tool for building snaps. It is developed and maintained by Canonical, the company behind Ubuntu. Rest assured that Snapcraft is safe and reliable.

Is Snap like Docker?

Yes and no. Snap and Docker are both container technologies but serve different purposes. Think of Snap as a packaging format for your application and Docker as a runtime environment for your application. You can set up WordPress with Nginx and PHP in a Docker container using the official WordPress and Nginx images but can’t create a Snap package for your WordPress site.

Is snap better than apt or apt-get?

It depends. Both systems have their own advantages and disadvantages. Apt and apt-get is the traditional package manager for Debian-based Linux distributions. It has been around for a long time and is very stable, yet it has a few drawbacks. For instance, apt does not support versioning and rollback. Snap, on the other hand, supports both versioning and rollback. So if you want to experiment with new versions of your application, Snap is the way to go. But if you want a stable system, apt and apt-get is the better choice.

Image credit: Freepik. All screenshots by Nicholas Xuan Nguyen.

Nicholas Xuan Nguyen
Nicholas Xuan Nguyen

I am a big fan of Linux and open source software. I have been using Linux for over a decade and I absolutely love it. I am also a big fan of writing. In my spare time, I enjoy reading, playing video games.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox