Building, Testing, and Verifying a Custom RPM

Red Hat provides software in the form of packages. These packages are called RPMs. If you want to build custom software for RHEL-based systems, you need to know the steps required to create RPMs. This tutorial explains these steps in detail through an example.

Necessary tools

To build a custom RPM, you need the rpm-build and rpmdevtools packages. The rpm-build package provides the rpmbuild command. This command creates an RPM package from source code. The rpmdevtools package provides the rpmdev-setuptree and rpmdev-newspec commands. These commands create the necessary directory structure and configuration files. The following command installs the rpm-build package.

#dnf install rpm-build

Installing the rpm-build package

The following command installs the rpmdevtools package.

#dnf install rpmdevtools

Installing the rpmdevtools package

Creating a regular user account

Typically, developers use regular user accounts for software development. To simulate a real-world scenario, create a regular account and switch the user account to it.

#useradd developer
#su developer

Change the working directory to the user's home directory.

$cd
$pwd

The useradd command

Setting up a directory structure for the RPM

The following directories are essential for an RPM.

rpmbuildThis directory is the parent directory of all directories in the RPM.
BUILDThis directory provides scratch space for compiling.
RPMSThis directory stores the binary.
SOURCESThis directory contains the source code.
SPECSThis directory holds the spec file.
SRPMSThis directory contains the source RPM.

You can manually create the above directories or use the rpmdev-setuptree command. This command places all essential directories in the correct location.

Setting up directories

Adding the source code

A source code is compulsory. It provides the package's functionalities. For a basic RPM, you do not need any complex source code written in a programming language. You can use a simple text file as the source code. Make a directory outside the rpmbuild directory and create a sample file in it.

$cd
$mkdir sample_rpm
$cd sample_rpm
$cat > sample_file
This is a sample file in the test rpm.
Ctrl+D
$cd
$ls  sample_rpm
$cat sample_rpm/sample_file

Creating the source code file

The SOURCES directory keeps the source code in the archive format. Create an archive, add the text file to it, and move the archive to the SOURCES directory.

$tar cf sample_rpm.tar.gz sample_rpm
$ls
$mv sample_rpm.tar.gz rpmbuild/SOURCES/
$ls
$ls rpmbuild/SOURCES/

Creating tar

Creating a specification file

A specification file provides information about the package. All RPMs need and include it. Move to the SPECS directory and run the rpmdev-newspec command. This command generates a template file. You can use this template file to build a specification file.

$cd rpmbuild/SPECS/
$rpmdev-newspec
$ls

Creating a specification file

Open the generated template file for editing.

$vim newpackage.spec

The default specification file

Update the template file as shown in the following image.

Updating specification file

Directive Description
Name This directive sets the name of RPM.
Version This directive sets the version of RPM. The default is 1.
Release This directive sets the subversion and the RHEL version. The default is 1.
Summary Small description of package contents.
License License information under which you want to release this package.
URL This directive allows you to set the URL of your site.
Source0 Name of the archived source code file.
BuildRoot Path of the parent directory that contains all necessary directories and the source code of the RPM.
%description A brief description of package functionality.
%prep Commands that prepare the source code.
%setup Move the source code in the BUILD directory and decompress the archived file.
%install Clean the build directory, copy the compressed file, and extract it.
%files Files of the package
%clean Clean all temporary files generated during this process.

Building the RPM

The following command builds the RPM from the specification file and places it in the RPMS/x86_64/ directory.

$rpmbuild –v --bb newpackage.spec

Building a RPM

You can build an RPM under the normal user account. But you cannot install it using the regular user account. To install it, you need root privileges. To verify this, install it using the same user account.

$ls RPMS/x86_64
$rpm -ivh

The rpm command installs the package. You can check the following tutorial to learn more about this command.
The rpm command in Linux Explained

Installing a RPM

Under a regular user account, it returns a 'permission denied' error. Exit the regular account to switch back to the root account. Under the root account, switch to the directory containing the custom RPM.

$exit
#cd /home/developer/rpmbuild/RPMS/x86_64/
#ls

Exit the root account

Installing and verifying the custom RPM

Use the following command to install the package.

#rpm -ivh [package-name]

The package you created in this exercise installs a file in the /opt directory. You can check this file to verify the custom RPM.

Installing an RPM

Removing the package

The following command removes the package.

#rpm -evh [package-name]

To verify the removal, you can recheck the file installed by the package.

Removing the package

Conclusion

This tutorial outlined the steps required to build, install, and test a custom RPM. Learning these steps helps you understand basic concepts and fundamentals of RPMs.

ComputerNetworkingNotes Linux Tutorials Building, Testing, and Verifying a Custom RPM

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com