Let’s take a look at how to build a really simple Linux package. The most common package formats are definitely .deb and .rpm. One will find them on Debian-variants and Redhat-variants respectively.
In the following examples, a package will be created and it will contain 1 script.The script depends on Python so I want the package management tool to handle that for me too. The script’s name is goldenRatio.sh and it will be installed to /usr/local/bin/
DEB
Create a directory structure for building
Here is what I created. Will talk about the control file in a minute. The usr subdirectory contains the path to where I want this script installed.
/root/goldenRatio
├── DEBIAN
│ └── control
└── usr
└── local
└── bin
└── goldenRatio.sh
The control file
This file describe the package. It is pretty much self-explanatory.
Package: goldenRatio
Version: 0.1.2
Maintainer: XPK
Architecture: all
Depends: python3
Description: Script to calculate golden ratio
Build & install the package
We are ready to build the deb package. The following command will create a package under /tmp/.
$ cd /root
$ dpkg-deb --build goldenRatio /tmp
dpkg-deb: building package 'goldenratio' in '/tmp/goldenratio_0.1.2_all.deb'.
$ apt install /tmp/goldenratio_0.1.2_all.deb
RPM
The RPM version is more involved. For rpm, we’ll need to write a spec file.
The spec file
The spec file does not just describe the package. It contains instructions to compile a software or in this case to copy the script to the target location.
Name: goldenRatio
Summary: Shell script for calculating golden ratio
Version: 0.1
Release : 3
License: GPLv3
Requires: python36
%description
Shell script for calculating golden ratio
%changelog
* Thu Jul 26 2018 XPK [email protected] 0.1-3
- Second build, adding python dependency
%prep
mkdir -p %{buildroot}%{_prefix}/local/bin
cp -pf goldenRatio.sh %{buildroot}%{_prefix}/local/bin/goldenRatio.sh
chmod 755 %{buildroot}%{_prefix}/local/bin/goldenRatio.sh
%files
%{_prefix}/local/bin/goldenRatio.sh
Install the build tool
$ yum install rpm-build
The build directory
After the previous step is performed, /root/rpmbuild is automatically created. Here, I’ll need to put my spec file in the SPECS directory, and my script under the BUILD directory.
/root/rpmbuild/
├── BUILD
│ └── goldenRatio.sh
├── BUILDROOT
├── RPMS
├── SOURCES
├── SPECS
│ └── goldenRatio.sh.spec
└── SRPMS
Build and install the package
$ cd /root/rpmbuild/SPECS
$ rpmbuild -ba goldenRatio.sh.spec
$ yum localinstall /root/rpmbuild/RPMS/x86_64/goldenRatio-0.1-3.x86_64.rpm
Wrapping up
In both case, apt and yum will automatically resolve the dependency for Python, and prompt you to install them if you don’t have it already.
These are very simply examples. I’ll do a follow-up post once I learn more.