Short version: how to test-build Debian/Ubuntu packages in a chroot

Install package «ubuntu-dev-tools» (version 0.63 or higher) and then do the following for every distribution for which you want to build packages (replacing "jaunty" everywhere with the codename of the distribution you want; both Ubuntu and Debian releases will work): «ln -s /usr/bin/pbuilder-dist /usr/local/bin/pbuilder-jaunty; pbuilder-jaunty create». Then, you can compile any package by running «pbuilder-jaunty build <package>_<version>.dsc».

If you prefer cowbuilder instead of pbuilder (the first one stores the chroots uncompressed, so they take up more disk space but don't need to be uncompressed every time) just replace «pbuilder» with «cowbuilder» everywhere above.

Long version

So you have created a new package for Ubuntu or you've done some changes to an existing one and now you want to create the .deb package. For many new contributors the obvious way to achieve this is  to just run dpkg-buildpackage, but this is not enough to ensure that the package will build once it is uploaded to Ubuntu.

Using dpkg-buildpackage you won't know if there's some build dependency missing in debian/control if you already have it installed,  stuff on your system may influence the build process, and so on. Even further, the package you're creating is most likely intended for the current development version of Ubuntu (at the time of writing this, Jaunty) but you are still running the stable release (Intrepid), or the release is the same but having to pollute with all dependencies needed to build a package is not ideal. I guess you get the point…

So, what can we do about this? Well, tools like pbuilder come to the rescue, allowing you to easily set up a minimal chroot where you can test-build packages for any Ubuntu/Debian release. Its usage is even more simplified by a wrapper script, pbuilder-dist, which makes having several pbuilder instances at once (like, one for Intrepid, one for Jaunty, etc. ) a walk in the park.

To use it, first of all get the latest ubuntu-dev-tools version from intrepid-backports and ensure that you have pbuilder installed. Then, create symlinks for every chroot you want, calling them pbuilder-intrepid, pbuilder-jaunty… (If you are running the 64 bit version of Ubuntu, you can even create an i386 chroot, calling it pbuilder-intrepid-i386). To create those symlink, use a command like the following one: «sudo ln -s /usr/bin/pbuilder-dist /usr/local/bin/pbuilder-intrepid» (changing the last bit to whatever chroot you want, as described before).

Note: You can also use cowbuilder instead of pbuilder, by simply installing the package cowdancer and giving the symlinks names starting with "cowbuilder-" instead of "pbuilder-". The pbuilder-dist wrapper can handle them both, and the difference between them is that pbuilder compresses the chroot as a tarball (thus taking up less disk space, but having to uncompress it every time you do something to it and so being slower), and cowbuilder doesn't (so it's faster but needs a bit more disk space); personally, I prefer the later one. Now that you have your symlinks setup, you can create the symlinks by running «pbuilder-intrepid create» (changing «-intrepid» to whatever you've named your symlinks, of course). This will take some time, as it will need to download and install a complete Ubuntu base system. Afterwards, you'll need to keep your chroots up to date, by running «pbuilder-intrepid update» from time to time. All files will go to ~/pbuilder/ (unless you change the directory by setting the PBUILDFOLDER environment variable -and yes, it is called the same for cowbuilder-dist-).

To build your package using the chroot, just do «pbuilder-intrepid build <packagename>_<version>.dsc» and watch the magic happen. Once it finishes you'll find the resulting files in ~/pbuilder/. (In case you get the error that some dependencies cannot be downloaded, try updating the chroot as explained above).

There's still a lot more stuff which I could tell you about pbuilder/cowbuilder, but I'll end this introductory post here… If you have any question don't hesitate to ask on #ubuntu-motu (after being a good kid and reading all relevant manpages ;)).