Create a Debian package using dpkg-deb tool

Reading Time: 5 minutes

Debian Package management tool dpkg is a low level tool which runs only Debian based Linux distributions. dpkg is used to search, install, remove, purge, unpack etc. the debian package.

In this blog we are going to covers the basics of creating custom .deb packages and managing them with dpkg and apt.

We will also cover the process of creating your own deb package using dpkg-deb tool and install it using dpkg and apt.

This blog is for those peoples who are interested in learning the basics of creating and managing packages in Debian based distributions.

.deb file is a Debian package. The dpkg tool is the Debian package manager and apt is a higher level tool for managing packages which uses dpkg internally.

Now lets go to play with dpkg-deb

Create a package

Now, we are going to create a simple debian package of jenkins.war file (you can download it from here) as a systemd service.

STEP-1

First, we have to create a new directory with any name eg. myfirstdebpkg. This directory will store all the package contents like package metadata ,binaries, configuration file, log files etc.

Inside the project directory we have to create a special directory name as DEBIAN with one special file inside it which name is control without any extention.

creating directory structure
Creating Debian Package Directory Structure

Tree Structure of the directory:

myfirstdebpkg

└── DEBIAN

    └── control

1 directory, 1 file

STEP-2

The next step is to define the metadata of the project in the control file.

Package: myfirstdebpkg 
Version: 1.0 
Section: base 
Priority: optional 
Architecture: all 
Depends: openjdk-8-jdk 
Maintainer: azmathasan92@gmail.com 
Description: run jenkins.war as a service.

These are the important fields in the control file of the package:

Package: Identifies the package name

Version: Version of the package

Section: This field specifies an application area into which the package has been classified.Eg debug, devel, doc, editors, education, electronics, embedded, fonts, gamesetc

Priority: You can set the priority of the package, we have these types: required, important, important, optional, extra

Architecture: You can define the architecture of the application (amd64 ,i386 etc).

Depends: If your package is depends on other package you will mention here, if you install this package from high level tool (apt, aptitude) then it will automatically download its dependencies.
 
Maintainer: who maintains this package

Description: Description of the package

We have created a minimal directory structure of the debian package and we have written control file.

STEP-3

Now, copy our jenkins.war file to the /opt/jenkins/ directory of the package, first we create the directory opt/jenkins directory in the myfirstdebpkg directory , see below

Copy Jenkins File to /opt/jenkins directory
Copy jenkins.war to /opt/jenkins directory
myfirstdebpkg/
 ├── DEBIAN
 │   └── control
 └── opt
     └── jenkins
         └── jenkins.war

 3 directories, 2 files

Now, we are going to write the systemd service unit script to run jenkins.war as a service named jenkins.service file.

 Unit]
 Description=Jenkins Daemon service
 [Service]
 ExecStart=/usr/bin/java -jar /opt/jenkins/jenkins.war --logfile=/var/log/jenkins.log
 SuccessExitStatus=143
 User=knoldus
 [Install]
 WantedBy=multi-user.target

In the above file we create a service unit for the package to run this package as a service and we can control this service using the systemctl tool, we can start, stop, status, enable, disable or check logs of the package.

Example : sudo systemctl status jenkins.

Place this file to the /etc/systemd/system/ directory, we need to create these directories in our package.

copy jenkins.service file to etc/systemd/system/ dir
copy jenkins.service file to etc/systemd/system/ dir
 /home/knoldus/myfirstdebpkg/
 ├── DEBIAN
 │   └── control
 ├── etc
 │   └── systemd
 │       └── system
 │           └── jenkins.service
 └── opt
     └── jenkins
         └── jenkins.war

 6 directories, 3 files

We have mentioned the logging location i.e /var/log/jenkins.log in the jenkins.service file, we have to manage the logs in the /var/log/jenkins.log. We have to create the directories and jenkins.log blank file in the root of the package.

Creating jenkins.log file
Creating jenkins.log file
 /home/knoldus/myfirstdebpkg/
 ├── DEBIAN
 │   └── control
 ├── etc
 │   └── systemd
 │       └── system
 │           └── jenkins.service
 ├── opt
 │   └── jenkins
 │       └── jenkins.war
 └── var
     └── log
         └── jenkins.log

Congratulations, you have successfully created a debian package structure, now we are going to build a debian package from the package structure using dpkg-deb tool:

building debian package
Creating debian package

After dpkg-deb –build operation, it will created a debian package with the .deb extention, you can see in the image below:

View Package
Created .deb package

you have created your own debain package, now times to install it.

You can install the debian package using the dpkg or apt package management tool.

Installing .deb package
Installing .deb package

or you can install using apt

sudo apt install ./myfirstdebpkg.deb

Check the status of the installed service which name as jenkins.service

Service status
Service status

Initially this is inactive, you can start the service using :

start service

Now, your package is running as a daemon service you can start , stop or check status or the package using:

sudo systemctl start jenkins
sudo systemctl stop jenkins
sudo systemctl status jenkins
sudo systemctl restart jenkins
stop service

You can remove this package with dpkg tool

sudo dpkg --remove myfirstdebpkg  //remove application but not configuration files
sudo dpkg --purge myfirstdebpkg   //complete unistall the application with all files
Remove .deb package
Removing .deb package

That’s all for now, I will follow it up with more knowledge on this topic next time.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies

knoldus
dpkg tool

Written by 

Azmat Hasan is a Software Trainee at Knoldus Software LLP. He has done MCA from CDAC Noida in 2019. He has good knowledge of languages C, C++, Java, Scala. He is self-motivated, enthusiastic person who believes in striving to achieve what we can sustain over a longer period of time, instead of working for short term benefits. He believe in working together to create synergy.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!