PKGBUILD

On this page 7

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.

StageControlled byWhat happens
Source retrievalsource arrayDownloads tarballs, patches or Git repositories into the source directory
Integrity checksha256sums, b2sums, validpgpkeysCompares checksums and verifies PGP signatures
Preparationprepare()Applies patches and adjusts the source tree
Version updatepkgver()Computes the version for packages built from version control
Compilationbuild()Configures and compiles the software
Testingcheck()Runs the upstream test suite when available
Packagingpackage()Installs files into $pkgdir with correct paths
Build stages controlled by a PKGBUILD

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.

VariableExample valuePurpose
pkgnamehtopName of the resulting package
pkgver3.3.0Upstream software version
pkgrel2Packaging 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
Common PKGBUILD variables

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.

FormatDistribution familyBuild toolNotable trait
PKGBUILDArch Linux, Manjaro, EndeavourOSmakepkgPlain Bash with few conventions
RPM spec fileFedora, RHEL, openSUSErpmbuildMacro-based sections and scriptlets
debian directoryDebian, Ubuntudpkg-buildpackageSeveral files such as control and rules
EbuildGentoo (Portage)emergeBash with eclasses, built on the user system
APKBUILDAlpine LinuxabuildDirectly inspired by PKGBUILD
Nix expressionNixOS and Nixnix-buildFunctional language with pure builds
Build recipe formats across Linux distributions

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 source URLs point to the official upstream project
  • Prefer strong checksums such as sha256sums or b2sums, and avoid SKIP except for version control sources
  • Build in a clean chroot with extra-x86_64-build or makechrootpkg to catch missing makedepends
  • Run namcap on 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.

SymptomCauseFix
Files installed into the live systemMissing DESTDIR or $pkgdirPass DESTDIR="$pkgdir" to make install
Build fails in a clean chrootUndeclared makedependsAdd the missing build tools to makedepends
Pacman does not offer the fixpkgrel not incrementedRaise pkgrel after packaging changes
Validity check failureUpstream tarball changed or checksum outdatedVerify the source, then run updpkgsums
Package rejected on other systemsWrong arch valueUse any only for architecture-independent files
Typical PKGBUILD errors and corrections

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.