Creating packages

From ArchWiki
Revision as of 19:47, 20 March 2010 by Pointone (talk | contribs) (Defining PKGBUILD variables: fix file with nowiki)
Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

This article aims to assist users creating their own packages using the Arch Linux "ports-like" build system. It covers creation of a PKGBUILD – a package build description file sourced by Template:Codeline to create a binary package from source. If already in possession of a Template:Filename, see makepkg.


Packages in Arch Linux are built using the makepkg utility and the information stored in a PKGBUILD file. When Template:Codeline is run, it searches for a Template:Filename in the current directory and follows the instructions therein to either compile or otherwise acquire the required files to be packaged within a package file (Template:Filename). The resulting package contains binary files and installation instructions; readily installed with pacman.

An Arch package is no more than a gzipped tar archive, or 'tarball', which contains:

  • The binary files to install
  • Template:Filename: contains all the metadata needed by pacman to deal with packages, dependencies, etc.
  • Template:Filename: an optional file kept by the package maintainer documenting the changes of the package. (It is not present in all packages.)


Prerequisite software

First ensure that the necessary tools are installed. The package group "base-devel" should be sufficient; it includes make and additional tools needed for compiling from source.

# pacman -S base-devel

One of the key tools for building packages is makepkg (provided by Template:Package Official) which does the following:

  1. Checks if package dependencies are installed.
  2. Downloads the source file(s) from the specified server(s).
  3. Unpacks the source file(s).
  4. Compiles the software and installs it under a fakeroot environment.
  5. Strips symbols from binaries and libraries.
  6. Generates the package meta file which is included with each package.
  7. Compress the fakeroot environment into a package file.
  8. Stores the package file in the configured destination directory, which is the present working directory by default.

Download and test the installation

Download the source tarball of the software you want to package, extract it, and follow the author's steps to install the program. Make a note of all commands and/or steps needed to compile and install it. You will be repeating those same commands in the PKGBUILD file.

Most software authors stick to the 3-step build cycle:

make install

This is a good time to make sure the program is working correctly.

Creating a PKGBUILD

When you run Template:Codeline, it will look for a Template:Filename file in the present working directory. If a Template:Filename file is found it will download the software's source code and compile it according to the instructions specified in the Template:Filename file. The instructions must be fully interpretable by the Bash shell. After successful completion, the resulting binaries and metadata of the package, i.e. package version and dependencies, are packed in a Template:Filename package file that can be installed with Template:Codeline.

To begin with a new package, you should first create an empty working directory, (preferably Template:Filename), change into that directory, and create a Template:Filename file. You can either copy the prototype PKGBUILD Template:Filename to your working directory or copy a Template:Filename from a similar package. The latter may be useful if you only need to change a few options.

Defining PKGBUILD variables

The Template:Filename file contains metadata about a package. It is a plain text file. The following is a prototype Template:Filename. It can be found in Template:Filename along with other templates.


An explanation of possible Template:Filename variables can be found in the PKGBUILD article.

The Template:Codeline function

Now you need to implement the Template:Codeline function in the Template:Filename file. This function uses common shell commands in Bash syntax to automatically compile software and create a Template:Filename directory to install the software to. This allows makepkg to package files without having sift through your filesystem.

The first step in the Template:Codeline function is to change into the directory created by uncompressing the source tarball. In most common cases the first command will look like this:

cd $srcdir/$pkgname-$pkgver

Now, you need to list the same commands you used when you manually compiled the software. The Template:Codeline function in essence automates everything you did by hand and compiles the software in the fakeroot build environment. If the software you are packaging uses a configure script, it is good practice to use Template:Codeline when building packages for pacman. A lot of software installs files relative to the Template:Filename directory, which should only be done if you are manually building from source. All Arch Linux packages should use the Template:Filename directory. As seen in the Template:Filename file, the next two lines often look like this:

./configure --prefix=/usr
make || return 1

The final step in the Template:Codeline function is to put the compiled files in a directory where makepkg can retrieve them to create a package. This by default is the Template:Filename directory - a simple fakeroot environment. The Template:Filename directory replicates the hierarchy of the root file system of the software's installation paths. If you have to manually place files under the root of your filesystem, you should install them in the Template:Filename directory under the same directory structure. For example, if you want to install a file to Template:Filename, it should instead be placed under Template:Filename. Very few install procedures require the user to copy dozens of files manually. Instead, for most software, calling Template:Codeline will do so. The final line should look like the following in order to correctly install the software in the Template:Filename directory:

make DESTDIR=$pkgdir install || return 1
Note: It is sometimes the case where Template:Codeline is not used in the Template:Filename; you may need to use Template:Codeline instead. If the package is built with autoconf/automake, use Template:Codeline; this is what is documented in the manuals. If Template:Codeline does not work, try building with Template:Codeline. If that does not work, you'll have to look further into the install commands that are executed by "Template:Codeline".

