1. 简介
apt
是在基于 Debian 的 Linux 发行版 (Debian, Ubuntu) 所使用的包管理工具. 在 Ubuntu 16.04 版之后推荐使用 apt
代替原来的 apt-get
.
2. apt 源
apt
发行版系统相关的源在/etc/apt/source.list
文件中
Ubuntu 20.04 (focal) 自带的源:
1
2
3
4
5
6
7
8
9
10
deb http://cn.archive.ubuntu.com/ubuntu/ focal main restricted
deb http://cn.archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb http://cn.archive.ubuntu.com/ubuntu/ focal universe
deb http://cn.archive.ubuntu.com/ubuntu/ focal-updates universe
deb http://cn.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ focal-updates multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu focal-security main restricted
deb http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse
阿里云的源
1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal universe
deb http://mirrors.aliyun.com/ubuntu/ focal-updates universe
deb http://mirrors.aliyun.com/ubuntu/ focal multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal-security universe
deb http://mirrors.aliyun.com/ubuntu/ focal-security multiverse
apt
的第三方源放在/etc/apt/source.list.d/
目录下, 文件名通常为xxx.list
.
这里给两个例子:
1
2
3
4
# docker 的源
deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable
# VSCode 的源
deb [arch=amd64,arm64,armhf] http://packages.microsoft.com/repos/code stable main
当然了, 我们同样可以把第三方的源放到 /etc/apt/source.list
文件的末尾.
3. 基本用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 更新包的列表及其版本 (推荐在使用 apt 其他命令之前执行该命令)
sudo apt update
# 搜索软件包
apt search <package>
# 显示软件包的信息
apt show <package>
# 安装一个软件包, 或把该软件包更新到最新版本
sudo apt install <package>
# 删除一个软件包
sudo apt remove <package>
# 把所有软件包升级到最新的版本
sudo apt upgrade
# 列出所有的软件包
apt list
# 列出安装了的软件包
apt list --installed
4. 进阶用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 删除一个软件包, 并清理其配置文件
sudo apt purge <package>
# 在 dpkg 安装 deb 包时, 报错缺少依赖, 使用 apt 来安装依赖
sudo apt install -f
# 固定软件包的版本, 不随 `apt upgrade` 升级
sudo echo "<package> hold" | sudo dpkg --set-selections
# 解锁软件包, 跟随 `apt upgrade` 升级
sudo echo "<package> install" | sudo dpkg --set-selections
# 查看软件包的状态 (install/hold)
dpkg --get-selections | grep <package>