Packaging software into RPM (Red Hat Package Manager) format is an essential skill for developers and system administrators working with Red Hat-based Linux distributions such as CentOS and RedHat. RPMs allow for easy installation, upgrade, and removal of software, ensuring that applications can be distributed and managed efficiently. This guide will walk you through the process of packaging your script into an RPM, step by step.
Prerequisites
Before we begin, ensure you have the following:
- A CentOS or RedHat system for packaging and testing.
- Your script that you wish to package.
- rpm-build and rpmdevtools packages installed. You can install them by running:
sudo dnf install rpm-build rpmdevtools
Step 1: Setting Up Your Environment
First, prepare your environment by setting up the RPM build directory structure in your home directory.
rpmdev-setuptree
This command creates a rpmbuild directory with several subdirectories (BUILD, RPMS, SOURCES, SPECS, and SRPMS) in your home directory.
Step 2: Preparing Your Script
Place your script into the SOURCES directory. If your script has external dependencies, ensure they are documented or included.
Step 3: Writing the Spec File
The spec file is a blueprint for building your RPM. It includes metadata about your package and instructions on how to build and install it. Create a new spec file in the SPECS directory. For example:
vi ~/rpmbuild/SPECS/myscript.spec
Here’s a basic template for a spec file:
Name: myscript
Version: 1.0
Release: 1%{?dist}
Summary: A brief description of your script.
License: GPL
URL: http://yourwebsite.com
Source0: %{name}-%{version}.tar.gz
BuildArch: noarch
%description
A longer description of your script.
%prep
%setup -q
%build
# Your build commands here (if any).
%install
mkdir -p %{buildroot}/usr/local/bin
install -m 755 %{name} %{buildroot}/usr/local/bin/%{name}
%files
/usr/local/bin/%{name}
%changelog
* Date Your Name <[email protected]> - 1.0-1
- Initial package
Replace myscript with the name of your script, and adjust the version, release, summary, license, and URL as needed. Fill in the description with a detailed description of your script. The %files section lists the files to be included in the package.
Step 4: Building the RPM
Before building the RPM, you need to create a source tarball of your script and place it in the SOURCES directory. Assuming your script is in the root of the rpmbuild/SOURCES directory:
tar czf myscript-1.0.tar.gz myscript
Now, build the RPM package:
rpmbuild -ba ~/rpmbuild/SPECS/myscript.spec
Step 5: Testing Your RPM
After building the RPM, you’ll find it in the RPMS subdirectory of your rpmbuild directory. Install it using the yum or dnf command to test:
sudo dnf localinstall ~/rpmbuild/RPMS/noarch/myscript-1.0-1.el7.noarch.rpm
Replace the path and filename as necessary according to your package’s name and version.
Conclusion
Packaging your script into an RPM can streamline the deployment process, making it easier to distribute, install, and manage your software on CentOS or RedHat systems. By following this guide, you’ve learned the basics of RPM packaging, from setting up your environment and preparing your script to writing a spec file and building your package. With these skills, you can ensure your software is accessible and maintainable across Red Hat-based distributions.
9 Comments
thankyou! your tutorials are greate .
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?
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?
Where can I find my installed package?
Hi Abhishek, The file locations of the installed file will be the same as defined in the .spec file.
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 ?
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.
Only One little problem. The Group tag “Rahul” is invalid. For a list of usable groups check out:
/usr/share/doc/rpm-/GROUPS
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 ?