In some odd cases, the software expects to be run from a single directory. In such cases, it is wise to simply copy these to Template:Filename.

More often than not, the installation process of the software will create any subdirectories below the Template:Filename directory. If it does not, however, makepkg will generate a lot of errors and you will need to manually create subdirectories by adding the appropriate Template:Codeline commands in the Template:Codeline function before the installation procedure is run.

Also, makepkg defines three variables that you should use as part of the build and install process:

This contains the absolute path to the directory where the Template:Filename file is located. This variable used to be used in combination with Template:Filename or Template:Filename postfixes, but the use of Template:Codeline and Template:Codeline variables is the modern method. Template:Codeline is not guaranteed to be the same as Template:Codeline, and likewise for Template:Codeline. Use of this variable is deprecated and strongly discouraged.
This points to the directory where makepkg extracts or copies all source files.
This points to the directory where makepkg bundles the installed package, which becomes the root directory of your built package.
Note: makepkg, and thus the Template:Codeline function, is intended to be non-interactive. Interactive utilities or scripts called in the Template:Codeline function may break makepkg, particularly if it is invoked with build-logging enabled (Template:Codeline). (See Arch Linux Bug #13214.)

Additional guidelines

Please read Arch Packaging Standards thoroughly for best practices and additional considerations.

Testing the PKGBUILD and package

As you are writing the Template:Codeline function, you will want to test your changes frequently to ensure there are no bugs. You can do this using the Template:Codeline command in the directory containing the Template:Filename file. With a properly formatted Template:Filename, makepkg will create a package; with a broken or unfinished Template:Filename, it will raise an error.

If makepkg finishes successfully, it will place a file named Template:Filename in your working directory. This package can be installed with the Template:Codeline command. However, just because a package file was built does not imply that it is fully functional. It might conceivably contain only the directory and no files whatsoever if, for example, a prefix was specified improperly. You can use pacman's query functions to display a list of files contained in the package and the dependencies it requires with Template:Codeline and Template:Codeline respectively.

If the package looks sane, then you are done! However, if you plan on releasing the Template:Filename file, it is imperative that you check and double-check the contents of the Template:Codeline array.

Also ensure that the package binaries actually run flawlessly! It is annoying to release a package that contains all necessary files, but crashes because of some obscure configuration option that doesn't quite work well with the rest of the system. If you're only going to compile packages for your own system, though, you don't need to worry too much about this quality assurance step, as you're the only person suffering from mistakes, after all.

Template:Codeline and Template:Codeline

Dependencies are the most common packaging error. There are two excellent tools you can use to check dependencies. The first one is ldd, which will show you the shared library dependencies of dynamic executables:

$ ldd gcc =>  (0xb7f33000) => /lib/ (0xb7de0000)
/lib/ (0xb7f34000)

The other tool is namcap, which not only checks for dependencies but the overall sanity of your package. Please read the namcap article for a detailed description.

Submitting packages to the AUR

Please read AUR User Guidelines#Submitting Packages to UNSUPPORTED for a detailed description of the submission process.


  1. Download the source tarball of the software you want to package.
  2. Try compiling the package and installing it into an arbitrary directory.
  3. Copy over the prototype Template:Filename and rename it to Template:Filename in a temporary working directory -- preferably Template:Filename.
  4. Edit the Template:Filename according to the needs of your package.
  5. Run Template:Codeline and see whether the resulting package is built correctly.
  6. If not, repeat the last two steps.


  • Before you can automate the package building process, you should have done it manually at least once unless you know exactly what you're doing in advance, in which case you would not be reading this in the first place. Unfortunately, although a good bunch of program authors stick to the 3-step build cycle of "Template:Codeline; Template:Codeline; Template:Codeline", this is not always the case, and things can get real ugly if you have to apply patches to make everything work at all. Rule of thumb: If you can't get the program to compile from the source tarball, and make it install itself to a defined, temporary subdirectory, you don't even need to try packaging it. There isn't any magic pixie dust in Template:Codeline that makes source problems go away.
  • In a few cases, the packages are not even available as source and you have to use something like Template:Codeline to get it to work. You will have to do quite a bit of research (read READMEs, INSTALL instructions, man pages, perhaps ebuilds from Gentoo or other package installers, possibly even the MAKEFILEs or source code) to get it working. In some really bad cases, you have to edit the source files to get it to work at all. However, Template:Codeline needs to be completely autonomous, with no user input. Therefore if you need to edit the makefiles, you may have to bundle a custom patch with the Template:Filename and install it from inside the Template:Codeline function, or you might have to issue some Template:Codeline commands from inside the Template:Codeline function.