How to Create and Install Your Own Debian Package on Debian 12 Bookworm

Learn how to create and install a custom Debian package on Debian 12 Bookworm.

Creating a Debian package (.deb) allows users to package software, scripts, or any files into an installable format that can be easily managed by the package manager. Debian 12 (Bookworm) follows the traditional Debian packaging system with a few enhancements. This guide walks through the complete process of creating and installing a Debian package on Debian 12 Bookworm.

Prerequisites

Before you start, ensure you have the following installed on your Debian system:

  1. Essential development tools:

    sudo apt update && sudo apt install build-essential fakeroot devscripts dh-make
    
  2. Version control (optional but recommended):

    sudo apt install git
    
  3. Lintian (for checking package correctness):

    sudo apt install lintian
    

Step 1: Set Up a Working Directory

Create a directory to store your package files:

mkdir -p ~/debian-packages/hello-world
cd ~/debian-packages/hello-world

Step 2: Create the Source Code

For this tutorial, let’s create a simple shell script that prints “Hello, World!”

mkdir -p hello-world-1.0/usr/local/bin
cd hello-world-1.0/usr/local/bin

cat <<EOF > hello-world.sh
#!/bin/bash
echo "Hello, World!"
EOF

chmod +x hello-world.sh
cd ~/

Step 3: Set Up the Debian Package Structure

Navigate to the package root directory:

cd ~/debian-packages/hello-world
mkdir -p hello-world-1.0/DEBIAN

Step 3.1: Create the control File

Inside DEBIAN, create a control file that defines package metadata:

touch hello-world-1.0/DEBIAN/control
nano hello-world-1.0/DEBIAN/control

Add the following content:

Package: hello-world
Version: 1.0
Architecture: all
Maintainer: Your Name <your.email@example.com>
Description: A simple Hello World script packaged for Debian
Depends: bash
Priority: optional
Section: misc

Step 3.2: Create the postinst and prerm Scripts (Optional)

The postinst script runs after installation. Create the file:

touch hello-world-1.0/DEBIAN/postinst
chmod +x hello-world-1.0/DEBIAN/postinst
nano hello-world-1.0/DEBIAN/postinst

Add the following content:

#!/bin/bash
ln -s /usr/local/bin/hello-world.sh /usr/bin/hello-world

Similarly, create a prerm script to remove the symbolic link when uninstalling:

touch hello-world-1.0/DEBIAN/prerm
chmod +x hello-world-1.0/DEBIAN/prerm
nano hello-world-1.0/DEBIAN/prerm

Add the following content:

#!/bin/bash
rm -f /usr/bin/hello-world

Step 4: Build the Debian Package

Once the directory structure and control files are in place, build the package using dpkg-deb:

dpkg-deb --build hello-world-1.0

This will generate a .deb package in the current directory:

ls -l hello-world-1.0.deb

Step 5: Install and Test the Debian Package

To install the package, use:

sudo dpkg -i hello-world-1.0.deb

Verify installation by running:

hello-world

If you encounter dependency issues, fix them with:

sudo apt-get install -f

Step 6: Verify the Package with Lintian

To check for potential packaging errors, run:

lintian hello-world-1.0.deb

Step 7: Uninstalling the Package

To remove the package, use:

sudo dpkg -r hello-world

Conclusion

By following this guide, you have successfully created a simple Debian package on Debian 12 Bookworm. You can apply this process to more complex applications by organizing files, adding dependencies, and refining package metadata. Mastering Debian packaging is valuable for developers who want to distribute their software efficiently within Debian-based systems.