HxHippy

pkg Package Manager Basics

Using the pkg package manager to install, update, and manage software.

Last updated: 2025-01-15

Introduction to pkg

pkg is FreeBSD's official package manager for installing pre-built binary packages. It's the fastest way to install software on FreeBSD.

Initial Setup

# Bootstrap pkg (first time only)
pkg bootstrap

# Or let it auto-bootstrap on first use
pkg update

Repository Configuration

# Repository config location
/etc/pkg/FreeBSD.conf  # Default repo
/usr/local/etc/pkg/repos/  # Custom repos

# View current repository
pkg -vv | grep -A5 "Repositories"

# Use quarterly (stable) vs latest packages
# Edit /etc/pkg/FreeBSD.conf:
FreeBSD: {
  url: "pkg+http://pkg.FreeBSD.org/${ABI}/quarterly",
  # or "latest" for newest versions
}

Searching for Packages

# Search by name
pkg search nginx

# Search with description
pkg search -D "web server"

# Search exact name
pkg search -e nginx

# Show package origin
pkg search -o nginx
# Output: www/nginx

Installing Packages

# Install single package
pkg install nginx

# Install multiple packages
pkg install nginx mysql80-server php83

# Install without confirmation
pkg install -y nginx

# Install specific version (if available)
pkg install nginx-1.24

Updating Packages

# Update package database
pkg update

# Upgrade all packages
pkg upgrade

# Upgrade specific package
pkg upgrade nginx

# Force upgrade (reinstall)
pkg upgrade -f nginx

Removing Packages

# Remove package
pkg delete nginx

# Remove with dependencies (unused)
pkg delete -y nginx
pkg autoremove

# Remove all packages (careful!)
pkg delete -a

# Force removal
pkg delete -f nginx

Package Information

# List installed packages
pkg info

# Detailed package info
pkg info nginx

# List package files
pkg info -l nginx

# Package dependencies
pkg info -d nginx

# Reverse dependencies (what depends on this)
pkg info -r nginx

# Package size
pkg info -s nginx

Package Queries

# Which package owns a file
pkg which /usr/local/bin/nginx

# List packages by size
pkg info -sa | sort -k2 -h

# List manually installed packages
pkg query -e '%a = 0' '%n-%v'

# Check for vulnerabilities
pkg audit -F

Maintenance Commands

# Clean package cache
pkg clean

# Remove orphaned dependencies
pkg autoremove

# Check package integrity
pkg check -s -a

# Lock package (prevent upgrades)
pkg lock nginx
pkg unlock nginx

# List locked packages
pkg lock -l

Offline Package Management

# Download package without installing
pkg fetch nginx

# Create package from installed software
pkg create nginx

# Install from local file
pkg add /path/to/nginx-1.24.txz

Troubleshooting

# Force repository update
pkg update -f

# Reinstall package
pkg install -f nginx

# Fix broken packages
pkg check -B -a

# View verbose output
pkg -d install nginx
beginner Package Management Updated 2025-01-15
  • freebsd
  • pkg
  • packages
  • software
  • installation