# makepkg

Related articles

- Creating packages
- PKGBUILD
- Arch User Repository
- pacman
- Official repositories
- Arch Build System

makepkg is a script to automate the building of packages. The requirements for using the script are a build-capable Unix platform and a PKGBUILD.

makepkg is provided by the pacman package.

## Configuration

See `man makepkg.conf` for details on configuration options for makepkg.

The system configuration is available in `/etc/makepkg.conf`, but user-specific changes can be made in `$XDG_CONFIG_HOME/pacman/makepkg.conf` or `~/.makepkg.conf`. It is recommended to review the configuration prior to building packages.

### Package output

Next, one can configure where source files and packages should be placed and identify themselves as the packager. This step is optional; packages will be created in the working directory where makepkg is run by default. For example, to store packages by user:

    $ mkdir $HOME/makepkg/packages

Then modify the `PKGDEST` variable in `makepkg.conf` accordingly.

The `PACKAGER` variable will set the `packager` value within compiled packages' `.PKGINFO` metadata file. By default, user-compiled packages will display:

    $ pacman -Qi package

    [...]
    Packager       : Unknown Packager
    [...]

Afterwards:

    $ pacman -Qi package

    [...]
    Packager       : John Doe <john@doe.com>
    [...]

This is useful if multiple users will be compiling packages on a system, or you are otherwise distributing your packages to other users.

### Signature checking

This article or section needs expansion.

