Skip to Content

How do I create a snap in Ubuntu?

How do I create a snap in Ubuntu?

Snaps are containerized software packages that provide an easy and safe way to install and run applications on Ubuntu and other Linux distributions. Snaps allow developers to bundle an application with all its dependencies into a single standardized package. This eliminates dependency issues and enables the app to work uniformly across different distributions. Snaps are secure, easy to install, and update automatically. Let’s look at how to create your own snap and distribute it on the Snap Store!

What are the benefits of snaps?

Here are some of the key advantages of using snaps:

  • Dependency management – All dependencies bundled into the snap package so no need to worry about dependency conflicts.
  • Security – Each snap runs isolated in its own sandbox and implements security confinement.
  • Automatic updates – Snaps auto-update directly from the publisher to ensure you always have the latest version.
  • Works across distributions – Snaps work uniformly across Ubuntu, Debian, Arch, Manjaro and other major Linux distributions.
  • Easy to install/remove – Snaps can be installed and removed just like regular software packages using apt or software center.
  • Universal Linux Package – Snaps use a common format across Linux distributions unlike .deb or .rpm packages.
  • Publisher Verification – All snaps are reviewed and verified by the Snap Store team for quality assurance.

These benefits make snaps a great way to distribute your software on Linux. Let’s look at how to build a snap for your application.

Prerequisites for creating a snap

Before you can create a snap, you need to ensure you have the following:

  • Ubuntu 16.04 or higher installed – Earlier Ubuntu versions don’t support snaps.
  • Snapcraft installed – This is the tool to build snaps. Install it with:
    sudo snap install snapcraft --classic
  • Git installed – Needed to clone snapcraft examples. Install it with:
    sudo apt install git
  • An application to package – You’ll need an app source code to build a snap for.
  • Optional: LXD for cleanbuilds – LXD provides throwaway build environments for better isolation. Install it with:
    sudo snap install lxd

That covers the basics you need. Let’s move on to actually creating a snap.

Creating a simple snap

Here are the steps to create a basic working snap from an example:

  1. Clone the examples repository:
    git clone https://github.com/snapcore/snapcraft.git
  2. Navigate to an example, like hello-world:
    cd snapcraft/examples/hello-world
  3. Run snapcraft to build the snap:
    snapcraft
  4. Install the built snap file:
    sudo snap install *.snap --dangerous
  5. Run the installed snap:
    hello-world

This demonstrates the basic workflow – get code, run snapcraft, install, and run. Now let’s look at how we can customize the snapcraft.yaml file to build our own snaps.

Understanding the snapcraft YAML file

The snapcraft.yaml is a configuration file that defines the snap contents and build instructions. Here is an overview of some key parts of snapcraft.yaml:

Metadata

This includes information like app name, version, description and other details about the snap:

name: my-snap-name # The name of the snap 
version: '1.0' # Version number
summary: Single-line summary description
description: Detailed description spanning multiple lines

Apps

You can specify different apps/services provided by the snap. Each app will get its own name and command:

apps:
  my-app:
    command: bin/start-my-app
  another-app:
    command: bin/another-app

Parts

Parts are reusable component pieces to build up a snap. You can define different parts to pull together your app:

parts:
  my-part:
    plugin: dump
    source: ./src
  libs:
    plugin: make
    make-options: [all]

Common parts include copy, dump, make, and more.

Build packages

List system packages needed to build the snap:

build-packages:
  - libssl-dev
  - libpng12-dev

These get automatically installed during snap build.

Stage packages

System packages to bundle into the snap:

stage-packages:
  - libc6
  - libssl1.0.0

Gets included in the snap so no need to install separately.

Organizing

There are additional sections like grade, plugs, hooks and more for additional configuration. Refer to the snapcraft.io documentation for complete details.

With this overview of the snapcraft.yaml, you can fully customize the contents and build process of your snap.

Building a nodejs snap

Let’s walk through an example of creating a snap for a simple nodejs application. Here are the steps:

  1. Create a directory for your snap, and initialize snapcraft.yaml:
      mkdir my-node-snap
      cd my-node-snap
      snapcraft init
      
  2. Edit snapcraft.yaml to add nodejs part:
      parts:
        my-node-app:
          plugin: nodejs
          source: .
      
  3. Add the node app files under a directory like src/
  4. Add build and stage packages:
      build-packages:
        - nodejs
        - npm
      stage-packages:
        - libc6
      
  5. Run
    snapcraft

    to build

  6. Install and test the snap file

This demonstrates the basics of building a nodejs snap. You can extend it further with plugins, hooks and more configuration to package complex snaps.

Publishing snaps to the Snap Store

Once you have a working snap, you can publish it to the Snap Store for others to install. Here is an overview of the publishing process:

Register and get developer credentials

First, you need to visit dashboard.snapcraft.io and register as a publisher. You will get developer credentials like a unique Snapcraft ID.

Name your snap

Come up with a unique name for your snap. This will be the identifier users type to install it. Verify the name is available.

