Skip to the content.

安装软件

aptdpkg都是 Debian 系发行版(包括 Ubuntu 等)特有的命令

目录


包管理器与软件源

包管理器是什么

像是安卓系统上的应用商店或是 IOS 系统的 AppStore,为了方便人们使用,Linux 可以使用包管理器便捷地进行软件包的下载、安装、初始配置、升级和卸载等动作,并自动化处理软件依赖关系。

在以 Ubuntu 为例的 Debian 系发行版中,这个包管理器是 apt,而在 RedHat 系发行版中,一般使用的是yum

软件源是什么

软件源是实际为包管理器提供软件包的服务器,一般在/etc/apt/sources.list文件中记录

当执行apt指令时看到的

$ apt update
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease
...

https://mirrors.tuna.tsinghua.edu.cn/ubuntu即为我的软件源

软件源镜像

为了加速软件源在特定地区的访问、减轻源服务器的压力,很多组织建立了镜像——即从源服务器上下载所有文件,并定期更新,以供他人访问使用的服务器

例如上面的https://mirrors.tuna.tsinghua.edu.cn/ubuntu就是清华大学 TUNA 协会维护的http://archive.ubuntu.com/ubuntu的镜像

有时国内访问源服务器较慢,换成国内镜像将显著改善软件包的下载速度

默认软件源与附加软件源

系统自带的软件源在大多数时候足够使用,但有些软件包——例如 docker、vscode——并没有包含在默认软件源中,此时需手动添加附加软件源

软件包的官方文档里一般会有提示,例如 docker

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
// Note: 以上指令并不完整,仅截取以作示例,请见官方文档。

其中

这样就配置好了一个附加软件源,并可以使用apt安装该源提供的软件包


apt

实际到了指令的部分就非常简单了

与软件包相关的操作大部分都是敏感操作,需要 sudo 权限

apt update

从更新软件源信息,在执行 install / upgrade 等操作前,或添加软件源后,先执行这一操作

有些安装包时出现的E: 无法定位软件包错误就是因为没有执行更新软件源导致的

相当于下载和更新软件源索引列表,告诉本地的包管理器“我这个软件源有哪些软件包”

示例:

$ sudo apt update
[sudo] password for <username>:
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease
Hit:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates InRelease
Hit:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security InRelease
Hit:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports InRelease
Hit:5 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu focal InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
53 packages can be upgraded. Run 'apt list --upgradable' to see them.

执行源更新时,系统有时会自动提示存在可升级的包(如上最后一行),通过apt list --upgradable列出

apt install <pkg1> [<pkg2>...]

安装名为 pkg1 的软件包,可以一次安装多个软件包

尖括号表示参数名无需输入

示例:

$ sudo apt install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  ...
Use 'sudo apt autoremove' to remove it.
Suggested packages:
  ctags vim-doc vim-scripts
The following NEW packages will be installed:
  vim
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 1238 kB of archives.
After this operation, 3112 kB of additional disk space will be used.
Do you want to continue? [Y/n]

输入y确认操作

apt list --upgradable

列出系统上所有已安装且可升级的软件包

示例:

$ apt list --upgradable
...
g++-9/focal-updates,focal-security 9.4.0-1ubuntu1~20.04 amd64 [upgradable from: 9.3.0-17ubuntu1~20.04]
gcc-9-base/focal-updates,focal-security 9.4.0-1ubuntu1~20.04 amd64 [upgradable from: 9.3.0-17ubuntu1~20.04]
gcc-9/focal-updates,focal-security 9.4.0-1ubuntu1~20.04 amd64 [upgradable from: 9.3.0-17ubuntu1~20.04]
...

格式如:包名/分支 版本号 架构 [可从旧版本升级]

apt upgrade

升级系统上所有已安装的软件包

示例:

$ sudo apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
  ...
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  ...
The following packages will be upgraded:
  ...
53 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
37 standard security updates
Need to get 219 MB of archives.
After this operation, 26.1 MB of additional disk space will be used.
Do you want to continue? [Y/n]

输入y确认操作

apt remove <pkg1> [<pkg2>...]

卸载名为 pkg1 的软件包,可以一次卸载多个软件包

apt autoremove

自动卸载不需要的包(通常是为了解决依赖关系自动安装,而现在不再被依赖的包)

输出信息

在执行apt操作时,系统会提示几类包:

  1. 为了解决某依赖关系自动安装,而现在不再被依赖的包。提示可以使用sudo apt autoremove自动移除
    The following package was automatically installed and is no longer required:
      ...
    Use 'sudo apt autoremove' to remove it.
    
  2. 人为指定安装,或者为了解决升级以后新的依赖关系自动安装的包
    The following NEW packages will be installed:
      ...
    
  3. 将被升级的包
    The following packages will be upgraded:
      ...
    
  4. 建议安装,但非必须的包
    Suggested packages:
      ...
    

dpkg

当一个软件包以.deb格式发布,而它不在任何已知 apt 源中时,需要手动下载软件包并用dpkg安装

cd移动到下载的包所在的目录随后执行:

$ dpkg -i <pkgname>.deb

此外,由于apt底层实际上也是调用dpkg进行安装,当使用apt安装或升级软件包异常中断时,需要使用

$ dpkg --configure -a

尝试恢复


编译安装

有的软件包既不提供 apt 源,亦无 deb 包,只提供了源代码或(.tar / .tar.gz格式的)源代码压缩包。此时需要编译安装。

这类情况的特征是提供的压缩包里存在CMakeMakeFilexxx.mkconfigure这样的编译配置文件。

情况较为复杂,请查询具体的软件包构建说明。

比较常见的流程是

$ tar -xvf <pkgname>.tar    // 解压缩
$ cd <pkgname>              // 进入解压缩得到的文件夹
$ ./configure               // 执行配置脚本
$ make                      // 编译
$ make install              // 编译安装

这一过程通常需要gccmake作为工具,可通过apt安装它们

$ sudo apt install gcc make