How to Build Package in Linux Based Systems.

Introduction

I will explain how to prepare a Debian package that all linux users can understand in this article. Debian based Linux systems although there is a huge package repository, sometimes it may be necessary to install a package which is not in the repository. In such a case, as you can install the application from source code, you can also install the application directly from the package manager by making it a .deb package.

Sometimes, a package in a package repository may not be enough for us to install values, so we want to prepare the own package. For example, There is a squid3 package and it is ready to install in the package repository but with this install, the sslbump feature may don’t come that we may need for squid. While installing from the source code also another way is making it as a ( .deb ) package.

 

What is Package System and Benefits

Linux users and system managers need programs, drivers etc. They benefit from the package repository to install dependencies with system tools.

The ready to install setups are expressed as a package for users install a package and managing easily, ( updates, remove, version etc.)

For example, when you would like to install chrome application in your Linux system ( debian, ubuntu, etc. ). You just find the install link for chrome, download into the system and without having to waste time setting up after the preconditions is completed. Using the ready to install packages already in the package repository can save important time and prevent any possible errors. Installing with “apt-get install chromium” takes seconds.

 

Known Package Management Systems on Linux Systems

apt = The goal of APT is to make life easier for users who use Debian based linux distributions and to make system package management more understandable.

yum = This is using for package management in red-hat based distributions. The yum features include: manage package, install package, remove a package, information of package, update, etc.

aptıtude = This package manager was using especially in uninstalling dependencies also it was good package manager at a long time ago. They all have the same function except for some small usage changes.

pkg = This is the new package manager and it came with FreeBSD 10 operation system. The pkg features include: install package, remove, update, lock/unlock the package.

 

Known Package Types On Linux Systems

Below is a list of mostly used package types in Linux based systems.

.rpm = This package type is developed by Red Hat also it has many powerful features and it is adapted to many linux distributions.

.deb = This package type is also created for Debian based systems.

 

Create Package for Debian Based Systems

What is Debian (.deb) Package?

Debian packages are the packages that contain all the files necessary to apply the commands or features.

Debian packages are divided to two;

Binary packages: This package contains the executive files, configuration files, man/info pages and others documentations.

Source packages: Sources packages, the .dsc file that specifies of this package. .orig.tar.gz file containing the original source code in the archive and .diff.gz file containing the debian-specific changes in the file.

 

What is Dependence?

Dependency is the package needed for the package to work/compile.

There are two different dependence;

Build Dependency: Packages needed for package compilation.

Runtime Dependency: Packages needed for the package to work.

 

Preconditions for Creating a Debian (.deb) Package

We need to install some tools for create/prepare a debian package. Be sure you installed properly the tools. First, we should update the system repository after that we may use the following second command to install the tools what we need.

Note: Make sure that tools installed properly on the system. You can use this command for check ( dpkg -s package_name ).

root# apt-get update ; apt-get upgrade -y 
root# aptitude install build-essential binutils fakeroot lintian debhelper dh-make devscripts patch libc6-dev file checkinstall makefile

 

Install From Source Code

The bzip2 tool is has used in this article as an example tool. In this example, I install bzip2 tool from the source code into the system and then we make it as a .deb package.

While I was preparing this article, I worked on this system. The commands, dependencies may can be changed in differents linux systems.

Operating system features used;

Distributor ID: Debian
Description : Debian GNU/Linux 8.1 (jessie)
Version : 8.1
Code name : jessie

Let’s get it start.

First, we should create a directory in a random directory then enter into the directory and after that install the bzip2 example tool. In this example, a directory called “test” is created in the /home directory and entered in the directory after that installed the tool in this directory.

root# cd /home/ ; mkdir test ; cd test
root# wget http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz

First command going to open the bzip2.tar.gz file with tar command into the /home/test directory and then we enter the directory that opens.

root# tar xvf bzip2-1.0.6.tar.gz -C /home/test/
root# cd /home/test/bzip2-1.0.6

Usually, many program’s source codes comes with the INSTALL and README files. If you don’t know what is the program doing, how to install, how works, dependencies etc, it is so helpful to read these files.

Note: When you using the following commands, you should be in bzip2-1.0.6/ directory.

root#  make
root#  make install

 

Create Debian (.deb) Package

We installed the bzip2 tool and we may use the following command convert this tool to the .deb package. In the menu below we can edit the necessary areas by entering the number of the information that we want to change.

Note: I didn’t use dh_make command because it was too old, that’s why I decided to use debmake or checkinstall tools. These are so easy to use and so useful.

