Linux : A Step-by-Step Guide to Creating and Building Your Own RPM Package



In the world of Linux, packaging software into RPMs (Red Hat Package Manager) is a fundamental skill for system administrators and developers alike. This blog post provides a comprehensive guide on how to create and build your own RPM package from scratch, using a simple Python script as an example.


Table of Contents:


Step 1: Installing Required Tools:

Using yum to install essential tools: rpmdevtools and rpmlint.

Ensuring necessary dependencies are met.


[root@server2 ~]#  yum -y install rpmdevtools rpmlint


Step 2: Setting Up RPM Build Environment:

Running rpmdev-setuptree to create the RPM directory structure.

Verifying the existence of essential directories: BUILD, RPMS, SOURCES, SPECS, and SRPMS.


[root@server2 ~]# rpmdev-setuptree 

[root@server2 ~]# pwd

/root

[root@server2 ~]# ls

anaconda-ks.cfg  curl-7.32.0  curl.tar.gz  rpmbuild  script  utils  webtoolplate

[root@server2 ~]# cd rpmbuild/

[root@server2 rpmbuild]# ll

total 0

drwxr-xr-x 2 root root 6 Jan  4 10:43 BUILD

drwxr-xr-x 2 root root 6 Jan  4 10:43 RPMS

drwxr-xr-x 2 root root 6 Jan  4 10:43 SOURCES

drwxr-xr-x 2 root root 6 Jan  4 10:43 SPECS

drwxr-xr-x 2 root root 6 Jan  4 10:43 SRPMS


Step 3: Creating a Test Project:


Creating a directory for the test project.

Writing a simple Python script (hello_echo.py) that prints a message.


[root@server2 ~]# mkdir testproject1-1.0

[root@server2 ~]# cd testproject1-1.0/

[root@server2 testproject1-1.0]# echo 'print("Hello, Builddevops Users!")' > hello_echo.py

[root@server2 testproject1-1.0]# ls

hello_echo.py


[root@server2 testproject1-1.0]# chmod +x hello_echo.py 


Step 4: Packaging the Test Project:


Compressing the project directory into a tarball (testproject1-1.0.tar.gz).

Moving the tarball to the rpmbuild/SOURCES directory.


[root@server2 ~]# tar -cvpf testproject1-1.0.tar.gz testproject1-1.0/

testproject1-1.0/

testproject1-1.0/hello_echo.py


[root@server2 ~]# mv testproject1-1.0.tar.gz rpmbuild/SOURCES/


Step 5: Creating the RPM Spec File:


Generating a new spec file using rpmdev-newspec.

Editing the spec file (testproject1.spec) to include metadata, dependencies, and build instructions.

[root@server2 ~]# rpmdev-newspec /root/rpmbuild/SPECS/testproject1.spec

/root/rpmbuild/SPECS/testproject1.spec created; type minimal, rpm version >= 4.11.


Step 6 : Building the Source RPM:


Running rpmbuild -bs to build the source RPM.

Verifying the creation of SRPMS/testproject1-1.0-1.el7.src.rpm


[root@server2 ~]#  rpmbuild -bs /root/rpmbuild/SPECS/testproject1.spec

Wrote: /root/rpmbuild/SRPMS/testproject1-1.0-1.el7.src.rpm


[root@server2 ~]# vi /root/rpmbuild/SPECS/testproject1.spec


sample spec file:

Name:           testproject1
Version:        1.0
Release:        1%{?dist}
Summary:        Script of testproject1
License:        GPL
URL:            https://builddevops.com
Source0:        %{name}-%{version}.tar.gz
Requires:       bash
%description
A test rpm creation for builddevops project
%prep
%setup -q
%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/%{_bindir}
cp hello_echo.py $RPM_BUILD_ROOT/%{_bindir}
%clean
rm -rf $RPM_BUILD_ROOT
%files
%{_bindir}/hello_echo.py
%changelog

Step 7 : Building the Binary RPM:


Executing rpmbuild -bb to build the binary RPM.

