Skip to main content
The pm module provides a unified interface for package management across different Linux distributions and operating systems. It automatically detects the system’s package manager and translates commands accordingly.
Requires runtime::pm() from the runtime module to detect the package manager. Most operations require sudo privileges.

Supported package managers

The module supports the following package managers:

apt

Debian, Ubuntu, Mint

pacman

Arch Linux, Manjaro

dnf

Fedora, RHEL 8+

yum

CentOS, RHEL 7

zypper

openSUSE, SUSE

apk

Alpine Linux

brew

macOS, Linux

pkg

FreeBSD

xbps

Void Linux

nix

NixOS, any with Nix

Functions

Install one or more packages.
# Install single package
pm::install curl

# Install multiple packages
pm::install git vim tmux htop
Automatically translates to the appropriate command:
  • apt: sudo apt-get install -y
  • pacman: sudo pacman -S --noconfirm
  • dnf: sudo dnf install -y
  • brew: brew install
  • etc.
Parameters:
  • packages... - One or more package names
Returns: Exit code from package manager
Synchronize package database (update package lists).
pm::sync
Translates to:
  • apt: sudo apt-get update
  • pacman: sudo pacman -Sy
  • dnf: sudo dnf check-update
  • brew: brew update
  • etc.
Returns: Exit code from package manager
Run this before pm::install() to ensure you’re installing the latest versions.
Upgrade all installed packages.
pm::update
Translates to:
  • apt: sudo apt-get upgrade -y
  • pacman: sudo pacman -Su --noconfirm
  • dnf: sudo dnf upgrade -y
  • brew: brew upgrade
  • etc.
Returns: Exit code from package manager
This upgrades ALL packages. Use with caution on production systems.
Uninstall one or more packages.
# Remove single package
pm::uninstall apache2

# Remove multiple packages
pm::uninstall apache2 nginx
Translates to:
  • apt: sudo apt-get remove -y
  • pacman: sudo pacman -R --noconfirm
  • dnf: sudo dnf remove -y
  • brew: brew uninstall
  • etc.
Parameters:
  • packages... - One or more package names
Returns: Exit code from package manager
Search for packages matching a query.
pm::search python
pm::search "^python3-"
Translates to:
  • apt: apt-cache search
  • pacman: pacman -Ss
  • dnf: dnf search
  • brew: brew search
  • etc.
Parameters:
  • query - Search term or regex
Returns: List of matching packages

Usage examples

Basic package installation

source compiled.sh

# Update package lists
pm::sync

# Install development tools
pm::install git curl wget build-essential

if [[ $? -eq 0 ]]; then
    echo "Packages installed successfully"
else
    echo "Installation failed"
    exit 1
fi

System maintenance script

source compiled.sh

echo "Updating system packages..."

# Sync package database
echo "Step 1/2: Syncing package database..."
pm::sync

# Upgrade all packages
echo "Step 2/2: Upgrading packages..."
pm::update

echo "System update complete!"

Conditional package installation

source compiled.sh

# Check if a command exists
if ! runtime::has_command docker; then
    echo "Docker not found. Installing..."
    pm::sync
    pm::install docker.io
    
    # Verify installation
    if runtime::has_command docker; then
        echo "Docker installed successfully"
    else
        echo "Docker installation failed"
        exit 1
    fi
else
    echo "Docker is already installed"
fi

Search and install

source compiled.sh

echo "Searching for Python packages..."
pm::search python | grep "^python3-"

read -p "Enter package to install: " package

if [[ -n "$package" ]]; then
    pm::sync
    pm::install "$package"
fi

Error handling

All functions return the exit code from the underlying package manager:
if pm::install nginx; then
    echo "nginx installed"
    systemctl start nginx
else
    echo "Failed to install nginx (exit code: $?)"
    exit 1
fi
If an unknown package manager is detected, functions return exit code 1 and print an error message to stderr:
pm::install: unknown package manager

Platform-specific notes

  • Does not require sudo
  • Best practice: run brew update before installing packages
  • Use brew install --cask for GUI applications (requires direct brew command)
  • Runs with -y flag (non-interactive)
  • Uses apt-get instead of apt for script stability
  • pm::sync is equivalent to apt-get update
  • Runs with --noconfirm flag
  • pm::sync updates database only (doesn’t upgrade packages)
  • Use pm::update to upgrade packages
  • Does not use sudo (user-level installation)
  • pm::search uses nix-env -qaP
  • Package names may differ from other distributions

Build docs developers (and LLMs) love