Packaging Linux Apps with Snapcraft: A Hands-On Walkthrough from Scratch

Maulik ManvarMaulik Manvar
5 min read

Author: Maulik Manvar

Tags: Linux, DevOps, Snapcraft, Packaging, Automation

🧠 Why I Looked Into Snapcraft

Distributing Linux applications has always been a headache. .deb, .rpm, PPA hell, dependency issues — been there, debugged that.

I wanted a cleaner, more portable way to ship my command-line tool across distros without making my users jump through hoops. That’s when I found Snapcraft — Canonical’s open-source tool for building snaps, which are universal Linux packages.

⚙️ What Is Snapcraft?

Snapcraft is a build tool used to create snaps, which are self-contained Linux packages. Think of snaps like Docker containers, but for desktop and CLI apps. They bundle all dependencies, run in a sandbox, and work on any major Linux distro that supports snaps — Ubuntu, Fedora, Arch, even Raspberry Pi OS.

🔧Step 1: Set Up Your Environment

  1. First, let’s prepare the canvas by installing Snapcraft and its dependencies.

    1. Install Snapcraft: Open a terminal and run:

       sudo snap install snapcraft --classic
      

      This installs Snapcraft as a ssnap, giving you the latest version.

  2. Install LXD: Snapcraft uses LXD to create isolated build environments. Install and initialize it:

     sudo snap install lxd
     sudo lxd init --auto
    

    Log out and back in to ensure LXD group permissions are active. Verify with:

     groups | grep lxd
    

    Look for lxd in the output.

📁 Step 2: Create Your Project

Now, let’s set up a workspace for our snap.

  1. Make a Project Directory:

     mkdir pyfiglet-snap
     cd pyfiglet-snap
    

    This is where our snap will come to life.

  2. Initialize the Snapcraft Project: Run:

     snapcraft init
    

    This creates a snap/snapcraft.yaml file, the blueprint for our snap.

✍️ Step 3: Configure snapcraft.yaml

The snapcraft.yaml file is where the magic happens—it defines how your snap is built. Open it with your text editor:

nano snap/snapcraft.yaml

Replace its contents with the following:

name: pyfiglet-snap
version: '0.1'
summary: ASCII art generator snap
description: |
  This snap packages pyfiglet, a Python tool that creates stunning ASCII art from text.
grade: stable
confinement: strict
base: core22

parts:
  pyfiglet:
    plugin: python
    source: https://github.com/pwaller/pyfiglet.git
    python-packages:
      - pyfiglet

apps:
  pyfiglet:
    command: bin/pyfiglet
    plugs: [network]

Let’s break down this masterpiece:

  • name: The snap’s unique identifier.

  • version: A human-readable version string (e.g., ‘0.1’).

  • summary and description: Describe your snap for the Snap Store.

  • grade: Set to stable for production-ready snaps.

  • confinement: strict ensures security by isolating the snap.

  • base: core22 uses Ubuntu 22.04’s core libraries.

  • parts: Defines the pyfiglet component, pulling source code from GitHub and using the python plugin.

  • apps: Specifies the pyfiglet command and grants network access (optional for this app).

Save and exit (Ctrl+O, Enter, Ctrl+X in nano).

🛠Step 4: Build the Snap

Time to bring your creation to life! Run:

snapcraft

Snapcraft will download the pyfiglet source, bundle dependencies, and create a .snap file. This might take a few minutes as it sets up a virtual environment. If successful, you’ll see a message like:

Snapped pyfiglet-snap_0.1_amd64.snap

🚀Step 5: Install and Test

Install your snap locally to test it:

sudo snap install pyfiglet-snap_0.1_amd64.snap --dangerous

The --dangerous flag is needed because the snap isn’t signed by the Snap Store yet.

Run the app:

pyfiglet-snap.pyfiglet Hello, Snapcraft!

You should see “Hello, Snapcraft!” rendered as ASCII art, like this:

 _          _ _       
| |__  ___| | | ___  
| '_ \/ __| | |/ _ \ 
| | | \__ \ | | (_) |
|_| |_|___/_| |\___/
            |_|

🌍Step 6: Share Your Creation

Want to share your snap with the world? Register it on the Snap Store:

snapcraft register pyfiglet-snap

Follow the prompts to create an account. Then, upload your snap:

snapcraft upload pyfiglet-snap_0.1_amd64.snap

Once approved, your snap will be available for millions to discover

🧩 Pros & Gotchas

✅ Pros:
- Dead-simple packaging
- Cross-distro support
- Auto-updates
- Clean isolation (AppArmor)

⚠️ Gotchas:
- AppArmor confinement can be restrictive
- Snap size can be larger than native packages
- GUI apps need extra permissions

Troubleshooting Tips

  • Missing Dependencies: If the build fails, check for missing Python packages and add them under python-packages in snapcraft.yaml.

  • Permission Issues: Ensure you’re in the lxd group and have internet access.

  • Inspect the Build: Run snapcraft snap --shell-after to enter the build environment and debug.

Creative Ideas to Extend Your Snap

Now that you’ve crafted a snap, let your creativity soar! Here are some ideas:

  • Customize the Output: Modify pyfiglet to use different fonts or colors by tweaking its source code.

  • Add a GUI: Package a graphical app like a simple game or editor using Snapcraft’s desktop support.

  • Automate Updates: Use build.snapcraft.io to auto-build new releases when your source code updates.

  • Explore Other Languages: Try packaging a Rust, Go, or Electron app to expand your skills.

Conclusion

Snapcraft is more than a tool—it’s a canvas for your coding creativity. By bundling dependencies and ensuring cross-platform compatibility, it frees you to focus on building innovative applications. This exercise introduced you to the basics of Snapcraft, from setup to sharing. Now, it’s your turn to experiment, create, and share your own snaps. Visit snapcraft.io for more tutorials and inspiration.

Challenge: Create a snap for another simple Python app (like cowsay) and share it in the Snapcraft forum. How will you make it uniquely yours?

Happy crafting!

🧠 Final Thoughts

I walked in with doubts, walked out impressed. Snapcraft doesn’t try to reinvent the wheel — it just makes the packaging road smoother. If you maintain a Linux tool (especially in Python, Go, or Rust), Snap is 100% worth a try.

0
Subscribe to my newsletter

Read articles from Maulik Manvar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Maulik Manvar
Maulik Manvar