Centos/RHEL/Fedora Build RPM Package From Scratch

Published on Author gryzli

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:

root@manvps [~/rpmbuild]# ls -lsa /root/rpmbuild/

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.

root@manvps [~]# touch test.txt 
root@manvps [~]# rpmbuild -bb test.txt

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:




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:


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


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:

root@manvps [~]# vim /root/rpmbuild/SPECS/example.spec

%define VER 1.0.0
Name:           example-package
Version:        %{VER}
Release:        1%{?dist}
Summary:        This is an example-package built with rpmbuild 
Group:          Development
License:        GPL
Prefix:         %{_prefix}
Source:         http://gryzli.info/example-package-1.0.0.tar.gz
AutoReqProv:    no

This is our example package, which will be used as a demonstration for my blog gryzli.info. 



mkdir -p ${RPM_BUILD_ROOT}/usr/local/custom_scripts
cp -a /usr/local/custom_scripts/scripts[123].pl ${RPM_BUILD_ROOT}/usr/local/custom_scripts/ 






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

mkdir -p ${RPM_BUILD_ROOT}/usr/local/custom_scripts
cp -a /usr/local/custom_scripts/scripts[123].pl ${RPM_BUILD_ROOT}/usr/local/custom_scripts/

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:

AutoReqProv:    no

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:


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:

root@manvps [~/rpmbuild/SPECS]# cd /root/rpmbuild/SPECS/
root@manvps [~/rpmbuild/SPECS]# rpmbuild  -v -bb example.spec

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

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