Whether I’m adding dependencies, updating package names, or creating new package spins, I always have issues testing my debian packages. Something will work locally, only to fail on jenkins under a clean environment. Fortunately, there’s a nifty tool called pbuilder
that exists to help out in these situations. pbuilder
uses a chroot to set up a clean environment to build packages, and can even be used to build packages for systems with architectures different from your own.
Note: All code samples were originally written from a machine running Ubuntu 16.10 64-bit. Your mileage may vary.
Clean builds for current distro
Given a typical debian-packaged project with a debian
directory (control
, rules
, .install
), you can use debuild
to build a package from your local environment:
1 2 3 4 5 |
|
This works pretty well for sanity checks, but sometimes knowing your sane just isn’t quite enough. My development environment is filled with libraries and files installed in all kinds of weird ways and in all kinds of strange places, so there’s a good chance packages built successfully on my machine may not work on everyone’s machine. To solve this, I can install pbuilder
and set up my first chroot:
1 2 3 4 |
|
Since I use debuild
pretty frequently, I also rely on pdebuild
which performs debuild
inside of the clean chroot environment, temporarily installing the needed dependencies listed in the control
file.
1 2 3 4 |
|
Alternatively, I could create the .dsc
file and then use pbuilder
to create the package from there:
1 2 3 4 5 6 7 8 |
|
Clean cross builds
Let’s say that you need to build for an older distribution of Ubuntu on a weird architecture. For this example, let’s say vivid
with armhf
. We can use pbuilder-dist
to verify and build our packages for other distros and architectures:
1 2 3 4 5 6 7 |
|
Custom, persistent chroot changes
In some cases, you may need to enable other archives or install custom software in your chroot. In the case of our vivid-armhf chroot, let’s add the stable-overlay ppa which updates the outdated vivid with some more modern versions of packages.
1 2 3 4 5 6 7 8 9 |
|
pbuilder
and chroots are powerful tools in the world of packaging and beyond. There are scripting utilities, as well as pre- and post-build hooks which can customize your builds. There are ways to speed up clean builds using local caches or other “cheats”. You could use the throwaway terminal abilities to create and destroy tiny worlds as you please. All of this is very similar to the utility which comes from using docker and lxc, though the underlying “container” is quite a bit different. Using pbuilder
seems to have a much lower threshold for setup, so I prefer it over docker for clean build environments, but I believe docker/lxc to be the better tool for managing the creation of consistent virtual environments.
Further reading:
Pbuilder HowTo on the Ubuntu wiki Pbuilder tricks from the debian wiki