pkg Package Manager (FreeBSD)

pkg - Binary package manager for FreeBSD.


Basic Commands

 1# Update repository catalog
 2pkg update
 3
 4# Upgrade all packages
 5pkg upgrade
 6
 7# Install package
 8pkg install package-name
 9
10# Install without confirmation
11pkg install -y package-name
12
13# Remove package
14pkg delete package-name
15
16# Remove package and dependencies
17pkg autoremove
18
19# Search for package
20pkg search package-name
21
22# Show package info
23pkg info package-name
24
25# List installed packages
26pkg info
27
28# Check for updates
29pkg version -l "<"

Search and Query

 1# Search by name
 2pkg search package-name
 3
 4# Search by description
 5pkg search -D keyword
 6
 7# Search by origin
 8pkg search -o category/package
 9
10# Show package details
11pkg info -f package-name
12
13# Show dependencies
14pkg info -d package-name
15
16# Show reverse dependencies
17pkg info -r package-name
18
19# List files in package
20pkg info -l package-name
21
22# Find which package owns file
23pkg which /path/to/file

Install from Repository

 1# Install package
 2pkg install nginx
 3
 4# Install specific version
 5pkg install nginx-1.24.0
 6
 7# Install from specific repository
 8pkg install -r FreeBSD package-name
 9
10# Reinstall package
11pkg install -f package-name
12
13# Download only
14pkg fetch package-name
15
16# Install downloaded package
17pkg add package-name.txz

Build from Ports

 1# Update ports tree
 2portsnap fetch extract
 3# Or for updates
 4portsnap fetch update
 5
 6# Or use Git (modern method)
 7git clone https://git.FreeBSD.org/ports.git /usr/ports
 8
 9# Navigate to port
10cd /usr/ports/category/package-name
11
12# Show build options
13make config
14
15# Build and install
16make install clean
17
18# Build without installing
19make
20
21# Install built package
22make install
23
24# Clean build files
25make clean
26
27# Deinstall
28make deinstall

Ports Management

 1# Search ports
 2cd /usr/ports
 3make search name=package-name
 4make search key=keyword
 5
 6# Show port info
 7cd /usr/ports/category/package
 8make describe
 9
10# Show dependencies
11make all-depends-list
12
13# Show build options
14make showconfig
15
16# Update all ports (using portmaster)
17pkg install portmaster
18portmaster -a
19
20# Update specific port
21portmaster category/package-name

Repository Configuration

 1# Repository config
 2cat /etc/pkg/FreeBSD.conf
 3
 4# Custom repository
 5mkdir -p /usr/local/etc/pkg/repos
 6cat > /usr/local/etc/pkg/repos/custom.conf <<EOF
 7custom: {
 8    url: "pkg+http://example.com/\${ABI}/latest",
 9    mirror_type: "srv",
10    enabled: yes
11}
12EOF
13
14# Disable default repo
15cat > /usr/local/etc/pkg/repos/FreeBSD.conf <<EOF
16FreeBSD: { enabled: no }
17EOF
18
19# List repositories
20pkg -vv | grep url

Lock/Unlock Packages

 1# Lock package (prevent upgrade)
 2pkg lock package-name
 3
 4# Unlock package
 5pkg unlock package-name
 6
 7# List locked packages
 8pkg lock -l
 9
10# Lock all packages
11pkg lock -a
12
13# Unlock all packages
14pkg unlock -a

Audit and Security

 1# Update vulnerability database
 2pkg audit -F
 3
 4# Check for vulnerabilities
 5pkg audit
 6
 7# Check specific package
 8pkg audit package-name
 9
10# Show vulnerable packages
11pkg audit -r

Clean and Maintenance

 1# Clean package cache
 2pkg clean
 3
 4# Clean all cached packages
 5pkg clean -a
 6
 7# Remove orphaned packages
 8pkg autoremove
 9
10# Check for consistency
11pkg check -d
12
13# Recompute checksums
14pkg check -s
15
16# Check and repair
17pkg check -B
18
19# Rebuild package database
20pkg check -r

Backup and Restore

 1# Backup installed packages list
 2pkg info -q > packages.txt
 3
 4# Restore packages
 5cat packages.txt | xargs pkg install
 6
 7# Create package repository from installed
 8pkg create -a -o /path/to/repo
 9
10# Backup package database
11tar -czf pkg-db-backup.tar.gz /var/db/pkg/

Statistics

 1# Show statistics
 2pkg stats
 3
 4# Show repository statistics
 5pkg stats -r
 6
 7# Show local statistics
 8pkg stats -l
 9
10# Disk usage
11pkg info -s
12
13# Largest packages
14pkg info -s | sort -k2 -n -r | head -10

Configuration

1# Main config file
2cat /usr/local/etc/pkg.conf
3
4# Example settings:
5PKG_DBDIR: "/var/db/pkg"
6PKG_CACHEDIR: "/var/cache/pkg"
7ASSUME_ALWAYS_YES: false
8REPOS_DIR: ["/etc/pkg", "/usr/local/etc/pkg/repos"]

Troubleshooting

 1# Fix broken dependencies
 2pkg check -d
 3pkg install -f
 4
 5# Rebuild database
 6pkg check -r
 7
 8# Force reinstall
 9pkg install -f package-name
10
11# Verbose output
12pkg -v install package-name
13
14# Debug mode
15pkg -d install package-name
16
17# Check integrity
18pkg check -s -a

Build from Source (Ports)

