snapcraft building arm snaps on raspberry PI

Snapcraft building arm snaps on raspberry pi! This post will focus on wrapping a python script using snap. Its recommended that you do this on ubuntu core 18, available from the ubuntu cd image repo here

Install snapd, snapcraft and lxd

sudo apt update && sudo apt upgrade
sudo apt install snapd
sudo snap install core
sudo snap install snapcraft --classic
sudo snap install lxd
sudo usermod -a -G lxd ${USER}
lxd init (answer the questions with their defaults)
reboot (this is important)

multipass doesnt work, so you need to install lxd

Getting your Python Project Ready

to use python inside a snap, you need a working setuptools env.
https://setuptools.readthedocs.io/en/latest/userguide/quickstart.html#entry-points-and-automatic-script-creationhttps://pythonhosted.org/an_example_pypi_project/setuptools.html

To install the latest version of setuptools, use:

sudo apt install python3-pip
pip3 install --upgrade setuptools

https://pythonhosted.org/an_example_pypi_project/setuptools.html
If you don’t already have a setup.py file, create one:

touch setup.py

Inside the file, add some template code:

import os
from setuptools import setup
def read(fname):
   return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
   name = "an_example_project",
   version = "0.0.1",
   author = "Your Name",
   author_email = "whoever@someplace",
   description = ("Description goes here"),
   license = "BSD",
   keywords = "example documentation tutorial",
   url = "whatever.com",
   packages=['an_example_pypi_project'],
   long_description=read('README'),
   classifiers=[
       "Development Status :: 3 - Alpha",
       "Topic :: Utilities",
       "License :: OSI Approved :: BSD License",
   ],
)

The directory structure for your project may need changed: it should look like this:

some_root_dir/
|-- README
|-- setup.py
|-- an_example_project
|   |-- __init__.py
|   |-- useful_1.py
|   |-- useful_2.py

building snaps on raspberry Pi – getting SNAP build files ready

Next, make a few files:

touch pyproject.toml:

Inside that file, add the following:

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
setup.cfg:
[metadata]
name = "an_example_project"
version = 0.0.1
[options]
packages = "an_example_project"
install_requires =
 requests,
 netifaces

You can then test if it all works with the following command:

python3 setup.py bdist_wheel

Once that works without error, then try to build your snap:

snapcraft --use-lxd

It takes a while to build on raspberry pi, but once its built, you can install with:

snap install ./an_example_project_armhf.snap --dangerous --devmode

To run as regular user privs:

snap run an_example_project

Or with a shell to explore the snap

snap run --shell an_example_project

To run as root privs:

sudo snap run an_example_project
sudo snap run --shell an_example_project

Ubuntu snap format is a software packaging and deployment system developed by Canonical for operating systems that use the Linux kernel and the systemd init system³. The packages, called snaps, and the tool for using them, snapd, work across a range of Linux distributions and allow upstream software developers to distribute their applications directly to users³. Snaps are much easier to secure and much easier to produce than traditional deb packages¹. They offer operational benefits for organizations managing many Ubuntu devices, which will bring more robust updates and more secure applications across all form factors from phone to cloud¹. Snaps can be installed alongside traditional deb packages². These two packaging formats live quite comfortably next to one another and enable Ubuntu to maintain its existing processes for development and updates². The snap directory can contain the command-chain declaration, a list of commands that are run before the actual application(s) bundled inside the snap. This can help set up the environment that snaps need, without having to resort to convoluted use of wrapper scripts⁴.

Sources:

Wikipedia. https://en.wikipedia.org/wiki/Snap_(software) Accessed 14/04/2023.
(2) Ubuntu 16.04 LTS introduces “snaps” for new robust, secure app format. https://www.helpnetsecurity.com/2016/04/21/ubuntu-16-04-lts/ Accessed 14/04/2023.
(3) Canonical unveils 6th LTS release of Ubuntu with 16.04. https://ubuntu.com/blog/canonical-unveils-6th-lts-release-of-ubuntu-with-16-04 Accessed 14/04/2023.
(4) What’s in a snap? | Ubuntu. https://ubuntu.com/blog/whats-in-a-snap Accessed 14/04/2023.
(5) The snap format | Snapcraft documentation. https://snapcraft.io/docs/the-snap-format Accessed 14/04/2023.

Leave a Reply