Cross Compile for Debian Stretch Using sbuild on Ubuntu 18.04

This article describes using sbuild to cross compile Debian packages on a Ubuntu machine. I assume the reverse (though I didn't test) is equally true. sbuild requires Debian Stretch or later, and should work on Debian derived distributions.


Many systems are based on a Debian distribution that native compilation isn't desirable due to limited resources. Ubiquiti EdgeRouter X is one of such systems. With EdgeRouter FW 2.0 based on Debian Stretch, cross compilation is however  much easier than before with the availability of sbuild.

Also to my surprise, Debian derived distributions are highly compatible. I thought I would need a machine running Debian Stretch to execute the cross compile using sbuild for a target device running Debian Stretch. That seems not a mandatory requirement. The distribution on the target device is agnostic to the distribution on the build machine as long as the latter supports sbuild.

Below are my notes from a successful build the haveged package for EdgeRouter X (running FW 2.0) by using sbuild on a Ubuntu 18.04 machine.

Install sbuild on Ubuntu

sudo apt install sbuild
sudo sbuild-adduser <your username>
sbuild-update –keygen

The last command line is required for updating packages in schroot images.

Create a schroot image

sudo sbuild-createchroot --make-sbuild-tarball=/home/<your username>/edgeX/stretch-sbuild.tgz stretch /tmp/stretch

--make-sbuild-tarball option specifies creating the schroot image and saving it into a tarball. This way saves disk space and let me elaborate a bit. When the schroot image is used later for cross compiling software, sbuild will expand it into temporary storage that is known as a schroot session.  This temporary storage is cleaned up when sbuild exits.

stretch is a good known distribution to sbuild that is used as the baseline for the schroot image. If your target device is based on a different distribution, change it accordingly. The directory next to this argument is a temporary working place for creating the schroot image. This work place is removed after sbuild-createchroot finishes.

The last URL is the Debian repository for retrieving packages and installing into the schroot image. It also stays as one of source lists inside the image.

Check schroot images

schroot -l

Sample output:


Customize schroot image

Use sbuild-shell to enter the schroot environment of a given schroot image. Note that pick the image beginning with “source:” so that when you exit schroot, changes you made will be saved back to the image. If the image is a tarball, then the tarball is re-archived. Nice! Isn't it?

sudo sbuild-shell source:stretch-amd64-sbuild

# Operations inside schroot
dpkg --add-architecture mipsel
apt-get update
apt-get install libc6:mipsel

The last few lines are standard Debian package operations. I added mipsel architecture and installed mipsel version of libc6 to the schroot image. It's the minimal dependency that I had to manually install for cross compiling haveged. Other dependencies could be downloaded and updated on demand inside the schroot session.

Using sbuild to cross compile

To build haveged from apt repository,

sbuild -d stretch --host=mipsel haveged

To build from manually downloaded Debian source packages,

cd haveged-1.9.1
sbuild -d stretch --host=mipsel --purge=successful haveged-xXXXxxx.dsc

--purge=successful is optional but useful for debugging a failed compilation as any schroot session is retained when sbuild exits.

Quit and purge all sessions

schroot sessions are in /var/run/schroot/mount. A session is cleaned automatically after sbuild exits. On occasions you need to manually purge all dangling sessions,

sudo schroot --end-session --all-sessions

Remove a schroot image

Meta data of schroot images are stored in /etc/schroot/chroot.d.

sudo rm /etc/schroot/chroot.d/stretch-amd64-sbuild-xxXXXxx

It's obvious to infer the meta file for a given schroot image. For my sample "stretch" image, the meta file is stretch-amd64-sbuild-xxXXXxx. It appears to be almost the image name from schroot -l with random suffix.


sbuild is amazingly easy to use once you figure out the key concepts in its design. To recap, sbuild instantiates a temporary build environment known as schroot session from a schroot image. sbuild can automagically resolve most of the dependencies required for a successful build to any given package. A schroot session is removed when sbuild exits. If you want any change to stay and subsequently used in sessions, you need to customize the schroot image. This way keeps a schroot image minimal and pristine!


comments powered by Disqus