DPKG

Post image

What is DPKG?

DPKG is a low-level system tool used to extract, analyze, unpack, install, and remove .deb files on Debian-based Linux distributions.

Debian Files

A .deb file is an archive that contains packaged data. It provides a standard way to distribute and install software on Debian and its derivatives (such as Ubuntu).

Anatomy of a Deb Package

The naming convention for .deb files follows this format:

<name>_<version>-<revision>_<architecture>.deb

Where:

  • <name> — the name of the application
  • <version> — the version number of the application
  • <revision> — the revision number of the deb package itself
  • <architecture> — the target hardware architecture (e.g., amd64, arm64)

For example, a program called hello, version 1.0, built for 64-bit ARM processors, would be named: hello_1.0-1_arm64.deb.

Building a Debian Package

1. Create the Package Directory

Start by creating a directory that follows the Debian package naming format:

mkdir hello_1.0-1_arm64

2. Create the Directory Structure

The package directory mirrors the root filesystem of the target system. Create the appropriate directory structure and copy your executable into it:

mkdir -p hello_1.0-1_arm64/usr/local/bin
cp ~/YourProjects/Hello/hello hello_1.0-1_arm64/usr/local/bin

3. Create the Control File

The control file resides inside a DEBIAN directory (uppercase). Note that a lowercase debian directory is used for source packages — these are not the same thing.

mkdir hello_1.0-1_arm64/DEBIAN
touch hello_1.0-1_arm64/DEBIAN/control

4. Fill in the Control File

The control file is a list of key-value fields that describe the package. The following fields are mandatory for binary packages:

  • Package — the name of the program
  • Version — the version of the program
  • Architecture — the target architecture
  • Maintainer — the name and email of the package maintainer
  • Description — a brief description of the program

Example:

Package: hello
Version: 1.0
Architecture: arm64
Maintainer: Internal Pointers <[email protected]>
Description: A program that greets you.
 You can add a longer description here. Mind the space at the beginning of this paragraph.

The control file can also include additional fields such as the section it belongs to or a dependency list. The dependency list is especially important if your program relies on external libraries.

5. Build the Package

With everything in place, invoke dpkg-deb to build the package:

dpkg-deb --build --root-owner-group <package-dir>

For our example:

dpkg-deb --build --root-owner-group hello_1.0-1_arm64

The --root-owner-group flag ensures that all files in the package are owned by root, which is the standard practice. Without it, files would be owned by your current user — which may not exist on the target system.

If successful, this generates a .deb file in the current directory. Make sure dpkg-deb is installed on your system before running this command.

Testing and Installing the Package

After building, it’s good practice to verify that the package installs and uninstalls cleanly.

Install the package:

sudo dpkg -i <APP_NAME>

Remove the package:

sudo dpkg -r <APP_NAME>

Remove the package along with its configuration files:

sudo dpkg -P <APP_NAME>

Verify the package has been fully removed:

dpkg -l | grep <APP_NAME>

If the output is blank, the package was removed successfully.

Maintainer Scripts

DPKG supports four lifecycle scripts known as maintainer scripts: preinst, postinst, prerm, and postrm. These files live inside the DEBIAN directory and run at specific stages during installation and removal:

  • preinst — runs before installation
  • postinst — runs after installation
  • prerm — runs before removal
  • postrm — runs after removal

These scripts must be executable, with permissions set between 0555 and 0775.

Resources

You May Also Like