Build Ubuntu/Debian packages from source (and apply a patch)

Sometimes there is a bug in a package from Ubuntu that is not yet fixed, or you just want to modify something in the source of a software. Here is an easy way to build your own package from the Ubuntu sources.

First you need to install the required tools for building software from source

sudo apt-get install build-essential

Let’s assume you want to build a package called foo and apply a patch bar.patch to it.

First you have to download the necessary build dependencies for foo

sudo apt-get build-dep foo

Now you are ready to download the actual sources of the package (Don’t use sudo here!!)

apt-get source foo

This gives you a directory called foo- you can now cd into it apply the patch and build the package

cd foo-<version>
patch -p1 < bar.patch
dpkg-buildpackage

Now you have a package called foo-.deb in the same directory where you have run apt-get source. You can install it using dpkg.

sudo dpkg -i foo-<version>.deb