From Fedora Project Wiki

(→‎For more information: Add a reference to "RPM at Idle")
(→‎Preparing your system: made the install command in a single command to make it easy for copy paste)
(377 intermediate revisions by 61 users not shown)
Line 1: Line 1:
== Creating Package HOWTO ==
{{autolang|base=yes}}


This page describes the basic mechanics of how to create an RPM package specifically for Fedora (such as
== Introduction ==
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).
This doesn't go into depth on some issues, but it does point to other documents that do.


Nearly all Linux distributions can install and uninstall programs as "packages".
This page describes in detail how to create an RPM package, and in particular, how to create a SPEC file. Unlike other RPM guides, this page explains the specifics for Fedora with links to Fedora-specific guidelines. Since it is maintained through the Fedora Wiki, it is likely to be more up-to-date than other guides. Despite the focus on Fedora, most of this document does apply to other RPM-based distributions. If you're impatient, you might start by looking at the shorter [[How to create a GNU Hello RPM package]].
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.


If you plan to create an RPM package for the Fedora repository, follow the process for
'''Currently Fedora Documentation has released a draft guide for packagers, see  [http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/Packagers_Guide Packagers Guide]'''
[[PackageMaintainers/Join|How to join the Fedora Package Collection Maintainers]],
including following the various Fedora guidance.


== Setting up ==
Please note that these are '''NOT''' the official package '''guidelines''' for Fedora, the [[Packaging:Committee| Packaging Committee]]  handles the rules and guidelines for packaging software in Fedora. The most important ones:


