How to: Recompiling / Rebuild Debian / Ubuntu Linux Binary Source File Packages

Q. I’d like to rebuild a Debian / Ubuntu package called foo with additional option. How do I recompile .deb packages?

A. To build a Debian package, you need the following packages / software installed on system
[a] fakeroot : fakeroot package – runs a command in an environment wherein it appears to have root privileges for file manipulation. This is useful for allowing users to create archives (tar, ar, .deb etc.) with files in them with root permissions/ownership.

[b] dpkg-dev : package building tools for Debian [c] Development environment – You need gcc, make and all other compiler collection installed on system. Under Debian / Ubuntu package build-essential takes care of everything.

Step #1: Install required packages

Type the following command
$ sudo apt-get install build-essential fakeroot dpkg-dev

Step #2: Install source code package

First, create a directory to store source package, enter:
$ mkdir build
$ cd build

Use apt-get command to install source code for a package called foo
$ sudo apt-get source foo
Install all build-dependencies, enter:
$ sudo apt-get build-dep foo
Unpacks Debian / Ubuntu source archives with Debian source package (.dsc) manipulation tool, enter:
$ dpkg-source -x foo_version-revision.dsc
To just compile the package, you need cd into foo-version directory and issue the command
$ dpkg-buildpackage -rfakeroot -b
If you want to pass custom additonal options to configure, you can set up the DEB_BUILD_OPTIONS environment variable. For instance, if you want pass option called –enable-radio –enable-gui, enter:
$ DEB_BUILD_OPTIONS="--enable-gui --enable-radio" fakeroot debian/rules binary
You can also pass some variables to the Makefile. For example, if you want to compile with gcc v3.4, enter:
$ CC=gcc-3.4 DEB_BUILD_OPTIONS="--enable-gui --enable-radio" fakeroot debian/rules binary

A complete example – mplayer

Let us see how to rebuild mplayer media player package with –enable-radio –disable-ivt options:
# sudo apt-get source mplayer
# sudo apt-get build-dep mplayer
# dpkg-source -x mplayer_version-revision.dsc
# DEB_BUILD_OPTIONS="--enable-gui --enable-radio --disable-ivt" fakeroot debian/rules binary

Now wait for some time as compile procedure going to take its own time. To install the newly-built package, enter:
# dpkg -i ../mplayer_version-revision_arch.deb

Further readings:

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
18 comments… add one
  • Scott Carlson Jan 15, 2008 @ 16:23

    I also use these lines to up the package number for a local deb to indicate what I’ve changed.

    export EMAIL=someemail@gmail.com
    dch -n

  • 🐧 nixCraft Jan 15, 2008 @ 16:44

    Scott,

    devscripts make the life of a Debian Package really easy.

  • housetier Jan 15, 2008 @ 20:25

    When I build from source, I create a build script that sets the environment variables and then executes debian/rules. That way I don’t have to find the correct setting again, when I build anew some time later. The script lives in ~/bin and thus is easily found.

  • atul_iims Jan 17, 2008 @ 6:11

    i have one query that is i am having one text file which include lots of version like

    likeCompress-Raw-Zlib-2.001.tar.gz
    Compress-Zlib-2.001.tar.gz
    ConsoleKit-0.2.3.tar.gz
    Convert-ASN1-0.20.tar.gz
    Crypt-SSLeay-0.51.tar.gz

    my problem is how to extract string one place like
    Compress-Zlib & version in other file 2.001.tar.gz
    please if you have any idea about this please mail me

  • Nilesh Jan 19, 2008 @ 1:53

    Is this possible for Fedora 7+ ?

  • 🐧 nixCraft Jan 19, 2008 @ 6:43

    Use rpmbuild under RHEL / CentOS / Suse / Fedora Linux

  • Killmyself Nov 7, 2008 @ 11:19

    All this command didn’t help me much because I didn’t have any network connection at my home. All I can do is by downloading all the binary sources packages. But, I didn’t know the correct command to compile and execute the file. So, how can you help me then?
    Before this I tried to install realplayer, but all those command I found from internet niether can help me much, but, when I drag the .bin file to terminal windows, it suddenly execute the file. Then, my installation complete, I didn’t need any command like:
    sudo chmod a+x XXXXXX.bin

    I didn’t need this command, I think my step quite easy. But know the problem is how can I install tar.gz packages? I tried the same way to but, it didn’t work at all. Can you suggest me any other alternative way to install this kind of packages?

    I want to install video codecs for my ubuntu 8.04, I search a lot of blog in internet, but must of the way they show required me to have internet connection, but I didn’t have one, so please I’m begging anyone that can help me?
    here my e-mail glistening.dream@gmail.com

  • Wilson Hung Dec 7, 2008 @ 13:25

    My environment is on ubuntu 8.10 and I entered $ sudo apt-get source foo at step 2, for all build-dependencies installation, but got an error : cannot find sources of foo package. What should I do to continue following steps? Thanks.

  • 🐧 nixCraft Dec 7, 2008 @ 13:48

    Replace foo with actual package name such as apache2 or whatever you wish to rebuild.

  • brownman Apr 20, 2009 @ 1:48

    u absolutly saved me years of pains..

    im struggeling to recompiling- other games – for my desired learning..

    10X alot – it’s a bless finding your page !

  • Chris Brito May 26, 2009 @ 13:55

    you help me so much! thank you

  • Maks Oct 3, 2009 @ 3:52

    hi! but isn’t “apt-get source foo” doing “dpkg-source -x foo.dsc” ???? so in
    # sudo apt-get source mplayer
    # sudo apt-get build-dep mplayer
    # dpkg-source -x mplayer_version-revision.dsc
    # DEB_BUILD_OPTIONS=”–enable-gui –enable-radio –disable-ivt” fakeroot debian/rules binary

    Isnt this step already included whet you do apt-get source ?? # dpkg-source -x mplayer_version-revision.dsc

  • Daniel Feb 9, 2010 @ 20:35

    When I rebuild a package this way and install it, then it will be overwritte with same version from repository next time I run apt-get update/dist-upgrade. I guess you forgot to tell how to push the version number. Perhaps “dch -i”?

  • Debianfan Jun 3, 2010 @ 8:27

    Thank You!
    I’ve Just installed Debian Squeeze/unstable after almost 2 years.guess what?Debian Version of sensors-applet fails to show My Nvidia GPU temperatures.Your Article Turns out great ,I’ve to enable nvidia-support with “DEB_BUILD_OPTIONS=”–enable-gui –with-nvidia” fakeroot debian/rules binary”
    Thanks!
    PS:GPU Temp Support in sensors-applet needs libNVCtrl and some other headers which comes with NVIDIA installer or ,for Debian it is in the package “nvidia-settings”

    Thanks Again!

  • Alex Sep 17, 2010 @ 19:34

    I think that the command ‘sudo apt-get source foo’ should actually be ‘apt-get source foo’ so that the source files will have the correct permissions when compiling with fakeroot under regular user permissions.

  • JustAComment Dec 9, 2010 @ 19:35

    only a little comment:

    There is a small mistake in the general instructions: the “dpkg-source -x foo_version-revision.dsc” command fails because the sources are owned by root. This can be avoided because the “sudo” in the step of getting the source is not really needed, so instead of “sudo apt-get source foo” you can do “apt-get source foo”.

    Otherwise, this howto is just perfect.

  • anonim Jul 22, 2011 @ 2:37

    How to setup this using Eclipse CDT ?
    I need autocomplete feature

  • Ravikant Feb 10, 2012 @ 7:22

    “Are you crazy?” and septpod it. Now I wish I had tried it, though. Just for giggles.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.