A Beginner’s Guide to Creating a Debian Package and Repository for Your Own Tool

Shaon Majumder
3 min readFeb 4, 2024

--

Are you looking to streamline the installation process of your custom tool on Debian-based systems? Creating a Debian package and setting up a local repository can make distribution and installation more convenient. This beginner-friendly guide will walk you through the process step by step.

Step 1: Prepare Your Tool

Open your terminal and navigate to the directory containing your tool. Replace ‘yourpackage’ and ‘yourpackage.sh’ with your tool’s name and main script.

#!/bin/bash

PACKAGE_NAME='yourpackage'
EXECUTABLE_FILE='yourpackage.sh'
REPO_ROOT="yourpackage-repo"

Step 2: Build the Local .deb Package

Create the necessary directories for the Debian package and define its metadata using a control file and building the .deb package.

2.1 Create Directories

mkdir -p $PACKAGE_NAME/DEBIAN
mkdir -p $PACKAGE_NAME/usr/local/bin
  • mkdir -p: Creates directories, and the -p flag ensures that parent directories are created if they don't exist.
  • $PACKAGE_NAME/DEBIAN: This directory will store the control file, which contains metadata about the package.
  • $PACKAGE_NAME/usr/local/bin: This is the standard location for user-installed executables.

2.2 Create the Control File

cat <<EOF > $PACKAGE_NAME/DEBIAN/control
Package: sslcert
Version: 1.0
Architecture: all
Maintainer: Your Name <smazoomder@gmail.com>
Description: A script to simplify the generation and management of SSL certificates. sslcert is a convenient tool for creating and handling SSL certificates for web servers, ensuring a streamlined process for securing your online applications. It supports easy certificate generation, renewal, and customization, making SSL certificate management hassle-free.
EOF
  • cat <<EOF > $PACKAGE_NAME/DEBIAN/control: This uses a "here document" to write multiple lines to the control file.
  • The content between <<EOF and EOF is written to $PACKAGE_NAME/DEBIAN/control.
  • It specifies metadata about the package, such as its name, version, architecture, maintainer, and description.

2.3 Copy the Executable

cp $EXECUTABLE_FILE $PACKAGE_NAME/usr/local/bin/
  • cp: Copies files or directories.
  • $EXECUTABLE_FILE: This is the main script or executable file of your tool.
  • $PACKAGE_NAME/usr/local/bin/: The destination directory where your executable will be copied.

2.4 Set Executable Permissions

chmod +x $PACKAGE_NAME/usr/local/bin/$EXECUTABLE_FILE
  • chmod +x: Adds execute permission to a file.
  • $PACKAGE_NAME/usr/local/bin/$EXECUTABLE_FILE: The path to your executable file.

2.5 Build the .deb Package

dpkg-deb - build sslcert
  • dpkg-deb: A tool for building Debian packages.
  • --build sslcert: Specifies the source directory (sslcert) to build the package.

Step 3: Build the Local Repository

Create a local repository for your package. This involves organizing your .deb package and generating the necessary index files.

3.1 Create Repository Directories

mkdir -p $REPO_ROOT/debian
  • mkdir -p: Creates directories, ensuring that parent directories are created if they don't exist.
  • $REPO_ROOT/debian: This directory will store the Debian package and metadata files for the repository.

3.2 Move the .deb Package to Repository

mv yourpackage.deb $REPO_ROOT/debian/
  • mv: Moves or renames files or directories.
  • yourpackage.deb: The Debian package file is generated in Step 2.
  • $REPO_ROOT/debian/: The destination directory within the repository.

3.3 Generate Package Metadata

dpkg-scanpackages $REPO_ROOT/debian /dev/null | gzip -9c > $REPO_ROOT/debian/Packages.gz
  • dpkg-scanpackages: Generates metadata files required for a Debian package repository.
  • $REPO_ROOT/debian: Specifies the source directory containing the Debian package.
  • /dev/null: Redirects any error output to null, indicating that we don't want error messages.
  • | gzip -9c: Pipes the output of dpkg-scanpackages to gzip with maximum compression.
  • $REPO_ROOT/debian/Packages.gz: The compressed metadata file containing package information.

3.4 Move Repository to Web Server Directory

sudo mv $REPO_ROOT /var/www/html
  • sudo mv: Moves the repository directory to a location accessible by the web server.
  • $REPO_ROOT: The repository directory is generated in Step 3.
  • /var/www/html: A common directory for web server content.

Step 4: Configure the Package Repository

Define the repository URL and update the system’s sources.list file to include your local repository.

4.1 Update APT Repository Configuration

REPO_PATTERN="http://localhost $REPO_ROOT/debian/"
sudo sed -i "\|$REPO_PATTERN|d" /etc/apt/sources.list
  • REPO_PATTERN="http://localhost $REPO_ROOT/debian/": Defines the repository URL pattern.
  • sudo sed -i "\|$REPO_PATTERN|d" /etc/apt/sources.list: Removes any existing entry matching the repository pattern from the APT sources list.

4.2 Add Repository to APT Sources

TEMP_SOURCES_LIST_ENTRY="deb [trusted=yes] $REPO_PATTERN"
echo "$TEMP_SOURCES_LIST_ENTRY" | sudo tee -a /etc/apt/sources.list
  • TEMP_SOURCES_LIST_ENTRY="deb [trusted=yes] $REPO_PATTERN": Defines the new APT sources list entry with trust settings.
  • echo "$TEMP_SOURCES_LIST_ENTRY" | sudo tee -a /etc/apt/sources.list: Appends the new entry to the APT sources list.

Step 5: Update and Install Your Tool

Update the package index and install your tool using the package manager.

sudo apt-get update
sudo apt-get install yourpackage

Congratulations! You’ve successfully created a Debian package, set up a local repository, and installed your tool using apt-get. This method provides a convenient way to distribute and manage your custom tools on Debian-based systems.

--

--

Shaon Majumder
Shaon Majumder

Written by Shaon Majumder

Software Engineer | Author | Data Scientist

No responses yet