How to build RPM and The commands usage

How to build RPM

Creating a RPM for your own software requires three basic steps.

  • Making sure the rpm-build software is installed.
  • Perhaps modifying your software install process to be rpm “aware”.
  • Writing a spec file.

This last step is the heart of the matter. The “spec” file tells rpm-build everything that it needs to know to make the software and install it.

Install rpm-build:-

First, you should check that you have rpmbuild installed on your system. This is the tool you will use to build RPMs from spec files or SRPM packages.

$ rpmbuild –showrc

If the system returns “rpmbuild command not found”, It means rpmbuild is NOT yet installed. You can install it with yum by running the following command.

$ sudo yum install rpm-build

After yum is finished, run the rpmbuild –showrc or rpmbuild –version command to verify the installation.

Most SRPMs targeted to be rebuilt on CentOS also need certain rpmbuild build macros and helper scripts, which are contained in package “redhat-rpm-config”. To verify redhat-rpm-config installation

$ rpm -qa redhat-rpm-config*

If it is not installed, install it using yum.

$ sudo yum install redhat-rpm-config

Other tools you may need:- In particular, you will most probably need to install “make” to build software

$ sudo yum install make

If you are building RPMs for software written in C, you will also need the gcc compiler.

$ sudo yum install gcc

Create directories for RPM building under your home:-

The next step is to create the files and directories under your home directory that you need to build RPMs.As noted before, to avoid possible system libraries and other files damage, you should NEVER build an RPM with the root user.You should always use an unprivileged user for this purpose.

The best solution is to create a separate account solely for building and testing RPMs.I created an “rpmbuild” account and use it for just this purpose.This might seem a bit tedious, but it is a lot safer.

When I refer ~/ I am assuming you are logged into the “rpmbuild” account. First you need to create the following directories structure to build RPMS.

$ mkdir ~/rpm ~/rpm/BUILD ~/rpm/RPMS ~/rpm/RPMS/i386 ~/rpm/RPMS/i686 ~/rpm/RPMS/x86_64 ~/rpm/RPMS/noarch ~/rpm/SOURCES ~/rpm/SPECS ~/rpm/SRPMS ~/rpm/tmp

  • SOURCES — Contains the original sources, patches, and icon files.
  • SPECS — Contains the spec files used to control the build process.
  • BUILD — The directory in which the sources are unpacked, and the software is built.
  • RPMS — Contains the binary package files created by the build process.
  • SRPMS — Contains the source package files created by the build process.

These are the directories where the build process will take place.You might have to add more directories if your system architecture is different from i386 or i686 or x86_64.

Then you need to create a file ~/.rpmmacros with at least the following lines:

%packager selvam

%_topdir /home/rpmbuild/rpm

%_tmppath /home/rpmbuild/rpm/tmp

Replace “rpmbuild” with the name of the account you will be using to build RPMs if different.

Writing the spec file:-

Just create file with the extension of .spec, it will create file with default option like Name, version etc..

$ cd ~/rpm/SPECS

$ vi myspec.spec

Building the RPMs

Finally you are ready to make an RPM.The following commands should do the trick.

$ cp myspec.spec ~/rpm/SPECS/

$ rpmbuild -ba ~/rpm/SPECS/myspec.spec

Notice that your source files should be in a directory with the appropriate name and version number before tar’ing.

RPM Commands:-

Here you can find some useful RPM command to install, remove and find details about the rpm package.

  1. –v => see more of RPM’s inner workings

    $ rpm -ivv rpm-2.0.1-1.x86_64.rpm

  2. –test => Perform installation or erase test only

    $ rpm -i –test rpm-2.0.1-1.x86_64.rpm

    $ rpm -e –test rpm-2.0.1-1.x86_64.rpm

  3. –replacepkgs => Install the package even if already installed

    $ rpm -iv –replacepkgs rpm-2.0.1-1.x86_64.rpm

  4. –replacefiles => Install the package even if it replaces another package’s files

    $ rpm -iv –replacefiles rpm-2.0.1-1.x86_64.rpm

  5. –nodeps => Don’t check dependencies before installing Package

    $ rpm -iv –nodeps rpm-2.0.1-1.x86_64.rpm

  6. –force => The big hammer

    $ rpm -iv –force rpm-2.0.1-1.x86_64.rpm

  7. –excludedocs => Don’t install documentation for this package

    $ rpm -iv –excludedocs rpm-2.0.1-1.x86_64.rpm

  8. –includedocs => Install documentation for this package

    $ rpm -iv –includedocs rpm-2.0.1-1.x86_64.rpm

  9. –prefix <path> => Relocate the package to <path>, if possible If the package is not relocatable, you’ll only see the word(none). If on the other hand, the command displays a path, that means the package is relocatable.

    $ rpm -qp –queryformat “%{defaultprefix}\n” rpm-2.0.1-1.x86_64.rpm /usr/local

    $ rpm -iv –prefix /tmp/test rpm-2.0.1-1.x86_64.rpm

  10. –noscripts => Don’t execute Pre- and Post-install scripts

    $ rpm -iv –noscripts rpm-2.0.1-1.x86_64.rpm

  11. –oldpackage => Upgrade To An Older Version

    $ rpm -Uv –oldpackage rpm-2.0.1-0.x86_64.rpm

  12. -qa => query all installed package

    $ rpm -qa | grep rpm-2.0.1

  13. -qi => query display installed package information

    $ rpm -qi rpm-2.0.1-0.x86_64

    $ rpm -qip rpm-2.0.1-0.x86_64.rpm

  14. -ql => query display installed package’s file list

    $ rpm -ql rpm-2.0.1-0.x86_64

    $ rpm -qlv rpm-2.0.1-0.x86_64 (Additional information)

  15. -qlp => query display rpm package’s file list

    $ rpm -qlp rpm-2.0.1-0.x86_64.rpm

  16. –scripts => Show Scripts Associated With a Package

    $ rpm -q –scripts rpm-2.0.1-0.x86_64

  17. Extract rpm without Installing

    $ rpm2cpio rpm-2.0.1-0.x86_64.rpm | cpio -idmv

  18. Verify rpm MD5 and signature

    $ rpm –Kv rpm-2.0.1-0.x86_64.rpm

    $ rpm -Vp rpm-2.0.1-0.x86_64.rpm

  19. Identifies the package associated with /path/to/file.

    $ rpm -qf /path/to/file

Leave a comment