Linux RedHat How To Create An RPM Package

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
 
 
 
 
 
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Linux 20 Netstat Commands for Linux Network Management
Viewed 8214 times since Mon, Sep 21, 2020
How to find your System details using inxi
Viewed 2066 times since Sat, Jun 2, 2018
ZFS: Verify/change properties of a zfs filesystem
Viewed 1501 times since Sun, Jun 3, 2018
RHEL: Building a custom kernel on RHEL 6
Viewed 1699 times since Sat, Jun 2, 2018
How to stop and disable auditd on RHEL 7
Viewed 26947 times since Tue, Aug 6, 2019
RHEL: Adding a boot entry to GRUB/GRUB2 configuration
Viewed 2207 times since Sun, May 27, 2018
Linux 16 Useful Bandwidth Monitoring Tools to Analyze Network Usage in Linux
Viewed 7889 times since Mon, Sep 21, 2020
Learn how to align an SSD on Linux
Viewed 9663 times since Fri, May 15, 2020
RHEL: Display swap/RAM size
Viewed 1917 times since Sat, Jun 2, 2018
ZFS: Remove an existing zfs filesystem
Viewed 1047 times since Sun, Jun 3, 2018