root# checkinstall -D make install
****************************************
*** Debian package creation selected ***
****************************************
This package will be built according to these values:
0 -  Maintainer: [ [email protected] ]
1 -  Summary: [ This is my simple .deb package ]
2 -  Name: [ bzip2 ]
3 -  Version: [ 1.0.6 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ bzip2-1.0.6 ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ bzip2 ]
Enter a number to change any of them or press ENTER to continue:
Installing with make install...

========================= Installation results =================
if ( test ! -d /usr/local/bin ) ; then mkdir -p /usr/local/bin ; fi
if ( test ! -d /usr/local/lib ) ; then mkdir -p /usr/local/lib ; fi
if ( test ! -d /usr/local/man ) ; then mkdir -p /usr/local/man ; fi
chmod a+r /usr/local/man/man1/bzmore.1
chmod a+r /usr/local/man/man1/bzdiff.1
echo ".so man1/bzgrep.1" > /usr/local/man/man1/bzegrep.1
======================== Installation successful ===============
Copying documentation directory...
./
./LICENSE
./README
./CHANGES
Copying files to the temporary directory...OK
Compressing man pages...OK
Building file list...OK
Deleting temp dir...OK
********************************************************
Done. The new package has been installed and saved to
/home/test/bzip2-1.0.6/bzip2_1.0.6-1_amd64.deb
You can remove it from your system anytime using:
 dpkg -r bzip2
******************************************************** 

You see the above results that mean the bzip2 tool is prepared. We can access the package from this way ( /home/test/bzip2-1.0.6/bzip2_1.0.6-1_amd64.deb ). If we want to remove it that just we can use “dpkg -r bzip2” command.

 

Testing The Debian (.deb) Package

Package we have prepared, we can use scp, filezilla etc. tools send to another debian based system for test and you can use the following commands to install package for test that we have prepared. At the end of this, we will save time and we will not have to problems every time to install from source code.

root# dpkg -i bzip2_1.0.6-1_amd64.deb

 

Updating an Existing Debian (.deb) Package.

If we want to take updates that we have prepared our own package, we can use the following command.

root# apt-get install apt-file -y

 


Creating a Manual Debian (.deb) Package

In this part of the article, we will not use the source code of the tool. We will create our own files and use bash to write the necessary commands into these files and prepare our own .deb package.

Before start the topic, let’s look at the files what files are doing.

DEBIAN/control: This file what we will write program name, version, size, description, dependencies of the file. Here is the contents of this file.

 

Package : Name of the application.

Version : Version of the application.

Source : The name of the application can be written.

Section : Which category the application entered.

Priority : The necessity of the application for the system.

Maintainer : The name or email address of the developer.

Architecture : Which architects are for.

Depends : Which packages it depend on. We separate to package names with commas (,).

Description : We can may write here a few sentences about what application doing.

 

DEBIAN/preinst : This file is the first start running while application is being installed. Backup commands are usually written into this file when you update. You can write these commands with bash or another language.

DEBIAN/postinst : This file is start after preinst file. Commands are written into this file for the operations to be performed after installing the application.

DEBIAN/postrm or DEBIAN/prerm: Commands are written into this one of the both files for delete the application.

DEBIAN/copyright : This file is contained the about package license information for the debian system. You can review the license information section at (https://www.debian.org/social_contract.en.html)

DEBIAN/rules : This is a Makefile for your debian package. Install a debian package with dpkg command by to the information in this file.

DEBIAN/changelog : This file is a logbook describing the development of the program. If you do something about program ( new features, resolved bugs, etc. ) you can write into this file.

DEBIAN/readme & DEBIAN/install : Actually many linux users knows that what these files are doing :). Usually the author is write information into this file about the how to install program or what is this program doing etc.

 

Let’s get it start.

First, a directory named test is created in the /home directory.

root# mkdir /home/ibrahim/test

And then create a file called file.tar.gz, which contains a test.txt file. After that use the following command create a file called tmp in the test directory and when we create a file called file.tar.gz, it is sent into the tmp directory.

Note: When you send the file which called file.tar.gz, it shouldn’t be open. If you want to see the contains of the file.tar.gz use this command ( tar tf file.tar.gz )

root# mkdir /home/ibrahim/test/tmp
root# cp /home/file.tar.gz /home/ibrahim/test/tmp/

Now we will use the following first command to create a file which is called DEBIAN into the /home/ibrahim/test/ directory. (The name must be DEBIAN) After that we enter the directory which is called DEBIAN and create the necessary files in this directory.

