Install required tools
rpm -qa | grep rpmdevtools
yum list | grep rpmdevtools
yum install rpmdevtools
Create directory structure required for rpm build if not exist
rpmdev-setuptree
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
Create source package under SOURCES
mkdir hello-0.0.1
cd hello-0.0.1
hello.sh
#!/bin/sh
echo "Hello world"
tar -cvzf hello-0.0.1.tar.gz hello-0.0.1
Create spec file and make necessary changes
cd ~/rpmbuild/SPECS
rpmdev-newspec hello.spec
Name: hello
Version: 0.0.1
Release: 1%{?dist}
Summary: A simple hello world script
BuildArch: noarch
License: GPL
Source0: %{name}-%{version}.tar.gz
Requires: bash
%description
A demo RPM build
%prep
%setup -q
%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/opt/hello
cp * $RPM_BUILD_ROOT/opt/hello
%clean
rm -rf $RPM_BUILD_ROOT
%files
/opt/hello/*
%postun
rm -vrf /opt/hello
%changelog
* Sun Feb 04 2024
- First version being packaged
Now directory structure should look like this
/home/tux/rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
│ └── hello-0.0.1.tar.gz
├── SPECS
│ └── hello.spec
└── SRPMS
Build rpm using following command
rpmbuild -ba SPECS/hello.spec
where -b is build a is for both source and binary
if you want only binary then put -bb flag
if you want only source then put -bs flag
Install the rpm using following command
sudo rpm -ivh ~/rpmbuild/RPMS/noarch/hello-0.0.1-1.el8.noarch.rpm
Verify rpm installed successfully using following command
rpm -qi hello
tree /opt/hello
The %changelog entry of a package can be viewed, too:
rpm -q hello –changelog
Uninstall or erase installed rpm
sudo rpm -q | grep hello
sudo rpm -ve hello
Troubleshoot
To extract files and directories from rpm
rpm2cpio hello-0.0.1-1.el8.noarch.rpm | cpio -idmv