Rebuilding Debian packages

Posted by Steve on Fri 1 Oct 2004 at 13:09

Most people are happy with the binary packages which Debian provides, as they tend to be setup to cover the common uses. But what happens if you are looking to rebuild an existing package with different options? Well you can rebuild a package from source very easily.

Rebuilding Debian packages is a two step process, first of all you must fetch the source which was used to build the package, and then you must actually rebuild it.

In order to build most common packages you will need a compiler of some description, and you may well find that you need some development libraries which are used in the rebuild process itself.

Thankfully installing the required packages to build a Debian package is a simple process which the apt-get tool will help you with. (If you're new to using apt-get you might find this simple introduction useful).

There are two commands that you will need to use to rebuild a package:

  • apt-get source foo
    • get the source to the package foo
  • apt-get build-dep foo
    • get and install the packages required to rebuild the package foo

These two steps you will have to perform for each rebuild you need to do, but thankfully they are very simple to remember and automate.

The only other thing you need are the Debian repackaging utilities which you can install with apt-get install devscripts build-essential fakeroot. This command will allow you to have a build environment, which will allow you to convert the sources you downloaded into .deb files you can actually install.

Lets have an example of how you would rebuild the package less.

First we download the source to the current package:

steve@earth:~$ apt-get source less
Reading Package Lists... Done
Building Dependency Tree... Done
Need to get 258kB of source archives.
Get:1 http://http.us.debian.org stable/main less 374-4 (dsc) [611B]
Get:2 http://http.us.debian.org stable/main less 374-4 (tar) [243kB]
Get:3 http://http.us.debian.org stable/main less 374-4 (diff) [14.3kB]
Fetched 258kB in 2s (120kB/s)
dpkg-source: extracting less in less-374

Now we can download and install the required packages to build it. (Depending on the packages you have installed on the machine you're using you may find you don't need to install anything).

root@earth:~# apt-get build-dep less
Reading Package Lists... Done
Building Dependency Tree... Done
Note, selecting libncurses5-dev instead of libncurses-dev
The following NEW packages will be installed:
  libncurses5-dev
0 packages upgraded, 1 newly installed, 0 to remove and 0  not upgraded.
Need to get 998kB of archives. After unpacking 4988kB will be used.
Do you want to continue? [Y/n]
Get:1 http://ftp.us.debian.org stable/main libncurses5-dev 5.2.20020112a-7 [998kB]
Fetched 998kB in 5s (177kB/s)
Selecting previously deselected package libncurses5-dev.
(Reading database ... 21135 files and directories currently installed.)
Unpacking libncurses5-dev (from .../libncurses5-dev_5.2.20020112a-7_i386.deb) ...
Setting up libncurses5-dev (5.2.20020112a-7) ...

In our case we discovered that we needed to install the libncurses5-dev package in order to compile less.

Now we can actually rebuild the package. We perform a rebuild by using the debuild command. If we are not the maintainer of the package will need to add two flags to this telling the building process not to sign the package. In most cases debuild -us -uc is what you wish to use.

steve@earth:~$ cd less-374/
steve@earth:~$ debuild -us -uc
...
...
dpkg-deb: building package `less' in `../less_374-4_i386.deb'.
 dpkg-genchanges
dpkg-genchanges: not including original source code in upload
dpkg-buildpackage: binary and diff upload (original source NOT included)

Now if we look in the parent directory we will find a brand new, recompiled, .deb file.

This can be installed with dpkg as follows:

steve@earth:~/less-374$ cd ..
steve@earth:~$ su
Password:
root@earth:~# dpkg --install less_374-4_i386.deb
(Reading database ... 21941 files and directories currently installed.)
Preparing to replace less 374-4 (using less_374-4_i386.deb) ...
Unpacking replacement less ...
Setting up less (374-4) ...

of course we did miss out any editing of the package to make it build differently, but if you know already you need to rebuild a package to change its options, or bahaviour, you will know how to do that!

 

 


Posted by Anonymous (82.153.xx.xx) on Mon 30 May 2005 at 22:13
An examle of a change would be nice, to see how you do it.

If compiling from source I might change things by passing variables to the configure script. How would I accomplish something similar here?

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Mon 30 May 2005 at 22:18
[ View Steve's Scratchpad | View Weblogs ]

Thanks for the comment, it might be nice to redo this piece in the future.

To specifically answer your question though - you can adjust which flags are passed to the configure script by looking at the file inside the debian/ directory called rules.

This is where "./configure" and "make" are called from ..

Steve
-- Steve.org.uk

[ Parent | Reply to this comment ]

Posted by dosha (131.251.xx.xx) on Tue 31 May 2005 at 11:46
Thanks for the reply, I'll have a look.

[ Parent | Reply to this comment ]

Posted by simonw (193.237.xx.xx) on Mon 27 Jun 2005 at 20:57
[ View Weblogs ]
I came unstuck doing this today.

The squid package has a series of patches in dpatch format, that it applies before building, and removes when cleaning up from a build. Thus attempts to modify the package fall foul if the changes conflict with these patches (and Murphy's law states they will, and they did).

A kind soul of the debian-mentors IRC channel pointed me at dpatch-edit-patch, which I think is the right solution. It all becomes much clearer when you know debian/rules is the Makefile and well as a statement of the obvious.

[ Parent | Reply to this comment ]

Posted by mverwijs (131.211.xx.xx) on Tue 2 Aug 2005 at 09:09

Note:

Before commanding "apt-get source ", cd to the folder you want the source downloaded in.

("apt-get source less" will download the source-files in your present working directory (pwd).
Since I was standing in /etc/apt in order to modify my sources.list, I was in for tiny shock! ;-))

[ Parent | Reply to this comment ]

Posted by Anonymous (212.81.xx.xx) on Fri 20 Jul 2007 at 05:40
Exactly the same happened to me.

[ Parent | Reply to this comment ]

Posted by hardik (61.95.xx.xx) on Tue 30 Aug 2005 at 09:22
You should also explain which URI they have to put in
/etc/apt/source.list, so then can download the source by your given command. Other wise give the error of "source uri not found in source.list:

"deb-src http://http.us.debian.org/debian stable main contrib non-free" This is the uri needed by user when they are downloading the source.

With Cheers,
Hardik Dalwadi.

[ Parent | Reply to this comment ]

Posted by Anonymous (99.152.xx.xx) on Tue 10 Feb 2009 at 21:14
Thank you so much for that info I've been looking for that!

[ Parent | Reply to this comment ]

Posted by trey (216.234.xx.xx) on Fri 21 Oct 2005 at 14:59
I rebuilt the gnome-panel using this method because I wanted to use the remove_debian_menu.patch so that people would stop asking me why there were two sets of menus (they said it was too confusing).

I did:

apt-get source gnome-panel
apt-get build-dep gnome-panel
cd gnome-panel-2.8.3
patch -p0 -l < debian/maintfiles/remove_debian_menu.patch
debuild -us -uc


When that was finished, I went up and installed the .deb's:

gnome-panel_2.8.3-1_i386.deb
gnome-panel-data_2.8.3-1_all.deb

I restarted gnome-panel and all was working as expected. (woohoo!)

The only thing I am concerned about is this morning I saw a security update, and an apt-get dist-upgrade also wanted to upgrade my gnome-panel, to the same version (2.8.3-1). If I let it upgrade, it put the default sarge gnome-panel back in place. I'm not sure what to do here to make my packages override. I'm thinking there's a way to set it in /etc/apt/preference, but I don't know how to do that, and I don't know the best approach to this problem. Will someone please advise?

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Fri 21 Oct 2005 at 15:02
[ View Steve's Scratchpad | View Weblogs ]

If you store your packages inside an Apt repository then you can use "pinning" to make your local package have a higher priority than others.

This doesn't work if you're just using "dpkg --install foo.deb" though. In that case you can either:

  • Put the package "on hold", so it is never upgraded.
  • Rebuild your package with a higher version number, eg foo-1.2sarge1

I hope that helps a little.

Steve
--

[ Parent | Reply to this comment ]

Posted by trey (216.234.xx.xx) on Fri 21 Oct 2005 at 15:05
Thanks! I'll set it on hold until I can get a personal apt repo set up. :-)

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Fri 21 Oct 2005 at 15:07
[ View Steve's Scratchpad | View Weblogs ]

There were a couple of comments explaining how to put packages on hold posted recently.

Steve
--

[ Parent | Reply to this comment ]

Posted by trey (70.128.xx.xx) on Mon 24 Oct 2005 at 20:27
I created a repository for my custom gnome-panel .deb package (and some others), and modified apt preferences.conf...

Package: *
Pin: release o=trey
Pin-Priority: 750

Package: *
Pin: release a=stable
Pin-Priority: 700

Package: *
Pin: release a=testing
Pin-Priority: 650

Package: *
Pin: release a=unstable
Pin-Priority: 600

Besides, 750 for my repository, I've pinned it at 999 and 1001. It never replaces gnome-panel with the one from debian stable, which is good, but the weird thing is that apt-get dist-upgrade always tells me I need to upgrade, and when I proceed, it always reinstalls the gnome-panel deb from my repository, o=trey. It's throwing off some scripts that tell me if i need to update/upgrade or not...

Am I doing something wrong or is that just normal behavior? If it's normal behavior, I'll change the version. I was just hesitant to do that because of administrivia and simplicity.

Thanks :-)

[ Parent | Reply to this comment ]

Posted by marian (84.191.xx.xx) on Mon 29 Mar 2010 at 20:00
Why do you want to prevent that security update from being installed? There is a reason it is supplied: It closes a security hole :)

So instead of doing that pinning stuff just get the new source and recompile it. What I always do is creating a new changelog entry and appending +local or something like that to the version number, leaving it the same apart from that (you may use `dch -v $version+local` for that; find out the version from the first line in debian/changelog). This way my modified package is higher than the one it was created from, but always lower than an potential upgrade. If there is an upgrade I just compile it again.

[ Parent | Reply to this comment ]

Posted by Anonymous (67.180.xx.xx) on Fri 20 Jul 2012 at 10:09
One reason would be that you only have one package that you are maintaining so you don't want to maintain your apt-repository for one deb. But, you still want to upgrade everything else with apt-get.

Your advice of just recompile is less than useless.

The proper thing of course is to set up one's own repository.

But, your statement of just recompile is ignoring the real requirement of system administration and not breaking things.

[ Parent | Reply to this comment ]

Posted by Anonymous (143.89.xx.xx) on Wed 16 Nov 2005 at 09:14
How to change the package name?

[ Parent | Reply to this comment ]

Posted by Anonymous (82.41.xx.xx) on Wed 16 Nov 2005 at 12:00
Edit the debian/control and debian/changelog files.

[ Parent | Reply to this comment ]

Posted by Anonymous (213.21.xx.xx) on Wed 11 Jan 2006 at 00:35
...and how to rebuild a binary only package? I've got a binary only .deb that works only if I add some files that make postinst finish its work. How can I do it?

[ Parent | Reply to this comment ]

Posted by Anonymous (202.164.xx.xx) on Sun 29 Jan 2006 at 10:27
I don't believe this is possible. I believe you'd need to get the source package.

[ Parent | Reply to this comment ]

Posted by Anonymous (193.190.xx.xx) on Fri 31 Mar 2006 at 15:53
A Debian package is just an "ar" archive. So, if I recall correctly, you can extract the .deb file, edit it and recreate the archive with ar.

--
Panagiotis Issaris

[ Parent | Reply to this comment ]

Posted by Anonymous (218.191.xx.xx) on Sun 30 Jul 2006 at 15:54
I follow the example to re-build less but I got this error

"dh_testroot: You must run this as root (or use fakeroot).
make: *** [clean] Error 1"

in the last step "fakeroot debuild -uc -us".
It went through alright if I use dpkg-buildpackage instead of debuild. I don't know why.

[ Parent | Reply to this comment ]

Posted by debian-wanna-be (82.57.xx.xx) on Wed 3 Jan 2007 at 23:19
Hi,
as you can tell from my nickname I'm far from expert in Debian.

I'm trying to modify apache source.

That's what i did

apt-get update
apt-get source apache

to get the source, but...

I actually can't find any c.file !

Clearly i've got lots to learn.

I googled massively on the subject, but i didn't find a practical example of:

- getting debian package (source, of course)
- unpacking it
- modify sources
- repackage it

What am i missing ?















[ Parent | Reply to this comment ]

Posted by Anonymous (2a01:0xx:0xx:0xxx:0xxx:0xxx:xx) on Wed 19 Dec 2007 at 12:12
debuild doesn't seem to exist anymore.
#apt-get install debuild
Reading package lists... Done
Building dependency tree... Done
E: Couldn't find package debuild

[ Parent | Reply to this comment ]

Posted by Anonymous (86.112.xx.xx) on Thu 20 Dec 2007 at 12:12
debbuild is in the 'devscripts' package.

[ Parent | Reply to this comment ]

Posted by hamd (80.21.xx.xx) on Wed 11 May 2011 at 11:41
Hi thanks a looooooooot for this guide, It helped me around 70% of job for which I was looking since last week. However, I tried this on debian machine, I have a question:

CAN I DO THIS IN UBUNTU? I am sorry I am new in the world of Linux

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Wed 11 May 2011 at 11:42
[ View Steve's Scratchpad | View Weblogs ]

Mostly yes, Ubuntu uses the same tools and techniques for their packages.

Steve

[ Parent | Reply to this comment ]

Posted by hamd (80.21.xx.xx) on Wed 11 May 2011 at 14:21
Wow it worked with the same steps in UBUNTU, thanks a lot steve

[ Parent | Reply to this comment ]

Posted by rhertzog (2a01:0xx:0xx:0xxx:0xxx:0xxx:xx) on Tue 5 Jul 2011 at 11:14
I also wrote a similar article (How to rebuild Debian packages) that might be helpful by providing a slightly different perspective.

[ Parent | Reply to this comment ]

Posted by Alan_B (192.93.xx.xx) on Mon 5 Dec 2011 at 11:56
Very helpful, except...
I wanted to rebuild ppp from source, my modem manufacturer (Iridium) insists that I need 2 stop bits and pppd is hardcoded to 1.
I got the source and built it to prove that I had everything that I needed. That was fine.
I modified the source (adding a cstopb option and supporting code) but the rebuild didn't work, this is what I got...

$ debuild -us -uc
dpkg-buildpackage -rfakeroot -D -us -uc
dpkg-buildpackage: export CFLAGS from dpkg-buildflags (origin: vendor): -g -O2
dpkg-buildpackage: export CPPFLAGS from dpkg-buildflags (origin: vendor):
dpkg-buildpackage: export CXXFLAGS from dpkg-buildflags (origin: vendor): -g -O2
dpkg-buildpackage: export FFLAGS from dpkg-buildflags (origin: vendor): -g -O2
dpkg-buildpackage: export LDFLAGS from dpkg-buildflags (origin: vendor):
dpkg-buildpackage: source package ppp
dpkg-buildpackage: source version 2.4.5-4
dpkg-buildpackage: source changed by Marco d'Itri <md@linux.it>
dpkg-source --before-build ppp-2.4.5
dpkg-buildpackage: host architecture armel
fakeroot debian/rules clean
QUILT_PATCHES=debian/patches \
quilt --quiltrc /dev/null pop -a -R || test $? = 2
Patch zzz_config does not remove cleanly (refresh it or enforce with -f)
make: *** [unpatch] Error 1
dpkg-buildpackage: error: fakeroot debian/rules clean gave error exit status 2
debuild: fatal error at line 1325:
dpkg-buildpackage -rfakeroot -D -us -uc failed


Just at the present, I am using make and installing manually. This works fine, but is there a better (correct) way?

Thanks, Alan

[ Parent | Reply to this comment ]

Posted by Steve (90.193.xx.xx) on Mon 5 Dec 2011 at 12:08
[ View Steve's Scratchpad | View Weblogs ]

The issue here is that you're modifying the source of the package which is built around the quilt system. The quilt tool applies patches from ./debian/patches prior to building - and that is where your complaints are coming from.

You might find this useful reading:

Steve

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 534 votes ~ 7 comments )