From Fedora Project Wiki

Line 108: Line 108:
* %prep (preparation) stage, which uncompresses and installs the sources and patches into %_builddir (a subdirectory of ~/rpmbuild/BUILD)
* %prep (preparation) stage, which uncompresses and installs the sources and patches into %_builddir (a subdirectory of ~/rpmbuild/BUILD)
* %build stage, which builds (e.g., compiles) the files to be installed in %_builddir. Usually this is some equivalent of "make".
* %build stage, which builds (e.g., compiles) the files to be installed in %_builddir. Usually this is some equivalent of "make".
* %install stage, which copies the files from %_builddir (under ~/rpmbuuild/BUILD) into the "BuildRoot:". The recommended setting of BuildRoot begins %{_tmppath}/%{name}..., and thus is typically inside /var/tmp.
* %install stage, which copies the files from %_builddir (under ~/rpmbuild/BUILD) into the "BuildRoot:". The recommended setting of BuildRoot begins %{_tmppath}/%{name}..., and thus is typically inside /var/tmp.
* Create the binary (and maybe source) packages; this uses the information from the %files list to determine what files in the RPM BuildRoot to use.
* Create the binary (and maybe source) packages; this uses the information from the %files list to determine what files in the RPM BuildRoot to use.



Revision as of 01:11, 20 June 2008

Creating Package HOWTO

This page describes the mechanics of how to create an RPM package for Fedora (such as how to create a .spec file). It also gives some practical warnings about stuff that will or won't work, which may save you hours of time later. This is not the list of official package guidelines for Fedora (though it should be compatible with them).

Nearly all Linux distributions can install and uninstall programs as "packages". Fedora, and many other Linux distributions, use the "RPM" format for packages. There are tools that make it easy to create RPM packages; the key is to write a ".spec" file that explains to RPM how to build and install the program.

Setting up

Before you create RPM packages on Fedora, you need to install some core development tools and set up the account(s) you will use. As root:

 # yum groupinstall "Development Tools"
 # yum install rpmdevtools

It's strongly recommended that you create a new "dummy user" specifically for creating rpm packages. That way, if something goes terribly wrong, the program or build process can't trash your files, or send your private files/keys to the world. At the very least, you should normally not create your packages as user root. You can create a new user named "makerpm" quickly by doing:

 # /usr/sbin/useradd makerpm

Then log in as that special dummy user (makerpm).

Once you're logged in as the user who is creating packages, create the directory structure in your home directory by executing:

 $ rpmdev-setuptree

The "rpmdev-setuptree" program will create an "rpmbuild" directory in your $HOME directory. Underneath "rpmbuild" are a set of subdirectories (such as SPECS and BUILD), which you will use for creating your packages.

One you've set up your system and user account, you won't normally need to do these again.

Setting up to package a particular program

If there are special programs that are required to build or run it, install them and write down what they were (you'll need that information).

To package a program, you must package pristine (original) sources, along with the patches and build instructions. It's generally not okay to start with pre-compiled code. Install the file with the original source (usually a .tar.gz file) in the "~/rpmbuild/SOURCES" directory (of your "makerpm" account).

Read through the manual installation instructions for your program; you're going to be automating this by editing a ".spec" file, so you have to understand what you're supposed to do first.

Sometimes it's easiest to start with an existing package, and then clean it up for Fedora. RPM Find may help you find rpm's for non-Fedora systems. Failing that, you might look at the source package files (not the .deb binary package files) for Ubuntu or Debian (source package files are standard tarballs with a "debian/" subdirectory, possibly associated with patch files). If the FreeBSD ports collection has it, you could download the FreeBSD ports tarball and see if their packaging information helps as a starting point. However, this is sometimes not helpful at all. Different distributions have different rules, and what they do may be quite inappropriate for Fedora.

Creating a spec file

You now need to create a ".spec" file in the "~/rpmbuild/SPECS" directory. Generally, you'll name it after the program, e.g., "program.spec".

When you're creating a spec file for the first time, create its initial version using emacs or vim; they will automatically create a template for you. E.G.:

 $ cd ~/rpmbuild/SPECS
 $ vi program.spec

The RPM Guide, section on creating RPMs, describes the details of how to fill in a spec file.

Some older documents about RPM have the most information, but older documents tend to assume that all rpm work happens in a shared /usr/src/redhat directory. This is an obsolete way of using rpm and not recommended.

Beware: Comments (beginning with #) do not work as you might expect. Do not include macros (words beginning with "%") in a comment, because they are expanded even inside comments; if the macro is multi-line it cause weird errors. Also, don't use in-line comments (a # not at the beginning of a line); in-line comments often don't work properly in a spec file. Instead, use a comment-only line (without macros).

Before trying to build anything from it, you might want to run rpmlint on the spec file:

rpmlint program.spec

This will catch many errors.

Scriptlet Snippets has some useful examples of scriptlets.

Creating RPMs from the spec file

Once you've create a spec file, say "program.spec", you can create source and binary RPMs by simply running this:

 $ rpmbuild --clean -ba program.spec

This will attempt to perform the following stages:

  • %prep (preparation) stage, which uncompresses and installs the sources and patches into %_builddir (a subdirectory of ~/rpmbuild/BUILD)
  • %build stage, which builds (e.g., compiles) the files to be installed in %_builddir. Usually this is some equivalent of "make".
  • %install stage, which copies the files from %_builddir (under ~/rpmbuild/BUILD) into the "BuildRoot:". The recommended setting of BuildRoot begins %{_tmppath}/%{name}..., and thus is typically inside /var/tmp.
  • Create the binary (and maybe source) packages; this uses the information from the %files list to determine what files in the RPM BuildRoot to use.

Watch out: the "build directory" (where compilations occur during %build) and the "build root" (where files are installed during the %install) are different.

When things go wrong, you can "cd" into the appropriate directory and see what's left over. If you want to skip earlier stages, use the "--short-circuit" option; this is handy if you had a successful build, but have an error in the %install section. For example, to restart at the %install stage (skipping earlier stages), do this:

 $ rpmbuild -bi --short-circuit program.spec

If it is successful, you'll find your binary RPM(s) in the "~/rpmbuild/RPMS/" subdirectory, and the source RPM in "~/rpmbuild/SRPMS". If you "cd" to the "~/rpmbuild/RPMS" directory, to the architecture subdirectory, and then find some rpms, you can quickly see what's in each rpm by using rpmls:

$ rpmls *.rpm

If those look okay, you can become root and try to install them:

# rpm -ivp XYZ1.rpm XYZ2.rpm XYZ3.rpm ...

Then, you can test them out.

You can uninstall them later using:

# rpm -e XYZ1 XYZ2 XYZ3

Guidelines and rules

When you create your packages, you'll need to follow the following rules and guidelines:

For more information

The Package Maintainers page links to many other useful pages, and the The Updating Package HOWTO describes how to update an existing package you already maintain in Fedora.

For more information, outside of the Fedora Wiki, see: