TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Linux

Build a Debian DEB File From Your Project’s Source

If you are building a Linux application, creating a package for Debian distributions is a must. Here's how.
May 5th, 2024 6:00am by
Featued image for: Build a Debian DEB File From Your Project’s Source

If you’re a developer, at some point you might need to build a Debian package from your source. Of course, this means your project would have to have been developed for Linux in the first place, as you certainly won’t be able to build a Debian package from the source designed for either Windows or macOS. But if you’re developing for Linux and you want to create a Debian package, read on.

Your first question might be, “Why create a Debian package?” Well, Debian-based distributions are the most widely used flavors of Linux on the market. You’ll find Debian-based distributions powering desktops, servers, IoT, cloud and more. Plus, Ubuntu is based on the nonprofit Debian, so the need for building a DEB package file makes sense.

The good news is that it’s not nearly as hard as you might think. Even better, you won’t need too much in the way of dependencies to make it happen.

I’m going to walk you through the process of building a DEB file using the source from the Firefox web browser (which is version 125 as of this writing). When the process is complete, you’ll have a package you can install on on Debian-based distribution.

What You’ll Need

To do this, you’ll need the following things:

  • A Debian-based distribution from which to work
  • The Firefox source (which can be downloaded from the official Firefox download page)
  • A user account on the Linux machine

That’s it. Let’s get to the building.

Creating the Necessary Directories

First off, as long as you’re using a Debian-based distribution, you won’t have to install any dependencies or third-party software. The tool used for this process is dpkg-deb, which is a part of the base dpkg package.

To verify you have the required app, issue this command:

which dpkg-deb

If you see /usr/bin/dpkg-deb in the output, you have everything needed.

Next, make sure you’ve downloaded the Firefox source file, which will arrive as a BZ2 file. Save that in ~/Downloads. Open a terminal window and navigate to the ~/Downloads directory:

cd ~/Downloads

Decompress the Firefox file with this command:

bunzip2 firefox-XXX.tar.bz2

Note that XXX should be replaced with the release version number for Firefox.

This will decompress the file and leave you with a TAR archive. Extract that archive with:

tar xvf firefox-XXX.tar

XXX should be replaced with the Firefox release number.

You should now see a directory named firefox.

Our first subdirectory will be used to build the package. Create this directory:

mkdir -p firefox/opt/firefox-deb

Next, we have to create a directory named DEBIAN with the command:

mkdir firefox/DEBIAN

Now, we create a control file. The control file stores control data that specifies the metadata for the package (and can include dependencies and other information). The control file includes fields such as Package, Version, Maintainer, Description, Homepage, Architecture, Depends and more. Most of these fields will be obvious (even Depends, which lists dependencies required for the package).

There are two other (optional) files that can be included:

  • postinst: the post-install script
  • my.ini: any other files that can be included with the package

Create the control file with the following command:

nano firefox/DEBIAN/control

Once the file has been created, open it and paste the following:

Replace the uppercase placeholder text as follows:

  • NAME is your name.
  • EMAIL is your email address.
  • XXX is the version number you’re building.

Save and close the file.

We’ll now create a desktop file so Firefox can be started. Create yet another directory:

mkdir -p firefox/usr/share/applications/

Create the desktop file with this command:

nano firefox/usr/share/applications/firefox-deb.desktop

At a bare minimum, that file should look something like this:

[Desktop Entry]
Type=Application
Terminal=false
Exec=/home/wonko/myapps/firefox/firefox
Name=Firefox
Comment=Firefox
Icon=firefox
Categories=GNOME;GTK;Network;WebBrowser;

You can obviously edit that file as needed, adding languages and more. For more information on these files, you can read about the different desktop entry keys on this specifications page.

Save and close the file.

We can now build the package with this command:

dpkg-deb --build firefox

If you’re building specifically for Ubuntu, you’ll have to use Zstandard compression, which means the command would be:

dpkg-deb -Zgzip --build firefox

It’ll take a minute or so to complete, but when it does, you’ll find a file named firefox.deb on the current working directory. That DEB file is installable with the command:

sudo dpkg -i firefox.deb

You might also want to sign the package with your private GPG key (for security reasons). If you don’t have a GPG key, create one:

gpg --full-gen-key

You’ll be prompted to answer a few straightforward questions. Then, you’ll be required to type and verify a password for the key. Once you have your GPG created, you’ll need to install the required app:

sudo apt-get install dpkg-sig -y

Once that’s installed, sign the package with this command:

dpkg-sig --sign builder firefox.deb

You’ll be prompted for your GPG key password. Upon successful authentication, the package will be signed with you key.

You can now share the package with others and they can install it with ease.

And that, my friends, is how you build a Debian package from your source. I would suggest you try this out with the Firefox source so you can understand how it works. Once you’re confident in the process, give it a try with your own source and see how it goes.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.