KnowledgebaseFreeBSD › Managing software on FreeBSD with pkg

Managing software on FreeBSD with pkg

FreeBSD ships pkg, a fast binary package manager, plus the ports tree if you ever need to build from source with custom options. For a VPS, pkg is what you will use day to day.

Everyday commands

pkg update             # refresh the catalog
pkg search nginx       # find a package
pkg install nginx      # install
pkg info               # list what's installed
pkg info -l nginx      # list files a package owns
which -a psql          # what provides a command already on PATH
pkg upgrade            # update everything
pkg delete nginx       # remove
pkg autoremove         # drop orphaned dependencies
pkg clean -a           # reclaim the download cache

quarterly vs latest

By default pkg tracks the quarterly branch — stable, security-patched, and slow to change. If you want newer software versions, switch to the latest branch:

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

Pick one branch per server and stick with it. Quarterly is the safer default for a production box.

Enabling and starting services

FreeBSD services are enabled in /etc/rc.conf and controlled with service. Use sysrc to edit rc.conf safely rather than hand-editing it:

sysrc nginx_enable=YES
service nginx start
service nginx status
service nginx reload

Pinning a package

Need to hold a package at its current version across upgrades (e.g. a database you upgrade deliberately)?

pkg lock postgresql16-server
pkg lock -l              # show locked packages

Ports (optional)

Need a build-time option the binary package doesn't set? Use the ports tree:

pkg install portsnap && portsnap auto     # fetch/extract the tree
cd /usr/ports/www/nginx && make config install clean

Most people never need ports — the binary packages cover the common cases and install in seconds. If you do build a few things from ports, pkg install poudriere lets you run your own build server so those custom packages upgrade as cleanly as binary ones.

Related Articles

← All FreeBSD articles