apt list 是什么
apt list 列出仓库中可安装的包及其候选版本,apt-cache show 输出包的完整元数据(依赖、描述、大小),apt-cache policy 显示各来源仓库的优先级以及当前安装版本与候选版本。三者组合,是升级前评估影响面的标准动作。
基本信息
| 适用平台 | Debian / Ubuntu 等使用 APT 的系统 |
|---|---|
| 命令语法 | apt list [--installed|--upgradable] [包名] / apt-cache show|policy 包名 |
| 常用选项 | --installed 已安装 / --upgradable 可升级 / -a 列出全部可用版本 |
| 权限说明 | 只读查询无需 root,但结果依赖 apt update 刷新过的索引 |
典型使用场景
- 升级前评估:apt list --upgradable 先看这次会动哪些包,避免盲目全量升级
- 确认安装来源:apt-cache policy 看候选版本来自哪个仓库,排查装到旧版本的原因
- 查依赖关系:apt-cache show 的 Depends 与 Recommends 字段说明会连带引入什么
- 找包名:不确定软件包叫什么时,用 apt list 加通配符或 grep 模糊匹配
- 核对已装版本:apt list --installed 配合 grep 确认组件版本,用于故障比对
常用示例
常用示例
apt update # 先刷新索引,否则名单是陈旧的
apt list --installed | grep -i nginx # 已安装的包(输出量大,必须配 grep)
apt list --upgradable # 本次可升级清单
apt list nginx -a # 该包全部可用版本
apt-cache policy nginx # 已装版本、候选版本与来源仓库
apt-cache show nginx | head -n 20 # 版本、依赖、包大小等元数据输出逐行读法
| 输出内容 | 含义 | 怎么看 |
|---|---|---|
| 包名/版本行 | 如 nginx/jammy-updates,now 1.18.0-6ubuntu14.4 amd64 | now 表示这一行为当前安装版本 |
| [已安装] / [可升级] | 状态标记 | 两者并存表示已安装且仓库有更新 |
| Installed: | policy 输出的已安装版本 | (none) 表示未安装 |
| Candidate: | 本次将要安装的候选版本 | 与 Installed 不同即存在可升级版本 |
| 优先级与仓库行 | 如 500 http://archive.ubuntu.com jammy/main amd64 Packages | 数字越大优先级越高,决定候选版本来自哪个仓库 |
apt list 会打印 apt does not have a stable CLI interface 警告:那是提示脚本改用 apt-cache,不是错误。
装不到期望版本时先看 apt-cache policy 的候选来源,再核对 sources.list 与仓库优先级,而不是直接怀疑包不存在。
apt list 的输出格式在不同版本间有调整,脚本解析要留容错;需要稳定结构时用 dpkg-query -W -f 或 apt-cache policy 的固定字段。