Posted on
Administration

Building DEB packages from source

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Building DEB Packages from Source: A Comprehensive Guide

In the world of Linux, managing packages and software installations is a routine task. DEB packages are a common format used by Debian and its derivatives like Ubuntu. While most users install packages from precompiled binaries, there are times when you might need to build a package from source. This can be due to the need for a specific configuration, the latest features, or patches that are not available in binary form. Here, we'll walk through how to build DEB packages from source and provide guidance for managing them across different Linux distributions using apt, dnf, and zypper.

Step 1: Setting Up Your Environment

Before you begin, ensure that your system is prepared with the necessary tools. You will need dpkg-dev, build-essential, and devscripts, which can be installed from the repository.

  • For Debian/Ubuntu systems, use apt:

    sudo apt update
    sudo apt install dpkg-dev build-essential devscripts
    
  • For Debian derivatives like Ubuntu, specifically using apt-get:

    sudo apt-get update
    sudo apt-get install dpkg-dev build-essential devscripts
    

Although distributions like Fedora or openSUSE primarily use rpm packages, it’s still possible to build DEB packages on them for experimental or cross-distribution purposes. The focus here, however, is primarily on Debian-based systems.

Step 2: Downloading Source Package

You can obtain source packages (*.dsc files along with accompanying files) from the Debian repository or the software's official site. For demonstration, we will use a generic package named example-package.

  • First, ensure you are in a suitable directory:

    mkdir ~/source-packages
    cd ~/source-packages
    
  • Download the source package using apt on Debian-based systems:

    apt source example-package
    

Step 3: Understanding the Source Package

The downloaded source package typically consists of three files: 1. .dsc - Debian source control file, which contains metadata. 2. .tar.gz or .tar.xz - Original source archive. 3. .debian.tar.xz - Debian-specific modifications.

Examine these files and make any necessary modifications to the source code or Debian scripts.

Step 4: Building the DEB Package

Now, navigate to the package source directory and build the package. This step involves configuring the package with the correct dependencies and compiling it.

  • Navigate to the source directory:

    cd example-package-*
    
  • Build the package using dpkg-buildpackage:

    dpkg-buildpackage -us -uc
    

This process may take some time depending on the complexity of the package. The -us and -uc flags disable the signing of the source and changes file, respectively.

Step 5: Installing the Built Package

Once the build process is complete, you should have one or more .deb files in the parent directory.

  • Install the package using dpkg: bash sudo dpkg -i ../example-package_*.deb

Step 6: Managing Built Packages

Managing built packages varies slightly across package managers.

  • On Debian/Ubuntu systems with apt: To ensure that your manually built package is not inadvertently upgraded during a system update, you can hold the package: bash sudo apt-mark hold example-package

While dnf and zypper are not typically used for handling DEB packages, knowing how to prevent automatic updates is equally important in distributions that use these tools:

  • Using dnf (Fedora, RHEL, CentOS):

    sudo dnf versionlock add example-package
    
  • Using zypper (openSUSE):

    sudo zypper addlock example-package
    

Conclusion

Building DEB packages from the source allows for a tailored software environment with fine control over the configurations and installed versions. This guide should help you not only to craft your DEB packages but to manage them efficiently across various package managers. Whether for a personal project or professional deployment, mastering this process increases your versatility as a Linux user and administrator.