Learn how to package your linux applications - parthibann/My_wiki GitHub Wiki
A developer may have created source code which works under Linux but which isn't currently packaged for your version of Linux. The packaging formats used by different distributions of Linux can be a pain point for software developers wishing to release their projects in an easily consumable way. Debian and Ubuntu rely on .deb packages, while Fedora and RedHat both use .rpm style packaging systems.

Using fpm, you can easily create both .deb and .rpm files without having to know any of the commands of the packaging tools that it leverages. fpm can make your life easier when attempting to create packages to use across your infrastructure or when distributing publicly downloadable packages of your project. fpm is a command-line program designed to help you build packages.
Installing FPM:
The main method of distribution for the fpm tool itself is as a Ruby gem. We can get our system prepared to install fpm by installing both the Ruby development packages and the software building tools needed to compile additional software.
First, update your local package cache and then proceed with the prerequisite installations:
sudo apt-get update
sudo apt-get install ruby-dev build-essential
Once the above packages are installed, you can install the fpm package itself by typing:
sudo gem install fpm
This will install fpm on the system level and make it available to any user on the system.
You should now have the fpm executable available on your system. You can verify this by checking out the tool's abundant help information:
fpm --help
The fpm tool is fairly good at telling you what it needs to complete a package build. To get an idea of the most basic requirements, you can call the command with no arguments:
fpm
Missing required -s flag. What package source did you want? {:level=>:warn}
Missing required -t flag. What package output did you want? {:level=>:warn}
No parameters given. You need to pass additional command arguments so that I know what you want to build packages from. For example, for '-s dir' you would pass a list of files and directories. For '-s gem' you would pass a one or more gems to package from. As a full example, this will make an rpm of the 'json' rubygem: `fpm -s gem -t rpm json` {:level=>:warn}
Fix the above problems, and you'll be rolling packages in no time! {:level=>:fatal}
This output tells us the basics of what we need to provide to create a package. A typical call will look like this in its basic form:
fpm -s source_type -t target_type source_name_or_location
The source can be any of a number of types. The source type will also dictate the way that the source name or location is passed to the command.
Source and Target Formats: The fpm tool can work with quite a few different source and target formats. These each have their own requirements and features. The following source types are currently supported:
| Source Type | Source Description | How to Pass Source Name or Location | Additional Packages Needed |
|---|---|---|---|
| dir | Directory or files | Pass the absolute or relative path(s) of the project files on the local filesystem. | (none) |
| tar | A tarball source package | Pass the path to the location of the tarball (compressed or uncompressed) on the local filesystem. | (none) |
| gem | A Ruby gem | Pass the name of a Ruby gem that can be find on www.rubygems.org. It will be auto-downloaded to create the package. | ruby, rubygems-integration |
| python | A Python package | Pass the name of a Python package as you would pass it to easy_install. Names are searched for in the Python Package Index and auto-downloaded to create the package. | python-setuptools |
| pear | A PHP extension | Pass the name of a PHP extension or application found on pear.php.net. The appropriate files will be auto-downloaded when creating the package. | php-pear |
| cpan | A Perl module | Pass the name of a Perl module as found at cpan.org. The files will be auto-downloaded and used to build the package. | cpanminus |
| zip | A zipped directory structure | Pass the location on the local filesystem of a zip file containing the package. | zip |
| npm | A Node.js module | Pass the name of a Node module as specified on npmjs.org. The package will be auto-downloaded and used to make the output package. | npm |
| osxpkg | An OS X package | Pass the location on the local filesystem of an OS X package (this will only work on OS X systems with pkgbuild in their path). | pkgbuild (only available for OS X systems) |
| empty | (no source) | Used to create a package without any actual packages. This is used most often for meta-packages that only contain dependencies. | (none) |
| deb | A .deb package | Pass the location of a .deb file on the local filesystem. | (none on Debian-based systems) |
| rpm | An .rpm package | Pass the location of an .rpm file on the local filesystem. | rpm |
There are also quite a few options for the target packaging formats that you wish to create. The table below describes some of the different options:
| Output Type | Output Description | Additional Packages Needed |
|---|---|---|
| deb | A Debian-style package that can be installed on Debian or Ubuntu systems. | (none on Debian-based systems) |
| rpm | A RedHat-style package that can be installed on CentOS, Fedora, or RedHat systems. | rpm |
| zip | A zip file containing the directory and file structure of the input package. | zip |
| tar | A tarball (compressed or uncompressed) of the directory structure of the input package. | (none) |
| dir | A directory in which to extract the inputted package. | (none) |
| sh | A self-extracting .sh file. This is a shell script with a bzipped tar file that will be extracted when run. | (none) |
| osxpkg | A package file for OS X. You must be working on an OS X installation with pkgbuild installed to create these packages. | pkgbuild (only available for OS X systems) |
| solaris | A package suitable for installing on a Solaris system. You must have pkgproto and pkgmk installed, which are only available on Solaris machines. | pkgproto, pkgmk (only available on Solaris systems) |
| pkgin | A package suitable for installing on BSD systems. You must have the pkg_create package installed, which is only available on BSD systems. | pkg_create (only available on BSD systems) |
| puppet | A puppet module that can be used to install on various systems. |
We can use the directory structure to build multiple packaging formats. For Example:
-
Create a folder named <test_package> inside /tmp folder
-
Create a text file <test_file.txt> inside the newly created dir <test_package>
-
run the following fpm command.
fpm -s dir -t deb -C /tmp/test_package --name test_project --version 1.0.0 --iteration 1 --description "A sample debain package" .
This will create a package called test-project_1.0.0-1_amd64.deb in the current directory.
you can install the newly created package using the following command.
dpkg -i test-project_1.0.0-1_amd64.deb
Check the installation using the following command:
dpkg -s test-project
To view the installation path, use the following command:
dpkg -L test-project
Remove the package using the following command:
dpkg -r test-project
You can then modify a few of the options to create an .rpm package (assuming you installed the rpm package from the repositories):
fpm -s dir -t rpm -C /tmp/test_package --name test_project --version 1.0.0 --iteration 1 --description "A sample rpm package" .
This will create a package called test_project-1.0.0-1.x86_64.rpm in your current directory.