Before you create RPM packages on Fedora, you need to install some core
* [[Packaging:Guidelines| Packaging Guidelines]]
development tools and set up the account(s) you will use.  As root (don't type the "#"!):
* [[Packaging:LicensingGuidelines |Licensing Guidelines]]
  # yum groupinstall "Development Tools"
* [[Packaging:NamingGuidelines| Package Naming Guidelines]]
  # yum install rpmdevtools
* [[Packaging:DistTag| Dist Tag Guidelines]]
* [[Packaging:ReviewGuidelines| Package Review Guidelines]]
* [[Packaging:ScriptletSnippets| Recipes for RPM post scripts]]


It's strongly recommended that you create a new "dummy user" specifically for
'''[[Packaging:Guidelines|Packaging Guidelines]] and [[Packaging:NamingGuidelines|Package Naming Guidelines]] are the main ones of Fedora Packaging. Having said that, this page should be compatible with them.
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).
If you plan to create an RPM package for the Fedora repository, follow the procedure depicted in [[Join the package collection maintainers]].


Once you're logged in as the user who is creating packages,
== Preparing your system ==
create the directory structure in your home directory by executing (don't type the "$"):
  $ rpmdev-setuptree


The "rpmdev-setuptree" program will create an "rpmbuild" directory in your $HOME
Before you create RPM packages on Fedora, you need to install some core development tools and set up the account(s) you will use:
directory. Underneath "rpmbuild" are a set of subdirectories
# dnf install @development-tools fedora-packager rpmdevtools
(such as SPECS and BUILD), which you will use for creating your packages.
The "rpmdev-setuptree" also creates an "~/.rpmmacros" file which will cause
You can create a dummy user specifically for creating RPM packages so that a build process went wrong can't trash your files or send your private keys to the world.
rpm and rpmbuild to use them when appropriate.
 
{{admon/caution|You should NEVER create your packages as the <code>root</code> user. Building RPM's as root is dangerous because the binary files are installed on the system before being packaged, thus you must always build as normal user so you won't accidentally pollute your system.}}
 
Create a new user named <code>makerpm</code>, add the user to the 'mock' group, set a password, and login as that user:
# /usr/sbin/useradd makerpm
# usermod -a -G mock makerpm
# passwd makerpm
 
Once you're logged in as the build/dummy user, create the required directory structure in your home directory by executing:
$ rpmdev-setuptree
 
The <code>rpmdev-setuptree</code> program will create the <code>~/rpmbuild</code> directory and a set of subdirectories (e.g. <code>SPECS</code> and <code>BUILD</code>), which you will use for creating your packages. The <code>~/.rpmmacros</code> file is also created, which can be used for setting various options.
 
[[Packaging:Guidelines#Timestamps|The packaging guidelines recommend preserving file timestamps]]; you can make this automatic if you use <code>wget</code> or <code>curl</code> to get the source files. If you use <code>wget</code> to get source files, add the text "<code>timestamping = on</code>" to <code>~/.wgetrc</code>.  If you use <code>curl</code>, add the text "<code>-R</code>" to <code>~/.curlrc</code>.
 
You won't normally need to do these steps again.
 
== The basics of building RPM packages ==
 
To create an RPM package, you will need to create a "<code>.spec</code>" text file that provides information about the software being packaged.
 
You then run the <code>rpmbuild</code> command on the <code>.spec</code> file, which will go through a series of steps to produce your packages.
 
Normally, you should place your original (pristine) sources, such as <code>.tar.gz</code> files from the original developers, into the <code>~/rpmbuild/SOURCES</code> directory.  Place your <code>.spec</code> file in the <code>~/rpmbuild/SPECS</code> directory and name it "''NAME''.spec", where ''NAME'' is the base name of the package.
 
Certain directories have certain purposes in the <code>rpmbuild</code> command, which can be substituted in the <code>.spec</code> file by the following (starting with <code>%</code>)  predefined macros:
{|border="1" cellspacing="0"
! Macro Name !! Name !! Usually !! Purpose
|-
|<code>%_specdir</code>||Specification directory||<code>~/rpmbuild/SPECS</code>||RPM specifications (<code>.spec</code>) files
|-
|<code>%_sourcedir</code>||Source directory||<code>~/rpmbuild/SOURCES</code>||Pristine source package (e.g. tarballs) and patches
|-
|<code>%_builddir</code>||Build directory||<code>~/rpmbuild/BUILD</code>||Source files are unpacked and compiled in a subdirectory underneath this.
|-
|<code>%_buildrootdir</code>||Build root directory||<code>~/rpmbuild/BUILDROOT</code>||Files are installed under here during the <code>%install</code> stage.
|-
|<code>%_rpmdir</code>||Binary RPM directory||<code>~/rpmbuild/RPMS</code>||Binary RPMs are created and stored under here.
|-
|<code>%_srcrpmdir</code>||Source RPM directory||<code>~/rpmbuild/SRPMS</code>||Source RPMs are created and stored here.
|}
 
 
To create both binary and source packages, change directory to <code>~/rpmbuild/SPECS</code> and run:
$ rpmbuild -ba ''NAME''.spec


One you've set up your system and user account, you won't normally need
When invoked this way, <code>rpmbuild</code> will read the <code>.spec</code> file and go through in order the stages listed below:
to do these again.


== Setting up to package a particular program ==
{|border="1" cellspacing="0"
! Stage !! Reads !! Writes !! Action
|-
|<code>%prep</code>||<code>%_sourcedir</code>||<code>%_builddir</code>||This reads the sources and patches in the source directory <code>%_sourcedir</code>.  It unpackages the sources to a subdirectory underneath the build directory <code>%_builddir</code> and applies the patches.
|-
|<code>%build</code>||<code>%_builddir</code>||<code>%_builddir</code>||This compiles the files underneath the build directory <code>%_builddir</code>.  This is often implemented by running some variation of "<code>./configure && make</code>".
|-
|<code>%install</code>||<code>%_builddir</code>||<code>%_buildrootdir</code>||This reads the files underneath the build directory <code>%_builddir</code> and writes to a directory underneath the build root directory <code>%_buildrootdir</code>. The files that are written are the files that are supposed to be installed when the binary package is installed by an end-user.  Beware of the weird terminology: The ''build root directory'' is '''not''' the same as the ''build directory''.  This is often implemented by running "<code>make install</code>".
|-
|<code>%check</code>||<code>%_builddir</code>||<code>%_builddir</code>||Check that the software works properly.  This is often implemented by running some variation of "<code>make test</code>".  Many packages don't implement this stage.
|-
|<code>bin</code>||<code>%_buildrootdir</code>||<code>%_rpmdir</code>||This reads the files underneath the build root directory <code>%_buildrootdir</code> to create binary RPM packages underneath the RPM directory <code>%_rpmdir</code>.  Inside the RPM directory is a directory for each architecture, and a "<code>noarch</code>" directory for packages that apply to any architecture.  These RPM files are the packages for users to install.
|-
|<code>src</code>||<code>%_sourcedir</code>||<code>%_srcrpmdir</code>||This creates a source RPM package (<code>.src.rpm</code>) inside the source RPM directory <code>%_srcrpmdir</code>.  These files are needed for reviewing and updating packages.
|}


If there are special programs that are required to build or run the program you are packaging, install those
<!-- Note: The words "in" and "underneath" in the table above have different meanings.  Given file /a/b/c, c is "underneath" but not "in" a. -->
other programs and write down what they were (you'll need that information).


To package a program, you ''must'' package pristine (original) sources, along
If a stage fails, look at the output to see ''why'' it failed and change the <code>.spec</code> file (or other input) as needed.
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;
== Getting ready to package a particular 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.
It's probably best if you try a "dry run", going through its installation
procedure without trying to do it via RPM first
(that's especially true if you're not familiar with RPM).


Try to reuse what you can.
If there are special programs that are required to build or run the program you are packaging, install those other programs and write down what they are.
Obviously, make sure you aren't packaging something that is already packaged;
you can find a list of existing packages in Fedora Package Collection in the [https://admin.fedoraproject.org/pkgdb/packages/ Fedora Package Database].
Also check the [[PackageMaintainers/InProgressReviewRequests | In Progress Review Requests]] (for packages that are currently being reviewed)
and the [[PackageMaintainers/RetiredPackages | Retired Packages]] list.
Failing that, see if someone has already started to package it for Fedora.
Google for "PROGRAMNAME Fedora rpm" or similar... maybe you can pick up where
they started.
You can use
http://cvs.fedoraproject.org/viewcvs/rpms/
directly view .spec files (and patches) of any similar packages already in Fedora.
You can download the source RPMs and install them, too;
go to a [http://mirrors.fedoraproject.org/publiclist Fedora mirror]'s http or ftp page,
select releases/9/Everything/source/SRPMS
(replace "9" with the Fedora release you want),
and download the source RPMs you want (they end in .src.rpm).
If you followed the directions above, you can install the source RPM
(which places its .spec file into ~/rpmbuild/SPECS and source files in ~/rpmbuild/SOURCES)
by running:
$ rpm -ivh PROGRAMNAME-*.src.rpm


''Sometimes'' it's easiest to start with an existing package, and then clean it up for Fedora.
To package a program for the Fedora repository, you must package pristine (original) sources, along with the patches and build instructions;
[http://rpmfind.net/ RPM Find] may help you find rpm's for non-Fedora systems.
it's '''not''' okay to start with pre-compiled code. Install the file with the original source (usually a <code>.tar.gz</code> file) in the
(You can install source RPMs for other systems the same way as for Fedora).
<code>~/rpmbuild/SOURCES</code> directory (of the RPM building user account).
Failing that, you might look at the source package files (not the .deb binary package files)
for [http://packages.ubuntu.com/ Ubuntu] or
[http://www.debian.org/distrib/packages Debian]
(source package files are standard tarballs with a "debian/" subdirectory, possibly
associated with patch files).
If the [http://www.freebsd.org/ports/installing.html FreeBSD ports collection] has it,
you could
[ftp://ftp.freebsd.org/pub/FreeBSD/ports/ports/ports.tar.gz 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 ==
Read through the manual installation instructions for your program. It's often a good idea to do a "dry run" by manually building the program before attempting to do so via RPM. With a few exceptions, all binaries and libraries included in Fedora packages must be built from the source code that is included in the source package.


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


=== Creating a blank spec file ===
Application source code is often released with the source code of other external libraries "bundled" into them. [[Packaging:No_Bundled_Libraries|Do not bundle external libraries with the main application into a single package]]. Instead, split them up into separate packages.


When you're creating a spec file for the first time, create its initial version using emacs or vim;
=== Licensing ===
they will automatically create a template for you.  E.G.:
  $ cd ~/rpmbuild/SPECS
  $ vi program.spec


Here's an example of what that template will look like:
Only package software that is legal for you to package. See [[Packaging:Guidelines#Legal]], [[Licensing:Main]] and [[Packaging:LicensingGuidelines]]. In general, only package software that is released as open source software (OSS) using an approved OSS license (such as the GPL, LGPL, BSD-new, MIT/X, or Apache 2.0 licenses). Check to make sure that the software really is licensed this way (e.g. spot-check source code headers, README files etc.). If there are bundled libraries, make sure they are also OSS.
  Name:
 
  Version:
=== Reuse existing package information ===
  Release: 1%{?dist}
 
  Summary:
Try to reuse what you can. Obviously, make sure you aren't packaging something that is already packaged. You can find a list of existing packages in Fedora Package Collection in the [https://admin.fedoraproject.org/pkgdb/ Fedora Package Database]. Also check the [[PackageMaintainers/InProgressReviewRequests|In Progress Review Requests]] and the [[PackageMaintainers/RetiredPackages|Retired Packages]] list. You can use [http://pkgs.fedoraproject.org/cgit Fedora Packages Git Repositories] directly to view SPEC files (and patches). You can download the SRPMS using DNF download Plugin:
  Group:
  $ dnf download --source sourcepackage-name
  License:
 
  URL:
Alternatively, get the source manually from the http/ftp page of a [http://mirrors.fedoraproject.org/publiclist Fedora mirror] within the <code>releases/{{FedoraVersion}}/Everything/source/SRPMS</code> directory. Replace "<code>{{FedoraVersion}}</code>" with the Fedora release you want and download the <code>.src.rpm</code> package you want.
  Source0:
 
  BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Once you have the SRPM, install it into <code>~/rpmbuild</code>:
  $ rpm -ivh sourcepackage-name*.src.rpm
 
You can also unpack the SRPM into a directory using <code>rpm2cpio</code>:
  $ mkdir PROGRAMNAME_src_rpm
$ cd PROGRAMNAME_src_rpm
  $ rpm2cpio ../PROGRAMNAME-*.src.rpm | cpio -i
 
Sometimes it's easiest to start with an existing package and then clean it up for Fedora. [http://rpmfind.net/ RPM Find] and [http://pkgs.org PKGS.org] may help you find RPMs for non-Fedora systems. You can install SRPMS for other systems the same way as for Fedora. Failing that, you might look at the source package files (not binary <code>.deb</code>) for [http://packages.ubuntu.com/ Ubuntu] or [http://www.debian.org/distrib/packages Debian] (source package files are standard tarballs with a "<code>debian/</code>" subdirectory). If the [http://www.freebsd.org/ports/installing.html FreeBSD ports collection] has it, you could [ftp://ftp.freebsd.org/pub/FreeBSD/ports/ports/ports.tar.gz 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 <code>~/rpmbuild/SPECS</code> directory. You should name it after the program name (e.g. "<code>program.spec</code>"). Use the archive name or the name advocated by the software author where you can, but be sure to follow the [[Packaging/NamingGuidelines|Package Naming Guidelines]].
 
=== SPEC templates and examples ===
==== Templates ====
 
You may use the <code>rpmdev-newspec</code> command to create a SPEC file for you.  <code>rpmdev-newspec &lt;package-name&gt;</code> can create an initial SPEC file for a new package, tailored to various types of packages. It will guess what kind of template to use based on the package name, or you can specify a particular template. See <code>/etc/rpmdevtools/spectemplate-*.spec</code> for available templates, and see <code>rpmdev-newspec --help</code> for more information. For example, to create a new SPEC file for a python module:
 
  cd ~/rpmbuild/SPECS
  rpmdev-newspec python-antigravity
  vim python-antigravity.spec
 
According to the [https://fedoraproject.org/wiki/Packaging:Guidelines#Writing_a_package_from_scratch packaging guidelines], new spec files should be created this way. Vim and emacs can automatically create simple templates, but these are different than those made with <code>rpmdev-newspec</code>. This is especially true for templates with names beginning with <code>python-</code> or <code>-perl</code>.
 
Here is an example auto-generated <code>.spec</code> template:
 
Name:          testprogram
Version:       
Release:        1%{?dist}
Summary:       
   
   
  BuildRequires:
License:       
  Requires:
URL:           
Source0:       
  BuildRequires:
  Requires:      
   
   
  %description
  %description
   
   
  %prep
  %prep
  %setup -q
  %setup -q
   
   
  %build
  %build
  %configure
  %configure
  make %{?_smp_mflags}
  make %{?_smp_mflags}
   
   
  %install
  %install
  rm -rf %{buildroot}
  rm -rf $RPM_BUILD_ROOT
  make install DESTDIR=%{buildroot}
  %make_install
   
   
%clean
rm -rf %{buildroot}
   
   
  %files
  %files
%defattr(-,root,root,-)
  %doc
  %doc
   
   
  %changelog
  %changelog
* Sun Apr  3 2016 makerpm
-
You can use <code>%{buildroot}</code> instead of <code>$RPM_BUILD_ROOT</code>. Both are acceptable, but be consistent.
==== Examples ====
===== eject =====
Here's a simple example showing a Fedora 16 SPEC file for the <code>eject</code> program:
<pre>
Summary:            A program that ejects removable media using software control
Name:              eject
Version:            2.1.5
Release:            21%{?dist}
License:            GPLv2+
Group:              System Environment/Base
Source:            %{name}-%{version}.tar.gz
Patch1:            eject-2.1.1-verbose.patch
Patch2:            eject-timeout.patch
Patch3:            eject-2.1.5-opendevice.patch
Patch4:            eject-2.1.5-spaces.patch
Patch5:            eject-2.1.5-lock.patch
Patch6:            eject-2.1.5-umount.patch
URL:                http://www.pobox.com/~tranter
ExcludeArch:        s390 s390x
BuildRequires:      gettext
BuildRequires:      libtool


%description
The eject program allows the user to eject removable media (typically
CD-ROMs, floppy disks or Iomega Jaz or Zip disks) using software
control. Eject can also control some multi-disk CD changers and even
some devices' auto-eject features.


You may have $RPM_BUILD_ROOT instead of %{buildroot}; just be consistent.
Install eject if you'd like to eject removable media using software
control.


=== An example: eject ===
%prep
%autosetup -n %{name}


Here's a simple example, a Fedora 9 package for the "eject" program:
%build
%configure
make %{?_smp_mflags}


Summary: A program that ejects removable media using software control.
%install
Name: eject
%make_install
Version: 2.1.5
 
Release: 11%{dist}
install -m 755 -d %{buildroot}/%{_sbindir}
License: GPL
ln -s ../bin/eject %{buildroot}/%{_sbindir}
Group: System Environment/Base
 
Source: http://metalab.unc.edu/pub/Linux/utils/disk-management/%{name}-%{version}.tar.gz
%find_lang %{name}
Source1: eject.pam
 
Patch1: eject-2.1.1-verbose.patch
%files -f %{name}.lang
Patch2: eject-timeout.patch
%doc README TODO COPYING ChangeLog
Patch3: eject-2.1.5-opendevice.patch
%{_bindir}/*
Patch4: eject-2.1.5-spaces.patch
%{_sbindir}/*
Patch5: eject-2.1.5-lock.patch
%{_mandir}/man1/*
Patch6: eject-2.1.5-umount.patch
 
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
%changelog
  URL: http://www.pobox.com/~tranter
* Tue Feb 08 2011 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.1.5-21
ExcludeArch: s390 s390x
- Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild
BuildRequires: gettext
 
  BuildRequires: automake
* Fri Jul 02 2010 Kamil Dudka <kdudka@redhat.com> 2.1.5-20
BuildRequires: autoconf
- handle multi-partition devices with spaces in mount points properly (#608502)
BuildRequires: libtool
</pre>
 
%description
===== =====
The eject program allows the user to eject removable media (typically
{{Anchor|Spec_file_pieces_explained}}
CD-ROMs, floppy disks or Iomega Jaz or Zip disks) using software
 
  control. Eject can also control some multi-disk CD changers and even
== SPEC file overview ==
  some devices' auto-eject features.
 
   
Other useful guides:
Install eject if you'd like to eject removable media using software
* [http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html RPM Guide] describes how to write a SPEC file.
  control.
* The IBM series "Packaging software with RPM" [http://www.ibm.com/developerworks/library/l-rpm1/ Part 1], [http://www.ibm.com/developerworks/library/l-rpm2/ Part 2], and [http://www.ibm.com/developerworks/library/l-rpm3/ Part 3].
   
* [http://rpm.org/max-rpm-snapshot/ Maximum RPM] has the most complete information, but is dated.
%prep
 
%setup -q -n %{name}
You will need to follow the Fedora guidelines: [[Packaging/NamingGuidelines|Package Naming Guidelines]], [[Packaging/Guidelines|Packaging guidelines]], and [[Packaging/ReviewGuidelines|Package review guidelines]].
%patch1 -p1 -b .versbose
 
%patch2 -p1 -b .timeout
Insert comments with a leading "<code>#</code>" character, and beware of macros (beginning with <code>%</code>) that are potentially multi-line, as they are expanded first. When commenting out a line, double the percent signs (<code>%%</code>) of the macros appearing after the "<code>#</code>". Also avoid inline comments on the same line as script commands.
%patch3 -p0 -b .opendevice
 
%patch4 -p0 -b .spaces
The major tags are listed below. Note that the macros <code>%{name}</code>, <code>%{version}</code> and <code>%{release}</code> can be used to refer to the Name, Version and Release tags respectively. When you change the tag, the macros automatically update to use the new value.
%patch5 -p0 -b .lock
* '''Name''': The (base) name of the package, which should match the SPEC file name. It must follow the [[Packaging/NamingGuidelines|Package Naming Guidelines]] and generally be lowercase.
  %patch6 -p1 -b .umount
* '''Version''': The upstream version number. See [[Packaging/NamingGuidelines#Version_Tag|Version tag section]] of the packaging guidelines. If the version contains tags that are non-numeric (contains tags that are not numbers), you may need to include the additional non-numeric characters in the Release tag. If upstream uses full dates to distinguish versions, consider using version numbers of the form <code>yy.mm[dd]</code> (e.g. <code>2008-05-01</code> becomes <code>8.05</code>).
* '''Release''': The initial value should normally be <code>1%{?dist}</code>. Increment the number every time you release a new package for the same version of software. When a new upstream version is released, change the Version tag to match and reset the Release number to <code>1</code>. See [[Packaging/NamingGuidelines#Release_Tag|Release tag section]] of the packaging guidelines. The optional [[Packaging/DistTag|Dist tag]] might be useful.
  %build
* '''Summary''': A brief, one-line summary of the package. Use American English. '''Do NOT end in a period.'''
  %configure
* '''Group''': This needs to be a pre-existing group, like "Applications/Engineering"; run "<code>less /usr/share/doc/rpm/GROUPS</code>" to see the complete list. Use the group "Documentation" for any sub-packages (e.g. <code>kernel-doc</code>) containing documentation. '''''Note: This tag is deprecated since Fedora 17. See [https://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/Packagers_Guide/chap-Packagers_Guide-Spec_File_Reference-Preamble.html Spec File Reference Preamble] '''''
make
* '''License''': The license, which must be an open source software license. Do ''not'' use the old Copyright tag. Use a standard abbreviation (e.g. "<code>GPLv2+</code>") and be specific (e.g. use "<code>GPLv2+</code>" for GPL version 2 or greater instead of just "<code>GPL</code>" or "<code>GPLv2</code>" where it's true). See [[Licensing]] and the [[Packaging/LicensingGuidelines|Licensing Guidelines]]. You can list multiple licenses by combining them with "<code>and</code>" and "<code>or</code>" (e.g. "<code>GPLv2 and BSD</code>").
* '''URL''': The full URL for more information about the program (e.g. the project website). '''''Note: This is not where the original source code came from which is meant for the Source0 tag below'''''.
%install
* '''Source0''': The full URL for the compressed archive containing the (original) pristine source code, as upstream released it. "<code>Source</code>" is synonymous with "<code>Source0</code>". If you give a full URL (and you should), its basename will be used when looking in the <code>SOURCES</code> directory. If possible, embed <code>%{name}</code> and <code>%{version}</code>, so that changes to either will go to the right place. [[Packaging:Guidelines#Timestamps|Preserve timestamps]] when downloading source files. If there is more than one source, name them <code>Source1</code>, <code>Source2</code> and so on. If you're adding whole new files in addition to the pristine sources, list them as sources ''after'' the pristine sources. A copy of each of these sources will be included in any SRPM you create, unless you specifically direct otherwise. See [[Packaging/SourceURL|Source URL]] for more information on special cases (e.g. revision control).
* '''Patch0''': The name of the first patch to apply to the source code. If you need to patch the files after they've been uncompressed, you should edit the files and save their differences as a "patch" file in your <code>~/rpmbuild/SOURCES</code> directory. Patches should make only one logical change each, so it's quite possible to have multiple patch files.
* '''BuildArch''': If you're packaging files that are architecture-independent (e.g. shell scripts, data files), then add "<code>BuildArch: noarch</code>". The architecture for the binary RPM will then be "<code>noarch</code>".
* '''BuildRoot''': This is where files will be "installed" during the %install process (after the %build process). This is now redundant in Fedora and is only needed for EPEL5. By default, the build root is placed in "<code>%{_topdir}/BUILDROOT/</code>".
* '''BuildRequires''': A comma-separated list of packages required for building (compiling) the program. This field can be (and is commonly) repeated on multiple lines. These dependencies are ''not'' automatically determined, so you need to include ''everything'' needed to build the program. It is possible to ensure you have specified all necessary build requires by [[Using_Mock_to_test_package_builds|performing a "mock build"]] of your package. You can specify a minimum version if necessary (e.g. "<code>ocaml >= 3.08</code>"). If you need the file <code>/EGGS</code>, determine the package that owns it by running "<code>rpm -qf /EGGS</code>". If you need the program <code>EGGS</code>, determine the package that owns it by running "<code>rpm -qf `which EGGS`</code>". Keep dependencies to a minimum (e.g. use <code>sed</code> instead of <code>perl</code> if you don't really need perl's abilities), but beware that some applications permanently disable functions if the associated dependency is not present; in those cases you may need to include the additional packages. The {{package|auto-buildrequires}} package may be helpful.
* '''Requires''': A comma-separate list of packages that are required when the program is installed. Note that the BuildRequires tag lists what is required to build the binary RPM, while the Requires tag lists what is required when installing/running the program; a package may be in one list or in both. In many cases, <code>rpmbuild</code> automatically detects dependencies so the Requires tag is not always necessary. However, you may wish to highlight some specific packages as being required, or they may not be automatically detected.
* '''%description''': A longer, multi-line description of the program.  Use American English. All lines must be 80 characters or less. Blank lines indicate a new paragraph. Some graphical user interface installation programs will reformat paragraphs; lines that start with whitespace will be treated as preformatted text and displayed as is, normally with a fixed-width font. See [http://docs.fedoraproject.org/drafts/rpm-guide-en/ch09s03.html RPM Guide].
* '''%prep''': Script commands to "prepare" the program (e.g. to uncompress it) so that it will be ready for building. Typically this is just "<code>%autosetup</code>"; a common variation is "<code>%autosetup -n NAME</code>" if the source file unpacks into <code>NAME</code>. See the %prep section below for more.
* '''%build''': Script commands to "build" the program (e.g. to compile it) and get it ready for installing. The program should come with instructions on how to do this. See the %build section below for more.
* '''%install''': Script commands to "install" the program. The commands should copy the files from the <code>BUILD</code> directory <code>%{_builddir}</code> into the buildroot directory, <code>%{buildroot}</code>. See the %install section below for more.
* '''%check''': Script commands to "test" the program. This is run after the %install procedure, so place it there if you have this section. Often it simply contains "<code>make test</code>" or "<code>make check</code>". This is separated from %build so that people can skip the self-test if they desire.
* '''%clean''': Instructions to clean out the build root. Note that this section is now redundant in Fedora and is only necessary for EPEL. Typically this contains only:
  rm -rf %{buildroot}
  rm -rf %{buildroot}
   
* '''%files''': The list of files that will be installed. See the %files section below for more.
make DESTDIR=%{buildroot} install
* '''%changelog''': Changes in the package. Use the format example above. '''Do NOT put software's changelog at here.This changelog is for RPM itself.'''
* '''ExcludeArch''': If the package does not successfully compile, build or work on a particular architecture, list those architectures under this tag.
# pam stuff
* You can add sections so that code will run when packages are installed or removed on the real system (as opposed to just running the %install script, which only does a pseudo-install to the build root).  These are called "scriptlets", and they are usually used to update the running system with information from the package.  See the "Scriptlets" section below for more.
install -m 755 -d %{buildroot}/%{_sysconfdir}/pam.d
 
install -m 644 %{SOURCE1} %{buildroot}/%{_sysconfdir}/pam.d/%{name}
RPM also supports the creation of several packages (called [[How_to_create_an_RPM_package#Subpackages|subpackages]]) from a single SPEC file, such as <code>name-libs</code> and <code>name-devel</code> packages.
install -m 755 -d %{buildroot}/%{_sysconfdir}/security/console.apps/
 
echo "FALLBACK=true" > %{buildroot}/%{_sysconfdir}/security/console.apps/%{name}
{{admon/caution|Do '''NOT''' use these tags|
* Packager
install -m 755 -d %{buildroot}/%{_sbindir}
* Vendor
  pushd %{buildroot}/%{_bindir}
* Copyright}}
mv eject ../sbin
 
  ln -s consolehelper eject
Do '''not''' create a "relocatable" package; they don't add value in Fedora and make things more complicated.
popd
 
== SPEC file sections explained ==
%find_lang %{name}
 
=== %prep section ===
%clean
 
rm -rf %{buildroot}
The %prep section describes how to unpack the compressed packages so that they can be built. Typically, this includes the "<code>%autosetup</code>" command. Alternatively, you can use "<code>%setup</code>" and "<code>%patch</code>" commands with reference to the Source0 (and Source1 etc.) lines. See the [http://rpm.org/max-rpm-snapshot/s1-rpm-inside-macros.html Maximum RPM section on %setup and %patch] for more details.
 
%files -f %{name}.lang
The %{patches} and %{sources} macros are available since RPM 4.4.2 and are useful if you have a large list of patches or sources and %autosetup is not what you want, then you can do:
%defattr(-,root,root)
  for p in %{patches}; do
%doc README TODO COPYING ChangeLog
    ...
  %attr(644,root,root) %{_sysconfdir}/security/console.apps/*
  done
  %attr(644,root,root) %{_sysconfdir}/pam.d/*
 
  %{_bindir}/*
However, keep in mind that using these will make your SPEC incompatible with RPMS used in RHEL and other RPM-based dirstributions.
  %{_sbindir}/*
 
  %{_mandir}/man1/*
==== %prep section: %autosetup command ====
   
 
  %changelog
The "<code>%autosetup</code>" command unpacks a source package. Switches include:
  * Wed Apr 02 2008 Zdenek Prikryl &lt;zprikryl at, redhat.com&gt; 2.1.5-11
* '''<code>-n</code> ''name''''' : If the Source tarball unpacks into a directory whose name is not the RPM name, this switch can be used to specify the correct directory name. For example, if the tarball unpacks into the directory FOO, use "<code>%autosetup -n FOO</code>".
- Added check if device is hotpluggable
* '''<code>-c</code> ''name''''' : If the Source tarball unpacks into multiple directories instead of a single directory, this switch can be used to create a directory named ''name'' and then unpack into it.
- Resolves #438610
 
If you use "<code>%setup</code>" command instead, then ''<code>-q</code>''' is commonly used to suppress unecessary output.  
 
There are [http://rpm.org/max-rpm-snapshot/s1-rpm-inside-macros.html more %setup options if you are unpacking multiple files], which is primarily useful if you
are creating subpackages (see below).  The key ones are:
 
{|
|-
| <code>-a number</code> || Only unpack the Source directive of the given number after changing directory (e.g. "<code>–a 0</code>" for Source0).
|-
| <code>-b number</code> || Only unpack the Source directive of the given number before changing directory (e.g. "<code>–b 0</code>" for Source0).
|-
| <code>-D</code> || Do not delete the directory before unpacking.
|-
| <code>-T</code> || Disable the automatic unpacking of the archives.
|}
 
==== %prep section: %patch commands ====
 
If you have used "<code>%autosetup</code>" command, the following manual patch management is not necessary. If you have complex requirements or need compatibility with EPEL, you may still need this. The "<code>%patch0</code>" command applies Patch0 (and %patch1 applies Patch1 etc.). Patches are the normal method of making necessary changes to the source code for packaging. The usual "<code>-pNUMBER</code>" option applies, which passes that argument onto the program <code>patch</code>.
 
Patch file names often look like "<code>telnet-0.17-env.patch</code>", which is the format <code>%{name} - %{version} - REASON.patch</code>" (though sometimes version is omitted). Patch files are typically the result of "<code>diff -u</code>"; if you do this from the subdirectory of <code>~/rpmbuild/BUILD</code> then you won't have to specify a <code>-p</code> level later.
 
This is a typical procedure for creating a patch for a single file:
  cp foo/bar foo/bar.orig
  vim foo/bar
  diff -u foo/bar.orig foo/bar > ~/rpmbuild/SOURCES/PKGNAME.REASON.patch
 
If editing many files, one easy method is to copy the whole subdirectory underneath <code>BUILD</code> and then do subdirectory diffs. After you have changed directory to "<code>~rpmbuild/BUILD/NAME</code>", do the following:
  cp -pr ./ ../PACKAGENAME.orig/
  ... many edits ...
diff -ur ../PACKAGENAME.orig . > ~/rpmbuild/SOURCES/''NAME''.''REASON''.patch
 
If you edit many files in one patch, you can also copy the original files using some consistent ending such as "<code>.orig</code>" before editing them. Then, you can use "<code>gendiff</code>" (in the <code>rpm-build</code> package) to create a patch with the differences.
 
Try to ensure that your patch match the context exactly. The default "fuzz" value is "<code>0</code>", requiring matches to be exact. You can work around this by adding "<code>%global _default_patch_fuzz 2</code>" to revert to the value found in older versions of RPM in Fedora, but it is generally recommended to avoid doing this.
 
As explained in [[Packaging/PatchUpstreamStatus]], all patches should have a comment above them in the SPEC file about their upstream status. This should document the upstream bug/email that includes it (including the date). If it is unique to Fedora, you should mention why it is unique. The Fedora Project tries not to deviate from upstream; see [[PackageMaintainers/WhyUpstream]] for the importance of this.
 
==== %prep section: Unmodified files ====


This is an old spec file, so its "BuildRoot:" value is old;
Sometimes, one or more of the Source files do not need to be uncompressed. You can "prep" those into the build directory like this (where <code>SOURCE1</code> refers to the relevant Source file):
you should normally use the "BuildRoot:" entry from the template instead.
cp -p %SOURCE1 .


=== Spec file pieces explained ===
=== %build section ===
The [http://docs.fedoraproject.org/drafts/rpm-guide-en/ch-creating-rpms.html RPM Guide, section on creating RPMs],
describes the details of how to fill in a spec file.
The developWorks series "Packaging software with RPM" [http://www.ibm.com/developerworks/library/l-rpm1/ Part 1], [http://www.ibm.com/developerworks/library/l-rpm2/ Part 2], and [http://www.ibm.com/developerworks/library/l-rpm3.html Part 3] is also handy.
[http://rpm.org/max-rpm-snapshot/ Maximum RPM] has the most complete information, but is dated.


You will need to follow the Fedora guidelines, such as the
The "%build" section is sometimes complicated; here you configure and compile/build the files to be installed.
[[Packaging/NamingGuidelines| Package Naming Guidelines]],
[[Packaging/Guidelines| Packaging Guidelines]], and
[[Packaging/ReviewGuidelines|Package review guidelines]].


You can insert comments with a leading "#" character, but
Many programs follow the GNU <code>configure</code> approach (or some variation). By default, they will install to a prefix of "<code>/usr/local</code>", which is reasonable for unpackaged files. However, since you are packaging it, change the prefix to "<code>/usr</code>". Libraries should be installed to either <code>/usr/lib</code> or <code>/usr/lib64</code> depending on the architecture.
don't insert potentially-multiline-macros (words beginning with "%") in a comment
(macros are expanded first); if you're commenting out a line, double the percent signs ("%%"). Also, don't use inline comments ("#") on the same line after
a script command.


Here are the major fields/areas that you will need to fill in:
Since GNU <code>configure</code> is so common, the macro "<code>%configure</code>" can be used to automatically invoke the correct options (e.g. change the prefix to <code>/usr</code>). Some variation of this often works:
* Name: The (base) name of the package.  It must follow the [[Packaging/NamingGuidelines| Package Naming Guidelines]].
* Version: The upstream version number. See [http://fedoraproject.org/wiki/Packaging/NamingGuidelines#Package_Version Packaging/Naming guidelines - package version] for more information. If the version is non-numeric (contains tags that are not numbers or digits), you may need to include the additional non-numeric characters in the release field.
* Release: This starts as "1%{?dist}"; increment the number every time you release a new package for the same version of software.  If a new version of the software being packaged is released, the version number should be changed to reflect the new software version, and the release number should be reset to 1.  See [http://fedoraproject.org/wiki/Packaging/NamingGuidelines#Package_Release Name Guidelines - package release] for more. [[Packaging/DistTag]] describes the "dist" tag, which isn't required but can be useful.
* Summary: A brief, one-line summary of the package. Use American English, and ''do not'' end in a period.
* Group: This needs to be a pre-existing group, like "Applications/Engineering"; run "less /usr/share/doc/rpm-*/GROUPS" to see the complete list.  If you create a sub-package "...-doc" with documentation, use the group "Documentation".
* License: Its license; for software, this must be an open source software license.  Use a standard abbreviation, e.g., "GPLv2+".  Try to be specific, e.g., use "GPLv2+" (GPL version 2 or greater) instead of just "GPL" or "GPLv2" where it's true.  See the [[Packaging/LicensingGuidelines| Licensing Guidelines]] for more information.  Call this tag "License"; don't use the older, inaccurately named tag "Copyright".
* URL: The URL for more information about the program, e.g., the project website.  Note: This is NOT where the original source code came from, see "Source" (next!).
* Source0: The URL for the compressed archive containing (original) pristine source code, as upstream released it. "Source" is synonymous with "Source0".  If you give a full URL (and you should), its basename will be used when looking in the SOURCES directory. If possible, embed %{name} and %{version}, so that changes to either will go to the right place.  (Note: This is different than "URL", which provides the URL to the project website.)
* BuildRoot: This is where files will be "installed" during the "%install" process (which happens after the %build compilation process).  Normally you should just leave this line alone; under the usual Fedora setup, this will be a special newly-created directory under /var/tmp.
* BuildRequires: A comma-separated list of packages required for building (beyond the packages that are required to run).  No need to list gcc or other common packages (as noted in the Fedora guidelines).  You can also specify minimum versions, if necessary.
* Requires: A comma-separate list of packages that are required to execute.  This is automatically determined by rpm, so normally you don't need to put anything here.  But if you want to highlight some specific packages, or require a package even though strictly speaking it's not called on, or rpm misses one, then add it here.
* %description - A longer, multi-line description of the program.  Use American English. All lines must be 80 characters or less.
* %prep - Script commands to "prepare" the program, that is, to uncompress it so that it will be ready for building (compiling).  Typically this is just "%setup -q" or some variation of it; a common variation is "%setup -q -n NAME" if the source file unpacks into NAME. Don't use in-line comments (a "#" comment on the same line after a command), and don't put macros (words beginning with "%") in a comment unless you quote the "%" as "%%" (macros can cause failures if they are in a comment). See the [http://rpm.org/max-rpm-snapshot/s1-rpm-inside-macros.html Maximum RPM section on %setup and %patch] for more details.
* %build - Script commands to "build" the program, that is, to compile it and get it ready for installing.  The program should come with instructions on how to do this.  Many programs follow the GNU configure approach (or some variation).  By default, they will install to a prefix of "/usr/local" (/usr/local/bin, /usr/local/lib, etc.), which is a reasonable default for unpackaged files - but since you ''are'' packaging it, you will want to change the prefix to "/usr". Often some variation of this will work:
   %configure
   %configure
   make %{?_smp_mflags}
   make %{?_smp_mflags}
* %install - Script commands to "install" the program.  The commands should copy the files from the "build directory" %{_builddir} (which would be under ~/rpmbuild/BUILD) into the buildroot directory, %{buildroot} (which would normally be under /var/tmp). Note that the script must ''not'' install the programs in the REAL final locations (e.g., in /usr/bin), but under the buildroot %{buildroot}. Normally, the install script would first erase the %{buildroot} directory, and then do some variation of "make install" (using DESTDIR=%{buildroot}, if the program supports it).  Try to copy the files in a way that retains the timestamps in the build directory, e.g., use "install -p". Here's an example:
 
To override makefile variables, pass them as parameters to <code>make</code>:
make %{?_smp_mflags} CFLAGS="%{optflags}" BINDIR=%{_bindir}
 
More more information, see [http://sourceware.org/autobook/ "GNU autoconf, automake, and libtool"] and [http://web.archive.org/web/20090411003817/http://www.suse.de/~sh/automake/automake.pdf "Open Source Development Tools: An Introduction to Make, Configure, Automake, Autoconf" by Stefan Hundhammer].
 
Some programs use <code>cmake</code>.  See [[Packaging/cmake]].
 
=== %install section ===
 
This section involves script commands to "install" the program, copying the relevant files from <code>%{_builddir}</code> to <code>%{buildroot}</code> (which usually means from <code>~/rpmbuild/BUILD</code> to <code>~/rpmbuild/BUILDROOT</code>) and creating directories inside <code>%{buildroot}</code> as necessary.
 
Some of the terminology can be misleading:
* The "build directory", also known as <code>%{_builddir}</code> is not the same as the "build root", also known as <code>%{buildroot}</code>. Compilation occurs in the former directory, while files to be packaged are copied from the former to the latter.
* During the %build section, the current directory will start at <code>%{buildsubdir}</code>, which is the subdirectory within <code>%{_builddir}</code> that was created during %prep stage. This is usually something like <code>~/rpmbuild/BUILD/%{name}-%{version}</code>.
* The %install section is '''not''' run when the binary RPM package is installed by the end-user, but is only run when creating a package.
 
Normally, some variation of "<code>make install</code>" is performed here:
%install
rm -rf %{buildroot} # redundant except for RHEL 5
%make_install
 
Ideally you should use %make_install which is equivalent to [http://www.gnu.org/prep/standards/html_node/DESTDIR.html <code>DESTDIR=%{buildroot}</code>] if the program supports it, as it redirects file installations to the specified directory and is exactly what we want to happen during the %install section.
 
If the program does not support <code>DESTDIR</code> (and only if), you can workaround it in one of several (inferior) ways:
* Patch the makefile so that is supports <code>DESTDIR</code>. Create directories inside <code>DESTDIR</code> where necessary and submit the patch upstream.
* Use the "<code>%makeinstall</code>" macro. This method might work, but can lead to subtle failures. It expands to something like "<code>make prefix=%{buildroot}%{_prefix} bindir=%{buildroot}%{_bindir} ... install</code>", which can result in some programs failing to work properly. Create directories inside <code>%{buildroot}</code> where necessary.
* Consider using the <code>auto-destdir</code> package. This requires "<code>BuildRequires: auto-destdir</code>", and changing "<code>make install</code>" to "<code>make-redir DESTDIR=%{buildroot} install</code>". This only works well if the installation uses only certain common commands to install files, like <code>cp</code> and <code>install</code>.
* Perform the installation by hand. This would involve creating the necessary directories under <code>%{buildroot}</code> and copying files from <code>%{_builddir}</code> to <code>%{buildroot}</code>. Be especially careful with updates, which often contain new or changed filenames. An example of this procedure:
%install
  rm -rf %{buildroot}
  rm -rf %{buildroot}
  make DESTDIR=%{buildroot} install
  mkdir -p %{buildroot}%{_bindir}/
* %clean - instructions to clean out the build root. Typically:
cp -p mycommand %{buildroot}%{_bindir}/
  rm -rf %{buildroot}
 
* %files - the list of files that will be installed. This normally begins with a "%defattr" line which sets the default file permissions, followed by names or patterns of the files to be installed. You can use wildcards; it's an error if ''no'' file matches the wildcard of a line, so only note the directories that actually matter. Also, you can't list the same file more than once. You should use macros for directory names, e.g., use %{_bindir}/myfile instead of /usr/bin/myfile, and %{_sbindir}/killaccount instead of /usr/sbin/killaccount. It's usually easier to just use wildcards, and that's also better at coping with changes in upstream. (Older RPM documentation typically shows long lists under %files with individual names, such as /usr/bin/program1 followed by /usr/bin/program2; Fedora spec files now use buildroots, so that is no longer necessary.) Typically there is a "%doc" entry with a list of documentation files that didn't get copied into the buildroot; usually there is at least a README and LICENSE file (you ''must'' include the license file, if there is one). You may prefix some of these with %attr(mode, user, group) to set the file permission mode / user / group. If you save configuration files (under /etc - don't put them under /usr), you should normally prefix them with %config(noreplace) unless this program version uses a non-backwards-compatible configuration format (in which case, use %config). You should follow the [http://www.pathname.com/fhs/ Filesystem Hierarchy Standard (FHS)], i.e., ordinary application executables go into /usr/bin, global configuration files go into /etc, ordinary libraries go into /usr/lib, and so on, with one exception: executables that should ''not'' normally be executed directly by users or administrators should go into a subdirectory of /usr/libexec; usually you'd refer to the necessary directory as "%{_libexecdir}/%{name}".
=== %check section ===
%defattr(-,root,root,-)
 
  %doc README LICENSE
If self-tests are available, it is generally a good idea to include them. They should be placed in the %check section (which immediately follows the %install section, since files in %buildroot may be tested) instead of within the %build section itself, so that they can be easily skipped when necessary.
 
Often, this section contains:
  make test
 
Sometimes it can be:
make check
 
Please explore the Makefile and choose the appropriate way.
 
=== %files section ===
This section declares which files and directories are owned by the package, and thus which files and directories will be placed into the binary RPM.
 
==== %files basics ====
 
The <code>%defattr</code> sets the default file permissions, and is often found at the start of the <code>%files</code> section. Note that this is no longer necessary unless the permissions need to be altered. The format of this is:
%defattr(<file permissions>, <user>, <group>, <directory permissions>)
The fourth parameter is often omitted. Usually one uses <code>%defattr(-,root,root,-)</code>, where "<code>-</code>" uses the default permissions.
 
You should then list all the files and directories to be owned by the package. Use macros for directory names where possible, which can be viewed at [[Packaging:RPMMacros]] (e.g. use <code>%{_bindir}/mycommand</code> instead of <code>/usr/bin/mycommand</code>). If the pattern begins with a "<code>/</code>" (or when expanded from the macro) then it is taken from the <code>%{buildroot}</code> directory. Otherwise, the file is presumed to be in the current directory (e.g. inside <code>%{_builddir}</code>, such as documentation files that you wish to include). If your package only installs a single file <code>/usr/sbin/mycommand</code>, then the <code>%files</code> section can simply be:
%files
%{_sbindir}/mycommand
 
To make your package less sensitive to upstream changes, declare all files within a directory to be owned by the package with a pattern match:
%{_bindir}/*
 
To include a single directory:
  %{_datadir}/%{name}/
 
Note that <code>%{_bindir}/*</code> does not claim that this package owns the <code>/usr/bin</code> directory, but only the files contained within. If you list a directory, then you are claiming that the package owns that directory and all files and subdirectories contained within. Thus, do '''not''' list <code>%{_bindir}</code> and be careful of directories that may be shared with other packages.
 
An error will occur if:
* a pattern match does not match any file or directory
* a file or directory is listed or matched more than once
* a file or directory in the <code>%{buildroot}</code> has not been listed
 
It is also possible to exclude files from a previous match by using the <code>%exclude</code> glob. This can be useful for including almost all of the files included by a different pattern match, but note that it will also fail if it does not match anything.
 
==== %files prefixes ====
You may need to add one or more prefixes to lines in the <code>%files</code> section; seperate them with a space. See [http://www.rpm.org/max-rpm/s1-rpm-inside-files-list-directives.html Max RPM section on %files directives].
 
Usually, "<code>%doc</code>" is used to list documentation files within <code>%{_builddir}</code> that were not copied to <code>%{buildroot}</code>. A <code>README</code> and <code>INSTALL</code> file is usually included. They will be placed in an appropriate directory under <code>/usr/share/doc</code>, whose ownership does not need to be declared.
 
'''Note:''' If specifying a <code>%doc</code> entry, rpmbuild < 4.9.1 removes the doc directory it installs files into before installing them. This means that files already in it, for example installed in the <code>%install</code> section, are removed and do not end up in the package. If you want to install some files in the <code>%install</code> section, install them into a temporary staging directory inside the build dir (not build root), for example <code>_docs_staging</code>, and include them in the in the <code>%files</code> list like <code>%doc _docs_staging/*</code>.
 
Configuration files should be placed in <code>/etc</code> and are normally specified like this (which makes sure user changes aren't overwritten on update):
%config(noreplace) %{_sysconfdir}/foo.conf
If the update uses a non-backwards-compatible configuration format, then specify them like this:
%config %{_sysconfdir}/foo.conf
 
"<code>%attr(mode, user, group)</code>" can be used for finer control over permissions, where a "<code>-</code>" means use the default:
%attr(0644, root, root) FOO.BAR
 
"<code>%caps(capabilities)</code>" can be used to give a file certain POSIX [http://linux.die.net/man/7/capabilities capabilities].  For example:
 
%caps(cap_net_admin=pe) FOO.BAR
 
If a file is in particular natural language, use <code>%lang</code> to note that:
%lang(de) %{_datadir}/locale/de/LC_MESSAGES/tcsh*
 
Programs using Locale files should follow the [[Packaging:Guidelines#Handling_Locale_Files|recommended method of handling i18n files]]:
* find the filenames in the <code>%install</code> step: <code> %find_lang ${name}</code>
* add the required build dependencies: <code>BuildRequires: gettext</code>
* use the found filenames: <code>%files -f ${name}.lang</code>
 
The <code>%readme</code> prefix is '''not''' valid in Fedora.
 
==== %files and Filesystem Hierarchy Standard (FHS) ====
 
You should follow the [http://www.pathname.com/fhs/ Filesystem Hierarchy Standard (FHS)]. Executables go in <code>/usr/bin</code>, global configuration files go in <code>/etc</code>, libraries go into <code>/usr/lib</code> (or <code>/usr/lib64</code>) and so on. There is one exception: executables that should not normally be executed directly by users or administrators should go in a subdirectory of <code>/usr/libexec</code>, which is referred to as <code>%{_libexecdir}/%{name}</code>.
 
Do '''not''' install files into <code>/opt</code> or <code>/usr/local</code>.
 
Unfortunately, many programs do not follow the FHS by default. In particular, architecture-independent libraries get placed in <code>/usr/lib</code> instead of <code>/usr/share</code>. The former is for architecture-dependent libraries, while the latter is for architecture-independent libraries, which means that systems with different CPU architectures can share <code>/usr/share</code>. There are many exceptions in Fedora (such as Python and Perl), but Fedora applies this rule more strictly than some distributions. <code>rpmlint</code> will generally complain if you put anything other than ELF files into <code>/usr/lib</code>.
 
==== %files example ====
 
Here's a simple example of a %files section:
%files
  %doc README
%license LICENSE COPYING
  %{_bindir}/*
  %{_bindir}/*
  %{_sbindir}/*
  %{_sbindir}/*
* %changelog - Changes in the package. Use the format example above.
%{_datadir}/%{name}/
* ExcludeArch: If the package does not successfully compile, build or work on an architecture, then those architectures should be listed in the spec in an ExcludeArch tag.
  %config(noreplace) %{_sysconfdir}/*.conf
* You can add sections so that code will run when packages are installed or removed on the real system (as opposed to just running the %install script, which only does a pseudo-install to the build root).  These are called "scriptlets", and they are usually used to update the running system with information from the package. The scriptlets in %pre and %post are run before and after a package is installed (respectively). The scriptlets %preun and %postun are run before and after a package is uninstalled. The scriptlets %pretrans and %posttrans are run at start and end of a transaction.  See [[Packaging/ScriptletSnippets]] for more examples and details.  For example, every binary RPM package which stores shared library files (not just symlinks) in any of the dynamic linker's default paths, must call ldconfig in %post and %postun (post-install and post-uninstall). If the package has multiple subpackages with libraries, each subpackage should also have a %post/%postun section that calls /sbin/ldconfig. For example:
 
==== Finding duplicates ====
 
You can list any duplicates of two binary packages by doing:
cd ~/rpmbuild/RPMS/ARCH # Substitute "ARCH" for your architecture
rpm -qlp PACKAGE1.*.rpm | sort > ,1
rpm -qlp PACKAGE2.*.rpm | sort > ,2
comm -12 ,1 ,2
 
=== Scriptlets ===
 
When an end-user installs the RPM, you may want some commands to be run. This can be achieved through scriptlets. See [[Packaging:Scriptlets]].
 
Scriptlets can be run:
* before ('''<code>%pre</code>''') or after ('''<code>%post</code>''') a package is installed
* before ('''<code>%preun</code>''') or after ('''<code>%postun</code>''') a package is uninstalled
* at the start ('''<code>%pretrans</code>''') or end ('''<code>%posttrans</code>''') of a transaction
 
For example, every binary RPM package that stores shared library files in any of the dynamic linker's default paths, must call <code>ldconfig</code> in <code>%post</code> and <code>%postun</code>. If the package has multiple subpackages with libraries, each subpackage should also perform the same actions.
  %post -p /sbin/ldconfig
  %post -p /sbin/ldconfig
  %postun -p /sbin/ldconfig
  %postun -p /sbin/ldconfig


Don't use the tags "Packager" or "Vendor". Don't use "Copyright" - use "License" instead.
If only running a single command, then the "<code>-p</code>" option runs the adjacent command without invoking the shell. However, for several commands, omit this option and include the shell commands beneath.
Don't create a "relocatable" package - they don't add value in Fedora yet they make things more complicated.
 
If you run any programs within the scriptlets, then you must specify any requirements in the form "<code>Requires(CONTEXT)</code>" (e.g. <code>Requires(post)</code>).


RPM supports subpackages, that is, a single spec file can generate many
<code>%pre</code>, <code>%post</code>, <code>%preun</code>, and <code>%postun</code> provide the argument <code>$1</code>, which is the number of packages of this name which will be left on the system when the action completes. Don't compare for equality with <code>2</code>, but instead check if they are greater than or equal to <code>2</code>. For <code>%pretrans</code> and <code>%posttrans</code>, <code>$1</code> is always <code>0</code>.
binary packages.  For example, if the documentation is very large, you might generate
a separate "doc" subpackage.
[http://docs.fedoraproject.org/drafts/rpm-guide-en/ch10s04.html RPM Guide chapter 10] discusses how to create subpackages.


=== Macros ===
For example, if the package installs an info manual, then the info manual index must be updated with <code>install-info</code> from the <code>info</code> package. Firstly, there is no guarantee that the <code>info</code> package will be available unless we explicitly declare it as required, and secondly, we don't want to fail completely if <code>install-info</code> fails:
Requires(post): info
Requires(preun): info
...
%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :
%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi


Spec files may contain "macro" references (text beginning with "%"), which are replaced with other values.
There is one other glitch related to installing info manuals. The <code>install-info</code> command will update the info directory, so we should delete the useless empty directory from the %{buildroot} during the <code>%install</code> section:
You can follow % by a word, e.g., "%name", but just like shell variables you must bracket the name with {...}
if letters or digits immediately follow, e.g., "%{name}".
rm -f %{buildroot}%{_infodir}/dir


As noted in the
Another scriptlet-like ability are "triggers", which can be run for your package when other packages are installed or uninstalled. See [http://rpm.org/api/4.4.2.2/triggers.html RPM Triggers].
[[Packaging/Guidelines| Packaging Guidelines]],
There are two styles for referring some values such as the rpm Build Root and Optimization Flags:
* "macro style": %{buildroot}, %{optflags}
* "variable style": $RPM_BUILD_ROOT, $RPM_OPT_FLAGS
Pick a style and use it consistently throughout your packaging; this document uses "macro style".


Here are some typical macros:
=== Macros ===
 
Macros are text in the format <code>%{string}</code>. Typical macros:


{|
{|
! Macro !! Typical Expansion
! Macro !! Typical Expansion !! Meaning
|-
| <code>%{_bindir}</code> || <code>/usr/bin</code> || Binary directory: where executables are usually stored.
|-
| <code>%{_builddir}</code> || <code>~/rpmbuild/BUILD</code> || Build directory: files are compiled within a subdirectory of the build directory. See <code>%buildsubdir</code>.
|-
| <code>%{buildroot}</code> || <code>~/rpmbuild/BUILDROOT/%{name}...</code> || Build root: where files are "installed" during the <code>%install</code> stage, which copies files from a subdirectory of <code>%{_builddir}</code> to a subdirectory of <code>%{buildroot}</code>. (Historically, <code>%{buildroot}</code> was in "/var/tmp/".)
|-
| <code>%{buildsubdir}</code> || <code>%{_builddir}/%{name}</code> || Build subdirectory: a subdirectory within <code>%{_builddir}</code> where files are compiled during the <code>%build</code> stage. It is set after <code>%autosetup</code>.
|-
|-
| %{_bindir} || /usr/bin
| <code>%{_datadir}</code> || <code>/usr/share</code> || Share directory.
|-
|-
| %{_builddir} || ~/rpmbuild/BUILD/... (build directory, where files are built)
| <code>%{_defaultdocdir}</code> || <code>/usr/share/doc</code> || Default documentation directory.
|-
|-
| %{buildroot} || /var/tmp... (buildroot, where files are installed)
| <code>%{dist}</code> || <code>.fc''NUMBER''</code> || Distribution+version short name (e.g. "<code>.fc{{FedoraVersion}}</code>")
|-
|-
| %{_datadir} || /usr/share
| <code>%{fedora}</code> || <code>''NUMBER''</code> || Number of fedora release (e.g. "<code>{{FedoraVersion}}</code>")
|-
|-
| %{_includedir} || /usr/include
| <code>%{_includedir}</code> || <code>/usr/include</code>
|-
|-
| %{_infodir} || /usr/share/info
| <code>%{_infodir}</code> || <code>/usr/share/info</code>
|-
|-
| %{_initrddir} || /etc/rc.d/init.d
| <code>%{_initrddir}</code> || <code>/etc/rc.d/init.d</code>
|-
|-
| %{_libdir} || /usr/lib
| <code>%{_libdir}</code> || <code>/usr/lib</code>
|-
|-
| %{_libexecdir} || /usr/libexec
| <code>%{_libexecdir}</code> || <code>/usr/libexec</code>
|-
|-
| %{_localstatedir} || /var
| <code>%{_localstatedir}</code> || <code>/var</code>
|-
|-
| %{_mandir} || /usr/share/man
| <code>%{_mandir}</code> || <code>/usr/share/man</code>
|-
|-
| %{name} || Name of package, set by Name: tag
| <code>%{name}</code> || || Name of package, set by Name: tag
|-
|-
| %{_sbindir} || /usr/sbin
| <code>%{_sbindir}</code> || <code>/usr/sbin</code>
|-
|-
| %{_sharedstatedir} || /usr/com
| <code>%{_sharedstatedir}</code> || <code>/var/lib</code>
|-
|-
| %{_sysconfdir} || /etc
| <code>%{_sysconfdir}</code> || <code>/etc</code>
|-
|-
| %{version} || Version of package, set by Version: tag
| <code>%{version}</code> || || Version of package, set by Version: tag
|}
|}


To see more about macros you
Learn more about macros by looking in <code>/etc/rpm/*</code> and <code>/usr/lib/rpm</code>, especially <code>/usr/lib/rpm/macros</code>. Also use <code>rpm --showrc</code> to show values that RPM will use for macros (altered by <code>rpmrc</code> and macro configuration files).
can look in /etc/rpm/* and the "macros" files under "/usr/lib/rpm/".
 
You can also invoke rpm, using "rpm --showrc" (to show all the configuration values)
You can set your own macro values using %global, but be sure to define them before you use them. (Macro definitions can also refer to other macros.) For example:
or "rpm --eval '%{macro}'" (to see the default value of a particular macro).
%global date 2012-02-08


You can set your own macro values using %define; be sure to define them before you use them. Macro definitions
Use the "<code>-E</code>" option of <code>rpmbuild</code> to find the value of a macro in a SPEC file:
can refer to other macros.  For example:
  rpmbuild -E '%{_bindir}' myfile.spec
  %define myvalue 50


[[Packaging/RPMMacros]] has more information on macros, as does
Also see [[Packaging/RPMMacros]] and [https://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch09s07.html RPM Guide chapter 9].
[http://docs.fedoraproject.org/drafts/rpm-guide-en/ch09s07.html RPM Guide chapter 9].
 
=== Other tags ===
 
In addition to Requires and BuildRequires tags, you can also use these for controlling dependencies:
* '''Provides''': list virtual package names that this package provides. For example, there might be a package "<code>foo</code>" that demands a particular functionality "bar" from another program. If there are several packages that can satisfy that demand, those packages can specify "<code>Provides: bar</code>" and the "<code>foo</code>" package can specify "<code>Requires: bar</code>". You could also use the [http://dailypackage.fedorabook.com/index.php?/archives/6-Wednesday-Why-The-Alternatives-System.html "alternatives" system], but avoid if multiple users on the same system might want different default, as these settings are system-wide. Use "<code>rpm -q --provides PACKAGENAME</code>" to see what a given package provides. Some examples of virtual packages in Fedora:
** MTA: Used for mail transport agents, such as sendmail.
** tex(latex): Used for latex
* '''Obsoletes''': remove another named package(s) when this package is installed. Use when the package name changes or when it totally replaces a different package.
* '''Conflicts''': state what other packages cannot be installed simultaneously to this one. Avoid this if you can. See [[Packaging/Conflicts]].
* '''BuildConflicts''': state what packages cannot be installed when building this package. Avoid this if you can.
 
To manage different architectures, there are two tags:
* '''ExcludeArch''': to exclude an architecture on which the package doesn't build. For example:
ExcludeArch: ppc
* '''ExclusiveArch''': to include only the specified architecture. Avoid this unless absolutely correct.
Valid architectures are listed at [[Architectures]].
 
=== Subpackages ===
 
A SPEC file can define several binary package. In other words, one SRPM with one SPEC file can result in several RPMS. Note that there is still only one creation (%prep, %build, %install etc.) process. <code>name-doc</code> and <code>name-devel</code> subpackages are common for documentation and development files respectively.
 
Use the <code>%package</code> directive to start defining a subpackage:
%package subpackage_name
 
After each <code>%package</code> directive, list the tags for the subpackage. This should at least include the Summary and Group tags, as well as the <code>%description subpackage_name</code> and <code>%files subpackage_name</code> directives:
 
Anything not specified by the subpackage will be inherited from its parent.
 
By default, if the package name is "<code>foo</code>" and the subpackage name is "<code>bar</code>", then the resulting subpackage will be "<code>foo-bar</code>". You can override it with the "<code>-n</code>" option (but you'll need to use it in all other directives too if you specify it here):
%package -n new_subpackage_name
 
[http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch10s04.html See the RPM Guide section on subpackages] for more information.
 
=== Conditionals ===
 
You can insert conditional statements, for example to test if you are creating a binary for a certain architecture:
%ifarch ARCHITECTURE_NAME
the negated version with:
%ifnarch ARCHITECTURE_NAME
or the more general conditional:
%if TRUE_OR_FALSE
 
There is an optional "<code>%else</code>" section; all of these are closed with "<code>%endif</code>".
 
=== Application Specific Guidelines ===
 
There are many application-specific guidelines that can help you (e.g., for specific programming languages, applications, libraries, and build systems).  Many of them are listed as part of the
[[Packaging/Guidelines#Application_Specific_Guidelines|Application Specific Guidelines of Packaging/Guidelines]].  Examples of application-specific guidelines are those for:
* [[Packaging:Cmake|Cmake]]
* [[Packaging:Emacs|Emacs]]
 
Failing that, some other ways of finding application-specific help are:
* The 'SEARCH' command on Fedoraproject.org.
* [[PackagingDrafts]]
* A [[SIGs|Special Interest Group (SIG)]]
* [http://fedoraproject.org/wiki/Special:PrefixIndex/Packaging Wiki pages prefixed with 'Packaging']


=== Miscellaneous hints ===
=== Miscellaneous hints ===


'''Beware''': Comments (beginning with #) do not work as you might expect.
[[Packaging/FrequentlyMadeMistakes]] has information on frequently-made mistakes. There are also some recommendations and controversial tricks on
'''Do not''' include macros (words beginning with "%") in a comment, because they are expanded even inside comments;
[[PackageMaintainers/Packaging Tricks]].
if the macro is multi-line it cause weird errors.
 
Also, '''do not''' use in-line comments (a # not at the beginning of a line);
Try to write your SPEC files so that it is likely to work when a new release is made upstream, without any changes aside from bumping the version number and refreshing the source files. For example, if it contains *.txt files with execute bits, instead of doing
in-line comments often don't work properly in a spec file.
  chmod a-x Filename1.txt Filename2.txt Filename3.txt
Instead, use a comment-only line (without macros).
consider doing this, which will handle new filenames that use the same file naming convention:
  chmod a-x *.txt


Use scriplets to make programs run before and after events (e.g., installing and uninstalling on the real system).
[[Packaging/ScriptletSnippets|Scriptlet Snippets]] has some useful examples of scriptlets.
'''Beware:''' the "-p" option of %pre, etc.,
[http://osdir.com/ml/freshrpms.user/2003-10/msg00233.html selects the program that will execute your script].
If you only have a single program that ignores input, then using "-p" is slightly more efficient, but
if you're writing a shell script you typically do ''not'' want to use "-p".
If you want to see lots of examples of scriptlets, you can show all the scriptlets on installed programs using:
If you want to see lots of examples of scriptlets, you can show all the scriptlets on installed programs using:
   rpm -qa --queryformat "\n\nPACKAGE: %{name}\n" --scripts | less
   rpm -qa --queryformat "\n\nPACKAGE: %{name}\n" --scripts | less


[[Packaging/FrequentlyMadeMistakes]] has information on frequently-made mistakes.
Don't try to interact with the user; RPM is designed to support batch installs.  If an application needs to show a EULA, that needs to be part of its initial execution, not its installation.
 
You might not want to start services, because in a big install that could slow things down. If you install an init or systemd script, consider using <code>chkconfig</code> or <code>systemctl</code> to arrange for the service to be started/stopped on the next reboot.  Before uninstalling, you should normally try to stop its services if they are running.
 
Uninstalling should reverse most changes made during installation, but don't remove any user-created files.
 
Normally, if there are binary executables, then debugging symbols are stripped from the normal binary packages and placed into a <code>name-debug</code> subpackage. If this shouldn't happen, you can disable the stripping and creation of this subpackage by putting this at the top of your SPEC:
%global _enable_debug_package 0
%global debug_package %{nil}
%global __os_install_post /usr/lib/rpm/brp-compress %{nil}


Some older documents about RPM have the most information, but older documents tend to assume that
To prevent stripping you may also need to do this in the <code>%install</code> section:
all rpm work happens in a shared /usr/src/redhat directory. This is an obsolete way of
  export DONT_STRIP=1
using rpm and ''not'' recommended.
They may also claim that "rpm" creates packages; it once did, but now that capability
is in the separate "rpmbuild" program.


== Quick test with rpmlint ==
A way to check for the version of Fedora in a SPEC file for conditional builds is:
%if 0%{?fedora} <= <version>
The <code>?</code> causes the macro to evaluate to evaluate to blank if <code>%fedora</code> is not defined. This causes the end result to be the <code>0</code> (which is a number and thus fine), while not interfering with the result if there is actually a value for <code>%fedora</code>. (Note that this trick does not work in Koji "scratch" builds, where <code>%fedora</code> is set during the creation of a SRPM.)


Before trying to build anything from it, you might want to run ''rpmlint'' on the spec file:
GUI programs must have a desktop entry so that people can invoke it from the graphical desktop menu. For <code>.desktop</code> files, see [[Packaging/Guidelines#Desktop_files|Fedora packaging guidelines for desktop files]] and [http://standards.freedesktop.org/desktop-entry-spec/latest/ desktop entry spec]. For icons within <code>/usr/share/icons</code>, see [http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html icon theme spec].
rpmlint program.spec
This will catch many errors early.


Generally, you should not have errors from rpmlint, but sometimes
== Building the binary package ==
rpmlint is excessively noisy.
The
[http://fedoraproject.org/wiki/Packaging/Guidelines#Use_rpmlint Fedora packaging guidelines]
explain which ones to ignore, e.g., ignore "no-packager-tag" and "no-signature" errors.


== Creating RPMs from the spec file ==
=== Test with rpmlint ===


Once you've create a spec file, say "program.spec", you can create
To catch many common errors early, run <code>rpmlint</code> on your SPEC file before trying to build anything from it:
source and binary RPMs by simply running this:
$ rpmlint program.spec
  $ rpmbuild --clean -ba program.spec
If the reported error doesn't make sense, run it again with the "<code>-i</code>" option for longer messages.


This will attempt to perform the following stages:
Aim to have no errors, but sometimes <code>rpmlint</code> reports false positives. The [[Packaging/Guidelines#Use_rpmlint|Fedora packaging guidelines]]
* %prep (preparation) stage, which uncompresses and installs the sources and patches into %_builddir (a subdirectory of ~/rpmbuild/BUILD)
explains which ones to ignore.
* %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 the build directory %_builddir (which would be under ~/rpmbuild/BUILD) into the buildroot directory, %{buildroot}.  The buildroot directory is set by the earlier "BuildRoot:"; if you leave it to its normal value beginning %{_tmppath}/%{name}..., then the buildroot will be 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''.
=== Create binary RPMS from the SPEC file ===


When things go wrong, you can "cd" into the appropriate directory and see what's left over.
Once you've created your SPEC file, build the SRPM and binary RPMS by running this:
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 -ba program.spec
  $ rpmbuild -bi --short-circuit program.spec


If it is successful, you'll find your binary RPM(s) in the "~/rpmbuild/RPMS/"
If successful, RPMS will be created within <code>~/rpmbuild/RPMS</code> and SRPMS will be created within <code>~/rpmbuild/SRPMS</code>.
subdirectory, and the source RPM in "~/rpmbuild/SRPMS".


== Testing RPMs you've built ==
If it fails, go to the appropriate directory and see what is left over. To help debug, you can skip earlier stages that succeeded with the "<code>--short-circuit</code>" option. For example, to restart at the <code>%install</code> stage (skipping earlier stages), do this:
$ rpmbuild -bi --short-circuit program.spec


If you "cd" to the "~/rpmbuild/RPMS" directory, to the architecture subdirectory,
If you just want to create an SRPM (which does not run the <code>%prep</code> or <code>%build</code> or other stages), run this:
and then find some rpms, you can quickly see what's in each rpm by using rpmls:
rpmbuild -bs program.spec
 
=== Testing binary RPMS with rpmlint ===
 
<code>rpmlint</code> can be run on SPEC files, RPMS and SRPMS to check for errors. You need to eliminate or justify warnings before posting a package. [[Common_Rpmlint_issues|This page]] offers explanations for some of the common issues that come up. If you are in the SPECS directory, do this:
$ rpmlint ''NAME''.spec ../RPMS/*/''NAME''*.rpm ../SRPMS/''NAME''*.rpm
 
Enter the <code>~/rpmbuild/RPMS</code> directory and into the architecture subdirectory. You will find some binary RPMS. Quickly see their files and permissions (to check whether they are correct) by doing:
  $ rpmls *.rpm
  $ rpmls *.rpm


If those look okay, you can become root and try to install them:
If they look fine, install them as root:
  # rpm -ivp XYZ1.rpm XYZ2.rpm XYZ3.rpm ...
  # rpm -ivp package1.rpm package2.rpm package3.rpm ...
 
Test the programs in a few different ways to see if everything works correctly. If it is a GUI tool, make sure it shows up in the desktop menu, otherwise the <code>.desktop</code> entry is wrong.
 
Uninstall packages later by doing:
# rpm -e package1 package2 package3
 
== Mock and Koji ==
 
[[Projects/Mock|Mock]] is a powerful tool that uses the SRPM you have created to build binary packages within a nearly empty environment. This can reveal if you have accurate build dependencies. If it fails, then you forgot to list something in BuildRequires. See [[Using Mock to test package builds]]. Once your account is a member of the "<code>mock</code>" group, you can run commands like this to do local testing:
$ mock -r fedora-23-i386 rebuild path_to_source_RPM


Then, you can test them out. If it's a command-line tool, can you invoke it
You can use Koji (which uses <code>mock</code>) to do builds on many different systems, some of which you may not have. [[PackageMaintainers/Join]] and [[PackageMaintainers/UsingKoji]] have more information about Koji. Once it's set up, you can test your SRPM on a variety of platforms by running commands like:
without prepending /usr/bin?  If it's a GUI tool, does it show up in the menu
$ koji build --scratch f23 path_to_source_RPM
(if it doesn't, something is wrong with your .desktop entry).


You can uninstall them later using:
Replace <code>f23</code> with any later release of Fedora, or <code>rawhide</code>.
# rpm -e XYZ1 XYZ2 XYZ3


If that all works, you can use Mock to do a rigorous test that you have
Your Koji builds can only depend on packages that are actually in the TARGET distribution repository.  Thus, you can't use Koji to build for released distributions if your package depends on other new packages that Bodhi hasn't released yet. If you need to build against a package that is not yet a stable released update, submit a Koji buildroot override request via Bodhi. If it's not your own package you depend on, contact its maintainer(s). [Before Bodhi could handle Koji buildroot override requests, the old method has been to file a ticket with rel-eng at: https://fedorahosted.org/rel-eng/newticket and request that that package be added as a buildroot override.]
accurate build requirementsSee [[PackageMaintainers/MockTricks]]
 
for more information about how to use Mock.
== Helpful tools ==
 
The <code>rpmdevtools</code> package has a number of helpful tools; "<code>rpm -qil rpmdevtools</code>" will show you what it installs.
* <code>rpmdev-bumpspec</code> : bump the release tag in the spec file and add a changelog comment with the right date and version format:
rpmdev-bumpspec --comment=COMMENT --userstring=NAME+EMAIL_STRING SPECFILES
 
DNF download plugin (from core DNF plugins) is also useful:
* <code>dnf download</code> : download the SRPM of the package by running:
dnf download --source PACKAGENAME
 
The <code>auto-buildrequires</code> package has a pair of nice tools for helping to figure out the proper BuildRequires entriesAfter installing this package, replace "<code>rpmbuild</code>" with "<code>auto-br-rpmbuild</code>" and you'll see an automatically generated BuildRequires list.
 
You might find [http://rust.sourceforge.net/ RUST] useful (GPL), though it does not create SPEC files of suitable quality for Fedora packages. [http://kitenet.net/~joey/code/alien/ Alien] converts between package formats. It won't produce clean SRPMS, but converting an existing package might provide helpful information.
 
If you are thinking about packing your rpm for Fedora, be sure you run your packages through [https://fedorahosted.org/FedoraReview/ Fedora Review], which helps ensure that you comply to the [https://fedoraproject.org/wiki/Packaging:Guidelines?rd=Packaging/Guidelines Packaging Guideline].
 
Finally, [https://github.com/alanfranz/docker-rpm-builder docker-rpm-builder] (APL 2.0) uses [http://www.docker.com Docker] to build RPM packages; while using rpmbuild requires building on the same host distro as the target, and mock works fine on Fedora/Centos/RHEL distributions for any target, '''this last tool works fine whenever Docker can run'''.
 
If you want to build your package for diferent distribution and architectures and to have publicly accesible dnf repository, you can submit your src.rpm to [https://copr.fedoraproject.org Copr].
 
If you need to sign your new package, use <code>rpmsign</code> tool from
<code>rpm-sign</code> package.


== Guidelines and rules ==
== Guidelines and rules ==


When you create your packages, you'll need to follow the following rules and guidelines:
When you create your packages, you'll need to follow the following rules and guidelines:
* [[PackageMaintainers/Join| How to join the Fedora Package Collection Maintainers]] - describes the process for becoming a Fedora package maintainer
* [[Join the package collection maintainers|How to join the Fedora Package Collection Maintainers]]
* [[Packaging/Guidelines|Packaging Guidelines]]
* [[Packaging:Guidelines|Packaging Guidelines]]
* [[Packaging/NamingGuidelines| Package Naming Guidelines]]  
* [[Packaging:NamingGuidelines|Package Naming Guidelines]]
* [[Packaging/DistTag| Dist Tag Guidelines]]  
* [[Packaging:LicensingGuidelines|Package Licensing Guidelines]]
* [[Packaging/ReviewGuidelines| Package Review Guidelines]]
* [[Packaging:DistTag|Dist Tag Guidelines]]  
* [[Packaging:ReviewGuidelines|Package Review Guidelines]]
 
There are many official guidelines that will help you with specific circumstances (e.g. Java programs, OCaml programs, GNOME programs). You can also learn more from the [[SIGs]] and [[:Category:Package Maintainers|Package Maintainers]] sections.


There are many guidelines that will help you with specific circumstances
(Java programs, OCaml programs, GNOME programs, etc.); the
[[Packaging/Guidelines|Packaging Guidelines] include cross-references to those guidelines.
You can also learn more from the [[SIGs]] and
[[PackageMaintainers|Package Maintainers]] sections.
[https://fedoraproject.org/wiki/Special:Prefixindex/Packaging You can also see the list of all Wiki pages about Packaging] to see if any apply.
[https://fedoraproject.org/wiki/Special:Prefixindex/Packaging You can also see the list of all Wiki pages about Packaging] to see if any apply.
Failing that, you might find some useful recommendations in the unofficial [https://fedoraproject.org/wiki/Special:Search?ns0=1&search=PackagingDrafts%2F&searchx=Search Packaging Drafts] and [[PackagingDrafts|Packaging Drafts To Do]].
You might find ideas from [http://en.opensuse.org/Packaging SuSE],
[http://www.debian.org/doc/debian-policy/ Debian], but
[http://www.mail-archive.com/distributions@lists.freedesktop.org/msg00156.html distributions differ in their rules], so do not presume they can be used directly.
'''The .spec files that you create must be open source software, as noted in the [[Legal:Fedora_Project_Contributor_Agreement|FPCA]].'''
== Maintaining the package ==
Once your package has been accepted, you and your co-maintainers will need to maintain it. See [[Package update HOWTO]] and [[Package update guidelines]]. If you update the version in multiple releases of Fedora, do it backwards in time (e.g. release for Fedora N, then once that's accepted, Fedora N-1). The system presumes that later versions of Fedora have the same or later versions of programs.
Encourage the upstream developers to use standard source code release conventions. Using standard conventions makes packaging much easier. For more information, see:
* [http://www.dwheeler.com/essays/releasing-floss-software.html Releasing Free/Libre/Open Source Software (FLOSS) for Source Installation] (a quick summary)
* [http://www.gnu.org/prep/standards/html_node/Managing-Releases.html GNU Coding Standards release process]
* [http://en.tldp.org/HOWTO/Software-Release-Practice-HOWTO/ Software Release Practice HOWTO]
* [http://www.pathname.com/fhs/ Filesystem Hierarchy Standard (FHS)]
* [http://offog.org/articles/packaging/ Packaging Unix software]


== For more information ==
== For more information ==


The [[PackageMaintainers|Package Maintainers]] page links to many other useful pages, and the
The [[:Category:Package Maintainers|Package Maintainers]] page links to many other useful pages, and the
[[PackageMaintainers/UpdatingPackageHowTo|Updating Package HOWTO]] describes how to update
[[Package update HOWTO]] describes how to update an existing package you already maintain in Fedora.
an existing package you already maintain in Fedora.


For more information, outside of the Fedora Wiki, see:
For more information, outside of the Fedora Wiki, see:
* [http://www.g-loaded.eu/2006/04/05/how-to-build-rpm-packages-on-fedora/ How to build RPM packages on Fedora] - very brief run-through
* [http://www.g-loaded.eu/2006/04/05/how-to-build-rpm-packages-on-fedora/ How to build RPM packages on Fedora] - very brief run-through
* Packaging software with RPM (developerWorks) [http://www.ibm.com/developerworks/library/l-rpm1/ Part 1], [http://www.ibm.com/developerworks/library/l-rpm2/ Part 2], and [http://www.ibm.com/developerworks/library/l-rpm3.html Part 3]
* Packaging software with RPM (developerWorks) [http://www.ibm.com/developerworks/library/l-rpm1/ Part 1], [http://www.ibm.com/developerworks/library/l-rpm2/ Part 2], and [http://www.ibm.com/developerworks/library/l-rpm3.html Part 3]
* Fedora Classroom had a IRC session on packaging and you can refer to the logs at https://fedoraproject.org/wiki/Building_RPM_packages_%2820090405%29
* [http://koti.welho.com/vskytta/packagers-handbook/packagers-handbook.html Fedora Packager's Handbook]
* [http://koti.welho.com/vskytta/packagers-handbook/packagers-handbook.html Fedora Packager's Handbook]
* [http://www.redhatmagazine.com/2008/02/28/when-sally-met-eddie-the-fedora-package-story/ When Sally met Eddie] - a simple tale, but little detail
* [http://www.redhatmagazine.com/2008/02/28/when-sally-met-eddie-the-fedora-package-story/ When Sally met Eddie] - a simple tale, but little detail
* [http://rpm.org/max-rpm-snapshot/ Maximum RPM Book] - most complete information, but in some cases old/obsolete
* [http://rpm.org/max-rpm-snapshot/ Maximum RPM Book] - most complete information, but in some cases old/obsolete
* [http://docs.fedoraproject.org/drafts/rpm-guide-en/ch-creating-rpms.html RPM Guide, section on creating RPMs] - this has lots of good information and focuses on Fedora, but is a draft
* [http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html RPM Guide, section on creating RPMs] - this has lots of good information, and is slightly more up-to-date, but is a draft
* [http://docs.fedoraproject.org/developers-guide/ch-rpm-building.html Developer's guide, section on building RPMs]
* [http://docs.fedoraproject.org/developers-guide/ch-rpm-building.html Developer's guide, section on building RPMs]
* [http://www.gurulabs.com/GURULABS-RPM-LAB/GURULABS-RPM-GUIDE-v1.0.PDF Creating RPMS slides] from Guru Labs
* [http://www.gurulabs.com/GURULABS-RPM-LAB/GURULABS-RPM-GUIDE-v1.0.PDF Creating RPMS slides] from Guru Labs
* [http://freshrpms.net/docs/fight/ The fight, my first attempt to make a readable rpm package building introduction.]
* [http://freshrpms.net/docs/fight/ The fight, my first attempt to make a readable rpm package building introduction.]
* [http://genetikayos.com/code/repos/rpm-tutorial/trunk/rpm-tutorial.html RPM Tutorial (Fullhart)]
* [http://www-uxsup.csx.cam.ac.uk/talks/rpmbuild/rpmbuild.pdf Cambridge RPM tutorial] is a presentation on creating basic RPMs
* [http://en.tldp.org/HOWTO/RPM-HOWTO/index.html RPM HOWTO: RPM at Idle by Donnie Barnes]
* [http://en.tldp.org/HOWTO/RPM-HOWTO/index.html RPM HOWTO: RPM at Idle by Donnie Barnes]
* [http://home.fnal.gov/~dawson/rpms/howto/index.html RPM HowTo by Dawson]
* [http://en.opensuse.org/Build_Service/cross_distribution_package_how_to Cross-distribution package HOWTO] has hints if you're building one RPM for many distributions.
* [http://wiki.mandriva.com/en/Development/Howto/RPM Mandriva Rpm HowTo (en)] is an RPM tutorial, though for Mandriva (nee Mandrake).  Note: In Fedora, do ''not'' recompress original tarballs, as Mandriva suggests, because that would change their cryptographic hashes.
* [http://linuxshellaccount.blogspot.com/2008/03/creating-your-own-linux-rpms-initial.html Creating Your Own Linux RPM's - The Initial Software Build] is another brief intro, but it makes the point that "The process of building RPM's is much simpler than creating packages for Solaris... Fewer steps, and the ability to add all of your software information into one specification file, makes for a much tighter (and easier to modify or reproduce) software packaging system."
* [http://fedoranews.org/alex/tutorial/rpm/ All you need to know about RPM] (more about installing packages than creating them)
* [http://fedoranews.org/alex/tutorial/rpm/ All you need to know about RPM] (more about installing packages than creating them)
* The [http://wiki.rpm.org/ rpm.org Wiki] has some useful information, such as the [http://wiki.rpm.org/Problems list of known RPM problems]


Note: The [http://rpm5.org/ rpm5.org] site has some documentation, but do not depend on it; that is the home of a ''fork'' of RPM maintained by Jeff Johnson.  The RPM used by Fedora (and Novell/SuSE) is instead based at [http://www.rpm.org rpm.org].
Note: The [http://rpm5.org/ rpm5.org] site has some documentation, but do not depend on it; that is the home of a ''fork'' of RPM maintained by Jeff Johnson.  The RPM used by Fedora (and Novell/SuSE) is instead based at [http://www.rpm.org rpm.org].
[http://lwn.net/Articles/236029/ lwn.net has a brief article] about this.
[http://lwn.net/Articles/236029/ lwn.net has a brief article] about this.
[[Category:Package Maintainers]]
[[Category:How to]]

Revision as of 12:56, 23 May 2016

Introduction

This page describes in detail how to create an RPM package, and in particular, how to create a SPEC file. Unlike other RPM guides, this page explains the specifics for Fedora with links to Fedora-specific guidelines. Since it is maintained through the Fedora Wiki, it is likely to be more up-to-date than other guides. Despite the focus on Fedora, most of this document does apply to other RPM-based distributions. If you're impatient, you might start by looking at the shorter How to create a GNU Hello RPM package.

Currently Fedora Documentation has released a draft guide for packagers, see Packagers Guide

Please note that these are NOT the official package guidelines for Fedora, the Packaging Committee handles the rules and guidelines for packaging software in Fedora. The most important ones:

Packaging Guidelines and Package Naming Guidelines are the main ones of Fedora Packaging. Having said that, this page should be compatible with them.

If you plan to create an RPM package for the Fedora repository, follow the procedure depicted in Join the package collection maintainers.

Preparing your system

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

# dnf install @development-tools fedora-packager rpmdevtools

You can create a dummy user specifically for creating RPM packages so that a build process went wrong can't trash your files or send your private keys to the world.

You should NEVER create your packages as the root user. Building RPM's as root is dangerous because the binary files are installed on the system before being packaged, thus you must always build as normal user so you won't accidentally pollute your system.

Create a new user named makerpm, add the user to the 'mock' group, set a password, and login as that user:

# /usr/sbin/useradd makerpm
# usermod -a -G mock makerpm
# passwd makerpm

Once you're logged in as the build/dummy user, create the required directory structure in your home directory by executing:

$ rpmdev-setuptree

The rpmdev-setuptree program will create the ~/rpmbuild directory and a set of subdirectories (e.g. SPECS and BUILD), which you will use for creating your packages. The ~/.rpmmacros file is also created, which can be used for setting various options.

The packaging guidelines recommend preserving file timestamps; you can make this automatic if you use wget or curl to get the source files. If you use wget to get source files, add the text "timestamping = on" to ~/.wgetrc. If you use curl, add the text "-R" to ~/.curlrc.

You won't normally need to do these steps again.

The basics of building RPM packages

To create an RPM package, you will need to create a ".spec" text file that provides information about the software being packaged.

You then run the rpmbuild command on the .spec file, which will go through a series of steps to produce your packages.

Normally, you should place your original (pristine) sources, such as .tar.gz files from the original developers, into the ~/rpmbuild/SOURCES directory. Place your .spec file in the ~/rpmbuild/SPECS directory and name it "NAME.spec", where NAME is the base name of the package.

Certain directories have certain purposes in the rpmbuild command, which can be substituted in the .spec file by the following (starting with %) predefined macros:

Macro Name Name Usually Purpose
%_specdir Specification directory ~/rpmbuild/SPECS RPM specifications (.spec) files
%_sourcedir Source directory ~/rpmbuild/SOURCES Pristine source package (e.g. tarballs) and patches
%_builddir Build directory ~/rpmbuild/BUILD Source files are unpacked and compiled in a subdirectory underneath this.
%_buildrootdir Build root directory ~/rpmbuild/BUILDROOT Files are installed under here during the %install stage.
%_rpmdir Binary RPM directory ~/rpmbuild/RPMS Binary RPMs are created and stored under here.
%_srcrpmdir Source RPM directory ~/rpmbuild/SRPMS Source RPMs are created and stored here.


To create both binary and source packages, change directory to ~/rpmbuild/SPECS and run:

$ rpmbuild -ba NAME.spec

When invoked this way, rpmbuild will read the .spec file and go through in order the stages listed below:

Stage Reads Writes Action
%prep %_sourcedir %_builddir This reads the sources and patches in the source directory %_sourcedir. It unpackages the sources to a subdirectory underneath the build directory %_builddir and applies the patches.
%build %_builddir %_builddir This compiles the files underneath the build directory %_builddir. This is often implemented by running some variation of "./configure && make".
%install %_builddir %_buildrootdir This reads the files underneath the build directory %_builddir and writes to a directory underneath the build root directory %_buildrootdir. The files that are written are the files that are supposed to be installed when the binary package is installed by an end-user. Beware of the weird terminology: The build root directory is not the same as the build directory. This is often implemented by running "make install".
%check %_builddir %_builddir Check that the software works properly. This is often implemented by running some variation of "make test". Many packages don't implement this stage.
bin %_buildrootdir %_rpmdir This reads the files underneath the build root directory %_buildrootdir to create binary RPM packages underneath the RPM directory %_rpmdir. Inside the RPM directory is a directory for each architecture, and a "noarch" directory for packages that apply to any architecture. These RPM files are the packages for users to install.
src %_sourcedir %_srcrpmdir This creates a source RPM package (.src.rpm) inside the source RPM directory %_srcrpmdir. These files are needed for reviewing and updating packages.


If a stage fails, look at the output to see why it failed and change the .spec file (or other input) as needed.

Getting ready to package a particular program

If there are special programs that are required to build or run the program you are packaging, install those other programs and write down what they are.

To package a program for the Fedora repository, you must package pristine (original) sources, along with the patches and build instructions; it's 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 the RPM building user account).

Read through the manual installation instructions for your program. It's often a good idea to do a "dry run" by manually building the program before attempting to do so via RPM. With a few exceptions, all binaries and libraries included in Fedora packages must be built from the source code that is included in the source package.

Split up the program

Application source code is often released with the source code of other external libraries "bundled" into them. Do not bundle external libraries with the main application into a single package. Instead, split them up into separate packages.

Licensing

Only package software that is legal for you to package. See Packaging:Guidelines#Legal, Licensing:Main and Packaging:LicensingGuidelines. In general, only package software that is released as open source software (OSS) using an approved OSS license (such as the GPL, LGPL, BSD-new, MIT/X, or Apache 2.0 licenses). Check to make sure that the software really is licensed this way (e.g. spot-check source code headers, README files etc.). If there are bundled libraries, make sure they are also OSS.

Reuse existing package information

Try to reuse what you can. Obviously, make sure you aren't packaging something that is already packaged. You can find a list of existing packages in Fedora Package Collection in the Fedora Package Database. Also check the In Progress Review Requests and the Retired Packages list. You can use Fedora Packages Git Repositories directly to view SPEC files (and patches). You can download the SRPMS using DNF download Plugin:

$ dnf download --source sourcepackage-name

Alternatively, get the source manually from the http/ftp page of a Fedora mirror within the releases/41/Everything/source/SRPMS directory. Replace "41" with the Fedora release you want and download the .src.rpm package you want.

Once you have the SRPM, install it into ~/rpmbuild:

$ rpm -ivh sourcepackage-name*.src.rpm

You can also unpack the SRPM into a directory using rpm2cpio:

$ mkdir PROGRAMNAME_src_rpm
$ cd PROGRAMNAME_src_rpm
$ rpm2cpio ../PROGRAMNAME-*.src.rpm | cpio -i

Sometimes it's easiest to start with an existing package and then clean it up for Fedora. RPM Find and PKGS.org may help you find RPMs for non-Fedora systems. You can install SRPMS for other systems the same way as for Fedora. Failing that, you might look at the source package files (not binary .deb) for Ubuntu or Debian (source package files are standard tarballs with a "debian/" subdirectory). 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. You should name it after the program name (e.g. "program.spec"). Use the archive name or the name advocated by the software author where you can, but be sure to follow the Package Naming Guidelines.

SPEC templates and examples

Templates

You may use the rpmdev-newspec command to create a SPEC file for you. rpmdev-newspec <package-name> can create an initial SPEC file for a new package, tailored to various types of packages. It will guess what kind of template to use based on the package name, or you can specify a particular template. See /etc/rpmdevtools/spectemplate-*.spec for available templates, and see rpmdev-newspec --help for more information. For example, to create a new SPEC file for a python module:

cd ~/rpmbuild/SPECS
rpmdev-newspec python-antigravity
vim python-antigravity.spec

According to the packaging guidelines, new spec files should be created this way. Vim and emacs can automatically create simple templates, but these are different than those made with rpmdev-newspec. This is especially true for templates with names beginning with python- or -perl.

Here is an example auto-generated .spec template:

Name:           testprogram
Version:        
Release:        1%{?dist}
Summary:        

License:        
URL:            
Source0:        

BuildRequires:  
Requires:       

%description


%prep
%setup -q


%build
%configure
make %{?_smp_mflags}


%install
rm -rf $RPM_BUILD_ROOT
%make_install


%files
%doc



%changelog
* Sun Apr  3 2016 makerpm
- 

You can use %{buildroot} instead of $RPM_BUILD_ROOT. Both are acceptable, but be consistent.

Examples

eject

Here's a simple example showing a Fedora 16 SPEC file for the eject program:

Summary:            A program that ejects removable media using software control
Name:               eject
Version:            2.1.5
Release:            21%{?dist}
License:            GPLv2+
Group:              System Environment/Base
Source:             %{name}-%{version}.tar.gz
Patch1:             eject-2.1.1-verbose.patch
Patch2:             eject-timeout.patch
Patch3:             eject-2.1.5-opendevice.patch
Patch4:             eject-2.1.5-spaces.patch
Patch5:             eject-2.1.5-lock.patch
Patch6:             eject-2.1.5-umount.patch
URL:                http://www.pobox.com/~tranter
ExcludeArch:        s390 s390x
BuildRequires:      gettext
BuildRequires:      libtool

%description
The eject program allows the user to eject removable media (typically
CD-ROMs, floppy disks or Iomega Jaz or Zip disks) using software
control. Eject can also control some multi-disk CD changers and even
some devices' auto-eject features.

Install eject if you'd like to eject removable media using software
control.

%prep
%autosetup -n %{name}

%build
%configure
make %{?_smp_mflags}

%install
%make_install

install -m 755 -d %{buildroot}/%{_sbindir}
ln -s ../bin/eject %{buildroot}/%{_sbindir}

%find_lang %{name}

%files -f %{name}.lang
%doc README TODO COPYING ChangeLog
%{_bindir}/*
%{_sbindir}/*
%{_mandir}/man1/*

%changelog
* Tue Feb 08 2011 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.1.5-21
- Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild

* Fri Jul 02 2010 Kamil Dudka <kdudka@redhat.com> 2.1.5-20
- handle multi-partition devices with spaces in mount points properly (#608502)

SPEC file overview

Other useful guides:

You will need to follow the Fedora guidelines: Package Naming Guidelines, Packaging guidelines, and Package review guidelines.

Insert comments with a leading "#" character, and beware of macros (beginning with %) that are potentially multi-line, as they are expanded first. When commenting out a line, double the percent signs (%%) of the macros appearing after the "#". Also avoid inline comments on the same line as script commands.

The major tags are listed below. Note that the macros %{name}, %{version} and %{release} can be used to refer to the Name, Version and Release tags respectively. When you change the tag, the macros automatically update to use the new value.

  • Name: The (base) name of the package, which should match the SPEC file name. It must follow the Package Naming Guidelines and generally be lowercase.
  • Version: The upstream version number. See Version tag section of the packaging guidelines. If the version contains tags that are non-numeric (contains tags that are not numbers), you may need to include the additional non-numeric characters in the Release tag. If upstream uses full dates to distinguish versions, consider using version numbers of the form yy.mm[dd] (e.g. 2008-05-01 becomes 8.05).
  • Release: The initial value should normally be 1%{?dist}. Increment the number every time you release a new package for the same version of software. When a new upstream version is released, change the Version tag to match and reset the Release number to 1. See Release tag section of the packaging guidelines. The optional Dist tag might be useful.
  • Summary: A brief, one-line summary of the package. Use American English. Do NOT end in a period.
  • Group: This needs to be a pre-existing group, like "Applications/Engineering"; run "less /usr/share/doc/rpm/GROUPS" to see the complete list. Use the group "Documentation" for any sub-packages (e.g. kernel-doc) containing documentation. Note: This tag is deprecated since Fedora 17. See Spec File Reference Preamble
  • License: The license, which must be an open source software license. Do not use the old Copyright tag. Use a standard abbreviation (e.g. "GPLv2+") and be specific (e.g. use "GPLv2+" for GPL version 2 or greater instead of just "GPL" or "GPLv2" where it's true). See Licensing and the Licensing Guidelines. You can list multiple licenses by combining them with "and" and "or" (e.g. "GPLv2 and BSD").
  • URL: The full URL for more information about the program (e.g. the project website). Note: This is not where the original source code came from which is meant for the Source0 tag below.
  • Source0: The full URL for the compressed archive containing the (original) pristine source code, as upstream released it. "Source" is synonymous with "Source0". If you give a full URL (and you should), its basename will be used when looking in the SOURCES directory. If possible, embed %{name} and %{version}, so that changes to either will go to the right place. Preserve timestamps when downloading source files. If there is more than one source, name them Source1, Source2 and so on. If you're adding whole new files in addition to the pristine sources, list them as sources after the pristine sources. A copy of each of these sources will be included in any SRPM you create, unless you specifically direct otherwise. See Source URL for more information on special cases (e.g. revision control).
  • Patch0: The name of the first patch to apply to the source code. If you need to patch the files after they've been uncompressed, you should edit the files and save their differences as a "patch" file in your ~/rpmbuild/SOURCES directory. Patches should make only one logical change each, so it's quite possible to have multiple patch files.
  • BuildArch: If you're packaging files that are architecture-independent (e.g. shell scripts, data files), then add "BuildArch: noarch". The architecture for the binary RPM will then be "noarch".
  • BuildRoot: This is where files will be "installed" during the %install process (after the %build process). This is now redundant in Fedora and is only needed for EPEL5. By default, the build root is placed in "%{_topdir}/BUILDROOT/".
  • BuildRequires: A comma-separated list of packages required for building (compiling) the program. This field can be (and is commonly) repeated on multiple lines. These dependencies are not automatically determined, so you need to include everything needed to build the program. It is possible to ensure you have specified all necessary build requires by performing a "mock build" of your package. You can specify a minimum version if necessary (e.g. "ocaml >= 3.08"). If you need the file /EGGS, determine the package that owns it by running "rpm -qf /EGGS". If you need the program EGGS, determine the package that owns it by running "rpm -qf which EGGS". Keep dependencies to a minimum (e.g. use sed instead of perl if you don't really need perl's abilities), but beware that some applications permanently disable functions if the associated dependency is not present; in those cases you may need to include the additional packages. The auto-buildrequires package may be helpful.
  • Requires: A comma-separate list of packages that are required when the program is installed. Note that the BuildRequires tag lists what is required to build the binary RPM, while the Requires tag lists what is required when installing/running the program; a package may be in one list or in both. In many cases, rpmbuild automatically detects dependencies so the Requires tag is not always necessary. However, you may wish to highlight some specific packages as being required, or they may not be automatically detected.
  • %description: A longer, multi-line description of the program. Use American English. All lines must be 80 characters or less. Blank lines indicate a new paragraph. Some graphical user interface installation programs will reformat paragraphs; lines that start with whitespace will be treated as preformatted text and displayed as is, normally with a fixed-width font. See RPM Guide.
  • %prep: Script commands to "prepare" the program (e.g. to uncompress it) so that it will be ready for building. Typically this is just "%autosetup"; a common variation is "%autosetup -n NAME" if the source file unpacks into NAME. See the %prep section below for more.
  • %build: Script commands to "build" the program (e.g. to compile it) and get it ready for installing. The program should come with instructions on how to do this. See the %build section below for more.
  • %install: Script commands to "install" the program. The commands should copy the files from the BUILD directory %{_builddir} into the buildroot directory, %{buildroot}. See the %install section below for more.
  • %check: Script commands to "test" the program. This is run after the %install procedure, so place it there if you have this section. Often it simply contains "make test" or "make check". This is separated from %build so that people can skip the self-test if they desire.
  • %clean: Instructions to clean out the build root. Note that this section is now redundant in Fedora and is only necessary for EPEL. Typically this contains only:
rm -rf %{buildroot}
  • %files: The list of files that will be installed. See the %files section below for more.
  • %changelog: Changes in the package. Use the format example above. Do NOT put software's changelog at here.This changelog is for RPM itself.
  • ExcludeArch: If the package does not successfully compile, build or work on a particular architecture, list those architectures under this tag.
  • You can add sections so that code will run when packages are installed or removed on the real system (as opposed to just running the %install script, which only does a pseudo-install to the build root). These are called "scriptlets", and they are usually used to update the running system with information from the package. See the "Scriptlets" section below for more.

RPM also supports the creation of several packages (called subpackages) from a single SPEC file, such as name-libs and name-devel packages.

Do NOT use these tags
  • Packager
  • Vendor
  • Copyright

Do not create a "relocatable" package; they don't add value in Fedora and make things more complicated.

SPEC file sections explained

%prep section

The %prep section describes how to unpack the compressed packages so that they can be built. Typically, this includes the "%autosetup" command. Alternatively, you can use "%setup" and "%patch" commands with reference to the Source0 (and Source1 etc.) lines. See the Maximum RPM section on %setup and %patch for more details.

The %{patches} and %{sources} macros are available since RPM 4.4.2 and are useful if you have a large list of patches or sources and %autosetup is not what you want, then you can do:

for p in %{patches}; do
    ...
done

However, keep in mind that using these will make your SPEC incompatible with RPMS used in RHEL and other RPM-based dirstributions.

%prep section: %autosetup command

The "%autosetup" command unpacks a source package. Switches include:

  • -n name : If the Source tarball unpacks into a directory whose name is not the RPM name, this switch can be used to specify the correct directory name. For example, if the tarball unpacks into the directory FOO, use "%autosetup -n FOO".
  • -c name : If the Source tarball unpacks into multiple directories instead of a single directory, this switch can be used to create a directory named name and then unpack into it.

If you use "%setup" command instead, then -q' is commonly used to suppress unecessary output.

There are more %setup options if you are unpacking multiple files, which is primarily useful if you are creating subpackages (see below). The key ones are:

-a number Only unpack the Source directive of the given number after changing directory (e.g. "–a 0" for Source0).
-b number Only unpack the Source directive of the given number before changing directory (e.g. "–b 0" for Source0).
-D Do not delete the directory before unpacking.
-T Disable the automatic unpacking of the archives.

%prep section: %patch commands

If you have used "%autosetup" command, the following manual patch management is not necessary. If you have complex requirements or need compatibility with EPEL, you may still need this. The "%patch0" command applies Patch0 (and %patch1 applies Patch1 etc.). Patches are the normal method of making necessary changes to the source code for packaging. The usual "-pNUMBER" option applies, which passes that argument onto the program patch.

Patch file names often look like "telnet-0.17-env.patch", which is the format %{name} - %{version} - REASON.patch" (though sometimes version is omitted). Patch files are typically the result of "diff -u"; if you do this from the subdirectory of ~/rpmbuild/BUILD then you won't have to specify a -p level later.

This is a typical procedure for creating a patch for a single file:

cp foo/bar foo/bar.orig
vim foo/bar
diff -u foo/bar.orig foo/bar > ~/rpmbuild/SOURCES/PKGNAME.REASON.patch

If editing many files, one easy method is to copy the whole subdirectory underneath BUILD and then do subdirectory diffs. After you have changed directory to "~rpmbuild/BUILD/NAME", do the following:

cp -pr ./ ../PACKAGENAME.orig/
... many edits ...
diff -ur ../PACKAGENAME.orig . > ~/rpmbuild/SOURCES/NAME.REASON.patch

If you edit many files in one patch, you can also copy the original files using some consistent ending such as ".orig" before editing them. Then, you can use "gendiff" (in the rpm-build package) to create a patch with the differences.

Try to ensure that your patch match the context exactly. The default "fuzz" value is "0", requiring matches to be exact. You can work around this by adding "%global _default_patch_fuzz 2" to revert to the value found in older versions of RPM in Fedora, but it is generally recommended to avoid doing this.

As explained in Packaging/PatchUpstreamStatus, all patches should have a comment above them in the SPEC file about their upstream status. This should document the upstream bug/email that includes it (including the date). If it is unique to Fedora, you should mention why it is unique. The Fedora Project tries not to deviate from upstream; see PackageMaintainers/WhyUpstream for the importance of this.

%prep section: Unmodified files

Sometimes, one or more of the Source files do not need to be uncompressed. You can "prep" those into the build directory like this (where SOURCE1 refers to the relevant Source file):

cp -p %SOURCE1 .

%build section

The "%build" section is sometimes complicated; here you configure and compile/build the files to be installed.

Many programs follow the GNU configure approach (or some variation). By default, they will install to a prefix of "/usr/local", which is reasonable for unpackaged files. However, since you are packaging it, change the prefix to "/usr". Libraries should be installed to either /usr/lib or /usr/lib64 depending on the architecture.

Since GNU configure is so common, the macro "%configure" can be used to automatically invoke the correct options (e.g. change the prefix to /usr). Some variation of this often works:

 %configure
 make %{?_smp_mflags}

To override makefile variables, pass them as parameters to make:

make %{?_smp_mflags} CFLAGS="%{optflags}" BINDIR=%{_bindir}

More more information, see "GNU autoconf, automake, and libtool" and "Open Source Development Tools: An Introduction to Make, Configure, Automake, Autoconf" by Stefan Hundhammer.

Some programs use cmake. See Packaging/cmake.

%install section

This section involves script commands to "install" the program, copying the relevant files from %{_builddir} to %{buildroot} (which usually means from ~/rpmbuild/BUILD to ~/rpmbuild/BUILDROOT) and creating directories inside %{buildroot} as necessary.

Some of the terminology can be misleading:

  • The "build directory", also known as %{_builddir} is not the same as the "build root", also known as %{buildroot}. Compilation occurs in the former directory, while files to be packaged are copied from the former to the latter.
  • During the %build section, the current directory will start at %{buildsubdir}, which is the subdirectory within %{_builddir} that was created during %prep stage. This is usually something like ~/rpmbuild/BUILD/%{name}-%{version}.
  • The %install section is not run when the binary RPM package is installed by the end-user, but is only run when creating a package.

Normally, some variation of "make install" is performed here:

%install
rm -rf %{buildroot} # redundant except for RHEL 5
%make_install

Ideally you should use %make_install which is equivalent to DESTDIR=%{buildroot} if the program supports it, as it redirects file installations to the specified directory and is exactly what we want to happen during the %install section.

If the program does not support DESTDIR (and only if), you can workaround it in one of several (inferior) ways:

  • Patch the makefile so that is supports DESTDIR. Create directories inside DESTDIR where necessary and submit the patch upstream.
  • Use the "%makeinstall" macro. This method might work, but can lead to subtle failures. It expands to something like "make prefix=%{buildroot}%{_prefix} bindir=%{buildroot}%{_bindir} ... install", which can result in some programs failing to work properly. Create directories inside %{buildroot} where necessary.
  • Consider using the auto-destdir package. This requires "BuildRequires: auto-destdir", and changing "make install" to "make-redir DESTDIR=%{buildroot} install". This only works well if the installation uses only certain common commands to install files, like cp and install.
  • Perform the installation by hand. This would involve creating the necessary directories under %{buildroot} and copying files from %{_builddir} to %{buildroot}. Be especially careful with updates, which often contain new or changed filenames. An example of this procedure:
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{_bindir}/
cp -p mycommand %{buildroot}%{_bindir}/

%check section

If self-tests are available, it is generally a good idea to include them. They should be placed in the %check section (which immediately follows the %install section, since files in %buildroot may be tested) instead of within the %build section itself, so that they can be easily skipped when necessary.

Often, this section contains:

make test

Sometimes it can be:

make check

Please explore the Makefile and choose the appropriate way.

%files section

This section declares which files and directories are owned by the package, and thus which files and directories will be placed into the binary RPM.

%files basics

The %defattr sets the default file permissions, and is often found at the start of the %files section. Note that this is no longer necessary unless the permissions need to be altered. The format of this is:

%defattr(<file permissions>, <user>, <group>, <directory permissions>)

The fourth parameter is often omitted. Usually one uses %defattr(-,root,root,-), where "-" uses the default permissions.

You should then list all the files and directories to be owned by the package. Use macros for directory names where possible, which can be viewed at Packaging:RPMMacros (e.g. use %{_bindir}/mycommand instead of /usr/bin/mycommand). If the pattern begins with a "/" (or when expanded from the macro) then it is taken from the %{buildroot} directory. Otherwise, the file is presumed to be in the current directory (e.g. inside %{_builddir}, such as documentation files that you wish to include). If your package only installs a single file /usr/sbin/mycommand, then the %files section can simply be:

%files
%{_sbindir}/mycommand

To make your package less sensitive to upstream changes, declare all files within a directory to be owned by the package with a pattern match:

%{_bindir}/*

To include a single directory:

%{_datadir}/%{name}/

Note that %{_bindir}/* does not claim that this package owns the /usr/bin directory, but only the files contained within. If you list a directory, then you are claiming that the package owns that directory and all files and subdirectories contained within. Thus, do not list %{_bindir} and be careful of directories that may be shared with other packages.

An error will occur if:

  • a pattern match does not match any file or directory
  • a file or directory is listed or matched more than once
  • a file or directory in the %{buildroot} has not been listed

It is also possible to exclude files from a previous match by using the %exclude glob. This can be useful for including almost all of the files included by a different pattern match, but note that it will also fail if it does not match anything.

%files prefixes

You may need to add one or more prefixes to lines in the %files section; seperate them with a space. See Max RPM section on %files directives.

Usually, "%doc" is used to list documentation files within %{_builddir} that were not copied to %{buildroot}. A README and INSTALL file is usually included. They will be placed in an appropriate directory under /usr/share/doc, whose ownership does not need to be declared.

Note: If specifying a %doc entry, rpmbuild < 4.9.1 removes the doc directory it installs files into before installing them. This means that files already in it, for example installed in the %install section, are removed and do not end up in the package. If you want to install some files in the %install section, install them into a temporary staging directory inside the build dir (not build root), for example _docs_staging, and include them in the in the %files list like %doc _docs_staging/*.

Configuration files should be placed in /etc and are normally specified like this (which makes sure user changes aren't overwritten on update):

%config(noreplace) %{_sysconfdir}/foo.conf

If the update uses a non-backwards-compatible configuration format, then specify them like this:

%config %{_sysconfdir}/foo.conf

"%attr(mode, user, group)" can be used for finer control over permissions, where a "-" means use the default:

%attr(0644, root, root) FOO.BAR

"%caps(capabilities)" can be used to give a file certain POSIX capabilities. For example:

%caps(cap_net_admin=pe) FOO.BAR

If a file is in particular natural language, use %lang to note that:

%lang(de) %{_datadir}/locale/de/LC_MESSAGES/tcsh*

Programs using Locale files should follow the recommended method of handling i18n files:

  • find the filenames in the %install step: %find_lang ${name}
  • add the required build dependencies: BuildRequires: gettext
  • use the found filenames: %files -f ${name}.lang

The %readme prefix is not valid in Fedora.

%files and Filesystem Hierarchy Standard (FHS)

You should follow the Filesystem Hierarchy Standard (FHS). Executables go in /usr/bin, global configuration files go in /etc, libraries go into /usr/lib (or /usr/lib64) and so on. There is one exception: executables that should not normally be executed directly by users or administrators should go in a subdirectory of /usr/libexec, which is referred to as %{_libexecdir}/%{name}.

Do not install files into /opt or /usr/local.

Unfortunately, many programs do not follow the FHS by default. In particular, architecture-independent libraries get placed in /usr/lib instead of /usr/share. The former is for architecture-dependent libraries, while the latter is for architecture-independent libraries, which means that systems with different CPU architectures can share /usr/share. There are many exceptions in Fedora (such as Python and Perl), but Fedora applies this rule more strictly than some distributions. rpmlint will generally complain if you put anything other than ELF files into /usr/lib.

%files example

Here's a simple example of a %files section:

%files
%doc README
%license LICENSE COPYING
%{_bindir}/*
%{_sbindir}/*
%{_datadir}/%{name}/
%config(noreplace) %{_sysconfdir}/*.conf

Finding duplicates

You can list any duplicates of two binary packages by doing:

cd ~/rpmbuild/RPMS/ARCH # Substitute "ARCH" for your architecture
rpm -qlp PACKAGE1.*.rpm | sort > ,1
rpm -qlp PACKAGE2.*.rpm | sort > ,2
comm -12 ,1 ,2

Scriptlets

When an end-user installs the RPM, you may want some commands to be run. This can be achieved through scriptlets. See Packaging:Scriptlets.

Scriptlets can be run:

  • before (%pre) or after (%post) a package is installed
  • before (%preun) or after (%postun) a package is uninstalled
  • at the start (%pretrans) or end (%posttrans) of a transaction

For example, every binary RPM package that stores shared library files in any of the dynamic linker's default paths, must call ldconfig in %post and %postun. If the package has multiple subpackages with libraries, each subpackage should also perform the same actions.

%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig

If only running a single command, then the "-p" option runs the adjacent command without invoking the shell. However, for several commands, omit this option and include the shell commands beneath.

If you run any programs within the scriptlets, then you must specify any requirements in the form "Requires(CONTEXT)" (e.g. Requires(post)).

%pre, %post, %preun, and %postun provide the argument $1, which is the number of packages of this name which will be left on the system when the action completes. Don't compare for equality with 2, but instead check if they are greater than or equal to 2. For %pretrans and %posttrans, $1 is always 0.

For example, if the package installs an info manual, then the info manual index must be updated with install-info from the info package. Firstly, there is no guarantee that the info package will be available unless we explicitly declare it as required, and secondly, we don't want to fail completely if install-info fails:

Requires(post): info
Requires(preun): info
...
%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :
%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi

There is one other glitch related to installing info manuals. The install-info command will update the info directory, so we should delete the useless empty directory from the %{buildroot} during the %install section:

rm -f %{buildroot}%{_infodir}/dir

Another scriptlet-like ability are "triggers", which can be run for your package when other packages are installed or uninstalled. See RPM Triggers.

Macros

Macros are text in the format %{string}. Typical macros:

Macro Typical Expansion Meaning
%{_bindir} /usr/bin Binary directory: where executables are usually stored.
%{_builddir} ~/rpmbuild/BUILD Build directory: files are compiled within a subdirectory of the build directory. See %buildsubdir.
%{buildroot} ~/rpmbuild/BUILDROOT/%{name}... Build root: where files are "installed" during the %install stage, which copies files from a subdirectory of %{_builddir} to a subdirectory of %{buildroot}. (Historically, %{buildroot} was in "/var/tmp/".)
%{buildsubdir} %{_builddir}/%{name} Build subdirectory: a subdirectory within %{_builddir} where files are compiled during the %build stage. It is set after %autosetup.
%{_datadir} /usr/share Share directory.
%{_defaultdocdir} /usr/share/doc Default documentation directory.
%{dist} .fcNUMBER Distribution+version short name (e.g. ".fc41")
%{fedora} NUMBER Number of fedora release (e.g. "41")
%{_includedir} /usr/include
%{_infodir} /usr/share/info
%{_initrddir} /etc/rc.d/init.d
%{_libdir} /usr/lib
%{_libexecdir} /usr/libexec
%{_localstatedir} /var
%{_mandir} /usr/share/man
%{name} Name of package, set by Name: tag
%{_sbindir} /usr/sbin
%{_sharedstatedir} /var/lib
%{_sysconfdir} /etc
%{version} Version of package, set by Version: tag

Learn more about macros by looking in /etc/rpm/* and /usr/lib/rpm, especially /usr/lib/rpm/macros. Also use rpm --showrc to show values that RPM will use for macros (altered by rpmrc and macro configuration files).

You can set your own macro values using %global, but be sure to define them before you use them. (Macro definitions can also refer to other macros.) For example:

%global date 2012-02-08

Use the "-E" option of rpmbuild to find the value of a macro in a SPEC file:

rpmbuild -E '%{_bindir}' myfile.spec

Also see Packaging/RPMMacros and RPM Guide chapter 9.

Other tags

In addition to Requires and BuildRequires tags, you can also use these for controlling dependencies:

  • Provides: list virtual package names that this package provides. For example, there might be a package "foo" that demands a particular functionality "bar" from another program. If there are several packages that can satisfy that demand, those packages can specify "Provides: bar" and the "foo" package can specify "Requires: bar". You could also use the "alternatives" system, but avoid if multiple users on the same system might want different default, as these settings are system-wide. Use "rpm -q --provides PACKAGENAME" to see what a given package provides. Some examples of virtual packages in Fedora:
    • MTA: Used for mail transport agents, such as sendmail.
    • tex(latex): Used for latex
  • Obsoletes: remove another named package(s) when this package is installed. Use when the package name changes or when it totally replaces a different package.
  • Conflicts: state what other packages cannot be installed simultaneously to this one. Avoid this if you can. See Packaging/Conflicts.
  • BuildConflicts: state what packages cannot be installed when building this package. Avoid this if you can.

To manage different architectures, there are two tags:

  • ExcludeArch: to exclude an architecture on which the package doesn't build. For example:
ExcludeArch: ppc
  • ExclusiveArch: to include only the specified architecture. Avoid this unless absolutely correct.

Valid architectures are listed at Architectures.

Subpackages

A SPEC file can define several binary package. In other words, one SRPM with one SPEC file can result in several RPMS. Note that there is still only one creation (%prep, %build, %install etc.) process. name-doc and name-devel subpackages are common for documentation and development files respectively.

Use the %package directive to start defining a subpackage:

%package subpackage_name

After each %package directive, list the tags for the subpackage. This should at least include the Summary and Group tags, as well as the %description subpackage_name and %files subpackage_name directives:

Anything not specified by the subpackage will be inherited from its parent.

By default, if the package name is "foo" and the subpackage name is "bar", then the resulting subpackage will be "foo-bar". You can override it with the "-n" option (but you'll need to use it in all other directives too if you specify it here):

%package -n new_subpackage_name

See the RPM Guide section on subpackages for more information.

Conditionals

You can insert conditional statements, for example to test if you are creating a binary for a certain architecture:

%ifarch ARCHITECTURE_NAME

the negated version with:

%ifnarch ARCHITECTURE_NAME

or the more general conditional:

%if TRUE_OR_FALSE

There is an optional "%else" section; all of these are closed with "%endif".

Application Specific Guidelines

There are many application-specific guidelines that can help you (e.g., for specific programming languages, applications, libraries, and build systems). Many of them are listed as part of the Application Specific Guidelines of Packaging/Guidelines. Examples of application-specific guidelines are those for:

Failing that, some other ways of finding application-specific help are:

Miscellaneous hints

Packaging/FrequentlyMadeMistakes has information on frequently-made mistakes. There are also some recommendations and controversial tricks on PackageMaintainers/Packaging Tricks.

Try to write your SPEC files so that it is likely to work when a new release is made upstream, without any changes aside from bumping the version number and refreshing the source files. For example, if it contains *.txt files with execute bits, instead of doing

 chmod a-x Filename1.txt Filename2.txt Filename3.txt

consider doing this, which will handle new filenames that use the same file naming convention:

 chmod a-x *.txt

If you want to see lots of examples of scriptlets, you can show all the scriptlets on installed programs using:

 rpm -qa --queryformat "\n\nPACKAGE: %{name}\n" --scripts | less

Don't try to interact with the user; RPM is designed to support batch installs. If an application needs to show a EULA, that needs to be part of its initial execution, not its installation.

You might not want to start services, because in a big install that could slow things down. If you install an init or systemd script, consider using chkconfig or systemctl to arrange for the service to be started/stopped on the next reboot. Before uninstalling, you should normally try to stop its services if they are running.

Uninstalling should reverse most changes made during installation, but don't remove any user-created files.

Normally, if there are binary executables, then debugging symbols are stripped from the normal binary packages and placed into a name-debug subpackage. If this shouldn't happen, you can disable the stripping and creation of this subpackage by putting this at the top of your SPEC:

%global _enable_debug_package 0
%global debug_package %{nil}
%global __os_install_post /usr/lib/rpm/brp-compress %{nil}

To prevent stripping you may also need to do this in the %install section:

export DONT_STRIP=1

A way to check for the version of Fedora in a SPEC file for conditional builds is:

%if 0%{?fedora} <= <version>

The ? causes the macro to evaluate to evaluate to blank if %fedora is not defined. This causes the end result to be the 0 (which is a number and thus fine), while not interfering with the result if there is actually a value for %fedora. (Note that this trick does not work in Koji "scratch" builds, where %fedora is set during the creation of a SRPM.)

GUI programs must have a desktop entry so that people can invoke it from the graphical desktop menu. For .desktop files, see Fedora packaging guidelines for desktop files and desktop entry spec. For icons within /usr/share/icons, see icon theme spec.

Building the binary package

Test with rpmlint

To catch many common errors early, run rpmlint on your SPEC file before trying to build anything from it:

$ rpmlint program.spec

If the reported error doesn't make sense, run it again with the "-i" option for longer messages.

Aim to have no errors, but sometimes rpmlint reports false positives. The Fedora packaging guidelines explains which ones to ignore.

Create binary RPMS from the SPEC file

Once you've created your SPEC file, build the SRPM and binary RPMS by running this:

$ rpmbuild -ba program.spec

If successful, RPMS will be created within ~/rpmbuild/RPMS and SRPMS will be created within ~/rpmbuild/SRPMS.

If it fails, go to the appropriate directory and see what is left over. To help debug, you can skip earlier stages that succeeded with the "--short-circuit" option. For example, to restart at the %install stage (skipping earlier stages), do this:

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

If you just want to create an SRPM (which does not run the %prep or %build or other stages), run this:

rpmbuild -bs program.spec

Testing binary RPMS with rpmlint

rpmlint can be run on SPEC files, RPMS and SRPMS to check for errors. You need to eliminate or justify warnings before posting a package. This page offers explanations for some of the common issues that come up. If you are in the SPECS directory, do this:

$ rpmlint NAME.spec ../RPMS/*/NAME*.rpm ../SRPMS/NAME*.rpm

Enter the ~/rpmbuild/RPMS directory and into the architecture subdirectory. You will find some binary RPMS. Quickly see their files and permissions (to check whether they are correct) by doing:

$ rpmls *.rpm

If they look fine, install them as root:

# rpm -ivp package1.rpm package2.rpm package3.rpm ...

Test the programs in a few different ways to see if everything works correctly. If it is a GUI tool, make sure it shows up in the desktop menu, otherwise the .desktop entry is wrong.

Uninstall packages later by doing:

# rpm -e package1 package2 package3

Mock and Koji

Mock is a powerful tool that uses the SRPM you have created to build binary packages within a nearly empty environment. This can reveal if you have accurate build dependencies. If it fails, then you forgot to list something in BuildRequires. See Using Mock to test package builds. Once your account is a member of the "mock" group, you can run commands like this to do local testing:

$ mock -r fedora-23-i386 rebuild path_to_source_RPM

You can use Koji (which uses mock) to do builds on many different systems, some of which you may not have. PackageMaintainers/Join and PackageMaintainers/UsingKoji have more information about Koji. Once it's set up, you can test your SRPM on a variety of platforms by running commands like:

$ koji build --scratch f23 path_to_source_RPM

Replace f23 with any later release of Fedora, or rawhide.

Your Koji builds can only depend on packages that are actually in the TARGET distribution repository. Thus, you can't use Koji to build for released distributions if your package depends on other new packages that Bodhi hasn't released yet. If you need to build against a package that is not yet a stable released update, submit a Koji buildroot override request via Bodhi. If it's not your own package you depend on, contact its maintainer(s). [Before Bodhi could handle Koji buildroot override requests, the old method has been to file a ticket with rel-eng at: https://fedorahosted.org/rel-eng/newticket and request that that package be added as a buildroot override.]

Helpful tools

The rpmdevtools package has a number of helpful tools; "rpm -qil rpmdevtools" will show you what it installs.

  • rpmdev-bumpspec : bump the release tag in the spec file and add a changelog comment with the right date and version format:
rpmdev-bumpspec --comment=COMMENT --userstring=NAME+EMAIL_STRING SPECFILES

DNF download plugin (from core DNF plugins) is also useful:

  • dnf download : download the SRPM of the package by running:
dnf download --source PACKAGENAME

The auto-buildrequires package has a pair of nice tools for helping to figure out the proper BuildRequires entries. After installing this package, replace "rpmbuild" with "auto-br-rpmbuild" and you'll see an automatically generated BuildRequires list.

You might find RUST useful (GPL), though it does not create SPEC files of suitable quality for Fedora packages. Alien converts between package formats. It won't produce clean SRPMS, but converting an existing package might provide helpful information.

If you are thinking about packing your rpm for Fedora, be sure you run your packages through Fedora Review, which helps ensure that you comply to the Packaging Guideline.

Finally, docker-rpm-builder (APL 2.0) uses Docker to build RPM packages; while using rpmbuild requires building on the same host distro as the target, and mock works fine on Fedora/Centos/RHEL distributions for any target, this last tool works fine whenever Docker can run.

If you want to build your package for diferent distribution and architectures and to have publicly accesible dnf repository, you can submit your src.rpm to Copr.

If you need to sign your new package, use rpmsign tool from rpm-sign package.

Guidelines and rules

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

There are many official guidelines that will help you with specific circumstances (e.g. Java programs, OCaml programs, GNOME programs). You can also learn more from the SIGs and Package Maintainers sections.

You can also see the list of all Wiki pages about Packaging to see if any apply.

Failing that, you might find some useful recommendations in the unofficial Packaging Drafts and Packaging Drafts To Do.

You might find ideas from SuSE, Debian, but distributions differ in their rules, so do not presume they can be used directly.

The .spec files that you create must be open source software, as noted in the FPCA.

Maintaining the package

Once your package has been accepted, you and your co-maintainers will need to maintain it. See Package update HOWTO and Package update guidelines. If you update the version in multiple releases of Fedora, do it backwards in time (e.g. release for Fedora N, then once that's accepted, Fedora N-1). The system presumes that later versions of Fedora have the same or later versions of programs.

Encourage the upstream developers to use standard source code release conventions. Using standard conventions makes packaging much easier. For more information, see:

For more information

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

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

Note: The rpm5.org site has some documentation, but do not depend on it; that is the home of a fork of RPM maintained by Jeff Johnson. The RPM used by Fedora (and Novell/SuSE) is instead based at rpm.org. lwn.net has a brief article about this.