Linux RedHat How To Create An RPM Package
Article Number: 724 | Rating: Unrated | Last Updated: Sun, Jan 9, 2022 7:50 PM
1. Install Required RPM-Build Packages
$yum install rpm-build rpmdevtools
2. Create the directory structure
$rpmdev-setuptree
The directory structure will look like this
$ls -l rpmbuild/
total 24
drwxr-x--- 4 root root 4096 Nov 6 11:59 BUILD
drwxr-x--- 2 root root 4096 Nov 6 12:00 BUILDROOT
drwxr-x--- 3 root root 4096 Nov 6 11:20 RPMS
drwxr-x--- 2 root root 4096 Nov 6 11:03 SOURCES
drwxr-x--- 2 root root 4096 Nov 6 11:58 SPECS
drwxr-x--- 2 root root 4096 Nov 6 12:00 SRPMS
3. Create rpmmacro File
~/.rpmmacros
%packager YOUR_NAME
%_topdir %(echo $HOME)/rpmbuild
%_smp_mflags %( \
[ -z "$RPM_BUILD_NCPUS" ] \\\
&& RPM_BUILD_NCPUS="`/usr/bin/nproc 2>/dev/null || \\\
/usr/bin/getconf _NPROCESSORS_ONLN`"; \\\
if [ "$RPM_BUILD_NCPUS" -gt 16 ]; then \\\
echo "-j16"; \\\
elif [ "$RPM_BUILD_NCPUS" -gt 3 ]; then \\\
echo "-j$RPM_BUILD_NCPUS"; \\\
else \\\
echo "-j3"; \\\
fi )
%__arch_install_post \
[ "%{buildarch}" = "noarch" ] || QA_CHECK_RPATHS=1 ; \
case "${QA_CHECK_RPATHS:-}" in [1yY]*) /usr/lib/rpm/check-rpaths ;; esac \
/usr/lib/rpm/check-buildroot
4. Copy your script and files under SOURCES directory
$cd ~/rpmbuild/SOURCES
$ls -l myadminscript-1/
total 8
-rwx------ 1 root root 31 Nov 12 09:53 myscript.sh
-rw-r----- 1 root root 24 Nov 12 09:54 README.md
5. Create a tarball of your code.
$tar -czf myadminscript-1.tar.gz myadminscript-1
6. 6. Create SPEC File
~/rpmbuild/SPECS/myadminscript.spec
Name: myadminscript
Version: 1
Release: 1
Summary: Write a summary here to explain what your code does
Group: TecAdmin
BuildArch: noarch
License: GPL
URL: https://github.com/nareshpadiyar/myadminscript.git
Source0: myadminscript-1.tar.gz
%description
Write some description about your package here
%prep
%setup -q
%build
%install
install -m 0755 -d $RPM_BUILD_ROOT/opt/myadminscript
install -m 0755 myscript.sh $RPM_BUILD_ROOT/opt/myadminscript/myscript.sh
install -m 0644 README.md $RPM_BUILD_ROOT/opt/myadminscript/README.md
%files
/opt/myadminscript
/opt/myadminscript/myscript.sh
/opt/myadminscript/README.md
%changelog
* Fri Nov 12 2020 Naresh Padiyar 1.0.0
- Initial rpm release
7. Build RPM
$rpmbuild -ba ~/rpmbuild/SPECS/myadminscript.spec
Sample Output
$rpmbuild -ba ~/rpmbuild/SPECS/myadminscript.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.3W18VG
+ umask 022
+ cd /root/rpmbuild/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /root/rpmbuild/BUILD
+ rm -rf myadminscript-1
+ /usr/bin/gzip -dc /root/rpmbuild/SOURCES/myadminscript-1.tar.gz
+ /bin/tar -xf -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd myadminscript-1
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.jJHoB7
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd myadminscript-1
+ LANG=C
+ export LANG
+ unset DISPLAY
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.DOq2hy
+ umask 022
+ cd /root/rpmbuild/BUILD
+ '[' /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64 '!=' / ']'
+ rm -rf /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64
++ dirname /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64
+ mkdir -p /root/rpmbuild/BUILDROOT
+ mkdir /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64
+ cd myadminscript-1
+ LANG=C
+ export LANG
+ unset DISPLAY
+ install -m 0755 -d /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64/opt/myadminscript
+ install -m 0755 myscript.sh /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64/opt/myadminscript/myscript.sh
+ install -m 0644 README.md /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64/opt/myadminscript/README.md
+ /usr/lib/rpm/find-debuginfo.sh --strict-build-id /root/rpmbuild/BUILD/myadminscript-1
+ '[' noarch = noarch ']'
+ case "${QA_CHECK_RPATHS:-}" in
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump
+ /usr/lib/rpm/brp-python-bytecompile /usr/bin/python
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: myadminscript-1-1.noarch
warning: File listed twice: /opt/myadminscript/README.md
warning: File listed twice: /opt/myadminscript/myscript.sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires: /bin/bash
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64
Wrote: /root/rpmbuild/SRPMS/myadminscript-1-1.src.rpm
Wrote: /root/rpmbuild/RPMS/noarch/myadminscript-1-1.noarch.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.0qbiER
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd myadminscript-1
+ /bin/rm -rf /root/rpmbuild/BUILDROOT/myadminscript-1-1.x86_64
+ exit 0
8. After successful built, a rpm file will created like ~/rpmbuild/RPMS/noarch/myadminscript-1-1.noarch.rpm
9. Install your rpm using the below command. After install check, the files are properly installed as defined location.
$ rpm -ivh myadminscript-1-1.noarch.rpm