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
HP-UX - Stunnel Configuration
Viewed 2456 times since Fri, Sep 28, 2018
Check a Website Availability from the Linux Command Line
Viewed 6725 times since Mon, Feb 18, 2019
How to run command or code in parallel in bash shell under Linux or Unix
Viewed 3316 times since Tue, Aug 6, 2019
www.unixarena.com
Viewed 2506 times since Fri, Jul 27, 2018
Easily Find Bugs In Shell Scripts With ShellCheck
Viewed 3382 times since Thu, Apr 18, 2019
RHEL7: How to get started with Firewalld.
Viewed 12174 times since Wed, May 22, 2019
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
Viewed 4892 times since Thu, Feb 11, 2021
LVM: Extend an existing Volume Group by adding a new disk
Viewed 2132 times since Sat, Jun 2, 2018
Prosty skaner portów TCP w bash
Viewed 3393 times since Thu, May 24, 2018
LVM: Reduce root PV/VG
Viewed 5290 times since Sat, Jun 2, 2018