• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Create RPM for Your Own Script in CentOS/RedHat

Written by Rahul, Updated on October 25, 2017

RPM (RedHat Package Manager) is a package management system for RHEL based systems. You may have seen that all the packages in Redhat based systems have extension .rpm. This tutorial will help you to how to create RPM for your own script

I had created a script to take database backup, Today I have created an RPM file of that script, This is my first RPM created ever. Below are the steps which I follow to do it.

Step 1 – Install Required Packages

First of all, you need to install the required packages on your system to create rpm files.

$ yum install rpm-build rpmdevtools

Step 2 – Create Directory Structure

Go to users home directory, and create required directory structure using below command.

$ rpmdev-setuptree

Above command will create a directory structure like below.

$ ls -l rpmbuild

drwxr-xr-x 2 root root 6 Oct 25 03:09 BUILD
drwxr-xr-x 2 root root 6 Oct 25 03:09 RPMS
drwxr-xr-x 2 root root 6 Oct 25 03:09 SOURCES
drwxr-xr-x 2 root root 6 Oct 25 03:09 SPECS
drwxr-xr-x 2 root root 6 Oct 25 03:09 SRPMS

In case rpmdev-setuptree command failed to create structure, you can manually create it

$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS,tmp}

Step 3 – Create rpmmacro File

Now create ~/.rpmmacros file, with the following contents. Change the values of red highlighted names

~/.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

Step 4 – Copy Files under SOURCES Directory

Copy all your files and scripts folder inside ~/rpmbuild/SOURCES directory, which we need to add in rpm file. For this tutorial, I have used all the files created in our tutorial Advance Bash Script for MySQL.

$ cd ~/rpmbuild/SOURCES
$ ls -l mydumpadmin-1

-rw-r--r-- 1 root root  125 Oct 25 03:54 credentials.txt
-rw-r--r-- 1 root root 4693 Oct 25 03:54 mysql-dump.sh
-rw-r--r-- 1 root root 1008 Oct 25 03:54 README.md
-rw-r--r-- 1 root root 3445 Oct 25 03:54 settings.conf

Create a tarball of your code.

$ tar czf mydumpadmin-1.0.tar.gz mydumpadmin-1

Step 5 – Create SPEC File

Create a spec file ~/rpmbuild/SPECS/mydumpadmin.spec using below content.

~/rpmbuild/SPECS/mydumpadmin.spec

Name:           mydumpadmin
Version:        1
Release:        0
Summary:        An Advance Bash Script for MySQL Database Backup

Group:          TecAdmin
BuildArch:      noarch
License:        GPL
URL:            https://github.com/tecrahul/mydumpadmin.git
Source0:        mydumpadmin-1.0.tar.gz

%description
Write some description about your package here

%prep
%setup -q
%build
%install
install -m 0755 -d $RPM_BUILD_ROOT/etc/mydumpadmin
install -m 0600 credentials.txt $RPM_BUILD_ROOT/etc/mydumpadmin/credentials.txt
install -m 0755 mysql-dump.sh $RPM_BUILD_ROOT/etc/mydumpadmin/mysql-dump.sh
install -m 0644 README.md $RPM_BUILD_ROOT/etc/mydumpadmin/README.md
install -m 0644 settings.conf $RPM_BUILD_ROOT/etc/mydumpadmin/settings.conf

%files
/etc/mydumpadmin
/etc/mydumpadmin/credentials.txt
/etc/mydumpadmin/mysql-dump.sh
/etc/mydumpadmin/README.md
/etc/mydumpadmin/settings.conf

%changelog
* Tue Oct 24 2017 Rahul Kumar  1.0.0
  - Initial rpm release

Change package name, script path, archive name, description etc, as per your requirement.

Step 6 – Build RPM

After completing above steps, lets build your rpm by executing following command.

$ cd ~/rpmbuild
$ rpmbuild -ba SPECS/mydumpadmin.spec

After successful built, a rpm file will created like ~/rpmbuild/RPMS/noarch/mydumpadmin-1-0.noarch.rpm

Install your rpm using the below command. After install check, the files are properly installed as defined location.

$ rpm -ivh mydumpadmin-1-0.noarch.rpm

Thank You for using this article. I hope above steps will help you to create your own rpm.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

9 Comments

  1. Avatar niloofar Reply
    June 23, 2020 at 5:05 am

    thankyou! your tutorials are greate .

  2. Avatar Soumya Jain Reply
    April 27, 2020 at 6:34 am

    Hi,

    We have custom scripts to perform certain installation with require dynamic inputs to be passed at the time we run the script which cannot be achieved using RPM packages. I want to understand if there is a way we can still use our own scripts,gather post installation information and create a SPEC file(no installation). May be just the below information:

    Name: mydumpadmin
    Version: 1
    Release: 0
    Summary: An Advance Bash Script for MySQL Database Backup

    Group: TecAdmin
    BuildArch: noarch
    License: GPL
    URL: https://github.com/tecrahul/mydumpadmin.git
    Source0: mydumpadmin-1.0.tar.gz

    %description
    Write some description about your package here

    With no source, can we package and create an RPM package with the above requirements?

  3. Avatar LucaC Reply
    September 25, 2019 at 8:40 am

    If I have to create a general package and then 3 others installation that change only some parameter in some text file, how this can be done?

  4. Avatar Abhishek Reply
    May 23, 2019 at 6:39 am

    Where can I find my installed package?

    • Rahul Rahul Reply
      May 24, 2019 at 12:01 pm

      Hi Abhishek, The file locations of the installed file will be the same as defined in the .spec file.

  5. Avatar sherif Reply
    July 25, 2017 at 8:25 am

    if i want to add another require RPMS before i install my script how to add as example want to include “yum install rsync ” in my rpm package how i can do that ?

    • Rahul Rahul K. Reply
      July 26, 2017 at 4:13 am

      You can use Requires tag to install dependencies for your package

      http://wiki.rosalab.ru/en/index.php/RPM_spec_file_syntax#Requires.2C_Obsoletes.2C_Provides.2C_Conflicts.2C_etc.

  6. Avatar Doctor Coldfoot Reply
    October 31, 2016 at 8:30 pm

    Only One little problem. The Group tag “Rahul” is invalid. For a list of usable groups check out:
    /usr/share/doc/rpm-/GROUPS

  7. Avatar arif Reply
    October 25, 2016 at 11:17 am

    I was preparing a new rpm to take backup of /root/Downloads/webmin to /var/tmp/webmin.tar.gz
    I installed the newly built rpm package but not able to see the package in rpm -qa output.

    [[email protected] noarch]# rpm -qa | grep -i webmin1-1-0.noarch.rpm
    [[email protected] noarch]#

    Can anybody let me know how can this be solved ?

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How To Install Python 3.9 on Ubuntu 18.04 0
  • How to Use AppImage on Linux (Beginner Guide) 2
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy