Building snaps in LXD (not Ubuntu 16.04)

These instructions are superseded by these docs, which contain a more streamlined way of using LXD with snapcraft. Just use lxc launch ubuntu:17.10 snapcraft -c security.privileged=true or whatever version you want instead of 16.04.

What if you want to use stage-packages from Ubuntu 17.04 rather than 16.10 or 16.04 (i.e., you want more up-to-date dependencies)? Well the easy answer is to build your Snappy package on 17.04, good job that you can do that really easily using the LXD snap!

[Note, you can now snap using LXD, but only with Ubuntu 16.04 as the container OS, as far as I’m aware, using SNAPCRAFT_CONTAINER_BUILDS=1 snapcraft for a persistent LXD container or snapcraft cleanbuild for a non-persistent LXD container]

First, you’ll want to install LXD

sudo snap install lxd
sudo lxd init

Pick the default options if asked

Use the following command to create a 17.04 container (when there’s a 17.10 LXD thing, you’ll want to use 17.10 instead of 17.04)

sudo lxc launch ubuntu-daily:17.04 my-ubuntu-dev

Open a shell in your new container

sudo lxc shell my-ubuntu-dev

(To start your container in future, you’ll want to use lxc start my-ubuntu-dev)

Make sure it’s updated, you’ll probably want to do this most times you go into your container so you have the latest dependencies to pull into your snap

apt update ; apt upgrade

Make folders to do your Snapcraft development in with mkdir and cd. Then go to the folder you want to build your Snap in, use vi snapcraft.yaml and i to start typing. Write your snapcraft.yaml file. Hit Esc and then type :x and hit Enterwhen you’re done.

Now install Snapcraft in your container

apt install snapcraft

Build your Snap by running snapcraft in the directory with your snapcraft.yaml in it.

Check what your snap is called with ls.

If your snapped application only uses the Terminal, you can install it with snap install mysnap.snap --dangerous and then run it with snap run mysnap (or just mysnap). Otherwise:

Exit your container with, um, exit.

Navigate to the directory you want to put your built snap file, use lxc file pull my-ubuntu-dev/root/DirectoryMySnapIsIn/mysnap.snap . I got a permission denied error trying to do this before, you might need to reboot or something (remember to relaunch your container with the command given earlier once you log back in).

Then install your Snap with sudo snap install mysnap.snap --dangerous, and run it with snap run mysnap or just mysnap assuming you don’t have a Deb package installed which uses the same command.

If there’s bugs, go back into your container (with the command to open Bash in your container, given before) and edit your snapcraft.yaml, rebuild, pull it out, install it, test it etc

This message is a wiki, so please feel free to edit and improve it to match reality.


Thanks, this is really useful.

@Ads20000 This is super helpful, thank you!

One suggestion: we have an alias nowdays that allows calling lxd.lxc as simply lxc.

And one question: can I turn your post into a wiki, so that people can contribute and keep it up to date over time?

Please do, and edit the commands for your suggestion while you’re about it! :slight_smile:

@Ads20000 Done! Main change was the alias, and changed the capitalization of the snap since snapd enforces lowercasing for their names. You can see the exact changes clicking on the pencil on the top-right corner of the message.

Feel free to edit/tune further, of course.

I think you meant lxd init.

Niemeyer agrees above that the correct command is lxc, which is an alias for lxd.lxc.

Edit: nope, @om26er is right.

ouch … given that all docs you will find online will tell you to use “lxd init” that sounds quite bad actually …

Right, that’s not related to what I said.

To initialize lxd you have to call lxd init, after that we use lxc for provisioning or whatever. A little confusing yes, but that’s how lxd works :slight_smile:

Cheers @om26er @ogra, have edited, I don’t even know that much about LXD but found this useful at the time so documented it. Niemeyer did make the post a wiki so anyone can edit and improve it, but thanks for the suggestion :slight_smile:

1 Like

Would these instruction be no longer relevant with the cleanbuild option? I only ask because of the reference to this being moved over to a wiki. Just a thought.

There’s a PR to allow building in a container with a specific image.