如何在Linux中使用Apt-Get命令?


高階軟體包工具 (APT) 是一款功能強大的命令列工具,用於基於 Debian 的系統(如 Ubuntu、Linux Mint 等)。apt-get 命令是與 APT 互動最常用的方法之一。它用於處理軟體包,允許您安裝、升級和刪除 Linux 系統上的軟體。

在本指南中,我們將引導您瞭解 apt-get 命令的基礎知識,並附帶示例及其輸出。

1. 更新軟體包列表:apt-get update

您應該瞭解的第一個命令是 apt-get update。此命令檢索有關最新版本的軟體包及其依賴項的資訊。它不會安裝或升級任何軟體包,但會更新軟體包列表以進行升級和新軟體包安裝。

示例

sudo apt−get update

輸出

Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://archive.ubuntu.com/ubuntu bionic−updates InRelease [88.7 kB]
...
Reading package lists... Done

2. 升級軟體包:apt-get upgrade

更新軟體包列表後,您可以使用 apt-get upgrade 升級已安裝的軟體包。此命令將安裝系統上當前安裝的所有軟體包的最新版本。

示例

sudo apt−get upgrade

輸出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
   libssl1.1 openssl
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,374 kB of archives.
After this operation, 1,024 B of additional disk space will be used.
Do you want to continue? [Y/n]

3. 安裝軟體包:apt-get install

要安裝新軟體包,請使用 apt-get install 命令,後跟軟體包名稱。

示例

sudo apt−get install firefox

輸出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
   firefox−locale−en
Suggested packages:
   fonts−lyx
The following NEW packages will be installed:
   firefox firefox−locale−en
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 49.4 MB of archives.
After this operation, 182 MB of additional disk space will be used.
Do you want to continue? [Y/n]

4. 刪除軟體包:apt-get remove

如果要刪除軟體包但保留其配置檔案,請使用 apt-get remove 命令。

示例

sudo apt−get remove firefox

輸出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be removed:
   firefox firefox−locale−en
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 182 MB disk space will be freed.
Do you want to continue? [Y/n]

5. 清除軟體包:apt-get purge

如果要刪除軟體包及其配置檔案,請使用 apt-get purge 命令。

示例

sudo apt−get purge firefox

輸出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be removed:
   firefox* firefox−locale−en*
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 182 MB disk space will be freed.
Do you want to continue? [Y/n]

6. 自動移除:apt-get autoremove

安裝軟體包時,它可能依賴於其他軟體包。刪除軟體包後,其依賴項可能不再需要。要刪除這些不必要的軟體包,可以使用 apt-get autoremove 命令。

示例

sudo apt−get autoremove

輸出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
   libfreetype6 libjpeg−turbo8 libjpeg8 libjbig0 libtiff5
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 1,525 kB disk space will be freed.
Do you want to continue? [Y/n]

7. 清理軟體包快取:apt-get clean

apt-get clean 命令用於透過清理本地儲存庫中檢索到的 .deb 檔案來釋放空間。

示例

sudo apt−get clean

輸出

// No output, but .deb files are removed from /var/cache/apt/archives/

8. 安裝軟體包的特定版本:apt-get install package=version

有時,您可能需要安裝軟體包的特定版本。可以透過在 apt-get install 命令中的軟體包名稱後附加 =version 來實現。

示例

sudo apt−get install apache2=2.4.29−1ubuntu4.14

輸出

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
   apache2−bin apache2−data apache2−utils libapr1 libaprutil1 libaprutil1−dbd−sqlite3 libaprutil1−ldap liblua5.2−0
Suggested packages:
   www−browser apache2−doc apache2−suexec−pristine | apache2−suexec−custom
The following NEW packages will be installed:
   apache2 apache2−bin apache2−data apache2−utils libapr1 libaprutil1 libaprutil1−dbd−sqlite3 libaprutil1−ldap liblua5.2−0
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,358 kB of archives.
After this operation, 5,353 kB of additional disk space will be used.
Do you want to continue? [Y/n]

9. 下載軟體包而不安裝:apt-get download

如果要下載 .deb 軟體包而不安裝它,可以使用 apt-get download 命令。

示例

apt−get download apache2

輸出

Get:1 http://archive.ubuntu.com/ubuntu bionic−updates/main amd64 apache2 amd64 2.4.29−1ubuntu4.14 [95.1 kB]
Fetched 95.1 kB in 1s (67.8 kB/s)

10. 檢查是否安裝了軟體包:dpkg -l

雖然不是 apt-get 命令,但 dpkg -l 通常與 apt-get 結合使用以檢查是否安裝了軟體包。

示例

dpkg −l | grep apache2

輸出

ii  apache2       2.4.29−1ubuntu4.14  amd64  Apache HTTP Server
ii  apache2−bin   2.4.29−1ubuntu4.14  amd64  Apache HTTP Server (modules and other binary files)
ii  apache2−data  2.4.29−1ubuntu4.14  all    Apache HTTP Server (common files)

11. 列出所有已安裝的軟體包:dpkg --get-selections

同樣,雖然不是 apt-get 命令,但 dpkg --get-selections 是列出所有已安裝軟體包的有用命令。

示例

dpkg −−get−selections

輸出

adduser                                         install
apache2                                         install
apache2−bin                                     install
apache2−data                                    install
apt                                             install
...

請記住,apt-get 命令是您 Linux 工具庫中的強大工具。始終謹慎使用它,您會發現它使系統管理變得容易得多。

結論

apt-get 命令是用於管理 Linux 系統上軟體包的強大工具。使用它,您可以輕鬆安裝、升級和刪除軟體。請記住,在每個命令之前使用 sudo 以確保您擁有必要的許可權。刪除軟體包時務必小心,以免意外刪除重要內容。祝您 Linux 使用愉快!

更新於:2023年7月13日

4K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告