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.

Overview

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 tar archive compressed using xz, or 'tarball', which contains:

The binary files to install

Template:Filename: contains all the metadata needed by pacman to deal with packages, dependencies, etc.

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:

./configure
make
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.

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 to 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:

The final step 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:

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.

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.

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:

Warnings

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.