Ubuntu apt Usage

2 minute read

Published:

Wikipedia

Advanced package tool, or APT, is a free-software user interface that works with core libraries to handle the installation and removal of software on Debian, and Debian-based Linux distributions. APT simplifies the process of managing software on Unix-like computer systems by automating the retrieval, configuration and installation of software packages, either from precompiled files or by compiling source code.

Man Page

apt-get is the command-line tool for handling packages, and may be considered the user’s “back-end” to other tools using the APT library. Several “front-end” interfaces exist, such as aptitude(8), synaptic(8) and wajig(1).

Description 可选操作

update

  • 默认从/etc/apt/sources.list目录中去扫描Package.gz文件
  • upgradedist-upgrade最好都执行下update
  • 对于the overall progress meter选项,如果不能提前设定全部文件大小,就无法正确显示

upgrade

  • apt-getupgrade操作回比aptaptitude更严格,它不会安装事先未安装过得包
  • 当Debian主版本升级,需要使用apt full-upgrade来升级,对应的apt-get工具下,这个命令的名称为apt-get dist-upgrade
  • upgrade的所有日志将存储在/var/log/apt/history.log/var/log/apt/term.log文件中,dpkg的日志则存储在/var/log/dpkg.log

install

  • 如果在package name后添加一个-,就执行remove而非install

remove & purge & autoremove

  • remove使用方法与install相同,但是不会移除配置文件
  • 如果在package name后添加一个+,就执行install而非remove
  • purge会在remove后删除所有配置文件
  • autoremove将会删掉曾经用于支持其他packages使用而自动安装,目前不再使用的Packages

clean & autoclean

  • 所有apt下载的deb包,都会在/var/cache/apt/archives/下存一份copy,不会自动删除
  • 在频繁update的情况下,备份目录将会变得十分庞大,每个包都会有许多版本存放其中
  • clean将会清除该目录下所有的deb文件,抹除这个目录
  • autoclean只会清除已经过期(无法再下载到)的deb包
  • 修改配置文件中APT::Clean-Installed参数,可以避免autoclean清理到已经安装的deb文件

source

  • 用于下载源码包,要求/etc/apt/source.list文件中,必须有deb-src开头定义的软件源代码库
  • 使用--complie参数,就可以将在下载后使用--host-architecture定义的dpkg-buildpackage编译源码包
  • 使用--download-only将只下载不编译
  • 下载后的源码将不会加入dpkg database,而是作为一个源代码压缩包下载到当前目录下

Options 命令参数

All command line options may be set using the configuration file, the descriptions indicate the configuration option to set. For boolean options you can override the config file by using something like -f-,–no-f, -f=no or several other variations.

特殊参数解析
-f --fix-brokenFix; attempt to correct a system with broken dependencies in place. This option, when used with install/remove, can omit any packages to permit APT to deduce a likely solution.
-m --ignore-missing --fix-missingIgnore missing packages; if packages cannot be retrieved or fail the integrity check after retrieval (corrupted package files), hold back those packages and handle the result.
-a --host-architectureThis option controls the architecture packages are built for by apt-get source –compile and how cross-builddependencies are satisfied.
-s --simulate --dry-runNo action; perform a simulation of events that would occur based on the current system state but do not actually change the system.

Files 配置文件

  • /etc/apt/sources.list: Locations to fetch packages from.
  • /etc/apt/sources.list.d/: Additional source list fragments.
  • /etc/apt/apt.conf: APT configuration file.
  • /etc/apt/apt.conf.d/: APT configuration file fragments.
  • /etc/apt/preferences.d/: Directory with version preferences files. This is where you would specify “pinning”, i.e. a preference to get certain packages from a separate source or from a different version of a distribution.
  • /var/cache/apt/archives/: Storage area for retrieved package files.
  • /var/cache/apt/archives/partial/: Storage area for package files in transit.
  • /var/lib/apt/lists/: Storage area for state information for each package resource specified in sources.list
  • /var/lib/apt/lists/partial/: Storage area for state information in transit.

Debian官方文档

sources.list语法

每个软件源的定义通常使用如下三部分定义

deb url distribution component1 component2 component3 [..] componentX
deb-src url distribution component1 component2 component3 [..] componentX
  • 第一部分:deb为binary文件,deb-src为源代码文件
  • 第二部分:要写完整url,可以是file://https://ftps://等定义的资源
  • 第三部分:取决于repo的结构,最简单的情况下,是子目录路径,例如.\。对于较为复杂的repos,通常需要写入对应的发布版,”suite name”以及需要enable的组件类别,一个常见的Debian镜像源通常根据license提供以下三类组件:
    • main: 完全满足Debian Free Software Guidelines
    • non-free: 不完全满足Debian Free Software Guidelines,但是可以不受限制分发的软件。通常用于提供对特定硬件的支持。
    • contrib: 是指需要non-free软件支持才可以正常工作的一类软件,同时也包括一些使用免费,但是需要使用特定编译工具生成的软件

Stable版本的软件库

# Security updates
deb http://security.debian.org/ buster/updates main contrib non-free
deb-src http://security.debian.org/ buster/updates main contrib non-free

# Debian mirror

# Base repository
deb https://deb.debian.org/debian buster main contrib non-free
deb-src https://deb.debian.org/debian buster main contrib non-free

# Stable updates
deb https://deb.debian.org/debian buster-updates main contrib non-free
deb-src https://deb.debian.org/debian buster-updates main contrib non-free

# Stable backports
deb https://deb.debian.org/debian buster-backports main contrib non-free
deb-src https://deb.debian.org/debian buster-backports main contrib non-free

Related Reading: