Creating packages
Related articles
- Arch Build System
- Arch packaging standards
- Arch User Repository
- Creating packages for other distributions
- makepkg
- pacman
- Patching in ABS
- PKGBUILD
- .SRCINFO
- DeveloperWiki:Building in a Clean Chroot This article aims to assist users creating their own packages using the Arch Linux "ports-like" build system, also for submission in AUR. It covers creation of a PKGBUILD – a package build description file sourced by to create a binary package from source. If already in possession of a , see makepkg. For instructions regarding existing rules and ways to improve package quality, see Arch packaging standards.
- The binary files to install.
.PKGINFO
: contains all the metadata needed by pacman to deal with packages, dependencies, etc.- : contains information needed for reproducible builds. This file is present only if a package is built with pacman 5.1 or newer. See .
- : contains hashes and timestamps of the files, which are included in the local database so that pacman can verify the integrity of the package.
- : an optional file used to execute commands after the install/upgrade/remove stage. (This file is present only if specified in the .)
- : an optional file kept by the package maintainer documenting the changes of the package. (It is not present in all packages.)
- Checks if package dependencies are installed.
- Downloads the source file(s) from the specified server(s).
- Unpacks the source file(s).
- Compiles the software and installs it under a fakeroot environment.
- Strips symbols from binaries and libraries.
- Generates the package meta file which is included with each package.
- Compresses the fakeroot environment into a package file.
- Stores the package file in the configured destination directory, which is the current working directory by default.
- This points to the directory where makepkg extracts or symlinks all files in the source array.
- This points to the directory where makepkg bundles the installed package, which becomes the root directory of your built package.
- Check PKGBUILD contents for common errors and package file hierarchy for unnecessary/misplaced files
- Scan all ELF files in package using , automatically reporting which packages with required shared libraries are missing from
depends
and which can be omitted as transitive dependencies - Heuristically search for missing and redundant dependencies
- Download the source tarball of the software to package.
- Try compiling the package and installing it into an arbitrary directory.
- Copy over the prototype and rename it to in a temporary working directory.
- Edit the according to the needs of your package.
- Run and check whether the package builds correctly.
- If not, repeat the previous 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 are 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 "; ; ", 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 cannot get the program to compile from the source tarball, and make it install itself to a defined, temporary subdirectory, you do not even need to try packaging it. There is not any magic pixie dust in 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 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, 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 and install it from inside the function, or you might have to issue some commands from inside the function.
Overview
Packages in Arch Linux are built using the makepkg utility and the information stored in a PKGBUILD file. When runs, it searches for a in the current directory and follows the instructions in it to acquire the required files and/or compile them to be packed within a package file (). The resulting package contains binary files and installation instructions ready to be installed by pacman.
An Arch package is no more than a tar archive, or 'tarball', compressed using zstd(1), which contains the following files generated by makepkg:
Preparation
Prerequisite software
First, ensure that the necessary tools are installed: the package group should be sufficient; it includes and additional tools needed for compiling from source.
The key tool for building packages is makepkg (provided by ), which does the following:
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 file.
Most software authors stick to the 3-step build cycle:
./configure make make installThis is a good time to make sure the program is working correctly.
Creating a PKGBUILD
When is run, it looks for a file in the current working directory. If it finds one, it downloads the software's source code and compiles it according to the instructions specified in the 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 package file. The newly created package can be installed by simply using makepkg --install
which will call pacman in the background, or by directly using .
To start building a new package, first create a new directory for the package and change current directory into this one. Then, a file needs to be created: a prototype PKGBUILD found in can be used or you can start from a from another package. The latter may be a good choice if a similar package already exists.
Defining PKGBUILD variables
Example PKGBUILDs are located in /usr/share/pacman/
. An explanation of possible variables can be found in the PKGBUILD article.
makepkg defines two variables that you should use as part of the build and install process:
They contain absolute paths, which means you do not have to worry about your working directory if you use these variables properly.
PKGBUILD functions
When building a package, will invoke the following five functions if they have been defined in the PKGBUILD. Function is required in every PKGBUILD and will always be invoked. If any of the other functions is not defined, will simply skip the invocation of that function.
During the build, the functions are invoked in the order in which they are listed here.
prepare()
With this function, commands that are used to prepare sources for building are run, such as patching. This function runs right after package extraction, before pkgver() and the build function. If extraction is skipped (), then is not run.
bash -e
mode, meaning any command that exits with a non-zero status will cause the function to exit.pkgver()
runs after the sources are fetched, extracted and prepare() executed. So you can update the pkgver variable during a makepkg stage.This is particularly useful if you are making git/svn/hg/etc. packages, where the build process may remain the same, but the source could be updated every day, even every hour. The old way of doing this was to put the date into the pkgver field which, if the software was not updated, makepkg would still rebuild it thinking the version had changed. Some useful commands for this are , , etc. Please test these before submitting a PKGBUILD, as a failure in the function can stop a build in its tracks.
-
). Using sed to correct this is common.build()
Now, you need to implement the function in the file. This function uses common shell commands in Bash syntax to automatically compile software and create a directory called to install the software to. This allows makepkg to package files without having to sift through your file system.
The first step in the function is to change into the directory created by uncompressing the source tarball. makepkg will change the current directory to before executing the function. Therefore, in most cases, like suggested in , the first command will look like this:
cd "$pkgname-$pkgver"Now, you need to list the same commands you used when you manually compiled the software. The 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 when building packages for pacman. A lot of software installs files relative to the /usr/local
directory, which should only be done if you are manually building from source. All Arch Linux packages should use the directory. As seen in the file, the next two lines often look like this:
check()
Place for calls to and similar testing routines. It is highly recommended to have as it helps to make sure software has been built correctly and works fine with its dependencies.
Users who do not need it (and occasionally maintainers who can not fix a package for this to pass) can disable it using in PKGBUILD/makepkg.conf or call with --nocheck
flag.
package()
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 directory—a simple fakeroot environment. The 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 directory under the same directory structure. For example, if you want to install a file to , it should instead be placed under . Very few install procedures require the user to copy dozens of files manually. Instead, for most software, calling will do so. The final line should look like the following in order to correctly install the software in the directory:
make DESTDIR="$pkgdir/" installmakepkg --repackage
runs only the function, so it creates a package without building. This may save time e.g. if you have changed just the variable of the package.
Testing the PKGBUILD and package
As you are writing the function, you will want to test your changes frequently to ensure there are no bugs. You can do this using the command in the directory containing the file. With a properly formatted , makepkg will create a package; with a broken or unfinished , it will raise an error.
If makepkg finishes successfully, it will place a file named in your working directory. This package can be installed with the 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 and respectively.
If the package looks sane, then you are done! However, if you plan on releasing the file, it is imperative that you check and double-check the contents of the 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 does not quite work well with the rest of the system. If you are only going to compile packages for your own system, though, you do not need to worry too much about this quality assurance step, as you are the only person suffering from mistakes, after all.
Checking package sanity
After testing package functionality, check it for errors using namcap:
$ namcap PKGBUILD $ namcap <package file name>.pkg.tar.zstNamcap will:
and much more.
Get into the habit of checking your packages with namcap to avoid having to fix the simplest mistakes after package submission.
Submitting packages to the AUR
Please read AUR submission guidelines for a detailed description of the submission process.
Summary
Warnings
More detailed guidelines
PKGBUILD generators
PKGBUILDs for some packages can be generated automatically.