Verifying the successful creation of the RPM package: RPMS/x86_64/testproject1-1.0-1.el7.x86_64.rpm.

 

[root@server2 ~]# rpmbuild -bb /root/rpmbuild/SPECS/testproject1.spec

Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.zzbnlH

+ umask 022

+ cd /root/rpmbuild/BUILD

+ cd /root/rpmbuild/BUILD

+ rm -rf testproject1-1.0

+ /usr/bin/tar -xf /root/rpmbuild/SOURCES/testproject1-1.0.tar.gz

+ cd testproject1-1.0

+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .

+ exit 0

Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.hRSVvX

+ umask 022

+ cd /root/rpmbuild/BUILD

+ '[' /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64 '!=' / ']'

+ rm -rf /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64

++ dirname /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64

+ mkdir -p /root/rpmbuild/BUILDROOT

+ mkdir /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64

+ cd testproject1-1.0

+ rm -rf /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64

+ mkdir -p /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64//usr/bin

+ cp hello_echo.py /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64//usr/bin

+ '[' '%{buildarch}' = noarch ']'

+ QA_CHECK_RPATHS=1

+ case "${QA_CHECK_RPATHS:-}" in

+ /usr/lib/rpm/check-rpaths

+ /usr/lib/rpm/check-buildroot

+ /usr/lib/rpm/redhat/brp-compress

+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip

+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump

+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip

+ /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1

+ /usr/lib/rpm/redhat/brp-python-hardlink

+ /usr/lib/rpm/redhat/brp-java-repack-jars

Processing files: testproject1-1.0-1.el7.x86_64

Provides: testproject1 = 1.0-1.el7 testproject1(x86-64) = 1.0-1.el7

Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1

Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64

Wrote: /root/rpmbuild/RPMS/x86_64/testproject1-1.0-1.el7.x86_64.rpm

Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.Pncuuu

+ umask 022

+ cd /root/rpmbuild/BUILD

+ cd testproject1-1.0

+ rm -rf /root/rpmbuild/BUILDROOT/testproject1-1.0-1.el7.x86_64

+ exit 0


[root@server2 ~]# ll /root/rpmbuild/RPMS/x86_64/testproject1-1.0-1.el7.x86_64.rpm 

-rw-r--r-- 1 root root 2104 Feb  3 18:55 /root/rpmbuild/RPMS/x86_64/testproject1-1.0-1.el7.x86_64.rpm


Step 8 :Installing and Verifying the RPM:


Using rpm -ivh to install the newly created RPM.


[root@server2 ~]# rpm -ivh /root/rpmbuild/RPMS/x86_64/testproject1-1.0-1.el7.x86_64.rpm 

Preparing...                          ################################# [100%]

Updating / installing...

   1:testproject1-1.0-1.el7           ################################# [100%]


Verifying the installation path and the functionality of the Python script.


[root@server2 ~]# ls -lrth /bin/hello_echo.py 

-rwxr-xr-x 1 root root 35 Feb  3 18:55 /bin/hello_echo.py


[root@server2 ~]# cat /bin/hello_echo.py 

print("Hello, Builddevops Users!")


[root@server2 ~]# python /bin/hello_echo.py 

Hello, Builddevops Users!


[root@server2 ~]# rpm -qa | grep test

testproject1-1.0-1.el7.x86_64


[root@server2 ~]# rpm -ql testproject1-1.0-1.el7.x86_64

/usr/bin/hello_echo.py


Congratulations! 

You have successfully created and built your own RPM package. This foundational skill opens up opportunities for efficient software distribution and deployment in the Linux ecosystem. Feel free to explore further, customize your spec files, and get into more advanced RPM packaging techniques.  Happy packaging!

======================================================================

        Thank you,check out more blogs for such information !!

                                                                            For any queries,contact shubhammore07007@gmail.com

Comments

Popular posts from this blog

Nginx : Setting Up Nginx with Load Balancing

Nginx : Virtual hosting using Location directives

Terraform project with AWS to build infra within seconds! (Web Ec2 with eIP)