Linux pacman Command
pacman (Package Manager) is the default package management tool for Arch Linux and its derivative distributions (such as Manjaro). It is a powerful command-line tool used to install, update, remove, and manage software packages.
Main features of pacman:
- Simple and easy-to-use command-line interface
- Automatically handles dependencies
- Supports local and remote packages
- Provides full system update functionality
- Efficient package query and search capabilities
Basic Syntax
The basic syntax format of the pacman command is as follows:
sudo pacman [选项] [操作] [包名/其他参数]
Where:
sudo: Most pacman operations require root privilegesOptions: Flags that modify command behaviorOperation: Specifies the main function to performPackage name: The name of the package to operate on
Common Operation Commands
System Update
Example
# Update all installed packages
sudo pacman -Syu
# Only update the package database (does not actually update software)
sudo pacman -Sy
sudo pacman -Syu
# Only update the package database (does not actually update software)
sudo pacman -Sy
Software Package Installation
Example
# Install a single package
sudo pacman -Spackage-name
# Install multiple packages
sudo pacman -Spackage-name1package-name2package-name3
# Install a local package file (does not handle dependencies)
sudo pacman -U /path/to/package-name.pkg.tar.zst
sudo pacman -Spackage-name
# Install multiple packages
sudo pacman -Spackage-name1package-name2package-name3
# Install a local package file (does not handle dependencies)
sudo pacman -U /path/to/package-name.pkg.tar.zst
Software Package Removal
Example
# Remove the package but keep its dependencies
sudo pacman -Rpackage-name
# Remove the package and its no-longer-needed dependencies
sudo pacman -Rspackage-name
# Remove the package and all its configuration files
sudo pacman -Rnspackage-name
sudo pacman -Rpackage-name
# Remove the package and its no-longer-needed dependencies
sudo pacman -Rspackage-name
# Remove the package and all its configuration files
sudo pacman -Rnspackage-name
Software Package Query
Example
# Search for packages (name and description)
pacman -Sskeyword
# List installed packages
pacman -Q
# Query which package a file belongs to
pacman -Qo /path/to/file
# Display detailed package information
pacman -Qipackage-name
pacman -Sskeyword
# List installed packages
pacman -Q
# Query which package a file belongs to
pacman -Qo /path/to/file
# Display detailed package information
pacman -Qipackage-name
Clean Cache
Example
# Clean cache of uninstalled packages
sudo pacman -Sc
# Clean cache of all packages (including installed ones)
sudo pacman -Scc
sudo pacman -Sc
# Clean cache of all packages (including installed ones)
sudo pacman -Scc
Common Option Parameters
| Option | Description |
|---|---|
-S |
Synchronize/Install packages |
-R |
Remove packages |
-Q |
Query local package database |
-U |
Install local package file |
-y |
Update package database |
-u |
Upgrade packages |
-s |
Include descriptions when searching |
-i |
Display package information |
-c |
Clean cache |
-d |
Skip dependency check |
-f |
Force operation |
--noconfirm |
Skip all confirmation prompts |
Practical Application Examples
Example 1: Install and Configure Development Environment
Example
# Update the system
sudo pacman -Syu
# Install development tool packages
sudo pacman -S base-devel git python
# Verify the installation
pacman -Q | grep python
sudo pacman -Syu
# Install development tool packages
sudo pacman -S base-devel git python
# Verify the installation
pacman -Q | grep python
Example 2: Find and Install Media Player
Example
# Search for media players
pacman -Ss media player
# Install the VLC media player
sudo pacman -S vlc
# View VLC's dependencies
pacman -Qi vlc
pacman -Ss media player
# Install the VLC media player
sudo pacman -S vlc
# View VLC's dependencies
pacman -Qi vlc
Example 3: Completely Remove Unneeded Software
Example
# Find the package to remove
pacman -Qs game
# Remove the game along with its dependencies and configuration
sudo pacman -Rns steam
pacman -Qs game
# Remove the game along with its dependencies and configuration
sudo pacman -Rns steam
Common Problem Solutions
1. Dependency Conflict
When encountering dependency conflicts, you can try:
Example
# Force downgrade a specific package
sudo pacman -U https://archive.archlinux.org/packages/p/package-name/package-name-version.pkg.tar.zst
# Or try to fix dependencies
sudo pacman -Syu --overwrite '*'
sudo pacman -U https://archive.archlinux.org/packages/p/package-name/package-name-version.pkg.tar.zst
# Or try to fix dependencies
sudo pacman -Syu --overwrite '*'
2. Database Lock
If you encounter the "could not lock database" error:
Example
# Remove the lock file
sudo rm /var/lib/pacman/db.lck
sudo rm /var/lib/pacman/db.lck
3. Insufficient Space
Clean up old version cache:
Example
# View cache disk usage
du -sh /var/cache/pacman/pkg/
# Clean old versions
sudo pacman -Sc
du -sh /var/cache/pacman/pkg/
# Clean old versions
sudo pacman -Sc
Best Practice Recommendations
- Regularly update the system: Execute at least once a week
sudo pacman -Syu - Read the output information: pacman's output contains important information; don't blindly confirm
- Back up important data: Back up important files before performing major system updates
- Use an AUR helper: For software in the Arch User Repository (AUR), consider using yay or paru
- Check the Arch Wiki: When encountering problems, the Arch Wiki usually has detailed solutions
Advanced Tips
View Package File Contents
Example
# List files included in the package
pacman -Qlpackage-name
# Extract a specific package file
sudo pacman -Swpackage-name&& tar -xf /var/cache/pacman/pkg/package-name*.pkg.tar.zst
pacman -Qlpackage-name
# Extract a specific package file
sudo pacman -Swpackage-name&& tar -xf /var/cache/pacman/pkg/package-name*.pkg.tar.zst
Build Custom Package
Example
# Download the PKGBUILD file
asp exportpackage-name
# Enter the directory and build
cdpackage-name
makepkg -si
asp exportpackage-name
# Enter the directory and build
cdpackage-name
makepkg -si
Query File Ownership
Example
# Find which package provides a specific command
pacman -Qo $(whichcommand-name)
# For example, find which package the vim command belongs to
pacman -Qo $(which vim)
pacman -Qo $(whichcommand-name)
# For example, find which package the vim command belongs to
pacman -Qo $(which vim)
By mastering the pacman command, you can efficiently manage all software packages on an Arch Linux system. Remember, pacman is a powerful tool; incorrect commands may cause system problems, so be sure to understand the effect of a command before executing it.
Other Extensions