Building RPMs for RHEL6 or CentOS6
In this post I will build a very simple RPM, this RPM will contain a very useful program/shell script. With this information you can build complex RPMs later on.
Set up your build environment
In this case I am using a RHEL 6.5 64bit system
[root@rpmbuild ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Install the tools: rpm-build: is what you need to build RPMs rpmdevtools: is not required but it is very helpful because it helps you create the directory tree and base SPEC file
[root@rpmbuild ~]# yum install rpm-build rpmdevtools
Create a non-privileged user to build the RPMs
[root@rpmbuild ~]# useradd rpmbuilder
[root@rpmbuild ~]# su - rpmbuilder
[rpmbuilder@rpmbuild ~]$
Create the directory tree using rpmdev-setuptree
[rpmbuilder@rpmbuild ~]$ rpmdev-setuptree
[rpmbuilder@rpmbuild ~]$ tree rpmbuild/
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
Package Application
Work on packaging your application/program (e.g. very_useful_script.sh) The folder and the archive naming is important for later when they get unarchived, the rpm tools will by default use name-version (e.g. name=very-useful-script, version=1.0, that is why the folder/archive was named very-useful-script-1.0/ ). That is the default and can be easily changed in the SPEC file.
[rpmbuilde@rpmbuild ~]$ ls
rpmbuild very_useful_script.sh
[rpmbuilde@rpmbuild ~]$ mkdir very-useful-script-1.0
[rpmbuilde@rpmbuild ~]$ mv very_useful_script.sh very-useful-script-1.0/
[rpmbuilde@rpmbuild ~]$ tar cvzf very-useful-script-1.0.tgz very-useful-script-1.0/
[rpmbuilde@rpmbuild ~]$ ls
rpmbuild very-useful-script-1.0 very_useful_script.sh very-useful-script-1.0.tgz
Move your packaged application to the SOURCES directory under rpmbuild/
[rpmbuilde@rpmbuild ~]$ mv very-useful-script-1.0.tgz ~/rpmbuild/SOURCES/
Now it is time to create the SPEC
Create a skeleton spec file
[rpmbuilde@rpmbuild ~]$ rpmdev-newspec
Skeleton specfile (minimal) has been created to "newpackage.spec".
Move it to your directory tree
[rpmbuilde@rpmbuild ~]$ mv newpackage.spec ~/rpmbuild/SPECS/very-useful-script.spec
This is how your directory tree should look like
[rpmbuilde@rpmbuild ~]$ tree rpmbuild/
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
│ └── very-useful-script.tgz
├── SPECS
│ └── very-useful-script.spec
└── SRPMS
5 directories, 2 files
SPEC file:
Name: very-useful-script
Version: 1.0
Release: 1%{?dist}
Summary: This is a very useful script
Group: Applications/System
License: MIT
URL: http://example.com
Source: very-useful-script-1.0.tgz
%description
This is a very useful script
%prep
%setup -q
%install
rm -rf $RPM_BUILD_ROOT
install -d $RPM_BUILD_ROOT/usr/local/bin/
install -m 755 very_useful_script.sh $RPM_BUILD_ROOT/usr/local/bin/very_useful_script.sh
%clean
rm -rf $RPM_BUILD_ROOT
%files
%dir /usr/local/bin
%defattr(-,root,root,-)
%doc
/usr/local/bin/very_useful_script.sh
%changelog
Dissecting the SPEC file The below is header information and just descriptive data
Name: very-useful-script
Version: 1.0
Release: 1%{?dist}
Summary: This is a very useful script
Group: Applications/System
License: MIT
URL: http://example.com
Source: very-useful-script-1.0.tgz
%description
This is a very useful script
The below is where we prepare our sources to be packaged into RPM %prep is a section where we can execute commands or use macros. %setup is a macro that unarchives the original sources. Earlier I was discussing the importance of naming the folder and archive as name-version, this is because the %setup macro expects that by default, but you can overwrite the default by specifying the folder name (e.g. %setup -q -n very-useful-script-1.0-john-x86)
%prep
%setup -q
OR
%prep
%setup -q -n very-useful-script-1.0-john-x86
The below removes previous remains of the files in the buildroot Then creates a folder /usr/local/bin/ in the buildroot Then puts our very_useful_script.sh in /usr/loca/bin with mode 755
%install
rm -rf $RPM_BUILD_ROOT
install -d $RPM_BUILD_ROOT/usr/local/bin/
install -m 755 very_useful_script.sh $RPM_BUILD_ROOT/usr/local/bin/very_useful_script.sh
The below just cleans the buildroot
%clean
rm -rf $RPM_BUILD_ROOT
The below specifies all the files that will be installed by the RPM You need to list them all, or use wildcards
%files
%dir /usr/local/bin
%defattr(-,root,root,-)
%doc
/usr/local/bin/very_useful_script.sh
Build the RPM using the SPEC file
[rpmbuilde@rpmbuild rpmbuild]$ rpmbuild -ba ~/rpmbuild/SPECS/very-useful-script.spec
After the RPM has been successfully been built, you can find it under:
[root@rpmbuild ~]# ls /home/rpmbuilde/rpmbuild/RPMS/x86_64/
Install it (need to be root)
[root@rpmbuild ~]# rpm -ivh /home/rpmbuilde/rpmbuild/RPMS/x86_64/very-useful-script-1.0-1.el6.x86_64.rpm
Hopefully this guide will help you when building RPMs.