How to Create a GNU Hello World RPM Package in Fedora

RPM (a recursive acronym for RPM Package Manager) is a free and open-source package management system for Linux. Although it was originally created for use in Red Hat Linux, now it is used in many Linux distributions such as CentOS, Fedora, and OpenSuse. Importantly, the name RPM refers to the package manager program and the .rpm is a file format.

In this article, we will explain on writing RPM files, showing how to easily create a simple source and binary software packages, for example, GNU “Hello World” RPM package in Fedora Linux distribution. We assume that you’ve some basic understanding of pre-made RPM packages, and with the Free Open Source Software building process.

Install Development Tools in Fedora

Let’s start by setting up the development environment in Fedora Linux by running the following command to install the necessary tools for building RPMs.

$ sudo dnf install fedora-packager @development-tools
Install Development Tools in Fedora

Install Development Tools in Fedora

Next, add your non-privileged account to the ‘mock‘ group as follows (replace tecmint with your actual username). This will enable you to test the build procedure in a clean chroot.

$ sudo usermod -a -G mock tecmint

Now, create an RPM build in your ~/rpmbuild directory and verify the build using the following commands. It will show a list of sub-directories, which contains project source code, RPM configuration files and binary packages.

$ rpmdev-setuptree
$ tree ~/rpmbuild/
Create Build Environment

Create Build Environment

Here is what each directory is meant for:

  1. BUILD – stores various %buildroot directories when packages are built.
  2. RPMS – will contain binary RPMs in sub-directories of Architecture.
  3. SOURCES – stores compressed source archives and any patches, this is where the rpmbuild command will look for them.
  4. SPECS – stores the SPEC files.
  5. SRPMS – stores the Source RPM instead of a Binary RPM.

Building a “Hello World” RPM

In this step, you need to download the source code (also known as the “upstream” source) of the Hello World project we are packaging, into the ~/rpmbuild/SOURCE directory with the following wget command.

$ cd ~/rpmbuild/SOURCES
$ wget -P ~/rpmbuild/SOURCES

Next, let’s configure the RPM package using a .spec file (let’s name it hello.spec in this case) in the ~/rpmbuild/SPECS directory, using the rpmdev-newspec program.

$ cd ~/rpmbuild/SPECS
$ rpmdev-newspec hello
$ ls
Create Hello Spec File

Create Hello Spec File

Then open the hello.spec file using your favorite editor.

$ vim hello.spec

The default template should look like this:

Name: hello
Release: 1%{?dist}
Summary: License:
Source0: BuildRequires:
Requires: %description %prep
%autosetup %build
%make_build %install
%make_install %files
%license add-license-file-here
%doc add-docs-here %changelog
* Tue May 28 2019 Aaron Kili

Let’s briefly explain the default parameters in a .spec file:

  • Name – used to set a name for the package.
  • Version – should mirror upstream.
  • Release – numbers you work within Fedora.
  • Summary – is a brief one-line description of the package, the first letter should be uppercase to avoid rpmlint complaints.
  • License – check the License status of the software by inspecting the source files and/or their LICENSE files, and/or by talking to the authors.
  • URL – specifies the home page of the software package.
  • Source0 – specifies the source files. It can be a direct URL or a path of the software’s compressed source code.
  • BuildRequires – specifies the dependencies needed to build the software.
  • Requires – specifies the dependencies needed to run the software.
  • %prep – is used to create the environment for building the rpm package.
  • %build – is used to compile and to build the source codes.
  • %install – this is used to install the programs. It lists command(s) to needed to copy the resultant file from the build process to the BUILDROOT directory.
  • %files – this section lists the files provided by the package, which will be installed on the system.
  • %changelog – should stores the work on preparing the RPM, especially if there are security and bug patches included on top of the base upstream source. It’s automatically generated while creating the hello.spec file. The changelog data is usually displayed by rpm --changelog -q <packagename>.

Now edit your .spec file and make changes as shown.

Name: hello
Version: 2.10
Release: 1%{?dist}
Summary: The "Hello World" program from GNU License: GPLv3+
Source0:{name}/%{name}-%{version}.tar.gz BuildRequires: gettext Requires(post): info
Requires(preun): info %description The "Hello World" program package %prep
%autosetup %build
make %{make_build} %install
%find_lang %{name}
rm -f %{buildroot}/%{_infodir}/dir %post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || : %preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi %files -f %{name}.lang
%{_bindir}/hello %doc AUTHORS ChangeLog NEWS README THANKS TODO
%license COPYING %changelog
* Tue May 28 2019 Aaron Kili

You will notice that we have used some new parameters in the above file which have not been explained. These are called macros, used to build system invocations defined by RPM to set installation paths for packages. Therefore, it’s usually preferable to not hard-code these paths in spec files either, but use the same macros for consistency.

The following are RPM build and directory macros together with their definitions and defaults values:

  • %{make_build} – is used in the %build section of the spec file, it runs the make command.
  • %{name} – defines package or directory name.
  • %{buildroot} – %{_buildrootdir}/%{name}-%{version}-%{release}.%{_arch}, same as $BUILDROOT
  • %{_infodir} – %{_datarootdir}/info, default: /usr/share/info
  • %{_mandir} – %{_datarootdir}/man, default: /usr/share/man
  • %{_bindir} – %{_exec_prefix}/bin, default: /usr/bin

Note that you can find the values for these macros in the /usr/lib/rpm/platform/*/macros or refer to Packaging Guidelines:RPM Macros.

Building the RPM Package

To build the source, binary and debugging packages, run the following rpmbuild command.

$ rpmbuild -ba hello.spec

After the build process, the source RPMs and binary RPMs wills be created in the ../SRPMS/ and ../RPMS/ directories respectively. You can use the rpmlint program to check and ensure that the spec file and RPM files created conform to RPM design rules:

$ rpmlint hello.spec ../SRPMS/hello* ../RPMS/*/hello*
Check Packages for Errors

Check Packages for Errors

If there any errors as shown in the above screenshot, fix them before you can proceed.

Last but not least, use mock program to check that the package build will succeed in the Fedora restricted build environment.

$ mock --verbose ../SRPMS/hello-2.10-1.fc29.src.rpm
Check RPM Package Build

Check RPM Package Build

For more information, consult the Fedora documentation: Creating RPM Packages.

That’s all! In this article, we have explained how to step up your Fedora system to create a simple source and binary software package. We also showed how to create a GUN Hello Word RPM package. Use the feedback form below to reach us for any questions or comments.

Posted by Web Monkey