Building ubuntu packages from source

Sometimes it is necessary to rebuild a Debian / Ubuntu package with an additional compile option. eg we require various packages to be built with the -fPIC flag but sometimes the default package is not compiled in this way.

What follows is a guide on how to recompile .deb packages from source.

There are a few tools you need to build a package from source. Use apt-get to install them if they are not already available on your system:

sudo apt-get install build-essential fakeroot dpkg-dev

The build-essential package will make sure tools like gcc and make are installed. The fakeroot package allows users to create archives (in this case .deb) with files in them with root permissions and ownership. The dpkg tools are required for extracting the source and building the package.

Make a directory for your build

To keep things tidy make a directory where you will be building your package:

mkdir foo
cd foo

Fetch the source

On some systems this can be done using apt-get:

sudo apt-get source foo
sudo apt-get build-dep foo

Alternatively you can determine what version of ubuntu you are running and then fetch the appropriate package source manually from the ubuntu packages website. To find your version use:

lsb_release -a

To download the source got to http://packages.ubuntu.com and scroll down to the search form. Fill in the ubuntu version (eg dapper) and then the package name to search for (eg libgtkglext1). If you know the exact name of your package you may be able to go directly to the package page (eg http://packages.ubuntu.com/dapper/libgtkglext1) Under the links bar on the right hand side is a link to download the package source. You need to download three files. They are a .dsc file, an original tar ball of the source (orig.tar.gz) and a diff file (diff.gz). Download these files and place them in your build directory.

Extract the source and patch it using the diff file

Before compiling the source needs to be extracted and patched with the contents of the diff file. Do this using the dpkg-source command:

dpkg-source -x foo_version.dsc

Build the source file

You will need to change into the extracted source directory. Then run:

dpkg-buildpackage -rfakeroot -b

Adding other compiler options

To pass options through to config set the value of the DEB_BUILD_OPTIONS variable. To pass options through to make set the appropriate environmental variables before rebuilding. eg to build with -fPIC under a t shell you can do the following: