apt 使用

November 25, 2018

apt

apt 是 Debian 和 Ubuntu 系統中的核心工具。可以使用 apt 指令安裝或刪除應用程式。

應用

因為我在學校有負責管理 bit 系統,因為當時交接給我的人得知的資訊很少,當時也想了解系統上所安裝的應用程式和依賴的應用程式,因此對 apt 指令的應用做了一個筆記。 交接時文件很重要,這會讓下一個交接者加速了解環境。

取得更新(update)

$ sudo apt update
Hit:1 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 https://download.docker.com/linux/ubuntu xenial InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Fetched 325 kB in 4s (66.5 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
163 packages can be upgraded. Run 'apt list --upgradable' to see them.

安全更新(upgrade)

升級系統上當前安裝的所有應用程式

$ sudo apt upgrade -y

查看可在系統上升級的應用程式(upgradable)

$ apt list --upgradable

完整的系統升級(full-upgrade)

如:ubuntu 16.04 安裝至 ubuntu 16.04.1 升級過程中會移除當前的應用程式。

$ sudo apt full-upgrade -y

在這過程中,也不能安裝應用程式

安裝軟體包(install)

$ sudo apt install [nginx] -y

刪除軟體包(remove)

除了系統上的配置檔案外,將刪除所有檔案。

$ sudo apt remove [nginx] -y
$ tree /var/www/
/var/www/
└── html
    └── index.nginx-debian.html

1 directory, 1 file
$ tree /etc/nginx/
/etc/nginx/
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── snippets
│   ├── fastcgi-php.conf
│   └── snakeoil.conf
├── uwsgi_params
└── win-utf

4 directories, 14 files

刪除應用程式和配置文件(purge)

刪除應用程式會刪除所有應用程式的數據,但通常會留下修改的用戶配置檔案。 誤刪的話,將刪除的應用程式安裝回來即可。

$ sudo apt purge [nginx*]  -y
...
Purging configuration files for nginx-common (1.10.3-0ubuntu0.16.04.3) ...
dpkg: warning: while removing nginx-common, directory '/var/www/html' not empty so not removed
...

AUTOREMOVE

用於刪除自動安裝的應用程式以滿足其他應用程式的依賴關係,現在不再需要這些應用程式,因為依賴關係已更改或者同時刪除了需要它們的應用程式。

$ sudo apt autoremove [nginx*] -y

搜尋軟體(search)

$ sudo apt search [mariadb*]

查找關於軟體的訊息(show)

$ apt show [apache2]
Package: apache2
Version: 2.4.18-2ubuntu3.9
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Apache Maintainers <debian-apache@lists.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 502 kB
Provides: httpd, httpd-cgi
Pre-Depends: dpkg (>= 1.17.14)
Depends: lsb-base, procps, perl, mime-support, apache2-bin (= 2.4.18-2ubuntu3.9), apache2-utils (>= 2.4), apache2-data (= 2.4.18-2ubuntu3.9)
Recommends: ssl-cert
Suggests: www-browser, apache2-doc, apache2-suexec-pristine | apache2-suexec-custom, ufw
Conflicts: apache2.2-bin, apache2.2-common
Replaces: apache2.2-bin, apache2.2-common
Homepage: http://httpd.apache.org/
Task: lamp-server, mythbuntu-frontend, mythbuntu-desktop, mythbuntu-backend-slave, mythbuntu-backend-master, mythbuntu-backend-master
Supported: 5y
Download-Size: 86.6 kB
APT-Sources: http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages
Description: Apache HTTP Server
 The Apache HTTP Server Project's goal is to build a secure, efficient and
 extensible HTTP server as standards-compliant open source software. The
 result has long been the number one web server on the Internet.
 .
 Installing this package results in a full installation, including the
 configuration files, init scripts and support scripts.

N: There are 2 additional records. Please use the '-a' switch to see them.

列出應用程式(list)

通常會配合 grep 並找自己想要的資訊

$ apt list | grep php7

列出所有安裝的應用程式(installed)

可以配合 grep 看是否有安裝過軟體

$ apt list --installed | grep nginx

列出應用程式依賴的應用程式(depends)

$ apt depends [nginx]
nginx
 |Depends: nginx-core (>= 1.10.3-0ubuntu0.16.04.3)
 |Depends: nginx-full (>= 1.10.3-0ubuntu0.16.04.3)
 |Depends: nginx-light (>= 1.10.3-0ubuntu0.16.04.3)
  Depends: nginx-extras (>= 1.10.3-0ubuntu0.16.04.3)
 |Depends: nginx-core (<< 1.10.3-0ubuntu0.16.04.3.1~)
 |Depends: nginx-full (<< 1.10.3-0ubuntu0.16.04.3.1~)
 |Depends: nginx-light (<< 1.10.3-0ubuntu0.16.04.3.1~)
  Depends: nginx-extras (<< 1.10.3-0ubuntu0.16.04.3.1~)

禁止應用程式升級(hold)

表示禁止升級除非取消(unhold)

$ apt hold [nginx] // 禁止
$ apt unhold [nginx] // 取消

ubuntu 16 前後的指令差異

apt ubuntu16 之後;apt-get ubuntu16 之前。差異其實不會很大…

aptapt-get
apt installapt-get install
apt removeapt-get remove
apt purgeapt-get purge
apt searchapt-cache search
apt showapt-cache show