Rebuilding an Ubuntu package

In this first article about debian/ubuntu packaging, we will learn to rebuild any existing ubuntu package locally, to get more familiar with the various tools used to build debian pacakges.

Getting the package source

Provided your /etc/apt/sources.list file contains deb-src entries, you can do the following:

mkdir tmp
cd tmp
apt-get source $package_name

Getting examples goes a long way in understanding something, and these simple steps give you access to a lot of them.

If you don't have deb-src entries in your sources.list file (if you are working inside a default LXC container for example), you can easily add them with the following command:

sudo sed -i 's/^deb \(.*\)/deb \1\ndeb-src \1/g' /etc/apt/sources.list

Getting the required tools

The second obvious step is to try and build the package you just obtained. We need a few tools in order to do that:

sudo apt-get install build-essential pbuilder
These packages install the following:
  • The build-essential package contains most of the useful tools to build binaries, like a compiler, linker, and plenty of other goodies.
  • The pbuilder package has all you need for the packaging itself.

Once you have the necessary tools, you will need to get the package's build dependencies. Build dependencies are packages needed to build a package, but not to run it. A common example of this would be the python-setuptools package to build python pacakges, while paramount at package building time, it is not necessary to actually use the python package.

It is quite easy to resolve and install build-dependencies for an arbitrary package:

sudo apt-get build-dep $package_name

This will fetch and install all the package's build dependencies.

Reuilding the package

Packages in Debian based systems are either source packages or binary packages. It is advisable to start by building the source pacakge, since that will allow you to distribute your changes to a number of architectures, and to send your newly build package to a PPA, for example.

Building a source package is easy, just cd into the pacakge's source directory (the directory that was created for you with the apt-get source command), and run the following:

debuild -S

This will create a *.changes file in the ../ directory. You can upload this file to a PPA pretty easily to let launchpad build the package for several different architectures easily.

To build the corresponding binary package (at least the one for your current architecture), run the following command instead:

debuild

This will create the debian package(s) in th ../ directory, once again. You can now install these newly built packages just like you would any other *.deb file.

Next time

Now that we know how to rebuild packages, we'll learn how to modify them before rebuilding them (which is more interesting), as well as how to make your changes available to the world using a PPA.