Package Management
Overview
Package management is one of the core tasks in Linux system administration. Different distributions use different package management systems; this chapter introduces the main package management tools.
Package Management System Comparison
| Distribution | Package Format | Low-level Tool | High-level Tool |
|---|---|---|---|
| Debian/Ubuntu | .deb | dpkg | apt, apt-get |
| RHEL/Fedora | .rpm | rpm | dnf, yum |
| Arch Linux | .pkg.tar.zst | pacman | pacman |
| openSUSE | .rpm | rpm | zypper |
APT (Debian/Ubuntu)
Update Software Sources
bash
# Update package list
$ sudo apt update
# Upgrade all packages
$ sudo apt upgrade
# Upgrade with dependency changes
$ sudo apt full-upgrade
# Update and upgrade together
$ sudo apt update && sudo apt upgrade -ySearch for Software
bash
# Search for package
$ apt search nginx
$ apt search python
# Show package information
$ apt show nginx
# List installed packages
$ apt list --installed
# List upgradable packages
$ apt list --upgradableInstall Software Packages
bash
# Install package
$ sudo apt install nginx
# Install multiple packages
$ sudo apt install nginx php mysql-server
# Install specific version
$ sudo apt install nginx=1.18.0-0ubuntu1
# Install local .deb file
$ sudo apt install ./package.deb
# Reinstall
$ sudo apt reinstall nginxRemove Software Packages
bash
# Remove package (keep configuration)
$ sudo apt remove nginx
# Remove package and configuration
$ sudo apt purge nginx
# Remove unused dependencies
$ sudo apt autoremove
# Clean downloaded package files
$ sudo apt clean
$ sudo apt autocleanSoftware Source Configuration
Software source configuration file: /etc/apt/sources.list
bash
# View software sources
$ cat /etc/apt/sources.list
# Add PPA (Ubuntu)
$ sudo add-apt-repository ppa:user/ppa-name
$ sudo apt update
# Remove PPA
$ sudo add-apt-repository --remove ppa:user/ppa-namedpkg - Low-level Package Management
bash
# Install .deb package
$ sudo dpkg -i package.deb
# Fix dependency issues
$ sudo apt install -f
# Remove package
$ sudo dpkg -r package_name
# List installed packages
$ dpkg -l
$ dpkg -l | grep nginx
# Show package information
$ dpkg -s nginx
# List package's files
$ dpkg -L nginx
# Find which package a file belongs to
$ dpkg -S /usr/sbin/nginxDNF/YUM (Fedora/RHEL)
Update Software
bash
# Check for updates
$ sudo dnf check-update
# Upgrade all packages
$ sudo dnf upgrade
$ sudo dnf update
# Upgrade specific package
$ sudo dnf upgrade nginxSearch and Install
bash
# Search for package
$ dnf search nginx
# Show package information
$ dnf info nginx
# Install package
$ sudo dnf install nginx
# Install local rpm
$ sudo dnf install ./package.rpm
# Install group
$ sudo dnf groupinstall "Development Tools"Remove Software Packages
bash
# Remove package
$ sudo dnf remove nginx
# Remove unused dependencies
$ sudo dnf autoremove
# Clean cache
$ sudo dnf clean allrpm - Low-level Package Management
bash
# Install rpm package
$ sudo rpm -ivh package.rpm
# Upgrade rpm package
$ sudo rpm -Uvh package.rpm
# Remove package
$ sudo rpm -e package_name
# Query installed packages
$ rpm -qa
$ rpm -qa | grep nginx
# Query package information
$ rpm -qi nginx
# List package's files
$ rpm -ql nginx
# Find which package a file belongs to
$ rpm -qf /usr/sbin/nginxSoftware Source Configuration
bash
# Source configuration directory
$ ls /etc/yum.repos.d/
# Add EPEL source
$ sudo dnf install epel-release
# List enabled sources
$ dnf repolist
# List all sources
$ dnf repolist allPacman (Arch Linux)
Sync and Update
bash
# Sync package database
$ sudo pacman -Sy
# Upgrade all packages
$ sudo pacman -Su
# Sync and upgrade
$ sudo pacman -SyuSearch and Install
bash
# Search for package
$ pacman -Ss nginx
# Show package information
$ pacman -Si nginx
# Install package
$ sudo pacman -S nginx
# Install local package
$ sudo pacman -U package.pkg.tar.zstRemove Software Packages
bash
# Remove package
$ sudo pacman -R nginx
# Remove package and dependencies
$ sudo pacman -Rs nginx
# Remove package, dependencies, and configuration
$ sudo pacman -Rns nginx
# Clean cache
$ sudo pacman -ScQuery
bash
# List installed packages
$ pacman -Q
$ pacman -Q | grep nginx
# Query installed package info
$ pacman -Qi nginx
# List package's files
$ pacman -Ql nginx
# Find which package a file belongs to
$ pacman -Qo /usr/sbin/nginxUniversal Tools
snap
bash
# Install snap
$ sudo apt install snapd
# Search snap packages
$ snap find package
# Install snap package
$ sudo snap install package
# List installed snaps
$ snap list
# Update snap packages
$ sudo snap refresh
# Remove snap package
$ sudo snap remove packageflatpak
bash
# Install flatpak
$ sudo apt install flatpak
# Add Flathub
$ flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
# Search for applications
$ flatpak search application
# Install application
$ flatpak install flathub com.example.App
# Run application
$ flatpak run com.example.App
# List installed
$ flatpak list
# Remove application
$ flatpak uninstall com.example.AppAppImage
bash
# AppImage is an executable file, no installation needed
$ chmod +x app.AppImage
$ ./app.AppImageCompile from Source
Typical Steps
bash
# 1. Download source code
$ wget https://example.com/software.tar.gz
$ tar xzf software.tar.gz
$ cd software
# 2. Configure
$ ./configure --prefix=/usr/local
# 3. Compile
$ make
# 4. Install
$ sudo make installInstall Compilation Tools
bash
# Debian/Ubuntu
$ sudo apt install build-essential
# Fedora
$ sudo dnf groupinstall "Development Tools"
# Arch Linux
$ sudo pacman -S base-develcheckinstall
Create manageable package:
bash
# Install checkinstall
$ sudo apt install checkinstall
# Use checkinstall instead of make install
$ sudo checkinstall
# This creates .deb package and installs itPractical Tips
View Installed Software Size
bash
# Debian/Ubuntu
$ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -20
# Arch Linux
$ pacman -Qi | awk '/^Name/{name=$3} /^Installed Size/{print $4$5, name}' | sort -h | tail -20List Manually Installed Packages
bash
# Debian/Ubuntu
$ apt-mark showmanual
# Arch Linux
$ pacman -QeFix Broken Packages
bash
# Debian/Ubuntu
$ sudo apt --fix-broken install
$ sudo dpkg --configure -a
# Fedora
$ sudo dnf distro-syncLock Package Versions
bash
# Debian/Ubuntu
$ sudo apt-mark hold package_name
$ sudo apt-mark unhold package_name
# View locked packages
$ apt-mark showholdClean System
bash
# Debian/Ubuntu
$ sudo apt autoremove
$ sudo apt autoclean
$ sudo apt clean
# Remove old kernels (Ubuntu)
$ sudo apt autoremove --purgeSummary
This chapter introduced Linux package management:
- APT (Debian/Ubuntu):
apt,dpkg - DNF/YUM (Fedora/RHEL):
dnf,rpm - Pacman (Arch):
pacman - Universal formats: snap, flatpak, AppImage
- Compile from source: configure, make, install
Choose appropriate package management method and keep your system clean and secure.
Previous chapter: Process Management
Next chapter: Service Management