Ubuntu 24.04 apt 包管理工具命令详解
详解 Ubuntu 24.04 的 apt/apt-get/dpkg 包管理工具,覆盖软件源配置、search、update、install、remove、purge、autoremove、list、show、clean、history、upgrade、dist-upgrade 全套命令,所有回显均为 Ubuntu 24.04 实测结果
Ubuntu 24.04 apt 包管理工具命令详解
目标:详解 Ubuntu 24.04 的 apt / apt-get / dpkg 包管理工具,覆盖软件源配置、搜索、安装、卸载、查询、缓存、历史与升级全套命令。所有命令与回显均为 Ubuntu 24.04 实测结果。
一、环境信息
- 操作系统:Ubuntu 24.04.5 LTS(noble)
- apt:
apt 2.8.3 (amd64)(apt 与 apt-get 同版本) - dpkg:
Debian 'dpkg' package management program version 1.22.6 (amd64)
二、apt 包管理器概览
Ubuntu 的包管理基于 dpkg,apt 在其之上提供仓库解析、依赖自动处理等能力:
| 命令 | 定位 |
|---|---|
| dpkg | 底层包管理,直接操作 .deb,不做依赖解析 |
| apt-get | 经典前端,脚本友好,输出稳定 |
| apt | 面向交互的新前端,带进度条与彩色输出 |
仓库位置与配置文件
Ubuntu 24.04 起使用 deb822 格式源文件:
ls /etc/apt/sources.list.d/
cat /etc/apt/sources.list.d/ubuntu.sources
ubuntu.sources
docker.list
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
...
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
💡 传统
deb http://...一行式写法仍兼容,可放在/etc/apt/sources.list.d/*.list中(如 docker.list)。GPG 密钥按惯例放/etc/apt/keyrings/。
三、软件源配置(官方源/国内镜像源)
1. 切换为阿里云 Ubuntu 镜像源
备份后把官方源替换为阿里云:
cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
sed -i -e "s|http://archive.ubuntu.com/ubuntu/|https://mirrors.aliyun.com/ubuntu/|" \
-e "s|http://security.ubuntu.com/ubuntu/|https://mirrors.aliyun.com/ubuntu/|" \
/etc/apt/sources.list.d/ubuntu.sources
grep -E "URIs|Suites" /etc/apt/sources.list.d/ubuntu.sources
URIs: https://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-backports
URIs: https://mirrors.aliyun.com/ubuntu/
Suites: noble-security
2. 验证镜像源生效
apt-get update
Get:15 https://mirrors.aliyun.com/ubuntu noble-backports/universe amd64 Packages [36.0 kB]
Get:16 https://mirrors.aliyun.com/ubuntu noble-backports/main amd64 Packages [49.0 kB]
...
Fetched 33.2 MB in 48s (697 kB/s)
Reading package lists...
3. 第三方仓库示例:阿里云 docker-ce 源
Ubuntu 24.04 用 noble 通道,GPG 密钥用 gpg --dearmor 转成 keyring 文件:
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
cat /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble stable
四、search 搜索
apt search sl
apt search 是全文搜索,匹配包名与描述,命中的相关性包也会出现:
Sorting...
Full Text Search...
2048/noble 0.20221023.1237-1 amd64
Slide and add puzzle game for text mode
abcm2ps/noble 8.14.14-1build2 amd64
精确按包名搜索用 apt-cache search(输出更简短):
apt-cache search sl
docker-ce-rootless-extras - Rootless support for Docker.
apache2-bin - Apache HTTP Server (modules and other binary files)
apache2-ssl-dev - Apache HTTP Server (mod_ssl development headers)
busybox-syslogd - Provides syslogd and klogd using busybox
cyrus-sasl2-doc - Cyrus SASL - documentation
💡
apt search默认做 Full Text Search,结果较多;apt-cache search同样匹配描述。想要精确定位可用apt list -a <包名>。
五、update 刷新仓库
安装前先刷新,获取最新的软件包索引:
apt-get update
apt update # 两种写法等价,输出略有差异
Reading package lists...
Building dependency tree...
Reading state information...
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
六、install 安装
1. 单包安装
apt install -y htop
Unpacking htop (3.3.0-4build1) ...
Setting up libnl-3-200:amd64 (3.7.0-0.3build1.1) ...
Setting up libnl-genl-3-200:amd64 (3.7.0-0.3build1.1) ...
Setting up htop (3.3.0-4build1) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
2. 多包安装
apt-get install -y sl tree
Setting up tree (2.1.1-2ubuntu3.24.04.2) ...
Setting up sl (5.02-1) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
3. 指定版本安装
先查看可用版本:
apt list -a htop
Listing...
htop/noble,now 3.3.0-4build1 amd64 [installed]
再按 包名=版本 安装(与已装版本一致时会提示已是最新):
apt-get install -y htop=3.3.0-4build1
Reading state information...
htop is already the newest version (3.3.0-4build1).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
4. --no-install-recommends 精简安装
不装推荐包,适合最小化环境:
apt-get install -y --no-install-recommends vim-tiny
Unpacking vim-tiny (2:9.1.0016-1ubuntu7.20) ...
Setting up vim-tiny (2:9.1.0016-1ubuntu7.20) ...
💡 对比:默认安装 vim-tiny 会一并装 Recommended 的依赖,加
--no-install-recommends后只装 Depends 硬依赖。
七、remove / purge / autoremove 卸载
1. remove 卸载(保留配置文件)
apt remove -y sl
After this operation, 60.4 kB disk space will be freed.
(Reading database ... 10964 files and directories currently installed.)
Removing sl (5.02-1) ...
💡 无配置文件的小包 remove 后直接消失;带配置文件的包 remove 后
dpkg -l会显示rc(已卸载但保留配置)。
2. purge 彻底清除(含配置文件)
apt-get purge -y sl
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
3. autoremove 清理孤儿依赖
apt autoremove -y
(Reading database ... 10940 files and directories currently installed.)
Removing libncurses6:amd64 (6.4+20240113-1ubuntu2.2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
💡 卸载 sl 后其依赖 libncurses6 变成"无主"依赖,autoremove 会将其清理。remove 输出中提示的
Use 'apt autoremove' to remove it.即指此类依赖。
八、list / info / show 查询
1. 列出已安装包
apt list --installed
docker-ce-cli/noble,now 5:29.8.2-1~ubuntu.24.04~noble amd64 [installed]
docker-ce-rootless-extras/noble,now 5:29.8.2-1~ubuntu.24.04~noble amd64 [installed,automatic]
docker-ce/noble,now 5:29.8.2-1~ubuntu.24.04~noble amd64 [installed]
htop/noble,now 3.3.0-4build1 amd64 [installed]
2. apt show 查看仓库信息
apt show htop
Package: htop
Version: 3.3.0-4build1
Priority: optional
Section: utils
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Installed-Size: 434 kB
Depends: libc6 (>= 2.38), libncursesw6 (>= 6), libnl-3-200 (>= 3.2.7), libnl-genl-3-200 (>= 3.2.7), libtinfo6 (>= 6)
Suggests: lm-sensors, lsof, strace
Homepage: https://htop.dev/
3. apt-cache show 查看多版本仓库信息
apt-cache show tree
Package: tree
Version: 2.1.1-2ubuntu3.24.04.2
Package: tree
Version: 2.1.1-2ubuntu3
4. dpkg -s / -L / -l
dpkg -s htop
dpkg -L htop
dpkg -l | grep -E "docker-ce|containerd"
Package: htop
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 424
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 3.3.0-4build1
/.
/usr
/usr/bin
/usr/bin/htop
/usr/share
/usr/share/applications
💡
dpkg -s看已装包状态,dpkg -L列出包安装的文件,dpkg -l列出全部已装包(第一列ii表示已安装)。安装与卸载也可绕过 apt 直接用 dpkg:dpkg -i xxx.deb、dpkg -r 包名、dpkg --purge 包名,但不做依赖解析。
九、clean / 缓存管理
apt 下载的 .deb 缓存在 /var/cache/apt/archives:
du -sh /var/cache/apt/archives
apt clean
apt autoclean
12K /var/cache/apt/archives
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
💡
apt clean清空全部缓存无输出;apt autoclean只清理不再需要的旧版本 .deb(自动装包产生的缓存被清后目录会明显变小)。
十、历史记录与升级(history / upgrade / dist-upgrade)
1. 操作历史
apt 每次操作都会记录到 /var/log/apt/history.log:
tail -20 /var/log/apt/history.log
Commandline: apt-get install -y sl tree
Install: libgpm2:amd64 (1.20.7-11, automatic), sl:amd64 (5.02-1), tree:amd64 (2.1.1-2ubuntu3.24.04.2), libncurses6:amd64 (6.4+20240113-1ubuntu2.2, automatic)
End-Date: 2026-10-07 06:26:16
Commandline: apt-get install -y --no-install-recommends vim-tiny
Install: vim-common:amd64 (2:9.1.0016-1ubuntu7.20, automatic), vim-tiny:amd64 (2:9.1.0016-1ubuntu7.20)
End-Date: 2026-10-07 06:26:24
Commandline: apt remove -y sl
Remove: sl:amd64 (5.02-1)
End-Date: 2026-10-07 06:26:41
💡 每条记录含 Start-Date/End-Date、命令行与 Install/Remove/Upgrade 明细,便于审计。
2. 查看可升级包
apt list --upgradable
Listing...
libaudit-common/noble-updates 1:3.1.2-2.1ubuntu0.1 all [upgradable from: 1:3.1.2-2.1build1.1]
libaudit1/noble-updates 1:3.1.2-2.1ubuntu0.1 amd64 [upgradable from: 1:3.1.2-2.1build1.1]
3. upgrade 普通升级
apt-get upgrade -y
Setting up libaudit-common (1:3.1.2-2.1ubuntu0.1) ...
Preparing to unpack .../libaudit1_1%3a3.1.2-2.1ubuntu0.1_amd64.deb ...
Unpacking libaudit1:amd64 (1:3.1.2-2.1ubuntu0.1) over (1:3.1.2-2.1build1.1) ...
Setting up libaudit1:amd64 (1:3.1.2-2.1ubuntu0.1) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
💡
upgrade只升级已有包,不安装新依赖、不卸载冲突包。
4. dist-upgrade 智能升级
apt-get dist-upgrade -y
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
💡
dist-upgrade在升级时会处理新依赖(安装/删除),处理完所有可升级包后显示 0 not upgraded。apt 对应的交互命令为apt upgrade(Ubuntu 22.04+ 默认启用 dist-upgrade 语义)。
十一、常见问题与真实行为
-
apt search结果太多:它是 Full Text Search,按描述匹配;想按包名精确搜用apt-cache search或apt list -a。 -
remove 后包仍占用空间:remove 保留配置文件,需
purge彻底清除;孤儿依赖需autoremove清理。 -
dpkg -i报依赖缺失:dpkg 不做依赖解析,执行apt-get install -f修复。 -
apt 与 apt-get 命令对应:
apt是交互优化版,常用命令为apt update/install/remove/purge/autoremove/list/show/search/upgrade;apt-get更稳定适合脚本:apt-get update/install/remove/purge/autoremove/upgrade/dist-upgrade/download。两者读取同一套源配置,无功能差异。
十二、命令与参数速查表
| 命令 | 作用 | 常用参数 |
|---|---|---|
| apt-get update | 刷新软件包索引 | - |
| apt-get install | 安装包 | -y 免确认;包名=版本 指定版本;--no-install-recommends 精简安装 |
| apt-get remove | 卸载(保留配置) | -y |
| apt-get purge | 卸载并清除配置 | -y |
| apt autoremove | 清理孤儿依赖 | -y |
| apt-get upgrade | 升级已有包 | -y |
| apt-get dist-upgrade | 智能升级(处理依赖变化) | -y |
| apt-get install -f | 修复损坏依赖 | - |
| apt search | 全文搜索 | - |
| apt list | 列出包 | --installed / -a 所有版本 / --upgradable |
| apt show / apt-cache show | 查看包详情 | - |
| apt clean / autoclean | 清理缓存 | - |
| dpkg -i / -r / --purge | 底层安装/卸载/清除 | - |
| dpkg -s / -l / -L | 查状态/列表/文件清单 | - |
| apt-get download | 仅下载 deb 不安装(离线用) | - |