TL;DR
This post will walk you through creating a RPM package from a simple hello world C program using rpmbuild
.
Setup
On an RPM-based system, install the following programs:
Additionally, you can create an .rpmmacros
file in your home directory, which rpmbuild
can use for determining various settings. A helpful setting is %_topdir
which tells rpmbuild
the base directory of where it will find the necessary components for building your package.
A common setting is to put %_topdir
in your home directory, like this:
When you run rpmbuild
the first time, it’ll construct a series of directoriesr under %_topdir
, or you can create them yourself:
Our “Hello World” program
For this example, we’ll be packaging a simple hello world program. You should begin by creating a directory for the source file and Makefile:
Next, you should create the files that comprise helloworld. In this case it consists of the source file main.c
and a Makefile
.
The main.c
is:
And the Makefile
:
Building and Testing
Let’s make sure everything works before we package it up. Run make
to build the program. This should create a helloworld
binary you can execute by running ./helloworld
.
You should see “Hi” printed to the screen.
Then, try installing the binary: run sudo make install
and now helloworld
should be in /usr/local/bin
.
Remove our test binary before continuing:
Packaging our program
Now that we’ve verified everything works, let’s create an RPM package for it.
1. tar
up the source files
The name of the tarball will be used in the next step.
2. Prepare the source directory
The build tool rpmbuild
can use source from a URL or from a local file. In our example, we’ll be using our tarball on our local system.
rpmbuild
will look in the SOURCES
directory under %_topdir
. If you used the .rpmmacros
from above, your directory would be: ~/rpmbuild/SOURCES
.
So, following that example, copy the source tar ball into the SOURCES
directory, so rpmbuild
can find it:
3. Create a SPEC file
A SPEC file describes to rpmbuild
how to build and package the software. In this case, it’s a C program that must be compiled and copied into the appropriate directory for installation.
Note the setting for Source0
below. This can be a URL, if desired. In this example, we are simply using a file.
4. Build the RPM with rpmbuild
You can now run the rpmbuild
tool to create an RPM package from this SPEC file:
The -ba
flag will build the source RPM (.src.rpm) and binary RPM (.rpm) packages.
You’ll see a bunch of output giving useful debug messages that you can use to tweak your SPEC file, should the build fail. You’ll notice toward the bottom of the output, rpmbuild
will list the files it is creating:
You can now copy the SRPM and RPM out of the output directory and into your YUM repository.
Sign your package with GPG using rpmsign
or rpm --sign
Refer to our other post on this topic: GPG signing RPM packages and YUM repositories.
Conclusion
Creating RPM packages gives you full control over how a program is compiled and the specific version being packaged. Uploading those packages to packagecloud lets you easily distribute them to all of your machines, exactly how you built it.