DEB Package Creation: A Step-by-Step Guide for Ubuntu

In the Linux world, crafting DEB files is essential for software distribution on Ubuntu and Debian-based systems. However, resources for building DEBs with dynamic libraries and shared files are scarce. To bridge this gap, I've created this comprehensive guide.

For the purpose of this guide, let's imagine we already have a test application named 'example-app.' This application comprises a binary, shared libraries (.so), and various assets. We'll use 'example-app' as our sample project to guide you through the process of building a DEB package with dynamic libraries and shared files."

Folder structure

This structured foundation is essential to ensure a smooth and error-free DEB package creation process. Below, we outlined the recommended folder structure you should adopt for this purpose.

example-app
├── DEBIAN
│   ├── control
│   ├── preinst
│   └── triggers
└── usr
    ├── bin
    │   └── example-app
    ├── lib
    │   └── example-app/
    └── share
        └── example-app/

Now, let's delve deeper into the essential folder structure that ensures your package is well-organized and contains the necessary configuration files:

DEBIAN Subfolder

This directory houses crucial configuration files.

control: This file describes the packaged application and defines its dependencies. These dependencies will be automatically installed when the package is installed (with apt install).

Package: example-app
Version: 1.0.0
Architecture: amd64
Maintainer: Name Lastname <email@email.com>
Description: This is example package app.
Depends: dep1, dep2

preinst: A Bash script with instructions executed before the DEB package installation. Ensure it has executable permissions (you can use sudo chmod 755 preinst). In this article, the content of this file will add libraries to the linker cache, allowing the linker to find and link the necessary libraries when the 'example-app' is called.

#!/bin/bash
set -e

CONF_FILE="/etc/ld.so.conf.d/example-app.conf"
LIB_PATH="/usr/lib/example-app"

# Create the directory if it doesn't exist
mkdir -p "$(dirname $CONF_FILE)"

# Add the library path to the configuration file
echo "$LIB_PATH" > "$CONF_FILE"

exit 0

triggers: This file defines triggers that execute after the installation. In our case, the content of this file creates a trigger that refreshes the linker cache, ensuring it loads any newly added libraries.

activate-noawait ldconfig

usr Subfolder

This directory contains the actual application files, organized as follows.

bin: The 'bin' folder holds all application binaries that users will call to use the application. The installation path for these binaries will be /usr/bin.

lib: Within the 'lib' folder, there's a subfolder with the same name as the application ('example-app'). This subfolder houses all the dynamic libraries required for the application to run. These libraries will be installed at the path /usr/lib/example-app.

share: Similarly, within the 'share' folder, there's a subfolder with the application's name ('example-app'). This subfolder contains various asset files required by the application. The assets will be installed at the path /usr/share/example-app.

Build DEB file

Now that we've established the necessary folder structure, we're ready to proceed with the DEB package creation process. To build your DEB file, you'll execute a command from the root of this structured directory. This command will take the contents of the folder structure and package them into a single .deb file, which can then be easily distributed and installed on Ubuntu and Debian-based systems.

dpkg-deb --build example-app

Managing DEB Package

Installation

To install the package, you can use the following command, incorporating the '-f' flag to ensure that the dependencies specified in the configuration file are also installed:

sudo apt install ./example-app.deb -f

Execution

After successfully installing the package, you can initiate the application by executing the following command:

example-app

Removal

To remove the installed application, simply execute the following command:

apt remove example-app
0
Subscribe to my newsletter

Read articles from Milan Lazarević directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Milan Lazarević
Milan Lazarević