Howto: create a deb package

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
Bas King
Level 1
Level 1
Posts: 17
Joined: Mon Jan 20, 2014 2:38 am

Howto: create a deb package

Post by Bas King »

Creating a package for debian/mint is a little bit confusing for noobs like me, so I did some research and created this howto.
It packages one single command, that just echos some line.

Go to home directory (or any location you have all permissions to)

Code: Select all

cd ~
Create a new folder, testpackage, with inside a folder structure /usr/bin, where the command shall be after install.
The file structure inside 'testpackage' will be re-created in the real file hierarchy, all files included, once the package gets installed.

Code: Select all

mkdir -p testpackage/usr/bin
Create a very simple command, for demonstration purposes. This will just echo a line of text.

Code: Select all

echo '#!/bin/sh
echo "Running Some Command..."' > testpackage/usr/bin/somecommand

Inspect it:

Code: Select all

cat testpackage/usr/bin/somecommand
Should return:
#!/bin/sh
echo "Running Some Command..."
Run it with bash:

Code: Select all

bash testpackage/usr/bin/somecommand
Should return:
Running Some Command...
Run it 'as is'

Code: Select all

testpackage/usr/bin/somecommand
Will return:
bash: testpackage/usr/bin/somecommand: Permission denied
We have already hinted to use #!/bin/sh in the first line (the shebang). But to invoke it directly we also, as superuser, have to set the 'allow execution' flag of the file with chmod: for owner (Read+Write+eXecute=7), group-members (Read+eXecute=5), and the rest (Read+eXecute=5) (See http://en.wikipedia.org/wiki/Chmod#Command_syntax ) like this:

Code: Select all

sudo chmod 755 testpackage/usr/bin/somecommand 
Run it 'as is' again:

Code: Select all

testpackage/usr/bin/somecommand
Should now return:
Running Some Command...
Try to build it:

Code: Select all

dpkg-deb --build testpackage
Returns:
dpkg-deb: error: failed to open package info file `testpackage/DEBIAN/control' for reading: No such file or directory
To create control file, first create DEBIAN folder:

Code: Select all

mkdir -p testpackage/DEBIAN
Then create control file there (use your email and name):

Code: Select all

echo 'Package: testpackage
Version: 1.1-1
Section: education
Priority: optional
Architecture: all
Maintainer: Your Name <your@email.com>
Description: Almost empty test package.
 This package is only used
 to show how packages work.' > testpackage/DEBIAN/control
Try to build package again:

Code: Select all

dpkg-deb --build testpackage
Should return:
dpkg-deb: building package `testpackage' in `testpackage.deb'.
So, all is fine? Not so! Lets install lintian:

Code: Select all

sudo apt-get install -y lintian
And inspect our package with it

Code: Select all

lintian testpackage.deb
Will return:
E: testpackage: debian-changelog-file-missing
E: testpackage: no-copyright-file
E: testpackage: wrong-file-owner-uid-or-gid usr/ 1000/1000
E: testpackage: wrong-file-owner-uid-or-gid usr/bin/ 1000/1000
E: testpackage: wrong-file-owner-uid-or-gid usr/bin/somecommand 1000/1000
W: testpackage: binary-without-manpage usr/bin/somecommand
To handle the E: testpackage: debian-changelog-file-missing, create folder /usr/share/doc/PACKAGENAME inside package:

Code: Select all

mkdir -p testpackage/usr/share/doc/testpackage
Create initial changelog, note: SPACES MATTER! 2 spaces before each *, 1 space before --, 2 spaces between email and date like so:

Code: Select all

echo 'testpackage (1.1-1) unstable; urgency=low
  * Initial Release. Closes: #14223
 -- Your Name <your@email.com>  Fri, 11 Apr 2014 08:38:32 +0100' > testpackage/usr/share/doc/testpackage/changelog.Debian
Must be gzipped with maximum compression:

Code: Select all

gzip -9 testpackage/usr/share/doc/testpackage/changelog.Debian
To handle the E: testpackage: no-copyright-file, I choose GPL-2: https://www.gnu.org/licenses/license-li ... reLicenses
(no compressing is allowed on this file, it should be readily readable):

Code: Select all

echo 'This package was first put together by Your Name <your@email.com> in 2014.

testpackage is Copyright (C) 2014 Your Name <your@email.com>.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the
    Free Software Foundation, Inc.,
    51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in "/usr/share/common-licenses/GPL-2".' > testpackage/usr/share/doc/testpackage/copyright
Now the W: testpackage: binary-without-manpage usr/bin/somecommand

Code: Select all

mkdir -p testpackage/usr/share/man/man1

Code: Select all

echo '.\" Manpage for somecommand.
.\" Contact your@email.com to correct errors or typos.
.TH SOMECOMMAND 1 "11 Apr 2014" "1.1-1" "somecommand man page"
.SH NAME
somecommand \- just some command.
.SH SYNOPSIS
.B somecommand
.SH DESCRIPTION
Somecommand is an stub command to show how a packages works. It simply echoes "Running Some Command..."
.SH OPTIONS
Somecommand does not take any options.
.SH BUGS
No known bugs.
.SH AUTHOR
Your Name (your@email.com)' > testpackage/usr/share/man/man1/somecommand.1
Check to see if the manpage makes any sense:

Code: Select all

man -l testpackage/usr/share/man/man1/somecommand.1
Zip with maximum compression:

Code: Select all

gzip -9 testpackage/usr/share/man/man1/somecommand.1
Now the wrong-file-owner-uid-or-gid errors: as superuser, CHange the OWNer and group of all files in the package folder to root:root, recursively (-R):

Code: Select all

sudo chown -R root:root testpackage
Rebuild

Code: Select all

dpkg-deb --build testpackage
And inspect our package

Code: Select all

lintian testpackage.deb
Should return: nothing!
No errors or warnings! Hurray!

Now we can install package as superuser (admin):

Code: Select all

sudo dpkg -i testpackage.deb
Try out our command:

Code: Select all

/usr/bin/somecommand
Should now return:
Running Some Command...
Check your PATH variable for /usr/bin

Code: Select all

echo $PATH
Normally /usr/bin is already in your PATH, so you can run commands from /usr/bin from anywhere without having to give the full path. Just try:

Code: Select all

somecommand
Should also return:
Running Some Command...
And try the manual-page for it like so:

Code: Select all

man somecommand
Post Reply

Return to “Tutorials”