How to Compile and Create Binary deb Installer from Source Files tar.gz tar.bz2 in Ubuntu

5 min


This simple tutorial will show how to compile source tarballs (*.tar.gz, *.tar.bz2) and create a *.deb installer in Ubuntu.

Pre-requisite

Make sure you have dh_make and build-essentials installed in your system. To install those in Ubuntu, run the below commands:

sudo apt-get install dh-make
sudo apt-get install build-essential

How to create deb file from source packages (e.g tar.gz files)

Follow the below steps to compile and create a deb file from the source package in Ubuntu.

Setup test directory

  • Create a folder which can be used to keep the source files and other files. You can have it named anything you want. For this tutorial, I have named it “testbox” and kept it on the Desktop. Also, I will use the source of software called “qgifer-0.2.1”, this software is used to create gif files from video files. I have downloaded the qgifer-0.2.1-source source files from SF.
testbox folder created in desktop
  • Keep your source file (i.e. the compressed tar file) inside testbox folder. Extract the compressed source files.
with source extracted
  • You can keep the extracted folder name as it is OR you can change it. For this tutorial, I will change the folder name from qgifer-0.2.1-source to qgifer-0.2.1. While renaming the source directory, make sure to follow rules: only use lower case letters, numbers, plus, dot and minus characters.
  • Delete the source tar file. We don’t need it anymore.
  • At this stage, your test directory should look like this.
start

The creation of a deb file requires “debian” directory and all the minimum necessary contents of it inside the source directory. Also, the source directory should contain the launcher i.e. *.desktop file, to make entries in the menu and include an icon. Typically all the source tarballs come with these files and sometimes the debian directory and its contents as well.

  • Change any path to the destination in source files or in the *.desktop file. E.g. for the below desktop file, possible changes you may want are the “Categories” and “Icons”.
desktop file look
  • After all, the change is done, compress the entire source folder to a tar.gz file type. Then copy the tar.gz file inside the source folder. Then delete the tar.gz file outside of the source folder.
copy and delete tar

Configure debian directory

  • Open a terminal and go inside the source folder and run the below command to create the basic “debian” directory structure with default values.
dh_make -e hello@mail.com -c gpl3 -f qgifer-0.2.1.tar.gz 
dh_make how

Note: You can put down your actual valid email address.
The above command will ask for certain values:
Type of package: Choose single binary

  • If you now go back to the source directory, you can see a directory named debian is created with a bunch of files inside it. Also, a file named *.orig.tar.gz s created outside of the source directory.
after dh_make
after dh_make 2

Now we need to edit the contents of the created ‘debian’ directory just been created. While compiling a source package, everything depends on the contents of this directory. So it is essential to update the contents as per your needs. Debian packaging is a complex process with many options for customising a compilation based on user needs. This tutorial would try to keep it as simple as possible to have the concept in place.

  • If you open the debian directory created by dh_make, you can see some files with extension *.ex and other files. These ex files are example files that can be used by modifying them and removing ex extension. If they are not needed, they can be deleted. For this tutorial, we will delete all the ex files and README files.
debian dir contents 1
debian dir contents 2
  • The minimum recommended files needed to compile source tarballs are: changelog, compat, control, copyright, and rules.
  • We need to edit each of these files as per our source tarball. Open all these files in a text editor.
  • changelog is the log of changes in the debian package. It contains the source package name, version, target version, and bug number.

Before change

qgifer (0.2.1-1) unstable; urgency=low

  * Initial release (Closes: #nnnn)  

 -- arindam <hello@mail.com>  Thu, 11 Feb 2016 22:37:44 +0530
</hello@mail.com>

After change

qgifer (0.2.1-1) vivid; urgency=low

  * Initial release (Closes: #1234)  <1234 is the bug number of your ITP>

 -- arindam <hello@mail.com>  Thu, 11 Feb 2016 22:37:44 +0530
</hello@mail.com>
  • control file describes the source and binary package. It contains information about package name, architecture, dependencies etc. See below for the section descriptions and changes.
debian control file change

Before Change

After Change

Source: qgifer
Section: Graphics
Priority: optional
Maintainer: arindam <hello@mail.com>
Build-Depends: debhelper (>= 9), libopencv-dev (>= 2.3), libgif-dev (>=4.1), libqt4-dev (>= 4.8.0), cmake (>= 2.6)
Standards-Version: 3.9.4
Homepage: 
#Vcs-Git: git://git.debian.org/collab-maint/qgifer.git
#Vcs-Browser: http://git.debian.org/?p=collab-maint/qgifer.git;a=summary

Package: qgifer
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: A video-based animated GIF creator
 Creates GIF from a video file.
</hello@mail.com>

Note: Check the readme files of the source package for dependencies needed for the package. Make sure to install all the dependencies in your system where you are compiling the source before you proceed further.

  • the rules file is the most complicated one. However, dh_make makes it as simple as possible. For this tutorial, we did not make any changes to it.
#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

%:
	dh $@ 

Note: For sample rule files, go to /usr/share/doc/debhelper/examples.

  • Keep other files unchanged for now, they are source/format, copyright, docs and compat.
  • Save all the changes.
  • Delete the tar.gz file from inside the source folder. This is very important. If you don’t delete this, the next buildpackage command will fail with the below error.
dpkg-source: info: using source format `3.0 (quilt)'
dpkg-source: info: building qgifer using existing ./qgifer_0.2.1.orig.tar.gz
dpkg-source: error: cannot represent change to qgifer-0.2.1.tar.gz: binary file contents changed
dpkg-source: error: add qgifer-0.2.1.tar.gz in debian/source/include-binaries if you want to store the modified binary in the debian tarball
dpkg-source: error: unrepresentable changes to source
dpkg-buildpackage: error: dpkg-source -b qgifer-0.2.1 gave error exit status 2

Compile

  • Now it’s time to build the package. Go to the source folder from the terminal and run the below command. We are using fakeroot and not sudo due to various permission issues that may occur in the target installation system after compilation. It is recommended not to use sudo while running dpkg-buildpackage.
fakeroot dpkg-buildpackage -F
  • dpkg-buildpackage the command will take some time to compile the source depending on your project’s size. Once the command is complete, go one level up from the source folder where you can see a bunch of files have been created including the *.deb file for your package. You can now double-click to install the deb file in your system.
final deb


Arindam

Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, LinkedIn, or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
2
0
Would love your thoughts, please comment.x
()
x