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
RHEL: Adding a boot entry to GRUB/GRUB2 configuration
Viewed 4464 times since Sun, May 27, 2018
How to use yum-cron to automatically update RHEL/CentOS Linux 6.x / 7.x
Viewed 4887 times since Tue, Dec 4, 2018
high swap space utilization in LINUX
Viewed 6446 times since Fri, Jul 13, 2018
How to Analyze or Read OS Watcher Output in three easy steps -- With Example ?
Viewed 41250 times since Thu, Jun 21, 2018
RHCS6: Basic operations on clustered services
Viewed 2548 times since Sun, Jun 3, 2018
Tip: SSD and Linux. Enable TRIM and check if it works
Viewed 14366 times since Fri, May 15, 2020
6 easy steps to setup offline two factor authentication in Linux
Viewed 12497 times since Mon, Apr 6, 2020
HOWTO: Use SSL/port 465 in smarthost stunnel
Viewed 3664 times since Fri, Sep 28, 2018
10 nmap Commands Every Sysadmin Should Know
Viewed 9796 times since Wed, May 22, 2019
Watchdog script to keep an application running
Viewed 18334 times since Tue, Jul 31, 2018