Table of Contents IntroductionAccording to the Linux manual pages (man yum), there are 35 standard YUM commands. That number increases to the hundreds when you
With our step-by-step guide, you can easily build your own RPM packages in Linux on RHEL and CentOS, and efficiently manage software updates and installations.
In the world of Linux, RPM packages are a common way to distribute software packages. RPM stands for “Red Hat Package Manager,” and it was originally created for Red Hat Linux. Today, RPM is used by many Linux distributions, including RHEL and CentOS, which are two popular enterprise-grade distributions.
In this article, we will guide you through the process of building your own RPM packages in Linux. We will focus specifically on RHEL and CentOS versions 7, 8, and 9. By the end of this article, you will have a good understanding of how RPM packages work, and you will be able to create your own RPM packages.
An RPM package is a software package that contains all the necessary files and instructions for installing and configuring software on a Linux system. RPM packages are used by many Linux distributions, including RHEL and CentOS. They are similar to Debian packages, which are used by Debian-based distributions such as Ubuntu.
The spec file: This file contains all the instructions for building the RPM package. It specifies the name of the package, the version number, and the architecture (i.e., 32-bit or 64-bit). It also specifies the dependencies that the package requires, as well as any pre- and post-installation scripts that need to be run.
The source code: This is the actual software that you want to package. It can be a tarball, a Git repository, or any other source code format.
The binary RPM package: This is the final product of the build process. It contains the compiled software, as well as any configuration files, documentation, and other necessary files.
There are several reasons why you might want to build your own RPM package:
You want to distribute your own software: If you have written your own software and you want to distribute it to others, building an RPM package is a convenient way to do so.
You want to customize an existing package: If you need to customize an existing package to meet your specific needs, building an RPM package is a good way to do so.You want to package software that is not available in the official repositories: If you need to install software that is not available in the official repositories, building an RPM package is a good way to do so.
Before we dive into the process of building RPM packages, there are a few prerequisites that you will need:
A Linux system: You will need a Linux system to build RPM packages. We will be using RHEL and CentOS versions 7, 8, and 9.
Development tools: You will need development tools installed on your system, including GCC, make, and rpm-build.
Basic understanding of the command line: You should be comfortable using the command line in a Linux environment.
A source code package: You will need a source code package that you want to package into an RPM.
Now that you have everything you need, let’s dive into the process of building RPM packages.
The first step is to install the development tools that you will need. On RHEL and CentOS, you can do this using the following command:
$ sudo yum groupinstall "Development Tools"
This will install a collection of tools that you will need to build RPM packages, including GCC, make, and rpm-build.
In addition to the basic development tools, you will also need to install the RPM development tools. On RHEL and CentOS, you can do this using the following command:
$ sudo yum install rpm-build rpmdevtools
This will install the necessary tools for building RPM packages, including rpmbuild, rpmlint, and spectool.
Before you can start building RPM packages, you need to set up the RPM build environment. This involves creating a directory structure that will hold your RPM package files.
On RHEL and CentOS, you can set up the RPM build environment using the following command:
$ rpmdev-setuptree
This will create a directory structure in your home directory, under ~/rpmbuild
. Inside this directory, you will find three subdirectories:
SOURCES
: This directory will contain the source code for your RPM package.
SPECS
: This directory will contain the spec file for your RPM package.
RPMS
: This directory will contain the binary RPM package that is generated when you build your RPM package.
BUILD
: This directory is used during the build process to compile and install the software.
SRPMS
: This directory will contain the source RPM package that is generated when you build your RPM package.
The next step is to create the spec file for your RPM package. The spec file contains all the necessary information for building the RPM package, including the name, version, and release of the package, as well as the dependencies and build instructions.
You can create a spec file using a text editor such as vi or nano. Here is an example spec file:
Name: mypackage
Version: 1.0
Release: 1%{?dist}
Summary: My package description
License: GPL
Group: Applications/System
URL: http://www.example.com/
Source0: http://www.example.com/mypackage-1.0.tar.gz
BuildRequires: gcc
%description
This is my package description.
%prep
%setup -q
%build
./configure
make
%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
%doc README
/usr/local/bin/mypackage
In this example, we are creating an RPM package called mypackage
with version 1.0. The spec file specifies the dependencies and build instructions, including the BuildRequires
line, which specifies that the package requires GCC to build.
Once you have created the spec file, you need to place the source code for your package in the SOURCES
directory. In our example, the source code is located at http://www.example.com/mypackage-1.0.tar.gz
.
You can download the source code using the spectool
command:
$ spectool -g -R mypackage.spec
This will download the source code and place it in the SOURCES
directory.
To build the RPM package, you will use the rpmbuild
command. In order to build the package, you must have the source files, the spec file, and the RPM development tools installed.
Here’s how you can build the RPM package:
$ rpmbuild -ba mypackage.spec
This will build the binary RPM package and the source RPM package. The binary RPM package will be located in the RPMS
directory, while the source RPM package will be located in the SRPMS
directory.
You can also build just the binary RPM package or just the source RPM package using the -bb
or -bs
options, respectively.
To install the RPM package, use the yum
command. Here’s how you can install the RPM package:
$ sudo yum install RPMS/x86_64/mypackage-1.0-1.el7.x86_64.rpm
This will install the binary RPM package on your system.
To verify that the RPM package is installed correctly, you can use the rpm
command. Here’s how you can verify the RPM package:
$ rpm -q mypackage
This will display the version and release of the installed RPM package.
If you encounter errors while building the RPM package, here are some common issues and their solutions:
Error: Could not find header file for package
Solution: You need to install the development package for the missing header file. For example, if you get an error about a missing libxml2
header file, you need to install the libxml2-devel
package.
Error: File not found: %files
Solution: Make sure that the file or directory specified in the %files
section of the spec file exists and is located in the correct directory.
Error: File listed twice: /path/to/file
Solution: Make sure that the file is listed only once in the %files
section of the spec file.
Error: No package provides xxx
Solution: You need to add the missing package as a dependency in the spec file. For example, if you get an error about a missing libjpeg
package, you need to add libjpeg-devel
as a dependency in the spec file.
Building your own RPM packages can seem daunting at first, but with the right tools and a little bit of practice, it can become a simple and efficient way to manage software on your Linux system.
In this article, we’ve covered the basic steps involved in building RPM packages on RHEL and CentOS, including installing the RPM development tools, setting up the RPM build environment, creating the spec file, placing the source code in the SOURCES
directory, building the RPM package, installing the RPM package, and troubleshooting common errors.
By following these steps, you should now be able to build your own RPM packages and manage software on your Linux system with ease.
Related Posts
Table of Contents IntroductionAccording to the Linux manual pages (man yum), there are 35 standard YUM commands. That number increases to the hundreds when you
In this article, we will review how to change DNS settings using nmcli. In RHEL7 and CentOS7, modifying the ifcfg scripts or /etc/resolv.conf files directly
In today’s tutorial, we will install CentOS8 on KVM. The install process is fairly straightforward and we will cover it here step-by-step. We will follow