What is a package?

If you are coming from a completely different operating system (like windows) or if you are not too familiar with how software is installed, understanding packages can be difficult. But actually they can be described simply as a file that holds all the data and information relevant for an application to install.
You could consider it a setup file while this is technically probably not correct, because the file itself it not performing the installation rather the package manager is. Do read up package tools like apt-get and yum together with package formats as deb rpm if you are interested in more technical background information.

It's not really interesting to know the exact technical specifications if you are just looking for a nice way to get software on your Linux system.

The problem

Whether you use CentOS, Fedora, Ubuntu or any other distro, often enough you come across a situation where there is no package available for your OS.
This means that there are actually not enough people who are making packages for other people to install easily. Especially for non-tech-savvy people just getting to the terminal will feel very advanced.

This post will show you how to make your own packages very easily. So you can use it at a later time, other computers or distribute it among your friends or over the internet.

Effing Package Management (FPM)

This is actually the title of a very nice piece of software freely available at github. Yes making packages used to be very complicated. I had never build a package before and i was able to make the package i wanted in 3 hours. 3 hours? Yes i had some additional requirements :) My first package literally took 2 minutes to look up and make, but i wasn't satisfied yet.

Installation

GCC and ruby are required for FPM to work. I made a package for Fedora 20 which uses the RPM format (just as RHEL and CentOS); this format requires another tool called rpmbuild.

# always required
sudo yum install gcc ruby ruby-devel

# rpm specific
sudo yum install rpm-build

# installing FPM
gem install fpm


Note: if you are using Ubuntu or Debian here use apt-get.

Building the package

In this example we will use modelio to create a package for it. Hop over to the download page and grab the tar.gz file. We will be building the package for a 64 bits architecture. This package doesn't need to be started from the command-line so we don't add a symlink or binary to /usr/bin/.

A simple start

To build the package

fpm -s tar -t rpm modelio-open-201312201301-linux.gtk.x86_64.tar.gz


Great! This works. You can install this as is. BUT there are a few things which aren't so nice.

1. It will install under your root directory /Modelio 3.1/
2. It leaves the installation directory present when removing the package.
3. It won't start because of write permissions.
4. It doesn't have a shortcut to start the application from your desktop.

Choosing directory

This can be simply doing with a prefix

--prefix opt


Will install modelio in /opt/Modelio 3.1/ this is a safe path for your software.

Removing the installation directory

A -to me- very weird "feature" of RPM is that it leaves the installation directory as is after removal of the package and just unregister your software from the installation applications list. Let's make sure the actual directory is removed as well, to clear up space when it's no longer needed.

--rpm-auto-add-directories


Setting permissions

This is actually specific to each application where it likes to write it's data to. Usually it will throw an error of some sort if it can't write to disk. For modelio you can specify a path that already has permissions set correctly (like your /home directory). To launch:

/opt/Modelio 3.1/modelio -data ~/.modelio


The -data flag will make sure it uses the home directory and no conflicts occur.

Note: this step is strictly not about building the package, but we need this later on. So test this first if you create a package yourself.

Making a shortcut

Fedora 20 uses the GNOME 3 desktop environment. But any environment that uses the standards from freedesktop.org will work (KDE is also supported). For these desktop a format for desktop launchers (the shortcuts) has been defined. This shortcut has to be placed during the installation process and gracefully removed when uninstalling.

Fortunately fpm has some nice hooks to start up scripts that can be run during installation and removal.

In the same directory as the tar.gz make two empty files called install.sh and remove.sh (or other names if you prefer). In these file place the following:

install.sh

#!/bin/sh
cp /opt/Modelio\ 3.1/icon.xpm /usr/share/pixmaps/modelio.xpm

echo '[Desktop Entry]
Name=Modelio
Comment=Modelio - the open source modeling environment
Exec=/opt/Modelio\ 3.1/modelio -data ~/.modelio
Icon=modelio.xpm
Type=Application
Categories=Development;FlowChart;' > /usr/share/applications/modelio.desktop


remove.sh

#!/bin/sh
rm /usr/share/applications/modelio.desktop
rm /usr/share/pixmaps/modelio.xpm


We are doing two things here (and reversing them at removal).

1. Copying the launcher icon to a special folder.
2. Making the launcher itself.

In install.sh you can customize the Name and the Comment (which will be used as tooltip) however you like. Valid choice for categories can be found here and here.

To run these script with your package use:

--after-install install.sh
--after-remove remove.sh


Extra's

If you plan to maintain your package in the future it's nice to add additional information to the package. This information shows up in the package manager, not in the shortcut or program.

-n Modelio  # the package name
-v 3.1  # the version number
--description "Modelio - the open source modeling environment" # the package description


Wrapping up

I'm sure there are tons of more features one could add to a package. For now to build a package for Modelio with all the features mentioned

fpm -s tar -t rpm --prefix opt --rpm-auto-add-directories --after-install install.sh --after-remove remove.sh -n Modelio -v 3.1 --description "Modelio - the open source modeling environment" modelio-open-201312201301-linux.gtk.x86_64.tar.gz


You can now install your package by double clicking it. Or if you prefer the terminal use

# install
sudo yum localinstall modelio-3.1-1.x86_64.rpm

# remove
sudo yum remove modelio.x86_64


One last note: there are a lot more command line options for FPM. Make sure you view the latest one by issuing the help command fpm --help.