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
RHCS: Configure an active/backup pacemaker cluster
Viewed 9164 times since Sun, Jun 3, 2018
Managing temporary files with systemd-tmpfiles on Red Hat Enterprise Linux 7
Viewed 9798 times since Sun, Nov 22, 2020
debian How to check Debian CVE status using python script
Viewed 3727 times since Sun, Sep 23, 2018
Easily Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
Viewed 4249 times since Thu, Apr 18, 2019
KONTO SFTP Z CHROOTEM Z UŻYCIEM OPENSSH-SERVER NA CENTOS/RHEL6
Viewed 1985 times since Fri, Nov 30, 2018
Linux Health Check Commands
Viewed 3313 times since Fri, Jun 8, 2018
RHCS6: Extend an existing Logical Volume / GFS2 filesystem
Viewed 3400 times since Sun, Jun 3, 2018
LVM: Extend an existing Logical Volume / Filesystem
Viewed 2657 times since Sat, Jun 2, 2018
How To Use Systemctl to Manage Systemd Services and Units
Viewed 7669 times since Mon, Dec 7, 2020
How to use yum command on CentOS/RHEL
Viewed 11181 times since Wed, Oct 17, 2018