A PKGBUILD is a Bash script with a defined structure that describes how to download, verify, compile and package a piece of software for Arch Linux and its derivatives. In package management on Linux, it is the recipe that the makepkg tool reads to produce a binary package that the Pacman package manager can install, upgrade and remove.
Every official Arch package and every entry in the Arch User Repository (AUR) starts as a PKGBUILD. The file combines metadata variables, such as name, version and dependencies, with shell functions that run the build steps, so one short text file fully documents how a package is made.
How makepkg turns a PKGBUILD into a package
makepkg sources the PKGBUILD as a Bash script and then executes a fixed sequence of stages. It downloads the files listed in the source array, checks them against declared checksums or signatures, extracts archives, runs the build functions inside a fakeroot environment and finally compresses the result into a package file, typically ending in .pkg.tar.zst.
The package itself contains the installed file tree plus generated metadata files such as .PKGINFO and .MTREE. Pacman reads this metadata to resolve dependencies, track file ownership and detect conflicts, so the accuracy of the PKGBUILD variables directly affects how reliably the package behaves on a system.
The table below lists the stages that makepkg runs and the PKGBUILD element that controls each one.
| Stage | Controlled by | What happens |
|---|---|---|
| Source retrieval | source array | Downloads tarballs, patches or Git repositories into the source directory |
| Integrity check | sha256sums, b2sums, validpgpkeys | Compares checksums and verifies PGP signatures |
| Preparation | prepare() | Applies patches and adjusts the source tree |
| Version update | pkgver() | Computes the version for packages built from version control |
| Compilation | build() | Configures and compiles the software |
| Testing | check() | Runs the upstream test suite when available |
| Packaging | package() | Installs files into $pkgdir with correct paths |
Only the package() function is mandatory for most packages. The fakeroot environment lets the packaging step assign root ownership to files without the build actually running as root, which is why makepkg refuses to run as the root user by default.
Key variables in a PKGBUILD
A PKGBUILD declares package metadata through shell variables and arrays at the top of the file. These values end up in the package database and determine how Pacman treats the package during installation and upgrades.
| Variable | Example value | Purpose |
|---|---|---|
| pkgname | htop | Name of the resulting package |
| pkgver | 3.3.0 | Upstream software version |
| pkgrel | 2 | Packaging revision, raised when only the PKGBUILD changes |
| arch | ('x86_64') or ('any') | Supported architectures |
| depends | ('ncurses' 'libnl') | Runtime dependencies installed with the package |
| makedepends | ('autoconf' 'git') | Dependencies needed only to build |
| optdepends | ('lsof: show open files') | Optional features with explanations |
| license | ('GPL-2.0-or-later') | Software license, preferably as an SPDX identifier |
| provides, conflicts, replaces | ('htop-git') | Relations with alternative packages |
The distinction between pkgver and pkgrel is a frequent source of mistakes. A new upstream release changes pkgver and resets pkgrel to 1, while a fix to the packaging alone, such as a corrected dependency, increments pkgrel so that Pacman offers the rebuilt package as an upgrade. A single PKGBUILD can also produce several packages by listing them in pkgname and defining one package_name() function for each, which is called a split package.
PKGBUILD compared with other packaging formats
The PKGBUILD format is known for being shorter and more readable than most alternatives. Other distributions describe builds with comparable recipes, but they differ in syntax, tooling and how much policy they encode.
| Format | Distribution family | Build tool | Notable trait |
|---|---|---|---|
| PKGBUILD | Arch Linux, Manjaro, EndeavourOS | makepkg | Plain Bash with few conventions |
| RPM spec file | Fedora, RHEL, openSUSE | rpmbuild | Macro-based sections and scriptlets |
| debian directory | Debian, Ubuntu | dpkg-buildpackage | Several files such as control and rules |
| Ebuild | Gentoo (Portage) | emerge | Bash with eclasses, built on the user system |
| APKBUILD | Alpine Linux | abuild | Directly inspired by PKGBUILD |
| Nix expression | NixOS and Nix | nix-build | Functional language with pure builds |
Compared with a Debian source package, a PKGBUILD keeps the entire recipe in one file, which makes review easy and changes quick. The trade-off is that the format enforces fewer rules, so quality depends heavily on the packager and on tools such as namcap, which lints both the PKGBUILD and the resulting package.
Building and reviewing packages safely
The typical workflow is to clone or download a PKGBUILD, read it, and run makepkg in the same directory. The command makepkg -si builds the package, installs missing dependencies with Pacman and installs the result, while makepkg --printsrcinfo > .SRCINFO regenerates the metadata file that the AUR requires.
- Always read the PKGBUILD before building, because it runs arbitrary shell code with the user’s privileges
- Check that the
sourceURLs point to the official upstream project - Prefer strong checksums such as
sha256sumsorb2sums, and avoidSKIPexcept for version control sources - Build in a clean chroot with
extra-x86_64-buildormakechrootpkgto catch missing makedepends - Run
namcapon the PKGBUILD and the package to detect common errors
Helpers such as yay or paru automate fetching and building AUR packages, but they do not replace review. The AUR is user-maintained and unsupported, and malicious PKGBUILDs have appeared there in the past, so inspecting diffs on every update remains good practice.
Common PKGBUILD mistakes
Most PKGBUILD problems come from incorrect paths, incomplete dependencies or wrong version handling. The following table maps typical symptoms to their causes and fixes.
| Symptom | Cause | Fix |
|---|---|---|
| Files installed into the live system | Missing DESTDIR or $pkgdir | Pass DESTDIR="$pkgdir" to make install |
| Build fails in a clean chroot | Undeclared makedepends | Add the missing build tools to makedepends |
| Pacman does not offer the fix | pkgrel not incremented | Raise pkgrel after packaging changes |
| Validity check failure | Upstream tarball changed or checksum outdated | Verify the source, then run updpkgsums |
| Package rejected on other systems | Wrong arch value | Use any only for architecture-independent files |
In simple terms
A PKGBUILD is like a recipe card for software. It lists the ingredients, where to buy them, how to check that they are fresh and the exact steps to cook the dish. A tool called makepkg follows the card and produces a ready meal, an installable package.
Because the whole recipe fits on one readable page, anyone can check it before cooking. That openness is the main strength of the format, and it is also why users should read the recipe before running it.
A practical example
A system administrator manages twenty Arch Linux workstations in a research lab and needs an internal data analysis tool that no repository provides. Instead of copying binaries by hand, the administrator writes a PKGBUILD of about 30 lines: the source array points to the internal Git tag, sha256sums pins the release archive, depends lists Python and two libraries, and package() installs the program into /usr/bin and its documentation into /usr/share/doc.
The package is built once in a clean chroot, which reveals a missing makedepends entry for a compiler toolchain. After the fix, the administrator publishes the result to a small local Pacman repository. Every workstation now installs and upgrades the tool with pacman -Syu, file ownership is tracked, and removal is a single command, which turns a manual deployment into a repeatable process.