Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Linux Tutorials»How to Create RPM for Your Own Script in CentOS/RedHat

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

    By RahulOctober 25, 20173 Mins Read

    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.

    Create own RPM Create RPM Create RPM for my Script Create RPM in CentOS Create RPM in Redhat Create RPM of your own script in CentOS Create RPM of your own script in Redhat How can I create RPM How do i create rpm How to create RPM Steps to create RPM
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Understanding the LD_LIBRARY_PATH Environment Variable

    The Beginner’s Guide to Building Your First RPM Package

    The Beginner’s Guide to Building Your First Debian Package

    View 9 Comments

    9 Comments

    1. niloofar on June 23, 2020 5:05 am

      thankyou! your tutorials are greate .

      Reply
    2. Soumya Jain on April 27, 2020 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?

      Reply
    3. LucaC on September 25, 2019 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?

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

      Where can I find my installed package?

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

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

        Reply
    5. sherif on July 25, 2017 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 ?

      Reply
      • Rahul K. on July 26, 2017 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.

        Reply
    6. Doctor Coldfoot on October 31, 2016 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

      Reply
    7. arif on October 25, 2016 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.

      [root@localhost noarch]# rpm -qa | grep -i webmin1-1-0.noarch.rpm
      [root@localhost noarch]#

      Can anybody let me know how can this be solved ?

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.