Purpose: In this post we will learn how to build a Debian package (.deb) from source packages. You might be thinking why would I ever need to do that, right? Well most of the times you are right, you won’t require to build packages from source, but there will be situations sometimes when you will be required to do this. Following are some situations where compiling packages from source will be necessary:

  • The package you want is not in the Debian stable repo because of a fatal bug and subsequently the package was removed from stable sources.
  • The package you want is not in the Debian stable repo because it is a new package and hence it has only made it;s way into testing/unstable branch.
  • The package you want is not at all in any of the Debian repos (stable, testing, unstable, experimental) but exits in other Debian-based system like Ubuntu, etc.
  • You want to customize the existing package to your needs and hence you need to make small changes to it.

Step 1: Make sure the deb-src line is added to your sources.list file

Once you identified the repository from which you are going to download the sources, you need to add the deb-src line to your /etc/apt/sources.list file. For example, suppose you would like to download a source package from Debian’s testing repository, then you would add the following line:

deb-src http://ftp.us.debian.org/debian/ testing/updates main contrib non-free

Step 2: Download the source package

Now you need to download the source package from the repository that you added above.

Note: If you have multiple deb-src lines then you need to make sure that the source is downloaded from the correct repo because multiple  repo may have the sources for the package.

In this example, I will be using “splashy” package as an example. It is always a good idea to create a directory first in which you will do all the compiling work.

# mkdir splashy-src
# cd splashy-src
# apt-get source splashy

The above command will download the source packages and create a directory like this:

# ls

Output:

splashy-0.3.13            splashy_0.3.13-3.dsc
splashy_0.3.13-3.diff.gz  splashy_0.3.13.orig.tar.gz

#

Now go into the newly crated directory which contains actual code files:

# cd splashy-0.3.13
# ls

Output:

ABOUT-NLS               config.status         INSTALL        README
aclocal.m4              config.sub            install-sh     schemas
AUTHORS                 config.sub.cdbs-orig  libtool        scripts
autogen.sh              configure             ltmain.sh      splashy.pc
ChangeLog               configure.ac          m4             splashy.pc.in
compile                 COPYING               Makefile       splashy.spec
config.guess            debian                Makefile.am    src
config.guess.cdbs-orig  depcomp               Makefile.in    stamp-h1
config.h                doc                   missing        themes
config.h.in             Doxyfile              mkinstalldirs  TODO
config.log              Doxyfile.in           NEWS
config.rpath            HACKING               po

Step 3: Download any dependency packages


Almost any package that you want to build will depend upon certain development libraries files which you need to satisfy first otherwise the compilation process will give you errors. In Debian you can easily do this by issuing a simple command:

# apt-get build-dep splashy

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
cdbs chrpath libdirectfb-dev libmagic-dev libmpeg3-1 libmpeg3-dev
libsysfs-dev quilt

0 upgraded, 8 newly installed, 0 to remove and 1 not upgraded.
Need to get 2486kB of archives.
After this operation, 7348kB of additional disk space will be used.
Do you want to continue [Y/n]? Y

As you can see from the above output, there are quite a few development libraries (in blue) that are required to build the packages.

Step 4: Customize your package (optional)

Here you can do whatever changes you want in your packages. Edit the source files, docs files, etc. Once you are done making all the changes you want simply compile the package as explained in the next step.

Step 5: Compile the package

Before we compile the package, we need certain tools that will do the compilation process for us. You can install them by giving following command:

# apt-get install dpkg-dev build-essential devscripts

Note: You might not require the devscripts package but just for the sake of completeness I am including it.

And now finally compile the package:

# dpkg-buildpackage -rfakeroot -us -uc -b

The options:

– b = Means create binary-only package

-uc = Means do not sign the .changes file

-us = Means do not sign the source packages

We do not sign the “change” file and the source package because we are just going to use the packages personally  and are not going to maintain them officially. In one of my next post, I will write about how to build packages so that you can make then maintainable.

Now it will be few minutes before your package gets compiled. At the end of the process you can see the newly created packages in your parent directory.

# ls ../

Output:

libsplashy1_0.3.13-3_amd64.deb      splashy_0.3.13-3_amd64.deb
libsplashy1-dev_0.3.13-3_amd64.deb
splashy_0.3.13-3.diff.gz
splashy-0.3.13                      splashy_0.3.13-3.dsc
splashy_0.3.13-3_amd64.changes      splashy_0.3.13.orig.tar.gz

The packages in green are the newly created packages and the packages in the blue are our source packages that we initially downloaded

Step 6: Install the binary deb package

Finally install your brand new shinny package by giving the following command:

# cd ..
# dpkg -i *.deb

That’s it. You are done. Similarly you can do this process for any possible source package. However, if you try this process on bigger and complex packages you might need to do some additional/funky stuff to get them compiling specially if the source packages are not from native Debian repo (like Ubuntu) or from a different branch of Debian than what you are currently running.

Happy compiling!

Be Sociable, Share!