Linux

How to rebuild a package from source

This article provides simple steps to rebuild a package from the source.
Captain Salem 3 min read
How to rebuild a package from source

The ability to rebuild a package from the source can benefit any Linux power user. It allows you to perform package changes, enable or disable a feature or even apply custom modifications.

The processes and steps are illustrated on a Debian 10 distribution.

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 by 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 is the contents of 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 telling the package manager to give use the source package and not the usual binary file.

Once enabled, save the file and use apt to update using the command:

sudo apt-get update

Fetch Source Package

After you run apt-get update, you can fetch the source package to modify for your system.

For example, let us use the tar package as an example.

Start by creating a directory to store the source packages.

mkdir apt-rebuilds
cd apt-rebuilds

Next, download the source package by using the command:

apt-get source tar
img

The above command should download the source packages for the tar command and store them in the current directory.

To view the files in the directory, use the ls command as:

ls -la
img

Check and Install Build Dependencies

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

Inside the source packages' directory, 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 more straightforward method is to use apt to install the source packages for you.

To do this, use the command:

sudo apt-get build-dep tar

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

img

Modify the package.

At this stage, you will want to make the changes to the package and enable or disable any feature you need. This is a broad aspect, and not possible to cover what you can change for each package.

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

dch --local tar

This will prompt you for your desired editor and launch the changelog for you to edit.

img

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

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.

img

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

Install Package

Once the build process is completed, it will generate a binary package in the parent directory. To install, 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 for a regular Linux user. This guide walks you through how to do so in simple steps.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.