Build rpm packages with the rpmbuild command

From Linuxintro

This article is about how to build rpm packages with the rpmbuild command and a SPEC file. The example is based on SuSE 11.3 x86_64. However, other distributions should work same or similar. In this article, we will create a program that outputs "hello world", add a makefile to build it, and create a SPEC file to package the software as an RPM file.

Hello! fegckkb interesting fegckkb site! I'm really like it! Very, very fegckkb good!

The program

Here is how we create our program, hello world:

cd
mkdir hello-1.0
cd hello-1.0
cat >main.c <<EOF
#include <stdio.h>

int main()
{
  printf("hello world");
}
EOF

The Makefile

To build our program, we need a makefile. Here is how we create it:

cat >Makefile <<EOF
all:hello

hello: main.c
        gcc main.c -o hello

install: hello
        mkdir -p \${prefix}/usr/local/bin
        cp hello \${prefix}/usr/local/bin
EOF
sed -i "s/        /\t/g" Makefile 

The SPEC file

The SPEC file is needed to create an RPM package out of your software. It will be named hello.spec and reside in the folder /root/hello-1.0. Create it like this:

cat >hello.spec<<EOF
Summary: hello greets the world
Name: hello
Version: 1.0
Release: 1
License: GPL
Group: Applications/Tutorials
Source: hello.tar.gz
URL: http://www.staerk.de/thorsten/
Distribution: SUSE Linux
Vendor: -
Packager: Thorsten Staerk

%description
hello greets the world

%prep
%setup

%build
make 

%install
make install prefix=\$RPM_BUILD_ROOT

%files 
%defattr(-, root, root)
"/usr/local/bin/hello"
EOF

License is a free-text field. You can enter what you want. Same goes for Group. Source is the file that will be stored in /usr/src/packages/SOURCES.

Build the rpm

Now we have the SPEC file and the software. First we store the source in the "appropriate" place:

cd
tar cvzf hello.tar.gz hello-1.0
cp hello.tar.gz /usr/src/packages/SOURCES

Then, build it with the command

rpmbuild -ba hello-1.0/hello.spec

You will find your ready RPM under /usr/src/packages/RPMS.

Test the rpm

  • let's see if the RPM really exists
# ll /usr/src/packages/RPMS/x86_64/
total 8
-rw-r--r-- 1 root root 4856 Feb 13 17:03 hello-1.0-1.x86_64.rpm
  • let's install it
# rpm -ivh /usr/src/packages/RPMS/x86_64/hello-1.0-1.x86_64.rpm
Preparing...                ########################################### [100%]
   1:hello                  ########################################### [100%]
  • list what files it contains
# rpm -ql hello
/usr/local/bin/hello
  • find out if it remembers the information we gave it
# rpm -qi hello
Name        : hello                        Relocations: (not relocatable)
Version     : 1.0                               Vendor: -
Release     : 1                             Build Date: Sun Feb 13 17:03:57 2011
Install Date: Sun Feb 13 17:21:42 2011         Build Host: tweedleburg.site
Group       : Applications/Tutorials        Source RPM: hello-1.0-1.src.rpm
Size        : 11847                            License: GPL
Signature   : (none)
Packager    : Thorsten Staerk
URL         : http://www.staerk.de/thorsten/
Summary     : hello greets the world
Description :
hello greets the world
Distribution: SUSE Linux
  • let's see if we can remove it
# ll /usr/local/bin/hello 
-rwxr-xr-x 1 root root 11847 Feb 13 17:03 /usr/local/bin/hello
# rpm -e hello
# ll /usr/local/bin/hello 
ls: cannot access /usr/local/bin/hello: No such file or directory
  • yes we can :)

Clean up

To clean up, run

rm /usr/src/packages/SOURCES/hello.tar.gz
rm -rf /usr/src/packages/BUILD/hello-1.0

See also