Creating deb packages

The most popular way to distribute programs on Linux is through software repositories. In the repositories, the programs are stored in a special format. Debian and its based distributions use the deb package format. These packages contain an archive of all the program files, instructions for installing them in the system for the package manager, as well as other service information.

In this article, we will look at how to build a deb package for your program without using any third-party tools. We will not consider signing packages for repositories, because the commands will be different for each type of repository.

  • Creating deb packages
    • Step 1. Preparation
    • 2. Creating a manifest
    • 3. File location
    • 4. Installation Scripts
    • 5. Build and test the package
  • Conclusions

Creating deb packages

Step 1. Preparation

Let’s create a small C program for this example. It will be called hellolosst and will display the string Hello from losst.ru to the terminal at startup. First create the hellolosst folder and navigate to it:

mkdir hellolosst

cd hellolosst

Then put the source code file in it:

vi hellolosst.c

#include 
int main(){
printf("Hello from losst.run");
}

To compile the program, run the following command:

gcc hellolosst.c -o hellolosst

Then you can execute it:

./hellolosst

So now we have a program that needs to be packaged in a deb package.

2. Creating a manifest

Each deb package contains not only the files of the program itself, but also a manifest file that describes the package, its dependencies and parameters. This file is called control and should be located in the DEBIAN folder. To build the package, we will use the package folder, so that the program files are not confused with the source files and they are not included in the package. Create this folder:

mkdir -p package/DEBIAN

Before you can create this file, you need to know a few things. First of all, you need to look at the size of the program files, since in this case there is only one file, just look at its size:

du -k ./hellolosst

If there are several files, you can delete the sources and see the total size of the folder with the program files. Next, you need to understand what packages your program will depend on. To do this, use the objdump command:

objdump -p ./hellolosst | grep NEEDED

In this case, the program only needs libc. To see which package it is in run:

dpkg -S libc.so.6

The package is called libc6. Then create a manifest file with the following contents:

vi package/DEBIAN/control

Package: hellolosst
Version: 1.0
Section: unknown
Priority: optional
Depends: libc6
Architecture: amd64
Essential: no
Installed-Size: 20
Maintainer: losst.ru
Description: Print hello from losst line

This is the minimum set of parameters in the manifest file. Here is their meaning:

  • Package – package name;
  • Version – the version of the program in the package that will be used when updating the package;
  • Section-the category of the package, allows you to determine why it is needed;
  • Priority – the importance of the package, for new packages that do not conflict with anything, usually specify optional, in addition, the values required, important or standard are available;
  • Depends – which packages your package depends on, it can’t be installed until these packages are installed;
  • Recommendations are optional packages, but they are usually installed by default in apt.;
  • Conflicts-the package will not be installed as long as the packages listed here are present on the system;
  • Architecture – the architecture of the system where this package can be installed, the available values are: i386, amd64, all, the latter means that the architecture does not matter;
  • Installed-Size – the total size of the program after installation;
  • Maintainer-specifies who built this package and who is responsible for its support;
  • Description – a brief description of the package.

3. File location

The manifest is ready. Now you need to create a folder structure in the package folder, similar to what is in the root file system. In this case, you need to create a folder usr/bin and put the executable file there:

mkdir -p package/usr/bin

mv ./hellolosst package/usr/bin

4. Installation Scripts

Despite the fact that the package installation system is very powerful and allows you to do a lot, some things still can not be done. To solve this problem, it was possible to run scripts before installing the package and after. This works similarly for deleting a package-before and after. These scripts are called preinst, postinst, prerm, and postrm. Each file simply contains a set of scripts to be executed. For example:

vi package/DEBIAN/postinst

#!/bin/bash
echo "Hello from losst installed"

Debian developers do not recommend using these scripts unless absolutely necessary, because they give you full control over the user’s system and you can accidentally damage something. These scripts are usually used to ask users questions and generate configuration files based on this.

5. Build and test the package

It remains to build the configured package. To do this, use the following command:

dpkg-deb --build ./package

Now you know how to build a deb package. After the build is complete, you can install it using apt:

sudo apt install ~/hellolosst.deb

After that, the executable file of the program will appear in /usr/bin, and the message from postinst will be output after installation.

Conclusions

In this short article, we looked at how to create a deb package using the dpkg-deb tool. As you can see, everything is very simple. You just need to write a manifest and put the files where they should be. We have covered only the basics here. In fact, creating packages is much more complicated. There is a whole set of debhelpers utilities that are used at various stages of building and installing deb packages, you can read more about all this in the official documentation.

Source: losst.ru

(Visited 33 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *