I have an application foobar
for which someone has written a patch to add a feature I like. How can I use the patch?
How to apply software patches
compilingpatchsoftware installation
compilingpatchsoftware installation
I have an application foobar
for which someone has written a patch to add a feature I like. How can I use the patch?
Best Answer
Patches are usually contained in
.diff
files, because the patches are created using thediff
command.A patch is a series of insertions and deletions into source code. For this reason, in order to use the patch, you must build the application (e.g., "foobar") from source after applying the patch. So, in steps:
1. Get the source package for foobar.
Most linux distributions (n.b. patching is not unique to linux) have "source packages" you can use for this purpose, but since these are heterogeneous, I will only refer to the format of the original source here. The original source is not part of the distro and may be hard to find. A good place to start is wikipedia, which has articles for many popular applications, and the article should contain a link to a homepage with a source download. You can also google yourself, obviously. The source package will be called something like
foobar.0.1.tar.bz2
. Unpack this -- you now have a directory calledfoobar.0.1
.2. Add the patch.
Sometimes patches are single files and sometimes they are a set of several files. Copy those into
foobar.0.1
andcd foobar.0.1
. Next, you need to run thepatch
command. This reads from standard input, so you want to pipe the.diff
file in. The tricky part is determining what to use for the-p
option (if there are no instructions with the patch). To do that you need to look at the beginning of the patch file. For example:In this case,
comm.c
is the name of the source file that will be altered. However, notice that there is a directory appended to it. Since these are not the same directory ("old" vs. "new"), this is a big clue that this part of the path is junk (for our purposes). The purpose of the-p
switch (seeman patch
) is to eliminate this prefix. It takes a number, which is the number of slashes (/
) to eliminate, with everything in between; in this case we would use-p1
to reduce the path to just plaincomm.c
.That presumes
comm.c
is actually in the same directory, which will be another clue as to whether your interpretation is correct. If both those lines weresrc/comm.c
, andcomm.c
is actually in thesrc
subdirectory of your build tree, then you need to use-p0
-- beware that not using-p
at all will remove ALL slashes. If the path is absolute (i.e., begins with/
), that's probably what you want. Now apply the patch:The source has now been modified. If there are more
.diff
files, apply those the same way.3. Build and install.
This is the normal process you would go through to build something from source -- first
./configure
, thenmake
,make check
,make install
. Before you do the last one, if you already have an existing installation offoobar
, decide whether you want to remove or overwrite that or how you are going to deal with the naming conflict. You probably wantfoobar
to refer to your new, patched version, and not the old one.