Build an RPM in CentOS, RHEL and Ubuntu Linux

build rpm linux, build rpm ubuntu

RPM Package Manager, commonly known as RPM, is a versatile open-source package management tool that provides a simple way to build, install, verify, update, and uninstall software packages. This article guides you through the steps to build an RPM package on various Linux distributions: Ubuntu, CentOS, Fedora, and Red Hat Enterprise Linux (RHEL).

Before proceeding, it’s crucial to understand that building an RPM requires a special user environment, generally referred to as a build environment. This safeguards your system from potential damage if anything goes wrong during the package build process.

Setting Up the Build Environment

Regardless of the Linux distribution you are using, you’ll need to create a build environment. We recommend creating a dedicated user for this purpose:

  1. Create a new user: sudo useradd makerpm
  2. Switch to the new user: su - makerpm
  3. Set up the build environment by creating a rpmbuild directory structure in the home directory: mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
  4. Create an .rpmmacros file in the home directory and include the following:
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

Now, you’re ready to install the required tools based on your Linux distribution.

Ubuntu

Ubuntu uses the DEB package format by default, so we need to install some additional tools:

  1. Update the package lists: sudo apt-get update
  2. Install the required packages: sudo apt-get install rpm

CentOS, Fedora, and RHEL

These distros natively support RPM. We just need to install the rpm-build package:

  1. Update the system: sudo yum update (for CentOS/RHEL) or sudo dnf update (for Fedora)
  2. Install rpm-build: sudo yum install rpm-build (for CentOS/RHEL) or sudo dnf install rpm-build (for Fedora)
build rpm

Creating an RPM Spec File

RPM uses a spec file to describe how to build the package. The spec file contains necessary information like the package’s name, version, release number, and instructions on how to set up, build, install, and clean up the package.

See also  How to install Ruby 1.8.7 on CentOS 5.5 Linux

Create a spec file under the ~/rpmbuild/SPECS directory. A sample spec file may look like this:

Name:           helloworld
Version:        1.0
Release:        1%{?dist}
Summary:        Hello World Program

License:        GPL
URL:            https://example.com

Source0:        https://example.com/source/helloworld-1.0.tar.gz

BuildRequires:  gcc

%description
A simple Hello World program in C.

%prep
%setup -q

%build
gcc -o helloworld helloworld.c

%install
rm -rf $RPM_BUILD_ROOT
install -d $RPM_BUILD_ROOT/usr/local/bin
install -m 755 helloworld $RPM_BUILD_ROOT/usr/local/bin

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
/usr/local/bin/helloworld

%changelog
* Fri May 19 2023 makerpm
- Initial RPM release

In this spec file, we define various sections that guide the package building process, including setting up (%prep), building (%build), installing (%install), cleaning up (%clean), and finally specifying the files to include in the package (`%files`).

Building the RPM Package in Linux

With the spec file ready, you can now build your RPM package:

  1. Change to the SPECS directory: cd ~/rpmbuild/SPECS
  2. Build the RPM package: rpmbuild -ba helloworld.spec

Your RPM package will be available in the ~/rpmbuild/RPMS directory.

Frequently Asked Questions (FAQ)

How to rebuild RPM from source in CentOS?

To rebuild an RPM from source in CentOS, first install the source RPM using yum install package-name.src.rpm. Then, use the rpmbuild command followed by the -ba option and the spec file to rebuild the RPM. The spec file can typically be found in the ~/rpmbuild/SPECS/ directory. For example: rpmbuild -ba ~/rpmbuild/SPECS/package-name.spec.

Leave a Comment