Package Resource¶
Install and manage system packages across multiple platforms.
Overview¶
The Package resource handles package installation and removal:
- Install packages on apt, dnf, pacman, and brew
- Remove packages
- Check package installation status
- Support for single and multiple packages
- Automatic platform detection
- Non-interactive installation (no confirmation prompts)
Supported Package Managers¶
| Platform | Package Manager | Install | Remove | Check |
|---|---|---|---|---|
| Ubuntu/Debian | apt | Yes | Yes | Yes |
| Fedora/RHEL | dnf | Yes | Yes | Yes |
| Arch Linux | pacman | Yes | Yes | Yes |
| macOS | brew | Yes | Yes | Yes |
Basic Usage¶
Single Package¶
Multiple Packages (List)¶
Multiple Packages (Named Group)¶
Remove Package¶
Parameters¶
name¶
Required. Package name or list of packages.
Can be: - String: Single package name - List: Multiple package names (convenience syntax)
packages¶
List of package names to install as a group.
ensure¶
Package state: "present", "absent", or "latest".
Default: "present"
# Install package
Package("nginx", ensure="present")
# Remove package
Package("apache2", ensure="absent")
# Upgrade to latest
Package("nginx", ensure="latest")
version¶
Specific package version to install (support varies by package manager).
Note: Version support depends on the package manager. Some package managers may not support pinning to specific versions.
Examples¶
Web Server Stack¶
from cook import Package
# Individual packages
Package("nginx")
Package("certbot")
# Or as a group
Package("web-stack", packages=[
"nginx",
"certbot",
"python3-certbot-nginx"
])
Development Tools¶
Database Server¶
Node.js Application Dependencies¶
# After adding NodeSource repository
Package("nodejs")
# Additional tools
Package("node-tools", packages=[
"npm",
"yarn"
])
Docker Installation¶
# After adding Docker repository
Package("docker", packages=[
"docker-ce",
"docker-ce-cli",
"containerd.io",
"docker-compose-plugin"
])
Remove Unwanted Packages¶
Non-Interactive Installation¶
All package installations run non-interactively to prevent blocking on user input.
APT (Debian/Ubuntu)¶
Prevents configuration prompts and package installation questions.
DNF (Fedora/RHEL)¶
The -y flag automatically confirms all prompts.
Pacman (Arch)¶
The --noconfirm flag bypasses all confirmation prompts.
Brew (macOS)¶
Homebrew does not prompt for confirmation by default.
Package Groups¶
Group related packages for better organization:
from cook import Package
# Web server stack
Package("web-server", packages=[
"nginx",
"certbot",
"python3-certbot-nginx"
])
# Application runtime
Package("app-runtime", packages=[
"python3",
"python3-pip",
"python3-venv"
])
# Monitoring tools
Package("monitoring", packages=[
"prometheus-node-exporter",
"netdata"
])
Idempotency¶
Package resources are idempotent. Repeated installation of the same package does not reinstall.
Package("nginx")
# First run: Installs nginx
# Second run: Detects nginx already installed, no changes
Package removal:
Package("apache2", ensure="absent")
# First run: Removes apache2
# Second run: Detects apache2 not installed, no changes
Platform-Specific Behavior¶
APT (Debian/Ubuntu)¶
Equivalent to:
Package check uses dpkg-query:
DNF (Fedora/RHEL)¶
Equivalent to:
Package check uses rpm:
Pacman (Arch)¶
Equivalent to:
Package check:
Homebrew (macOS)¶
Equivalent to:
Package check:
Complete Workflow¶
Typical pattern for system setup:
from cook import Repository, Package
# Update package cache
Repository("apt-update", action="update")
# Upgrade existing packages
Repository("apt-upgrade", action="upgrade")
# Install system tools
Package("system-tools", packages=[
"curl",
"wget",
"git",
"vim",
"htop"
])
# Install web server
Package("nginx")
# Install application runtime
Package("python-runtime", packages=[
"python3",
"python3-pip",
"python3-venv"
])
Working with Repositories¶
Add custom repositories before installing packages from them:
from cook import Repository, Package
# Add NodeSource repository
Repository(
"nodesource",
action="add",
repo="deb https://deb.nodesource.com/node_20.x nodistro main",
key_url="https://deb.nodesource.com/gpgkey/nodesource.gpg.key"
)
# Update cache after adding repository
Repository("apt-update", action="update")
# Install from new repository
Package("nodejs")
Version Management¶
Request specific versions when needed:
# Specific version
Package("nginx", version="1.18.0-1ubuntu1")
# Latest version
Package("nginx", ensure="latest")
Note: Version support varies by package manager. Test on your target platform.
Security Considerations¶
Package Sources¶
Only install packages from trusted repositories:
# Good: Official repository
Package("nginx")
# Good: Well-known third-party with GPG verification
Repository(
"docker",
action="add",
repo="deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable",
key_url="https://download.docker.com/linux/ubuntu/gpg"
)
Package("docker-ce")
System Updates¶
Keep systems updated:
from cook import Repository, Package
# Regular updates
Repository("system-update", action="update")
Repository("system-upgrade", action="upgrade")
Minimal Installation¶
Install only necessary packages:
# Install minimal set
Package("production-tools", packages=[
"nginx",
"python3",
"certbot"
])
# Avoid installing development tools on production unless needed
Common Patterns¶
Full LEMP Stack¶
from cook import Repository, Package
# Update system
Repository("apt-update", action="update")
# Install LEMP stack
Package("lemp", packages=[
"nginx",
"mysql-server",
"php-fpm",
"php-mysql"
])
Python Development Environment¶
Package("python-dev", packages=[
"python3",
"python3-pip",
"python3-venv",
"python3-dev",
"build-essential"
])
Container Platform¶
# Add Docker repository first
Repository(
"docker",
action="add",
repo="deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable",
key_url="https://download.docker.com/linux/ubuntu/gpg"
)
Repository("apt-update", action="update")
# Install Docker
Package("docker", packages=[
"docker-ce",
"docker-ce-cli",
"containerd.io",
"docker-compose-plugin"
])
SSL/TLS Certificates¶
Troubleshooting¶
Package Not Found¶
Ensure repository is added and cache is updated:
# Add repository
Repository("custom-repo", action="add", repo="...")
# Update cache
Repository("apt-update", action="update")
# Then install
Package("custom-package")
Installation Fails¶
Check platform compatibility and package name:
# Ubuntu/Debian
Package("nodejs")
# Some platforms may use different names
# Verify with: apt-cache search nodejs
Version Conflicts¶
Use version pinning when specific versions are required:
API Reference¶
See cook/resources/pkg.py for complete source code and implementation details.