Debian Packaging Guide (NodeJs)

Reading Time: 6 minutes

In this Blog, we will package a nodejs module to the Debian Standards and by the end of this blog, you will have a package that you can ask the Debian Developers to have in the archive of Debian.

Debian is a universal Linux based operating system, currently using the Linux or the FreeBSD kernel. Debian also forms the base for one of the most commonly known Linux distribution, Ubuntu. Debian Based Operating Systems like Debian, Ubuntu, and it’s flavours have a built-in packaging system that allows users to install applications with just a command, sudo apt install, feeling nostalgic yet?

The need for people is application software, programs that help them do what they want, from editing documents to running a business to playing games to writing more software and more. Debian comes with over 51,000 packages (precompiled software that is bundled up in a nice format for easy installation on your machine), a package manager (APT), and other utilities that make it possible to manage thousands of packages on thousands of computers as easily as installing a single application. All of it free.

In this article, I’ll be guiding you to the basics of What packaging in Debian is and a basic how-to tutorial on the same.

It’s like a tower. At the base is the kernel. On top of that are all the basic tools. Next is all the software that you run on the computer. At the top of the tower is Debian carefully organizing and fitting everything so it all works together.

What is an Operating system ?

An operating system is the set of basic programs and utilities that make your computer run. At the core of an operating system is the kernel. The kernel is the most fundamental program on the computer and does all the basic housekeeping and lets you start other programs.

What is meant by Debian Packaging?

Ever done sudo apt install and wondered how apt install works and what file it fetches and how the file gets auto-installed? This is where Debian Packaging comes into play. The aim of packaging is to allow the automation of installing, upgrading, configuring, and removing computer programs for Debian in a consistent manner.

In simple words, debian packaging is a process of creating .deb files, that are same as .exe installer files in Windows.

By doing Debian packaging we make Debian packages, which is a collection of files that allow for applications or libraries to be distributed via the Debian package management system allowing a user to easily install the packaged application with just a command. To read more about Debian Packaging, you can refer to the official Debian Packaging Document.

Enough with the theory, let’s do some hands-on!

Prerequisites

To start with Debian packaging, we need a Debian SID environment.

Are you on a Non-Debian SID Enviroment?

If you’re running Ubuntu/Arch/Gentoo/Mac or any other OS, you need not worry about installing another OS on your system! We can use Dockerlxc, or a virtual machine to simulate the environment!

Refer to instructions given below to setup Debian Sid or scroll down to see how I did it:

Personally, I prefer Docker and started with a Docker Container during my first packaging.

Tips

For Linux users using apt package manager, installing docker is now as easy as sudo apt install docker.io

How I Installed the Docker SID Enviroment?

I’ve divided the work and the command I ran to install the SID Environment in the table below, so, fire up a terminal and follow the steps one after the other. If you face any obstacles or have any queries, you can ask them in the comment below!

Work to be doneCode
Pull Debian Sid image from docker hub using the following commanddocker pull debian:sid
Create a container with it and start bash on itdocker run – -privileged – -name “sid” -it debian:sid /bin/bash
Update and upgrade to latest versions of packagesapt-get update && apt-get upgrade

Once done with above, you can start and connect with the docker container using the below command and it’ll land you into the container’s bash shell:

sudo docker start sid && sudo docker attach sid

Installing the required tools in Container

To start with packaging we need to install a few packages, do so by running the following commands:

  • Install packaging tools inside the container using:
    • For Generic Packages: apt-get install dh-make
    • For GO Packages: apt-get install dh-make-golang
    • For Ruby Gems: apt-get install gem2deb
    • For NodeJS Modules: apt-get install npm2deb 
  • Next, install a few required packages: apt-get install git-buildpackage sbuild dh-buildinfo quilt lintian 

Still having troubles setting it up?

If you’re still facing trouble, you can join Debian’s real-time chat group using one of the 4 options given below and say Hi!

1. Matrix: #debian-browserify:diasp.in
2. IRC: #debian-browserify on irc.oftc.net or http://webchat.oftc.net/?channels=debian-browserify&uio=MT11bmRlZmluZWQb1 to join the IRC room
3. Telegram: https://t.me/debian_browserify

If you are new to these technologies, I suggest you try matrix, see https://matrix.org for details. https://riot.im has a list of apps you can use.

To start with packaging, start with nodejs packages and understand the workflow, then you can shift to other language modules and then to higher multi language modules. 

Manas Kashyap

All Set? Let’s Start!

Install a text editor (I like vim , for installation run sudo apt install vim) and update .bashrc of docker container with the following environment variables, if you’re unable to find the .bashrc file, head to your home directory and run ls -la to show hidden files. You can then use any text editor to edit the file, for vim you can run vim .bashrc to open the file. Add the content below at the end of .bashrc file.

export DEBEMAIL=your@email.domain
export DEBFULLNAME='Your Name'
alias lintian='lintian -iIEcv --pedantic --color auto'
alias git-import-dsc='git-import-dsc --author-is-committer --pristine-tar'
alias clean='fakeroot debian/rules clean'
export LANG=C.UTF-8
export LC_ALL=C.UTF-8

Tips for Vim

Use the following keys to jump to various modes in Vim:
– i: To enter the insert mode
– :wq: To write the file and exit vim
– :q!: To exit the file without saving

Once the .bashrc is updated, you can update your current environment by running source ~/.bashrc

Now, setup username and email address for git, if you haven’t already by running:

git config –-global user.name "Your name"

git config –-global user.email "email@domain.tld"

The Steps:

Now we’ll follow the steps below to package a node module, qw:

#1 Check dependency use npm2deb depends

