Operating System BSD

How to Use the pkg Package Manager on FreeBSD 15

A practical guide to pkg on FreeBSD 15: repositories, installing and upgrading packages, security audits, clean-up, locks and base-system updates.

pkg is FreeBSD's binary package manager: one command with subcommands for searching, installing, upgrading, auditing and removing third-party software. This guide covers day-to-day use on FreeBSD 15 (15.1-RELEASE at the time of writing) and notes where FreeBSD 14 differs. FreeBSD 12 reached end of life at the end of 2023; the commands are the same on current releases, but the repository layout has changed.

Every command that changes the system must run as root. FreeBSD does not install sudo by default, so the examples assume a root shell (su -). Read-only commands such as pkg info and pkg search work as a normal user.

Packages, ports and the base system

FreeBSD separates the base system (kernel and userland) from add-on software. Add-on software comes from the Ports Collection in two forms:

  • Packages are pre-built binaries, compiled by the FreeBSD project from ports with default options. pkg installs them in seconds.
  • Ports are build recipes under /usr/ports that compile software from source, which lets you change build options at the cost of time.

Packages install under /usr/local: programs in /usr/local/bin and /usr/local/sbin, configuration in /usr/local/etc, service scripts in /usr/local/etc/rc.d. /etc is reserved for the base system. Package metadata is kept in an SQLite database under /var/db/pkg.

Bootstrap pkg

A fresh installation contains only a small bootstrap program at /usr/sbin/pkg. The first time you run any pkg command it offers to download and install the real package manager:

pkg bootstrap

Answer y. Many provider images already have this done; pkg -N tells you whether pkg is installed. Show the version, the full configuration and the active repositories with:

pkg -vv

Repositories: quarterly and latest

The official repositories are defined in /etc/pkg/FreeBSD.conf. On FreeBSD 15 that file contains three:

RepositoryContentsDefault
FreeBSD-portsPackages built from the Ports Collectionenabled
FreeBSD-ports-kmodsKernel-module packages built for your exact minor releaseenabled
FreeBSD-baseThe base system as packages (pkgbase)disabled

FreeBSD 14 uses different repository names in the same file; pkg -vv prints the names on your system.

Release installations follow the quarterly branch, which is cut from the ports tree in January, April, July and October and then receives mostly security and bug fixes. The latest branch follows the ports tree continuously. Do not edit /etc/pkg/FreeBSD.conf, because system updates overwrite it. Put overrides in /usr/local/etc/pkg/repos/FreeBSD.conf instead. To switch the ports repository to latest:

mkdir -p /usr/local/etc/pkg/repos
echo 'FreeBSD-ports: { url: "pkg+https://pkg.FreeBSD.org/${ABI}/latest" }' > /usr/local/etc/pkg/repos/FreeBSD.conf
pkg update -f

Then run pkg upgrade so that everything installed comes from the same branch. Mixing branches leads to dependency conflicts.

Search for and install packages

Refresh the catalogue and search by name:

pkg update
pkg search nginx

Add -o to list the port origin (such as www/nginx) instead of the version, or -D to search descriptions too. Install a package and its dependencies:

pkg install curl

pkg lists what it will download and asks for confirmation; -y skips the question in scripts. Read the messages a package prints after installing. They usually tell you which sysrc line enables the service, for example:

pkg install nginx
sysrc nginx_enable=YES
service nginx start

To download a package with its dependencies without installing it, use pkg fetch -d -o /var/tmp/packages nginx.

Inspect what is installed

pkg info

lists every installed package. Useful variations:

CommandShows
pkg info curlVersion, origin, options and description of one package
pkg info -l curlFiles the package installed
pkg info -d curlPackages it depends on
pkg info -r curlPackages that depend on it
pkg which /usr/local/bin/curlThe package that owns a file
pkg prime-listPackages you installed explicitly, without their dependencies
pkg statsNumber of packages and disk space used

Upgrade packages and check for vulnerabilities

Upgrade everything to the versions currently in the repository:

pkg upgrade

Review the list before confirming, and restart upgraded services afterwards (service nginx restart). Notes about upgrades that need manual steps are published in the ports tree's UPDATING file; pkg updating shows the entries that apply to your installed packages when a ports tree is present.

Compare installed packages with the FreeBSD vulnerability database:

pkg audit -F

-F downloads the current database first. Each finding names the package, the issue and a link to the advisory; the fix is normally pkg upgrade once a patched package is available.

Remove packages and clean up

pkg delete curl

removes one package. Dependencies that were pulled in automatically and are no longer needed stay behind until you run:

pkg autoremove

If autoremove wants to delete something you use directly, mark it as manually installed with pkg set -A 0 devel/cmake (-A 1 does the opposite).

Downloaded package files accumulate in /var/cache/pkg. Remove outdated ones, or all of them:

pkg clean
pkg clean -a

Lock, verify and repair

Hold a package at its current version so that pkg upgrade and pkg delete leave it alone:

pkg lock nginx
pkg unlock nginx

pkg lock -l lists the locks. Remove a lock as soon as it has served its purpose; a locked package can block upgrades of everything that depends on it.

Check installed files against the recorded checksums, and look for missing dependencies:

pkg check -s -a
pkg check -d -a

Reinstall a damaged package from the repository with pkg install -f nginx.

Updating the base system

On a standard installation, pkg manages only third-party software. The kernel and userland are patched separately:

freebsd-update fetch
freebsd-update install

FreeBSD 15.0 introduced installation of the base system itself from packages (pkgbase) as a technology preview. On such a system, the FreeBSD-base repository is enabled and the base is updated with pkg upgrade -r FreeBSD-base instead of freebsd-update. If you did not choose packaged base at install time, leave FreeBSD-base disabled. The FreeBSD Handbook describes both methods in its chapters on installing applications and updating FreeBSD.

Frequently asked questions

How do I get help for a subcommand?

pkg help lists all subcommands and pkg help install opens the manual page for one of them. The same pages are available as man pkg-install, man pkg-info and so on, and man pkg.conf documents the configuration file /usr/local/etc/pkg.conf.

Can I mix packages and ports?

Yes, but only when both come from the same branch: packages from latest match the main ports tree, and quarterly packages match the corresponding quarterly ports branch. Lock or otherwise track anything you built with custom options, because pkg upgrade will replace it with the default-options package.

A package was renamed. How do I move to the new one?

When a port changes origin, such as a move to a new default Python or Ruby version, UPDATING gives the exact commands. They follow the pattern pkg set -o lang/python3:lang/python311 followed by an upgrade; copy the origins from the UPDATING entry rather than guessing them.

Is there a backup of the package database?

The daily periodic jobs keep a compressed dump in /var/backups/pkg.sql.xz. The pkg manual page shows how to load it back through pkg shell if the database under /var/db/pkg is ever damaged.

Share story:

Continue reading other guides on How VPS.