How to Build a Package from Source in Linux

Rebuild Package Featured

Besides its open-source nature, customizability is one of the other reasons many users love Linux: you can modify and configure almost every file to meet your specific needs and style. This includes the ability to rebuild a package from source.

The ability to rebuild a package from the source can be beneficial to any Linux power user because it allows you to change packages, enable or disable a feature, or even apply custom modifications.

This article describes simple steps to rebuild a package from the source.

Also read: Linux Running Too Slow? Here’s How to Find the Cause

1. Enable Source Repositories

The first step before rebuilding a source package is to enable the source repositories for your distribution. Enabling the source repos allows you to search and download source packages using the default apt package manager.

In Debian systems, you can add the source packages by editing the “/etc/apt/sources.list” file.

For example, the following are the contents of the “sources.list” for Debian buster with the source packages enabled.

deb http://deb.debian.org/debian buster main
deb-src http://deb.debian.org/debian buster main
deb http://deb.debian.org/debian-security/ buster/updates main
deb-src http://deb.debian.org/debian-security/ buster/updates main
deb http://deb.debian.org/debian buster-updates main
deb-src http://deb.debian.org/debian buster-updates main

The deb-src enables the source packages, informing the package manager to give us the source package and not the usual binary file.

Once enabled, save the file and update the system using the command:

sudo apt-get update

2. Fetch Source Package

After running the update process, you can then fetch the source package to modify your system.

For example, let us use the tar package. Start by creating a directory to store the source packages:

mkdir apt-rebuilds
cd apt-rebuilds

Next, download the source package using the command:

apt-get source tar
Apt Source Tar

To view the files in the directory:

ls -la
Apt Tar Ls La

3. Check and Install Build Dependencies

The next step involves checking and installig the required build dependencies for the package you wish to rebuild.

Inside the directory location for the source packages, enter the command below to check for the unmet build dependencies.

sudo dpkg-checkbuilddeps

The command will display all the unmet dependencies for the package. Although you can install them manually, a simpler method is to use apt to install the source packages.

To do this, use the command:

sudo apt-get build-dep tar

The command above will fetch the dependencies and install them for you.

Apt Get Build Dep

4. Modify the Package

At this stage, you will want to make the changes to the package and enable or disable any feature you need. (Doing this is a broad aspect, and thus, we cannot possibly cover every change you can make for each package.)

Once you make all the changes and personal tweaks, recompile the source and save it with a different version number. You can do this using the command:

dch --local tar

Running this command will prompt you for your desired editor and launch the changelog for you to edit.

Dhc Local Tar

You can add a few lines to describe the changes made and change the version.

5. Build Package

The final step is to build the source package. Ensure you are in the source package directory and run the command:

dpkg-buildpackage --force-sign

The command will initialize the build process using all the changes made in the above step.

Dpkg Buildpackage

Depending on the changes and the package to rebuild, the process can take a few minutes or longer.

6. Install Package

Once the build process completes, it will generate a binary package in the parent directory. To install, all you have to do is use the dpkg command:

sudo dpkg -i *.deb

In closing

Building packages is a must-have skill for any Linux administrator and a good skill to have as a regular Linux user. Now that you know how to build a package from source, you can also learn how to easily rename files in Linux and how to use and edit the Hosts file in Linux.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.