Reason: Expand a bit on `validpgpkeys()`, e.g: "I then check the person who signed is expected from the software mailing list, check if they sign emails, look if the PGP fingerprint is published on their homepage, … That verifies the signature enough to add the validpgpkeys array for me." (Discuss in Talk:Makepkg#)

If a signature file in the form of `.sig` is part of the PKGBUILD source array, makepkg validates the authenticity of source files. For example, the signature `pkgname-pkgver.tar.gz.sig` is used to check the integrity of the file `pkgname-pkgver.tar.gz` with the gpg program.

If desired, signatures by other developers can be manually added to the GPG keyring. See GnuPG article for details. To temporarily disable signature checking, call the makepkg command with the `--skippgpcheck` option.

Note: The signature checking implemented in makepkg does not use pacman's keyring, relying on the user's keyring and the `validpgpkeys()` array instead. [1]

To show the current list of GPG keys, use the gpg command:

    $ gpg --list-keys

If the `pubring.gpg` file does not exist, it will be created for you immediately.

The GPG keys are expected to be stored in the user's `~/.gnupg/pubring.gpg` file. In case it does not contain the given signature, makepkg will abort the installation:

    $ makepkg

    [...]
    ==> Verifying source file signatures with gpg...
    pkgname-pkgver.tar.gz ... FAILED (unknown public key 1234567890)
    ==> ERROR: One or more PGP signatures could not be verified!

Make sure that a keyserver is configured in `gpg.conf`, for example:

    ~/.gnupg/gpg.conf

    keyserver hkp://keys.gnupg.net

To import the key from the server, use:

    $ gpg --recv-keys 1234567890

Or to automate:

    ~/.gnupg/gpg.conf

    keyserver-options auto-key-retrieve

## Usage

Warning: Only build or install packages from trusted sources.

Before continuing, install the base-devel group. Packages belonging to this group are not required to be listed as build-time dependencies (makedepends) in PKGBUILD files. In addition, the base group is assumed to be installed on all Arch systems.

Note:

- Make sure sudo is configured properly for commands passed to pacman.
- Running makepkg itself as root is disallowed as of v4.2.[2] Besides how a `PKGBUILD` may contain arbitrary commands, building as root is generally considered unsafe.[3] Users who have no access to a regular user account should run makepkg as the nobody user.

To build a package, one must first create a PKGBUILD, or build script, as described in Creating packages. Existing scripts are available from the ABS tree or the AUR. Once in possession of a `PKGBUILD`, change to the directory where it is saved and issue the following command to build the package described by said `PKGBUILD`:

    $ makepkg

If required dependencies are missing, makepkg will issue a warning before failing. To build the package and install needed dependencies, add the flag `-s`/`--syncdeps`:

    $ makepkg -s

Adding the `-r`/`--rmdeps` flag causes makepkg to remove the make dependencies later, which are no longer needed. If constantly building packages, consider using Pacman tips#Removing orphaned packages once in a while instead.

Note:

- These dependencies must be available in the configured repositories; see pacman#Repositories for details. Alternatively, one can manually install dependencies prior to building (`pacman -S --asdeps dep1 dep2`).
- Only global values are used when installing dependencies, i.e any override done in a split package's packaging function will not be used. [4]

Once all dependencies are satisfied and the package builds successfully, a package file (`pkgname-pkgver.pkg.tar.xz`) will be created in the working directory. To install, use `-i`/`--install` (same as `pacman -U pkgname-pkgver.pkg.tar.xz`):

    $ makepkg -i

To clean up leftover files and folders, such as files extracted to the `$srcdir`, add the option `-c`/`--clean`. This is useful for multiple builds of the same package or updating the package version, while using the same build folder. It prevents obsolete and remnant files from carrying over to the new builds:

    $ makepkg -c

Arch Linux packages can be signed automatically by makepkg:

    $ makepkg --sign --key <key-id>

For more, see makepkg(8).

## Tips and Tricks

### Creating optimized packages

A performance improvement of the packaged software can be achieved by enabling compiler optimizations for the host machine. The downside is that packages compiled for a specific processor architecture will not run correctly on other machines. On x86_64 machines, there are rarely significant enough real world performance gains that would warrant investing the time to rebuild official packages.

The options passed to a C/C++ compiler (e.g. gcc or clang) are controlled by the `CFLAGS`, `CXXFLAGS` and `CPPFLAGS` environment variables. Similarly, the make build system uses `MAKEFLAGS`. For use in the Arch build system, makepkg exposes these environment variables as configuration options in `makepkg.conf`. The default values are configured to produce generic packages that can be installed on a wide range of machines.

Note: Keep in mind that not all build systems use the variables configured in `makepkg.conf`. For example, cmake disregards the preprocessor options environment variable, `CPPFLAGS`. Consequently, many PKGBUILDs contain workarounds with options specific to the build system used by the packaged software.

As of version 4.3.0, GCC offers the `-march=native` switch that enables CPU auto-detection and automatically selects optimizations supported by the local machine at GCC runtime. To use it, modify the default settings by changing the `CFLAGS` and `CXXFLAGS` lines as follows:

    # -march=native also sets the correct -mtune=
    CFLAGS="-march=native -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"
    CXXFLAGS="${CFLAGS}"

To see what `march=native` flags are, use gcccpuopt or run:

    $ gcc -march=native -E -v - </dev/null 2>&1 | sed -n 's/.* -v - //p'

Further optimizing for CPU type may theoretically enhance performance as `-march=native` enables all available instruction sets and improves scheduling for a particular CPU. This could be noticeable with applications (for example: audio/video encoding tools, scientific applications, math-heavy programs, etc.) that take advantage of newer instructions sets not enabled with the default options (or packages) provided by Arch Linux.

It is however very easy to reduce performance by using "non-standard" CFLAGS, because depending the options, compilers tend to heavily blow up the code size with loop unrolling, bad vectorization, crazy inlining, etc. Unless you can verify/benchmark that something is faster, there is a very good chance it is not!

See the `man gcc` for a complete list of available options. The Gentoo Compilation Optimization Guide and Safe CFLAGS wiki article provide more in-depth information.

#### MAKEFLAGS

The `MAKEFLAGS` option can be used to specify additional options for make. Users with multi-core/multi-processor systems can specify the number of jobs to run simultaneously. This can be accomplished with the use of nproc to determine the number of available processors, e.g. `-j4` (where "4" is the output of nproc). Some PKGBUILDs specifically override this with `-j1`, because of race conditions in certain versions or simply because it is not supported in the first place. Packages that fail to build because of this should be reported on the bug tracker (or in the case of AUR packages, to the package maintainer) after making sure that the error is indeed being caused by your `MAKEFLAGS`.

See `man make` for a complete list of available options.

### Improving compile times

#### tmpfs

As compiling requires many I/O operations and handling of small files, moving the working directory to a tmpfs may bring improvements in build times.

The `BUILDDIR` variable can be temporarily exported to makepkg to set the build directory to an existing tmpfs. For example:

    $ BUILDDIR=/tmp/makepkg makepkg

Warning: Avoid compiling larger packages in tmpfs to prevent running out of memory.

Persistent configuration can be done in `makepkg.conf` by uncommenting the `BUILDDIR` option, which is found at the end of the `BUILD ENVIRONMENT` section in the default `/etc/makepkg.conf` file. Setting its value to e.g. `BUILDDIR=/tmp/makepkg` will make use of the Arch's default `/tmp` tmpfs.

Note:

- The tmpfs folder must be mounted without the `noexec` option, else it will prevent build scripts or utilities from being executed.
- Keep in mind that any package compiled in tmpfs will not persist across reboot. Consider setting the PKGDEST option appropriately to move the built package automatically to another (persistent) directory.

#### ccache

The use of ccache can improve build times by caching the results of compilations.

### Generate new checksums

Since pacman 4.1, `makepkg -g >> PKGBUILD` is no longer required because pacman-contrib was merged into upstream pacman, including the `updpkgsums` script that will generate new checksums and/or replace them in the PKGBUILD. In the same directory as the PKGBUILD file, run the following command:

    $ updpkgsums

### Create uncompressed packages

If you only want to install packages locally, you can speed up the process by avoiding the LZMA2 compression and subsequent decompression:

    /etc/makepkg.conf

    [...]
    #PKGEXT='.pkg.tar.xz'
    PKGEXT='.pkg.tar'
    [...]

### Utilizing multiple cores on compression

xz supports symmetric multiprocessing (SMP) on compression. This can be done by using the `-T 0`/`--threads=0` flag, which makes xz use as many threads as there are cores on the system:

    /etc/makepkg.conf

    [...]
    COMPRESSXZ=(xz -T 0 -c -z -)
    [...]

## Troubleshooting

### Makepkg sometimes fails to sign a package without asking for signature passphrase

This article or section needs language, wiki syntax or style improvements.

Reason: Vague instructions (Discuss in Talk:Makepkg#)

With gnupg 2.1, gpg-agent no longer has to be started manually and will be started automatically on the first invokation of gpg. Thus if you do not manually start gpg-agent, makepkg will start it.

The problem is that makepkg runs gpg inside a fakeroot, so gpg-agent is also started in that same environment. This leads to bad behavior. The obvious remedy is to manually start the gpg-agent, either on boot or by command, before you run makepkg.

See GnuPG#gpg-agent for ways to do this.

### CFLAGS/CXXFLAGS/CPPFLAGS in makepkg.conf do not work for QMAKE based packages

Qmake automatically sets the variable `CFLAGS` and `CXXFLAGS` according to what it thinks should be the right configuration. In order to let qmake use the variables defined in the makepkg configuration file, you must edit the PKGBUILD and pass the variables QMAKE_CFLAGS_RELEASE and QMAKE_CXXFLAGS_RELEASE to qmake. For example:

    PKGBUILD

    ...

    build() {
      cd "$srcdir/$_pkgname-$pkgver-src"
      qmake-qt4 "$srcdir/$_pkgname-$pkgver-src/$_pkgname.pro" \
        PREFIX=/usr \
        CONFIG+=LINUX_INTEGRATED \
        INSTALL_ROOT_PATH="$pkgdir"\
        QMAKE_CFLAGS_RELEASE="${CFLAGS}"\
        QMAKE_CXXFLAGS_RELEASE="${CXXFLAGS}"

      make
    }

    ...

Alternatively, for a system wide configuration, you can create your own `qmake.conf` and set the QMAKESPEC environment variable.

### WARNING: Package contains reference to $srcdir

Somehow, the literal strings `$srcdir` or `$pkgdir` ended up in one of the installed files in your package.

To identify which files, run the following from the makepkg build directory:

    $ grep -R "$(pwd)/src" pkg/

Link to discussion thread.

## See also

- makepkg(8) Manual Page
- makepkg.conf(5) Manual Page
- A Brief Tour of the Makepkg Process

Categories:

- Package development
- About Arch