Prepare your snap

Polish your snapcraft.yaml with proper metadata like description, icons, and screenshots that will display on the store. Make sure the snap works properly.

Release to beta channel

Initially release your snap to the beta channel to test it out:

snapcraft upload --release=beta my-snap_1.0_amd64.snap

Install it with

sudo snap install my-snap --beta

Release to stable channel

Once testing is complete, promote it to the stable channel:

snapcraft release my-snap 1 stable

It will now be available to all users on stable channel.

Publish subsequent updates

When you make updates, bump the version number in snapcraft.yaml and release to beta first for testing before pushing to stable. Rinse and repeat!

Following these steps, you can publish your snap and engage with a growing audience of Linux users. The snapcraft publishing guidehas more details on the process.

Conclusion

In summary, here is the overall workflow to create and publish a snap:

  1. Ensure you have snapcraft and prerequisites installed
  2. Initialize a snapcraft.yaml file for your app
  3. Customize metadata, apps, parts and other sections
  4. Run snapcraft to build the snap
  5. Test installing and running the snap locally
  6. Register on Snapcraft and prepare snap details
  7. Release your snap to the Snap Store!
  8. Maintain the snap with updates

Snaps provide a great platform to distribute your software on Linux with benefits like automatic updates, security confinement and cross-distribution support. We hope this guide gave you a good overview of how to get started creating snaps for your applications. The snapcraft documentation has a wealth of resources to take your snaps to the next level. Happy Snapping!

Frequently Asked Questions

What are the hardware requirements to create snaps?

There are no specific hardware requirements. Snapcraft is designed to run on commodity hardware. At minimum, you just need a system with Ubuntu 16.04+ and enough HDD space to build your snaps.

How hard is it to make a snap vs a traditional Linux package?

Creating a basic snap with snapcraft is actually quite easy compared to building .deb or .rpm packages traditionally. The yaml syntax is intuitive and getting started just takes a few commands. More complex snaps may take more work to configure the details properly.

Can I monetize my snaps?

Yes, you can charge for private/proprietary snaps on the Snap Store and set your own pricing. There is currently no way to charge for public snaps that are free to install, but that may change in the future.

What programming languages and frameworks are supported?

Snaps support a wide range of languages like NodeJS, Python, Java, Go, Ruby, PHP and more. Frameworks like Django, Android, Electron, ROS, Docker and many others have snapcraft plugins.

Can I control updates and versions of snaps I install?

The default is to receive automatic stable updates from the publisher. However, you can switch to a specific channel like beta or edge to get newer pre-releases. You can also lock to a specific version or use –dangerous to install unverified snaps.

How do snap permissions and security work?

Snaps run confined within an isolated sandbox and can only access resources defined in their manifest (snapcraft.yaml). All access attempts are mediated via AppArmor, Seccomp or other security mechanisms. This prevents snaps from harming the rest of the system.

Advanced snap usage

Once you are comfortable building basic snaps, you can explore some more advanced features to enhance your snaps:

Interfaces and slots

Interfaces allow snaps to securely access resources outside their sandbox like network, GPU, pulseaudio and more. Slots can provide interfaces to other snaps.

Hooks

Hooks allow running scripts during snap lifecycle events like install, upgrade, remove etc. Useful for configuring a snap during install.

Services and daemons

Snaps can run services and daemons in the background. The snap init system manages service start/stop and ensures they run across upgrades.

Snapcraft plugins and parts

An extensive library of modular plugins and parts are available to incorporate common build steps into your snap.

Build on LXD

Building snaps on pristine LXD containers provides isolation and reproducibility compared to local host.

Channels and releases

Manage beta, candidate, stable and other release channels for testing and publishing life cycle.

We encourage you to refer the official snapcraft documentation to make use of these advanced functionalities.

Overview of snap vs flatpak vs appimage

There are a few popular application packaging and distribution technologies for Linux – primarily snap, flatpak and AppImage. Here is a comparison:

Snap Flatpak AppImage
Package format SquashFS + metadata OSTree ELF + AppImage spec
Dependency bundling Apps bundled with own libs Runtimes provide dependencies Apps bundled with own libs
Sandboxing and security Strong confinement via AppArmor/Seccomp Permissions-based sandbox Limited sandboxing
Language and frameworks Extensive support via plugins Good support Binaries for many languages
Installation requirement snapd daemon Flatpak runtime + OSTree None, self-contained
Software Framework snapcraft build tool Flatpak build system AppImage packaging format

In summary, each has its own strengths catering to different use cases:

  • Snaps have robust security and single-format across distros.
  • Flatpaks integrate with the system via runtimes and focus on desktop apps.
  • AppImages are lightweight but have lesser confinement.

For some applications, making them available via multiple packaging formats can help reach more users across different distributions.

Learn more and Get involved

We have just scratched the surface of the exciting world of snaps. Here are some additional resources to take your snap development further:

We hope you are now empowered to package and distribute your applications using snaps. Join the growing snapcraft community to keep learning and share your snaps with the world!