Using Poudriere (Build Farm)

 1# Install poudriere
 2pkg install poudriere
 3
 4# Create jail
 5poudriere jail -c -j 13amd64 -v 13.2-RELEASE
 6
 7# Create ports tree
 8poudriere ports -c
 9
10# Build packages
11poudriere bulk -j 13amd64 category/package-name
12
13# Build from list
14poudriere bulk -j 13amd64 -f package-list.txt
15
16# Serve built packages
17# Configure nginx to serve /usr/local/poudriere/data/packages/

Useful Aliases

1# Add to ~/.shrc or ~/.bashrc
2alias update='pkg update && pkg upgrade'
3alias install='pkg install'
4alias remove='pkg delete'
5alias search='pkg search'
6alias clean='pkg autoremove && pkg clean -a'

Common Packages

 1# Development tools
 2pkg install git vim tmux
 3
 4# Web servers
 5pkg install nginx apache24
 6
 7# Databases
 8pkg install postgresql15-server mysql80-server
 9
10# Languages
11pkg install python39 go rust node
12
13# System tools
14pkg install htop bash zsh

Create and Publish FreeBSD Port

Port Structure

1mypackage/
2β”œβ”€β”€ Makefile
3β”œβ”€β”€ distinfo
4β”œβ”€β”€ pkg-descr
5β”œβ”€β”€ pkg-plist
6└── files/
7    └── patch-configure

Makefile

 1# Makefile
 2PORTNAME=       mypackage
 3PORTVERSION=    1.0.0
 4CATEGORIES=     sysutils
 5MASTER_SITES=   https://github.com/username/mypackage/releases/download/v${PORTVERSION}/
 6
 7MAINTAINER=     email@example.com
 8COMMENT=        Short description
 9
10LICENSE=        MIT
11LICENSE_FILE=   ${WRKSRC}/LICENSE
12
13USES=           gmake
14USE_GITHUB=     yes
15GH_ACCOUNT=     username
16GH_PROJECT=     mypackage
17GH_TAGNAME=     v${PORTVERSION}
18
19PLIST_FILES=    bin/mypackage \
20                man/man1/mypackage.1.gz
21
22.include <bsd.port.mk>

Generate distinfo

1# Generate checksums
2make makesum
3
4# Creates distinfo:
5TIMESTAMP = 1702387200
6SHA256 (username-mypackage-v1.0.0_GH0.tar.gz) = abc123...
7SIZE (username-mypackage-v1.0.0_GH0.tar.gz) = 123456

pkg-descr

1This is a longer description of the package.
2It can span multiple lines and should describe
3what the package does in detail.
4
5WWW: https://github.com/username/mypackage

pkg-plist

1bin/mypackage
2man/man1/mypackage.1.gz
3%%DATADIR%%/config.sample
4@sample etc/mypackage/config.conf.sample

Test Port

 1# Test build
 2make
 3
 4# Test install
 5sudo make install
 6
 7# Test deinstall
 8sudo make deinstall
 9
10# Clean
11make clean
12
13# Check with portlint
14portlint -AC
15
16# Test in clean environment
17poudriere testport -j 13amd64 sysutils/mypackage

Submit to FreeBSD Ports

 1# 1. Fork FreeBSD ports repository
 2# https://github.com/freebsd/freebsd-ports
 3
 4# 2. Create port directory
 5mkdir -p sysutils/mypackage
 6cd sysutils/mypackage
 7
 8# 3. Add files
 9# Create Makefile, pkg-descr, pkg-plist, distinfo
10
11# 4. Test thoroughly
12portlint -AC
13poudriere testport -j 13amd64 sysutils/mypackage
14
15# 5. Create patch
16cd ../..
17git add sysutils/mypackage
18git commit -m "sysutils/mypackage: New port"
19git format-patch HEAD^
20
21# 6. Submit to Bugzilla
22# https://bugs.freebsd.org/bugzilla/
23# Category: Ports & Packages
24# Attach patch file

Create Custom Package Repository

 1# Build packages with Poudriere
 2sudo pkg install poudriere
 3
 4# Create jail
 5sudo poudriere jail -c -j 13amd64 -v 13.2-RELEASE
 6
 7# Create ports tree
 8sudo poudriere ports -c
 9
10# Build packages
11sudo poudriere bulk -j 13amd64 sysutils/mypackage
12
13# Packages are in:
14# /usr/local/poudriere/data/packages/13amd64-default/
15
16# Serve with nginx
17# Configure nginx to serve /usr/local/poudriere/data/packages/
18
19# Users configure:
20# /usr/local/etc/pkg/repos/myrepo.conf
21myrepo: {
22    url: "https://pkg.example.com/13amd64-default",
23    mirror_type: "http",
24    enabled: yes
25}
26
27# Then: sudo pkg install mypackage

Best Practices

 1# 1. Follow Porter's Handbook
 2# https://docs.freebsd.org/en/books/porters-handbook/
 3
 4# 2. Use proper USES
 5USES= gmake pkgconfig
 6
 7# 3. Handle options
 8OPTIONS_DEFINE= DOCS EXAMPLES
 9OPTIONS_DEFAULT= DOCS
10
11DOCS_BUILD_DEPENDS= sphinx-build:textproc/py-sphinx
12
13# 4. Install documentation
14post-install-DOCS-on:
15    ${MKDIR} ${STAGEDIR}${DOCSDIR}
16    ${INSTALL_DATA} ${WRKSRC}/README.md ${STAGEDIR}${DOCSDIR}
17
18# 5. Test with portlint
19portlint -AC
20
21# 6. Test with Poudriere
22poudriere testport -j 13amd64 category/port

Related Snippets