close

Translation(s): English - Español - Italiano - Português (Brasil)- Русский

Take an existing package, re-build it, with a goal of familiarizing with a debian source package and process of building .deb files from source packages. Also useful if you want to rebuild a testing/unstable package (e.g. as a backport) on a stable system.

For more tutorials, see Packaging. This is part of Packaging/Learn tutorial series for newbies.



Simplified in 2025

This page was simplified in 2025.

For more guides, see Packaging.

Introduction

This tutorial is a simplified version of BuildingTutorial.

Requirements

You need very little previous knowledge for this tutorial, just no fear of the command line :-)

Technical requirements:

Remember:

   sudo apt install build-essential fakeroot devscripts

configure apt

Once you have installed the needed packages, the next thing that you need to do, is make sure that you have some source repositories configured in your computer.

Open your /etc/apt/sources.list file and check if you have one or more lines that start with deb-src.

  • deb-src http://deb.debian.org/debian unstable main

This line is needed in order to work with source packages.

If you don't have any deb-src lines, you'll need to add at least one . This is usually achieved by copying one of the existing deb lines and changing it to deb-src. You can do that by running an editor with admin rights (sudo gedit, sudo kate or sudo vim).

Usually, it's a good idea to use unstable as the repository, since you'll be working with the latest version of the package. But if you intend to modify a package as it is in stable or testing you could use those distributions as well.

If you use stable/testing/whatever as your running distribution, getting source from unstable won't affect it.

Once you've added the line, you'll need to do

   sudo apt update

in order to update the list of packages available for installation.

create a working directory

With the sources URL added to your apt repositories, you'll now be able to get the source of any Debian package that you like.

For this particular tutorial, we are going to download the source of one package and make a small modification to it, so that it works better.

It's always a good idea to have a directory that you use to work with source software, separated from other directories used for other stuff. In case you don't already have one, I'd suggest that you create a directory src with another one called debian inside it:

   mkdir -p src/debian/; cd src/debian

Inside this directory we will get the source of the package that we want to work with.

Choose the package

In this example, we'll use a package called node-pretty-ms, a library to print time in human readable format.

Get the source package

In order to get the source of any package, what you need to do is go to your chosen directory (src/debian in this example) and do (as normal user):

   apt source node-pretty-ms

Advanced: using dget

You can also find link to .dsc file from https://tracker.debian.org (or for older versions from https://snapshot.debian.org) and use dget command to download the source.

dget https://deb.debian.org/debian/pool/main/n/node-pretty-ms/node-pretty-ms_7.0.1-1.dsc

You have now downloaded the 3 files (1. .dsc, 2. .orig.tar.*, 3. .diff.gz or .debian.tar.*), composing the Debian source package.

Once the package is downloaded, you can check the directory where you are (typing ls), and you'll find that apart from the 3 files that were downloaded you also have a directory, called node-pretty-ms-8.0.0. This is the unpacked source/source directory of the Debian package.

To enter that directory, type:

   cd node-pretty-ms-8.0.0

When you check the contents of this directory (typing ls again), you'll see quite a number of files of different sorts, and a debian directory.

Every Debian (or Debian derivative) package includes a debian directory, where all the information related to the Debian package is stored. Anything that's outside of that directory, is the upstream code, i.e. the original code released by whoever programmed the software.

Go into the debian directory, by typing

  cd debian

This is the directory that the package maintainer has added to the source code to build the package.

In this directory you'll usually find lots of files related to Debian's version of the program, Debian specific patches, manpages, documentation, and so on. We won't be going any deeper about these files here. Look at its contents by typing ls.

Just keep in mind that

Let's move one directory back, by doing

   cd ..

You should be again at node-pretty-ms-8.0.0, the main directory of the source code.

Get the build dependencies

In order to build almost any program, you will need some dependencies installed.

The dependencies are the programs or libraries needed to compile your program. Usually it's a bunch of packages that end in -dev, but it might also be other things like automake or gcc, depending on how many development tools you've ever installed in that machine.

apt provides a way of easily installing all the needed dependencies:

   sudo apt build-dep node-pretty-ms

Once you've downloaded these tools, you'll be ready to build the package.

Rebuild without changes

Before making any changes to the code, let's build the package as it is right now, just to make sure that it builds and it installs properly. Do:

   debuild -b -uc -us

The extra parameters are to build a binary only package (.deb) and prevent it from signing the package, since we don't need to sign it right now.

This command will probably take a while to run, it will run the commands that are listed in the debian/rules file and will hopefully end with a message like:

dpkg-deb: building package 'node-pretty-ms' in '../node-pretty-ms_8.0.0-2_all.deb'.

In your own language. <your arch> can be i386, amd64, depending on which architecture you are running your machine on. Architecture indepent packages can be all.

Once the package has correctly built, the next step is to install this file with:

   apt install ../node-pretty-ms_8.0.0-2_all.deb

That's it, get ready for the next package

You're done with modifying the package, you can now keep fixing bugs in other Debian packages! These are the important commands you'll need to remember:

   apt source the-package
   apt build-dep the-package
   debuild -us -uc
   apt install ./the-package_blabla.deb
   debdiff package_blabla.dsc package_blabla.1.dsc > another_debdiff.diff

See also