Centos/RHEL/Fedora Build RPM Package From Scratch

Published on Author gryzliLeave a comment
Build custom RPM on centos/rhel/fedora

Why to create your own RPM Package ?

In the following post, I will explain how one could easily build RPM package from a custom files.

Let say you have added some custom scripts or files and you want to create RPM of them, here is how to do it.

 

How to build your own RPM Package ?

1| Install rpm-build

yum install rpm-build

 

2| Check your rpmbuild building tree

After installing rpm-build you must have rpmbuild tree, which looks something like that:

total 32
4 drwxr-xr-x  8 root root 4096 Oct  6 18:23 ./
4 dr-xr-x--- 35 root root 4096 Oct  7 22:52 ../
4 drwxr-xr-x  6 root root 4096 Oct  7 19:01 BUILD/
4 drwxr-xr-x  5 root root 4096 Oct  7 19:01 BUILDROOT/
4 drwxr-xr-x  3 root root 4096 Oct  6 18:41 RPMS/
4 drwxr-xr-x  2 root root 4096 Oct  7 18:58 SOURCES/
4 drwxr-xr-x  2 root root 4096 Oct  7 18:55 SPECS/
4 drwxr-xr-x  2 root root 4096 Oct  6 18:41 SRPMS/

 

If you don’t already have this tree (which will be later needed for the build) you could create it easily, by trying to run rpmbuild with wrong arguments.

error: File /root/test.txt does not appear to be a specfile.

Even that the command returned error, you will already have automatically created BUILD tree undre /root/rpmbuild directory.

 

3| Prepare your files for building

Let say you have some files, which resides here:

/usr/local/custom_scripts/script1.pl

/usr/local/custom_scripts/script2.pl

/usr/local/custom_scripts/script3.pl

And you want your RPM package to be able to install these files.

 

3.1| Create .spec file

Building RPM needs you to create SPECS file, which will be used for the build process.

SPEC files are usually resided in the following directory:

/root/rpmbuild/SPECS/

More information on the syntax of .spec files may be found here:

http://www.rpm.org/max-rpm/s1-rpm-build-creating-spec-file.html

Another useful source of well written .spec files are the SRPM packages, which you can download , install and inspect.

I will give my example with some pretty simple .spec file:

[email protected] [~]# vim /root/rpmbuild/SPECS/example.spec

 

Basically our .spec files tells the following with this section:

1) Create /usr/local directory tree inside the ${RPM_BUILD_ROOT}

2) Copy our scripts to this directory

${RPM_BUILD_ROOT} directory is a special variable, which usually points to this directory: /root/rpmbuild/BUILDROOT/example-package-1.0.0-1.el6.x86_64/

Whatever is the tree under that directory, this would be installed after installing the package.

Another very important part of the spec file is this directive:

Without this directive, RPM will examine all files, which begin with “#!” and add the executables as a dependencies for the package.

So if your script begins with something like this:

#!/usr/bin/perl

AutoReqProv, will add automatically perl as an dependency to your RPM, which I don’t want.

3.2| Build your RPM Package from the SPEC file

Now that your SPEC file is ready, you could try build your RPM package:

If everything is okay, your resulting RPM package must be here:

[email protected] [~/rpmbuild/SPECS]# ls  /root/rpmbuild/RPMS/x86_64/  
./  ../  example-package-1.0.0-1.el6.x86_64.rpm

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.