HOWTO: Compiling packages from source on Debian Linux
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!

Free Email Subscription









July 5th, 2009 at 3:17 pm
[...] 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. More here [...]
July 5th, 2009 at 9:22 pm
Thanks for documenting this. I usually end up compiling packages from source for two reasons:
1. Bandwidth is expensive where I live (so I want to reuse the source if I switch from Debian to Slackware or whatever, instead of getting only a .deb).
2. Debian’s usually a few versions behind
This works well for packages which aren’t integrated too much into the core of the system, otherwise you get into dependency hell and it gets ugly.
Also, this requires knowledge of the ./configure, make, make install process! Deb sources is a good way for beginners to learn, though!
Thanks again! Long live Debian!
Alan
Reply to this comment
September 22nd, 2009 at 12:53 pm
What about debuild? It’s what I’ve been using, and from the manpage, it seems to be a wrapper for dpkg-buildpackage along with some other tools.
Also, if you are compiling the same version as the one from the repositories, Debian will try to “upgrade” to the binary package from the server, which is not what you want. To fix this, use to bump the version of the source package in a portable way. Just run debchange –nmu and then enter a description of what you changed.
Reply to this comment
Admin Reply:
September 23rd, 2009 at 10:39 pm
Hi Ryan,
You are right. debuild is a wrapper function.
Thanks for the versioning tip. I just wanted to post something by which anyone can quickly compile packages. Certainly if those packages are going to be used by a lot of people, then yes, one should do it in a more *correct* manner.
Reply to this comment
phr3ak Reply:
October 20th, 2009 at 3:35 pm
Anyone knows debian why want upgrade the compiled package if that is same version like in repository?
Reply to this comment
Ryan Reply:
October 20th, 2009 at 4:49 pm
Please read my previous comment. I addressed exactly this problem.
Reply to this comment
October 21st, 2009 at 12:49 am
but i don’t understand well. could you explain, please?
Reply to this comment
Admin Reply:
October 26th, 2009 at 10:14 pm
What do you not understand exactly?
Reply to this comment
October 27th, 2009 at 3:48 am
why want upgrade if the version of installed package is same as stock package? where is the difference between local compiled package and stock package?
Reply to this comment
February 14th, 2010 at 2:36 am
Do you understand my question?
Reply to this comment
Admin Reply:
February 15th, 2010 at 9:57 am
I think Ryan is trying to tell that if you don’t change the version of the package, next time when you do an apt-get update/upgrade it will try to overwrite your custom compiled package and hence your changes will get lost. So by changing the version, apt-get won’t interfere with your custom deb package.
Reply to this comment