Set Up an RPM Build Environment under CentOS

This document will guide you on how to install and configure an environment to build RPMs (and rebuild SRPMs) under CentOS.

<!> Building RPMs should NEVER be done with the root user. It should ALWAYS be done with an unprivileged user. Building RPMs as root might damage your system. You have been warned.

Check that you have rpmbuild installed

First, you should check that you have rpmbuild installed on your system. This is the tool you will use to build RPMs from specfiles or SRPM packages. To check that it is installed and , issue the rpmbuild --showrc command. A large set of data should be displayed, enumerating details of the build environment that rpmbuild is using. Quite useful for debugging what a .spec file is doing

If the system returns: $ rpmbuild: command not found this means rpmbuild is NOT yet installed. You can install it with yum by running the following command as root:

[root@hostname ~]# yum install rpm-build

As becoming root for running a command is only logged in the bash history file, most careful admins set up and use sudo for the task instead:

[userid@hostname ~]$ sudo yum install rpm-build

<!> Note: That for historical reasons, the package containing /usr/bin/rpmbuild is called rpm-build (that is, with a dash in the package name).

Verify that yum listed a version of the rpm-build package in the list of packages to install, and answer "y" to allow yum to go ahead and install the package.

After yum is finished, run the rpmbuild --showrc or the more terse rpmbuild --version command to check that it is installed.

Most SRPMs targetted to be rebuilt on CentOS also need certain rpmbuild build macros and helper scripts, which are contained in package: redhat-rpm-config. To get results as desired, you should also install it in the same fashion as noted above, substituting the new package name.

[userid@hostname ~]$ sudo yum install redhat-rpm-config

<!> Note: You may have this package already installed. In such a case yum will just output Nothing to do on the last line of its output. You can check if a package is already installed (here checking on redhat-rpm-config) with the command rpm -q redhat-rpm-config . If there is any output, that means the package is already there. The output of the rpm command will also include the version and release information of that package that is installed on your system.

Create directories for RPM building under your home

After you have rpmbuild installed, 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.

To build RPMs with an unprivileged user, you must create a directory structure for that purpose, and then create the .rpmmacros file under your home directory overriding the default location of the RPM building tree to the one you created.

The instructions below will create a rpmbuild directory under your home directory to build RPMs. If you want to use a different directory, you will have to adapt the instructions below to your usage. See the external references for documentation on how to do more complex configurations.

To create the RPM building environment, run the two commands below:

[userid@hostname ~]$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

<!> Beware: this next command will overwrite an existing .rpmmacros file if it exists, so check that you don't already have one before continuing.

[userid@hostname ~]$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

After running the two commands above, your environment is set up to build most RPMs without further setup.

Other tools you may need

In general, building RPMs means building and compiling software. To do that, in general you will need tools needed to compile and build source packages.

In particular, you will most probably need to install make to build software (even software that is not written in C or in a compiled language usually uses Makefiles for its install process). As before:

[userid@hostname ~]$ sudo yum install make

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

[root@host ~]# yum install gcc

Some dependencies will probably be installed together with gcc to allow for the compilation against system libraries.

If you are building software that uses system libraries (such as OpenSSL, for example), you will need to install additional RPMs that allow you to build software against those libraries.

Using OpenSSL as an example, there are two separate binary RPMs: openssl and openssl-devel. The openssl RPM package contains the libraries needed to run binaries linked against openssl. For example, wget needs OpenSSL libraries for encrypted connections, so to install the wget RPM, the openssl RPM will be required, and installed as well.

However, the openssl RPM does not contain the library header files needed to compile code against the OpenSSL libraries. A decision by a spec file packaging team to split these less commonly needed header files in designing a distribution is a common one , in order to conserve space on non-developer machines.

For example, if you download wget source code and try to build it, it will complain that it can not find the OpenSSL libraries. The files needed to compile code with OpenSSL are included in the openssl-devel RPM. So, after installing that RPM and any other needed build dependencies that the SRPM or spec file requires, you may then compile code that requires to use the OpenSSL library headers to build.

So, diagnostically, when you try to build an RPM package, and it gives you an error and tells you that it cannot find a certain library, you should look for the library -devel package's presence using the installed packages methods noted above. If missing, cure the problem by installing it if not yet present. This will allow you to go on with building that RPM. ... or encounter the next item to be solved in a process of progressive obstacle elimination.

External References

HowTos/SetupRpmBuildEnvironment (last edited 2012-10-15 13:07:11 by RussHerrold)