Compile a Source Package

Compile a Source Package

Rebuild packages with custom options to suit your architecture, environment, or whims.

Software is generally installed on Ubuntu using binary packages that contain a precompiled copy of the program (the binary), but sometimes it can be useful to recompile the program yourself using custom options. To make that process easier, all official Ubuntu packages are also available in source form, and tools are provided to enable you to build your own custom binary package from the source package.

To compile a source package, take the following steps.

Enable Source Repositories

Each official binary-package repository in your /etc/apt/sources.list has a matching source-package repository that has the exact same address but a deb-src prefix instead of deb:

deb dapper main restricted
deb-src dapper main restricted

You can "Modify the List of Package Repositories" [Hack #60] if you don't already have source repositories enabled.

Install Package Build Tools

Install the basic tools required for building binary packages:

$ sudo apt-get install devscripts build-essential fakeroot

Install Build Dependencies and Fetch the Source

Packages have varying build requirements that are defined in the package header. For example, rebuilding a package for a PHP module requires that you have the PHP headers installed on your system so they can be linked when the module is compiled. The package system can take care of fetching all the build dependencies for you automatically:

$ sudo apt-get build-dep 

For example, this command fetches dependencies for the php4-apd module:

$ sudo apt-get build-dep php4-apd

Next, fetch the source package you want to rebuild. Note that, unlike almost every other use of apt-get, you do not need to run this with root privileges because it doesn't actually install the package; all it does is download the source package and place it in your current directory. To keep things neat, you may want to create a directory in which to do the build and change into it before fetching the source:

$ mkdir php4-apd
$ cd php4-apd
$ apt-get source php4-apd

This will place a number of files in your current location, including a directory containing a fully extracted copy of the package:

[email protected]:~/php4-apd$ ls -l
drwxr-xr-x 6 jon jon   4096 2005-11-30 14:43 php4-apd-0.4p2
-rw-r--r-- 1 jon jon    532 2005-12-02 04:25 php4-apd_0.4p2-6.dsc
-rw-r--r-- 1 jon jon 368800 2005-12-02 04:25 php4-apd_0.4p2-6.tar.gz

Apply Changes

Enter the extracted package directory:

$ cd php4-apd-0.4p2

Any necessary changes can now be made to the extracted copy of the package, but note that the build process will be controlled by a special makefile in debian/rules rather than any makefiles provided with the original program. If you need to make changes to the configure or build flags, you should apply them by editing debian/rules.

Build Binary Package

If you are not listed as the package maintainer in debian/control and debian/changelog, you will need to pass in flags to tell debuild not to sign the new package:

$ debuild -us -uc -b

The -us and -uc flags signify unsigned source and unsigned changes files, and the -b flag signifies to build only a binary package.

You will now have a number of new files in your working directory, including a .deb binary package file built with your custom options applied. You can use dpkg to install and remove standalone packages [Hack #57] and make sure it works as intended, and even set up a package repository [Hack #65] if you want to distribute your custom package to the world.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows