Mastering Package Management with pkg on FreeBSD 12

FreeBSD is an open-source Unix-like operating system renowned for its stability and advanced networking, security, and storage capabilities. Developed by a large community for over three decades, FreeBSD powers numerous modern servers, desktops, and embedded platforms. It’s well-regarded in the tech world and is used by giants like Netflix, Yahoo!, WhatsApp, BBC, and Sony. While it hasn’t gained as much desktop presence as Linux, FreeBSD remains a trusted platform, primarily for server applications.

This guide will provide you with insights into binary package management using pkg on FreeBSD 12, the latest version at the time of writing. Most of the information here is also applicable to FreeBSD 11.

Prerequisites

  • FreeBSD 12 operating system
  • Access to root or a sudo-enabled account

Ports and Packages

FreeBSD allows you to install additional software via ports or pre-configured packages. Ports enable you to build software from source, configuring it to meet your specific needs. In contrast, packages are pre-compiled, ready-to-install software that provides convenience and speed. Most users prefer using packages, managed through FreeBSD’s package system called pkg, which stores information in a queryable SQLite database.

In FreeBSD, installed package binaries are located in /usr/local, with most configuration files found in /usr/local/etc, different from the typical /etc directory used in Linux distributions.

The FreeBSD Package Manager – pkg

pkg is a user-friendly, modern replacement for older FreeBSD package management tools. It simplifies installing, removing, and managing packages and requires root or sudo privileges to execute package operations. Here’s how to install the acme.sh package on FreeBSD:

pkg install acme.sh

To remove the package, execute:

pkg delete acme.sh

Use pkg help followed by a subcommand to get a quick reference or detailed manual.

pkg help install
pkg help delete

Installing pkg

FreeBSD doesn’t come with pkg pre-installed. Attempting to install any package, such as wget, will prompt you to install pkg with a simple confirmation. You can also run pkg bootstrap to set up the packaging system independently, usually in system setup scripts.

How to Search for Packages

With pkg installed, you can now search for and install packages. FreeBSD’s continuously growing package repository contains over 25,000 packages. For instance, to search for the Apache web server, use:

pkg search apache
# apache24-2.4.38 Version 2.4.x of Apache web server

You can use various command-line options to refine your searches. See the pkg search man page for more details.

How to Install New Packages with pkg

Use pkg install followed by the package name to install software. pkg will resolve dependencies and store package information in an SQLite database. To manage downloaded packages and dependencies without immediate installation, use pkg fetch and clean up with pkg clean.

How to Configure pkg

Customize pkg behavior via the pkg.conf file, found at /usr/local/etc/pkg.conf. Use this universal configuration language file to adjust default settings and define command aliases. Refer to pkg.conf(5) man page for more details.

How to View Information About Installed Packages

To see installed packages, use pkg info. For specific package details, provide the package name as an argument. Use pkg info -l to see all files associated with a package.

How to Remove Packages

Uninstall packages using pkg delete or its alias pkg remove. pkg will handle dependencies accordingly and provide a summary of changes.

How to Lock Packages

Lock packages to prevent updates or removal using pkg lock. Unlock or list locked packages with pkg unlock and pkg lock -l, respectively.

Package Repositories

FreeBSD utilizes package repositories, which can be managed through individual configuration files in designated directories like /etc/pkg for official repositories and /usr/local/etc/pkg/repos for custom ones.

Example pkg Commands

# Installs a package without asking any questions
pkg install -y package

# Makes a backup of the local package database
pkg backup

# Lists all installed packages
pkg info

# Shows extended information for a package
pkg info package

# Searches package repository
pkg search -i package

# Shows packages with known security vulnerabilities
pkg audit -F

# Shows which package owns the named file
pkg which file

# Removes unused packages
pkg autoremove

# Uninstalls a package
pkg delete package

# Removes cached packages from /var/cache/pkg
 pkg clean -ay

# Updates local copy of the package catalog
pkg update

# Upgrades installed packages to their latest version
pkg upgrade

# Checks the integrity of all your packages
pkg check -saq

# Verifies that a package's files are unaltered
pkg check -s nginx

# Shows what files came with the package
pkg info -l nginx

# Lists non-automatic packages
pkg prime-list

Conclusion

FreeBSD provides both ports and packages for third-party software installation. While ports enable customization through source code, managing software through pkg is increasingly favored for simplicity and efficiency. Reserve the use of ports for unique situations where no suitable package version exists or specific customization is necessary.

Links

Frequently Asked Questions (FAQ)

What is FreeBSD used for?

FreeBSD is used for a variety of applications, including powering modern servers, desktops, and embedded platforms, known for its robust networking, security, and storage features.

What is the difference between ports and packages in FreeBSD?

Ports allow users to compile software from source, offering customization of features, while packages are pre-compiled binaries that provide ease of installation and management.

Can I use FreeBSD packages on Linux?

No, FreeBSD packages are specifically built for FreeBSD and not compatible with Linux systems, which have their own package management systems.

How can I clean the package cache?

Use the pkg clean command to remove outdated cached packages, and if necessary, use pkg clean -a to remove all cached packages.

How do I update installed packages on FreeBSD?

To update installed packages, run pkg update to refresh the local package catalog and pkg upgrade to upgrade the packages to their latest versions.