Package Management with APT
Overview
- What you’ll learn: How to use APT and dpkg to install, update, and manage software packages on Ubuntu Server.
- Prerequisites: Lesson 5 – systemd and Service Management
- Estimated reading time: 16 minutes
Introduction
One of the most important tasks in Linux system administration is managing software packages. Ubuntu uses the APT (Advanced Package Tool) system, which provides a high-level interface for installing, updating, and removing software. Under the hood, APT works with .deb packages and the lower-level dpkg tool.
In this lesson, you will learn how to search for packages, manage repositories, keep your system up to date, and handle common package management tasks that you will perform regularly as a system administrator.
dpkg Basics
dpkg is the low-level package manager for Debian-based systems. While you will primarily use APT, understanding dpkg is useful for troubleshooting:
$ dpkg -l | head -10 # List all installed packages
$ dpkg -l nginx # Check if nginx is installed
$ dpkg -L nginx # List files installed by nginx
$ dpkg -S /usr/sbin/nginx # Find which package owns a file
nginx-core: /usr/sbin/nginx
$ sudo dpkg -i package.deb # Install a .deb file directly
$ sudo dpkg -r package # Remove a package
$ sudo dpkg --configure -a # Fix broken package configuration
APT Commands
Updating Package Lists
Before installing or upgrading packages, always refresh the local package index:
$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Fetched 229 kB in 1s (229 kB/s)
Reading package lists... Done
Building dependency tree... Done
15 packages can be upgraded. Run 'apt list --upgradable' to see them.
Installing and Removing Packages
$ sudo apt install nginx # Install a package
$ sudo apt install nginx=1.18.0-6ubuntu14 # Install specific version
$ sudo apt install -y nginx php mysql-server # Install multiple, auto-confirm
$ sudo apt remove nginx # Remove package (keep config files)
$ sudo apt purge nginx # Remove package and config files
$ sudo apt autoremove # Remove unused dependencies
Upgrading the System
$ sudo apt upgrade # Upgrade all packages (safe)
$ sudo apt full-upgrade # Upgrade with dependency changes
$ sudo apt list --upgradable # List packages with updates available
Searching for Packages
$ apt search "web server"
nginx/jammy 1.18.0-6ubuntu14 amd64
small, powerful, scalable web/proxy server
$ apt show nginx # Show detailed package information
Package: nginx
Version: 1.18.0-6ubuntu14
Priority: optional
Section: httpd
Installed-Size: 44.3 kB
Depends: nginx-core | nginx-full | nginx-light | nginx-extras
$ apt-cache policy nginx # Show version and repository info
nginx:
Installed: 1.18.0-6ubuntu14
Candidate: 1.18.0-6ubuntu14
Version table:
*** 1.18.0-6ubuntu14 500
500 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages
Repository Management
/etc/apt/sources.list
APT repositories are configured in /etc/apt/sources.list and files under /etc/apt/sources.list.d/:
$ cat /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu jammy main restricted
deb http://archive.ubuntu.com/ubuntu jammy-updates main restricted
deb http://archive.ubuntu.com/ubuntu jammy universe
deb http://security.ubuntu.com/ubuntu jammy-security main restricted
Adding PPAs (Personal Package Archives)
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.2
$ sudo add-apt-repository --remove ppa:ondrej/php # Remove a PPA
Adding Third-Party Repositories
# Download and add the GPG key
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add the repository
$ echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg]
https://download.docker.com/linux/ubuntu jammy stable" |
sudo tee /etc/apt/sources.list.d/docker.list
$ sudo apt update
$ sudo apt install docker-ce
Package Pinning and Unattended Upgrades
Pinning
Package pinning lets you control which version of a package is preferred:
$ sudo nano /etc/apt/preferences.d/nginx
Package: nginx
Pin: version 1.18.*
Pin-Priority: 1001
Unattended Upgrades
For production servers, automatic security updates are essential:
$ sudo apt install unattended-upgrades
$ sudo dpkg-reconfigure -plow unattended-upgrades
$ cat /etc/apt/apt.conf.d/50unattended-upgrades | head -8
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
"${distro_id}ESMApps:${distro_codename}-apps-security";
"${distro_id}ESM:${distro_codename}-infra-security";
};
Key Takeaways
dpkgis the low-level tool for .deb packages;aptis the high-level interface you use daily.- Always run
sudo apt updatebefore installing or upgrading packages. - Use
apt install,apt remove,apt purge, andapt autoremovefor package lifecycle management. - Repositories are configured in
/etc/apt/sources.listand/etc/apt/sources.list.d/. - PPAs and third-party repos extend the available software; always verify GPG keys for security.
- Enable
unattended-upgradeson production servers for automatic security patches.
What’s Next
In Lesson 7, you will learn about Text Processing and Shell Scripting — how to use grep, sed, awk, pipes, and write bash scripts to automate tasks.
繁體中文
概述
- 學習目標:學習如何使用 APT 和 dpkg 在 Ubuntu Server 上安裝、更新和管理軟體套件。
- 先決條件:第 5 課 – systemd 和服務管理
- 預計閱讀時間:16 分鐘
簡介
Linux 系統管理中最重要的任務之一是管理軟體套件。Ubuntu 使用 APT(進階套件工具)系統,它提供了安裝、更新和移除軟體的高階介面。APT 底層使用 .deb 套件和底層的 dpkg 工具。
dpkg 基礎
dpkg 是 Debian 系統的底層套件管理器:
$ dpkg -l | head -10 # 列出所有已安裝的套件
$ dpkg -L nginx # 列出 nginx 安裝的檔案
$ dpkg -S /usr/sbin/nginx # 查找檔案屬於哪個套件
APT 命令
更新套件清單
$ sudo apt update # 重新整理本地套件索引
安裝和移除套件
$ sudo apt install nginx # 安裝套件
$ sudo apt remove nginx # 移除套件(保留設定檔)
$ sudo apt purge nginx # 移除套件和設定檔
$ sudo apt autoremove # 移除未使用的相依性
升級系統
$ sudo apt upgrade # 升級所有套件(安全)
$ sudo apt full-upgrade # 包含相依性變更的升級
搜尋套件
$ apt search "web server" # 搜尋套件
$ apt show nginx # 顯示套件詳細資訊
$ apt-cache policy nginx # 顯示版本和儲存庫資訊
儲存庫管理
APT 儲存庫在 /etc/apt/sources.list 和 /etc/apt/sources.list.d/ 中設定。
新增 PPA
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.2
套件固定和自動升級
套件固定可控制偏好的套件版本。unattended-upgrades 可在生產伺服器上自動安裝安全更新。
重點摘要
dpkg是 .deb 套件的底層工具;apt是日常使用的高階介面。- 安裝或升級套件前始終執行
sudo apt update。 - 使用
apt install、apt remove、apt purge、apt autoremove進行套件生命週期管理。 - 儲存庫在
/etc/apt/sources.list中設定。 - 在生產伺服器上啟用
unattended-upgrades以自動安裝安全修補程式。
下一步
在第 7 課中,您將學習文字處理和 Shell 腳本——如何使用 grep、sed、awk、管道,以及編寫 bash 腳本來自動化任務。
日本語
概要
- 学習内容:APT と dpkg を使用した Ubuntu Server でのソフトウェアパッケージのインストール、更新、管理。
- 前提条件:レッスン5 – systemd とサービス管理
- 推定読了時間:16分
はじめに
Linux システム管理で最も重要なタスクの一つがソフトウェアパッケージの管理です。Ubuntu は APT(Advanced Package Tool)システムを使用しており、ソフトウェアのインストール、更新、削除のための高レベルインターフェースを提供します。
dpkg の基本
$ dpkg -l | head -10 # インストール済みパッケージの一覧
$ dpkg -L nginx # nginx がインストールしたファイルの一覧
$ dpkg -S /usr/sbin/nginx # ファイルが属するパッケージを検索
APT コマンド
パッケージリストの更新
$ sudo apt update # ローカルパッケージインデックスを更新
パッケージのインストールと削除
$ sudo apt install nginx # パッケージをインストール
$ sudo apt remove nginx # パッケージを削除(設定ファイルは保持)
$ sudo apt purge nginx # パッケージと設定ファイルを削除
$ sudo apt autoremove # 未使用の依存関係を削除
システムのアップグレード
$ sudo apt upgrade # すべてのパッケージをアップグレード
$ sudo apt full-upgrade # 依存関係の変更を含むアップグレード
パッケージの検索
$ apt search "web server" # パッケージを検索
$ apt show nginx # パッケージの詳細情報を表示
$ apt-cache policy nginx # バージョンとリポジトリ情報を表示
リポジトリ管理
APT リポジトリは /etc/apt/sources.list と /etc/apt/sources.list.d/ で設定されます。
PPA の追加
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.2
パッケージのピン留めと自動アップグレード
パッケージのピン留めで優先するパッケージバージョンを制御できます。unattended-upgrades で本番サーバーにセキュリティアップデートを自動インストールできます。
重要ポイント
dpkgは .deb パッケージの低レベルツール、aptは日常的に使用する高レベルインターフェース。- パッケージのインストールやアップグレード前には必ず
sudo apt updateを実行。 apt install、apt remove、apt purge、apt autoremoveでパッケージライフサイクルを管理。- リポジトリは
/etc/apt/sources.listで設定。 - 本番サーバーでは
unattended-upgradesを有効にして自動セキュリティパッチを適用。
次のステップ
レッスン7では、テキスト処理とシェルスクリプトについて学びます。grep、sed、awk、パイプの使い方、bash スクリプトの作成方法を学びます。