$ npm2deb depends -b -r qw
Module qw has no dependencies.

 From the output we can see that, qw has no dependencies

$ npm2deb search qw
Looking for similar package: None
Looking for existing repositories: None
Looking for wnpp bugs: None

Output shows no existing work and so we can start working on it!

#3 Preview more info using npm2deb view

$ npm2deb view qw
Version: 1.0.1
Description: None
Homepage: https://github.com/iarna/node-qw#readme
License: ISC
Debian: None (None)
License is automatically recognized as BSD, if this does not happen, please pass –upstream-license option during creation to set a correct license.

#4 Automate debian package creation using npm2deb create

Run npm2deb create qm to automatically create the package:

$ npm2deb create qw
You may want fix first these issues:
qw/node-qw-1.0.1/debian/control:Description: FIX_ME write the Debian package description
qw/node-qw_itp.mail:Subject: ITP: node-qw – FIX_ME write the Debian package description

From the output above we can see that npm2deb failed to do it completely, so our job is to fix those error which is shown by the prefix FIX_ME. Run the ls command and you’ll see that npm2deb has created a directory called qw, change directory to qw, and you’ll find it has the following files:

$ ls
qw
$ cd qw/ && ls
node-qw node-qw-1.0.1.tar.gz node-qw_1.0.1-1.dsc qw_1.0.1- 1_amd64.buildinfo node-qw_1.0.1.orig.tar.gz node-qw-1.0.1 node-qw_1.0.1-1.debian.tar.xz node-qw_1.0.1-1_all.deb node- qw_1.0.1-1_amd64.changes node-qw_itp.mail

node-qw is a temporary folder created by npm2deb itटs better to delete it to avoid confusion later

$ rm -rf node-qw

#5 Import package to git

$ gbp import-dsc --pristine-tar node-qw_1.0.1-1.dsc

Now, we’ll get a new dir called node-qw which is tracked by git, cd into the newly created directory run git branch and git tag

$ git branch
masterpristine-tarupstream *
$ git tag
debian/1.0.1-1
upstream/1.0.1

Delete the debian tag by running git tag -d debian/1.0.1-1

#6 Take the Ownership, ITP

ITP is a method by which we take ownership of the module by mailing to submit@bugs.debian.org and in return, we get a bug number. A sample mailing template is created by npm2deb for our use:

Run $ cat node-qw_itp.mail (to view the template ) and update the template by fixing the error with the prefix FIX_ME. Refer https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=881423 for a sample ITP

#7 Make package lintian clean

Follow the commands below to find out errors we need to fix:

/qw/node-qw $ ls
LICENSE README.md debian package.json qw.js test

/qw/node-qw $ dpkg-buildpackage && lintian ../node-qw_1.0.1-1.dsc
I: node-qw source: file-contains-fixme-placeholder debian/control:20 FIX_ME
W: node-qw source: out-of-date-standards-version 4.1.1 (current is 4.2.1)

lintian points out the errors we need to fix. Places where errors were fixed regarding this module: debian/controldebian/copyrightdebian/changelog. Make sure to commit your changes as they are fixed using git commit -m "comment description" path/of/fixed/file, For reference visit https://salsa.debianorg/js-team/node-qw.git/tree/

#8 Enable tests, if any

Find the test command from package.json

  $ cat package.json

  {
  "name": "qw",
  "version": "1.0.1",
  "description": "Quoted word literals!",
  "main": "qw.js",
  "scripts": {
  "test": "tap test"
  },
  "keywords": [],
  "author": "Rebecca Turner <me@re-becca.org> (http://re-
  becca.org/)",
  "license": "ISC",
  "devDependencies": {
  "tap": "^8.0.0"
  },

Now, add the test command under override_dh_auto_test from debian/rules

override_dh_auto_test:
 tap -J test/*.js

Add the test framework (mocha, node-tape etc) as a build dependency in debian/control

Build-Depends:
debhelper (>= 9), dh-buildinfo, nodejs, node-tap
Standards-Version: 4.1.2

Add a Test-Command section to debian/tests/control

$ cat tests/control
  Tests: require
  Depends: node-qw
  Test-Command: tap -J test/*.js
  Depends: @, node-tap

Now, run dpkg-buildpackage and check if the tests ran successfully and change debian/changelog to release by running dch -r.

#9 Upload to https://salsa.debian.org ( as the team maintains it)

Create a new project on https://salsa.debian.org as per your node-module name and push the directory to the repository using:

$ git remote add origin git@salsa.debian.org:username/node-module-name.git #example link
$ git push -u origin –all –follow-tags

#10 Clean build with sbuild

Run the command below and logout and re-login:

$ sudo sbuild-adduser $LOGNAME

or use newgrp sbuild in your current shell

$ sudo sbuild-createchroot –include=eatmydata,ccache,gnupg unstable
/srv/chroot/unstable-amd64-sbuild http://deb.debian.org/debian

and now run:

$ sudo sbuild -A -d unstable ../node-qw_1.0.1-1.dsc

#11 Install deb package

$ apt-get install autopkgtest
$ dpkg -i ../node-qw_1.0.1-1_all.deb

and you’re done! Congratulations, you just packaged your first debian package!



I hope this article helped you in Getting started with Debian Packaging, if you’ve any queries or if you faced any difficulties, you can ask them in the comment below or you can interact directly with the debian community with the links I shared above.

This image has an empty alt attribute; its file name is blog-footer.jpg

Written by 

Manas Kashyap is a DevOps Consultant, he is a tech enthusiast guy who likes to learn and play with technologies. He is also an open-source contributor and an active contributor to Debian.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading