In this tutorial we will see:

  • How to create a basic debian package from a binary file
  • How to create a local debian repository
  • How to add the repository to the list of software sources

PS: let´s suppose we have a helloword C++ program (helloword.cpp). we compile the code and obtain an executable called “helloword” which prints some string on the screen.

First of all, we need to create the debian package structure. The only files required to build a debian package are:

  • DEBIAN/control
  • custom files to be part of the package (not required)

Step 1: Create the directories

Create a directory for our package and create a directory DEBIAN.

mkdir helloworld && mkdir helloworld/DEBIAN

Step 2: Copy files into our package

Inside the helloworld directory we create a directory tree which represents the path where our program will be installed in the system, and copy the executable into it:

let´s assume we want to put our apps in /usr/local/bin/ , let´s put it in helloworld/usr/local/bin/

mkdir -p helloworld/usr/local/bin 
cp /path/to/helloworld helloworld/usr/local/bin/

Step 3: Create the control file (Required)

Let´s create a file ‘control’ in the DEBIAN directory

Package: helloworld
Version: 0.2
Maintainer: Brice Nguenkam
Architecture: all
Description: Print hello world  on the screen 

These are the mandatory fields in the control file. For more options see http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-binarycontrolfiles

At this point we are ready to create/build the package. So we can skip step 4 & 5, if we don´t need it.

Step 4: Add a post-installation script (Optional)

We can create a file ‘postinst’ in the DEBIAN directory. Make sure it is executable. It will run when the installation is complete.

PS: In general, we can also add preinstpostinstprerm, and postrm files in the DEBIAN directory. They are all executable scripts that Debian automatically runs before or after package installation.

Step 5: Add an installation script (Optional)

If specifics(extra) files need to be installed in the package but normal “make install” (Makefile or other install system) does not do it, then we should add the filenames and their destination to this install file.

The syntax is simply:

path/sourcedir/file path/installdir
# for example
data/foo.jpg usr/share/packagename

In This script , for every file we want to install, we will write one line with the name of the file (relative to the main build directory) followed by a space, then the installation directory (relative to the install directory).

For example, let us suppose a ‘src/fis-pack’ binary is not installed, the install file might look like:

src/fis-pack usr/bin

This means that when installing the package, there will be an executable command /usr/bin/fis-pack.

PS: It’s normally unnecessary unless you specifically need an empty directory or if there is a problem with the source Makefile

Step 6: Create the package

To do that we use the dpkg-deb tool.

$ dpkg-deb --build helloworld
dpkg-deb: building package `helloworld' in `helloworld.deb'.
$ ls
helloworld  helloworld.deb

This creates a helloworld.deb file, which we can now install on any Debian installation with following command:

dpkg -i helloworld.deb

ps: You may want to change the name of the package so that it includes the program version and the package architecture. For example: (packagename_VersionNumber_DebianArchitecture).

$ mv helloworld.deb helloworld-0.2_all.deb

//or if we want to set a specific architectur
$ mv helloworld.deb helloworld-0.2_amd64.deb

There are various platforms : amd64, arm64, etc.  X64, amd64 and x86-64  are names for the same processor type.

eg: Tree structure of a project with the Debian package

Setting up a local package repository

To create a local package repository we need a working. In this case we will assume the use of Apache with default settings. To install the Apache Webserver, all we need to do is to run:

$ sudo apt-get install apache2

Once installed, to verify that the webserver works, we can navigate to the IP address of the machine (or to ‘http://localhost’, if running a browser on the machine itself) which,  in our case is http://10.1.1.4. We should see the
famous It works! message.

The web server software is running but no content has been added, yet. The DocumentRoot of the default Apache VirtualHost, is /var/www/html: this is where we will create our repository.

Let’s create the “debian” directory inside /var/www/html and copy the helloworld-1.0_amd64.deb package inside it:

$ sudo mkdir /var/www/html/debian
$ sudo cp /path/to/helloworld-0.2_amd64.deb /var/www/html/debian/

The next step consists in the generation of a package list. We move into the debian directory, and use the dpkg-scanpackages utility to accomplish the task. You may need to install the dpkg-dev package in case the dpkg-scanpackages command is missing:

$ dpkg-scanpackages . | gzip -c9  > Packages.gz
dpkg-scanpackages: info: Wrote 1 entries to output Packages file.

Our local repository is now ready.

Adding the repository to the software sources

At this point to be able to install our package from the local repository we created, we need to edit the /etc/apt/sources.list file, add the entry relative to it (change IP address to reflect that of your machine), and
synchronize the repositories:

echo "deb [trusted=yes] http://10.1.1.4/debian ./" | tee -a /etc/apt/sources.list > /dev/null

Be sure to add the above [trusted=yes] to avoid the following error message:

Release' does not have a Release file.                   
N: Updating from such a repository can't be done securely, and is therefore disabled by default.

Synchronize repositories:

$ sudo apt-get update

To install our package, we can now use the apt-get tool:

$ sudo apt-get install helloworld

Execute:

$ helloworld

To remove the package from the system, just run:

$ sudo apt-get remove linuxconfig

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

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