root# mkdir /home/ibrahim/test/DEBIAN
root# cd /home/ibrahim/test/DEBIAN
root# touch control postinst prerm
root# ls -l

 

Control File

This file what we will write program name, version, size, description, dependencies of the file. Here is the contains of the file. After writing as below, you can press CTRL+O followed by the enter key to save it, and then press CTRL+X to exit.

Note : In this area you can add or modify new areas by to your own.

root# nano control

Package: test
Version: 1.1.1
Architecture: all
Essential: no
Section: webPriority: optional
Depends: php5 (>=2.3), php5-mysql
Maintainer: İbrahim UÇAR
Description: This is my example .deb package.

 


Preinst File

This file is start first running when a program installed. Backup commands are usually written into this file when you update. This file isn’t in my example that’s why I won’t use this file in article. If you want check the program updates, you should create this file and write necessary commands into this file.

 

Postinst File

This file start running after preinst file. This file contains the operations to be performed after the application is installed. Let me explain the topic for example after we installed the package into the system, a directory will create which called is test into the /tmp directory. Then the file named file.tar.gz will be opened in the /tmp/test directory which is created. Copy all the sentences and paste it directly.

root# nano postinst

#!/bin/sh
FILE=file.tar.gz
DIRECTORY=test

mkdir -p "/tmp/$DIRECTORY"
tar xf "/tmp/$FILE" -C "/tmp/$DIRECTORY/"

 

Postrm & Prerm Files

These two files name are different but they are doing the same job. We will write commands into this one of the two files for remove the package from the system. We are writing the below commands into the which named is prerm file. This file will start, when a user has used ( dpk -r test.deb , apt-get remove test ) these commands to remove the package from the system. The contents of the file are as follows.

Note: In my example, deleting the directory /tmp/test test is included. And bash command is written into this file.

root# nano prerm

#!/bin/sh
if [ "$1" = "remove" ]; then
       rm -rf /tmp/test
fi

Now It’s time to build .deb package. We will complete the mission and have own .deb package when we use the following commands.

  • In the first command, we created the .deb package with the existing directory name.
  • In the second command, we created the .deb package with another name.
root# dpkg-deb --build /home/ibrahim/test/
dpkg-deb: building package `test in `test.deb'.
root# ls
test  test.deb
root# dpkg-deb --build /home/ibrahim/test/  test.1.2.3.deb
dpkg-deb: building package `test in `test.1.2.3.deb'.
root# ls
test  test.1.2.3.deb

Now we can use the following command for install the .deb package into the system.

root# dpkg -i test.deb
or
root# dpkg -i test.1.2.3.deb

If we want to remove the .deb package from the system, we can use the following commands.

root# dpkg -r test.deb
or
root# dpkg -r test.1.2.3.deb

 

Test Time

In this stage, we will test own package is working or not by made manually. You can follow the steps below.

After you had used below command, a directory will create which called is test into the /tmp directory. Then the file named file.tar.gz will be opened in the /tmp/test directory which is created.

As you can see output below, everything works correctly. The necessary file was opened successfully where necessary directory. But If you can’t see the output as below as in your system this means you missed something at somewhere.

root# dpkg -i test.1.2.3.deb
(Reading database ... 130889 files and directories currently installed.)
Preparing to unpack test.1.2.3.deb ...
Unpacking test (1.4.2) ...
Setting up test (1.4.2) …
root# ls -alh /tmp/test/ibrahimucar/
-rwxrwxr-x 1 ibrahim ibrahim 67 Nov  6 04:37  test.txt

If we want to remove the package which is installed into the system. We can use the following commands. Then you can look at the /tmp/test/ibrahimucar directory and you can see that there is no such like this file.

root# dpkg -r test.1.2.3
( Reading database ... 130889 files and directories currently installed.)
Preparing to unpack test.1.2.3.deb ...
Unpacking test (1.4.2) ...
Setting up test (1.4.2) …
root# ls -alh /tmp/test/ibrahimucar/
ls: cannot access /tmp/test/ibrahimucar/:  No such file or directory

 

Displaying The Directory Structure

After all the process are finished, we can use the following command if we want to see the directory structure.

root# tree test

├── DEBIAN
│   ├── conffiles
│   ├── control
│   ├── postinst
│   └── prerm
└── tmp
    └── file.tar.gz
2 directories, 5 files

You can also access to this article from on my Slideshare account. <3

Tagged with: , ,

Leave a Reply

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

*

eighteen − three =

Archives

Tweets