Getting started with snapcraft

There have been a few days I’m struggling with creating a snap.

My setup is: Ubuntu server 20.04 LTS on an RPI, and UC20 on an other RPI.

After a fresh install, I wanted to do a simple thing on my Ubuntu server :

mkdir test cd test snapcraft init snapcraft

And then it said that I need multipass, and asked me if I wanted to set up. I answered Yes. Then re-run snapcraft, and it says "Unable to find an image matching “core20”. …

Diging in the docs of multipass, there is really no image called core20, and I have no idea how to install one. But I don’t really understand why I need the code20 image on my Ubuntu server 20.04.

the snap documentation doesn’t mention this whole multipass business.

Can you help me how to proceed?

Multipass provides a wrapper around Ubuntu virtual machines. In Snapcraft, it’s used to provide a clean build environment, and prevents the host environment from being destroyed with package changes. I don’t think there are armhf/arm64 Core20 images on Multipass, and if there were, I don’t think the Raspberry Pi has CPU extensions to be able to accelerate virtualisation (though I could be wrong), so they’d likely not be usable here anyway.

Multipass isn’t the only build environment though. Snapcraft can also use LXD, which provides similar characteristics of ensuring a clean build environment and protecting the host. You can set it up with

sudo snap install lxd
sudo lxd init --auto
sudo usermod -a -G lxd ${USER}

And rebooting to ensure the system is a good state. After that first time setup, you could run snapcraft as snapcraft --use-lxd, and the various other commands would need the --use-lxd parameter too, e.g, snapcraft clean --use-lxd

Alternatively you can drop the --use-lxd parameter by setting SNAPCRAFT_BUILD_ENVIRONMENT=lxd, so you’d be able to just run snapcraft

You might also be interested in making use of the Remote Build service, if you’re allowed to submit your source code to the public builders, they’ll be able to run the build for you, including armhf/arm64.

Hi James-Caroll,

Thank you very much for your answer. I am now getting confused. I thought snapcraft is just a packaging utility, like apt. So if I create the yaml, and I say snapcraft, it’ll get my stuff and pack it to a snap. I wasn’t aware of that I need a virtual machine at all.

Is it reasonable what I am trying to achieve? Using an RPI to create a snap? Unfortunately, I can’t upload the source code, and my application has a non-standard build mechanism. However, I can install an ubuntu on a VM on my Mac. Would that help?

What I really want to achieve, is to pack up my manually built artifacts in a snap.

So now I get this error:

Launching a container.
An error occurred with the instance when trying to launch with 'LXD': Failed creating instance record: Failed initialising instance: Invalid devices: Failed detecting root disk device: No root device could be found.
Ensure that 'LXD' is setup correctly and try again.

Snapcraft is a packaging utility, the virtual machines provided by Multipass is just an implementation detail, it’s not expected that you set up any virtual machines manually yourself, nor required that the target platform supports them, merely just that because they are used by default for building and the Pi can’t support them, Snapcraft needs to be told to use LXD instead.

It’s absolutely feasible that you should be able to use the Pi to create the snap. And while the Ubuntu VM in a Mac scenario could potentially help, it’s also probably opening another can of worms that’s probably best avoided for now.

The error looks like you may have missed sudo lxd init --auto, try re-running this command again and then try re-running snapcraft --use-lxd again.

Hi James-Carrol,

Thank you for your reply. Unfortunately, this doesn’t work.

ubuntu@ubuntu:~/test$ lxd init --auto
ubuntu@ubuntu:~/test$ snapcraft --use-lxd
Launching a container.
Build environment is in unknown state, cleaning first.
An error occurred with the instance when trying to launch with 'LXD': Failed creating instance record: Failed initialising instance: Invalid devices: Failed detecting root disk device: No root device could be found.
Ensure that 'LXD' is setup correctly and try again.

So I don’t know what I’m doing wrong.

BR, Levente

Unfortunately, I’m not an expert in how LXD fundamentally works. I expect there might be people here though who might be able to help you, but as far as I’m aware, you’re not doing anything wrong and ideally this experience would have been frictionless. Hopefully someone else can chime in and give more expert advice here. All I could suggest at this point is try sudo lxd init without the --auto parameter, this might produce some feedback that perhaps is being skipped in automatic mode, ideally you’d just select the default response for everything (which is what --auto does I believe).

There is an option that’s considered extreme, namely snapcraft --destructive-mode, this will actually just build directly in the host environment outside of any virtual machine or container environment. In this scenario, the operating system would have to match the equivilent base distribution of the snap it’s building. E.G, if you’re building a core18 snap, you’d need Ubuntu 18.04, if you’re building a core20 snap, you’d need Ubuntu 20.04. Please keep in mind that’s exclusively about the snap you’re building, not where the snap is being deployed, so it doesn’t matter if the snap ends up running on Ubuntu Core 18, Ubuntu Core 20, or even Fedora, for example.

The reason it’s considered extreme is it’s intended for short lived environments that are usually reset upon use. It’ll make package changes to your system and these might cause conflicts for example. It’s really recommended you prefer LXD/Multipass, but if you’d like to push on regardless for now, it is technically an option.

Sorry I can’t assist further here.

you seem to have skipped the:

sudo usermod -a -G lxd ${USER}

step … (note that this also requires a logout/login to take affect … or a reboot)