How to Rebuild All Packages with `makepkg` on Arch Linux

How to Rebuild All Packages with makepkg on Arch Linux

Arch Linux is known for its simplicity, transparency, and do-it-yourself (DIY) approach. One powerful feature that highlights these principles is the ability to rebuild software packages using makepkg, the core utility in Arch’s packaging system. Rebuilding packages from source provides benefits like performance optimizations, customization, and improved understanding of how software is built and packaged.

In this article, we’ll walk through the process of rebuilding all installed packages on your Arch Linux system using makepkg. This guide is useful for those looking to optimize builds with custom compiler flags, enable or disable specific features, or even audit all package builds for educational or security reasons.


1. Why Rebuild All Packages?

Rebuilding all packages on your system is not something most users need to do. However, there are several valid reasons for doing so:

  • Custom Compiler Flags: Optimize binaries for your specific CPU architecture.
  • Security Auditing: Ensure that all software is built in a clean and verifiable way.
  • Customization: Enable or disable features not available in the official binary packages.
  • Hardening: Apply additional security flags or sanitizers.
  • Educational: Learn more about the internals of software packaging and compilation.

Keep in mind that this is a time-consuming and resource-intensive process, especially on lower-end hardware.


2. Prerequisites

Before proceeding, make sure you have the following:

  • A working Arch Linux installation.

  • A fast and stable internet connection.

  • Sufficient disk space (tens of GBs recommended).

  • Development tools:

    sudo pacman -S base-devel git
    
  • The devtools package (optional but recommended for clean chroot builds):

    sudo pacman -S devtools
    

3. Understanding makepkg

makepkg is the tool used to build Arch packages from a PKGBUILD. A PKGBUILD is a shell script that defines how a package is built, including dependencies, source code, and build instructions.

When you run makepkg, it follows a standardized process:

  1. Fetch source files
  2. Verify integrity
  3. Extract and patch sources
  4. Compile the program
  5. Package the compiled binaries into a .pkg.tar.zst file

Understanding how makepkg works is crucial for successfully rebuilding packages and troubleshooting any issues.


4. Getting the List of Installed Packages

To rebuild all installed packages, we need a list of them. This can be done using pacman:

pacman -Qqen > pkglist.txt
  • -Q – Query installed packages
  • -q – Only show package names
  • -e – Only explicitly installed packages
  • -n – Only packages from the official repositories

You can include all packages (including dependencies) by dropping the -e flag:

pacman -Qqn > fullpkglist.txt

If you also want AUR packages, list them separately:

pacman -Qqm > aurpkglist.txt

5. Fetching PKGBUILDs from the Arch Build System

The Arch Build System (ABS) allows you to retrieve PKGBUILDs for official repository packages.

Clone the official Arch PKGBUILD Git repository:

git clone https://github.com/archlinux/svntogit-packages.git
cd svntogit-packages

You can also use the asp tool (from the asp package) to get individual PKGBUILDs:

sudo pacman -S asp
asp update
asp checkout <package-name>

This will download the latest PKGBUILD for a given package.

Alternatively, use the Arch Linux Archive if you need older versions.


6. Modifying Build Flags or Options (Optional)

One of the reasons for rebuilding is to change how packages are built. To do that, edit the PKGBUILD files accordingly.

For example, to optimize for a specific CPU, you can set your own CFLAGS in makepkg.conf:

sudo nano /etc/makepkg.conf

Edit lines like:

CFLAGS="-march=native -O2 -pipe -fstack-protector-strong"
CXXFLAGS="${CFLAGS}"
MAKEFLAGS="-j$(nproc)"

You can also enable or disable optional features in PKGBUILD by editing the build() or configure lines.


7. Automating the Rebuild Process

You can write a simple script to automate the rebuild process. Here is a basic example using a loop:

#!/bin/bash

mkdir -p ~/builds
cd ~/builds

while read pkg; do
    echo "Cloning and building $pkg"
    asp checkout "$pkg" || continue
    cd "$pkg/trunk" || continue
    makepkg -s --noconfirm --clean || echo "$pkg failed" >> ~/failed.txt
    cd ~/builds
done < pkglist.txt

Save it as rebuild-all.sh, make it executable, and run it:

chmod +x rebuild-all.sh
./rebuild-all.sh

You can adjust this script to suit your needs, such as using devtools for clean builds or signing packages.


8. Handling Build Failures and Dependencies

Common Issues

  • Missing Dependencies: Use makepkg -s to automatically install them.
  • Circular Dependencies: Manually resolve by building in a specific order.
  • Outdated PKGBUILD: Use the latest version from the Arch Git repositories.
  • Permission Problems: Never run makepkg as root—use regular users.

Clean Chroot Builds

For consistent and reproducible builds, consider building in a clean chroot using devtools:

mkarchroot ~/chroot/root base-devel
extra-x86_64-build

This ensures each package builds in a clean environment, like the official repos.


9. Installing Rebuilt Packages

Once you’ve built packages successfully, you can install them using pacman:

sudo pacman -U package-name.pkg.tar.zst

To install all rebuilt packages from a directory:

sudo pacman -U *.pkg.tar.zst

To avoid overwriting existing packages unintentionally, consider backing them up or maintaining a local repository.

Creating a Local Repo (Optional)

If you plan to manage your rebuilt packages long-term:

repo-add myrepo.db.tar.gz *.pkg.tar.zst

Then add this repo to your /etc/pacman.conf:

[myrepo]
SigLevel = Optional TrustAll
Server = file:///home/youruser/myrepo

This allows you to manage updates and installations through pacman.


10. Conclusion

Rebuilding all packages on Arch Linux using makepkg is a powerful but advanced process. It can serve performance, security, or educational purposes. However, it’s not a task to be taken lightly—it demands time, storage, and attention to detail.

The process can be broken down into manageable steps: fetching installed packages, retrieving PKGBUILDs, customizing build settings, automating the rebuilds, and managing installation or deployment. Tools like asp, makepkg, and devtools significantly simplify this journey.

If you’re a power user or enthusiast looking to push the limits of what Arch Linux can offer, this exercise is not only beneficial but also deeply rewarding. Just be sure to test everything carefully and keep backups of your system—things can go wrong, and recovery is much easier with a good plan in place.