National Taiwan University
Uedu Main Site
Explore Uedu
Student Console
Register as Member/Login
Research Informed Consent Center
Survey Center
Educational Big Data Challenge
Claim or create a course
Try a Scenario Task
Support & Messages
Uptime Data

UeduGPTs

--

Jupyters

2

Local AI

--

Uedu Code

--

台灣大學 AQI 70 24°C PM2.5 17
AI Reply Desktop Notifications

Show a desktop notification when the AI TA finishes replying

Chat Message Notifications

Notify me when classmates post messages in the forum

Sound notification

Play an alert sound whenever there is a new notification

Unit 19

軟體管理 APT

APT 設定檔、dpkg 與 apt 指令
Interaction 20 minute(s)

Learning Objectives

  • 說明套件、套件庫與相依性,看懂 Ubuntu 24.04 的套件來源設定檔 /etc/apt/sources.list.d/ubuntu.sources
  • 分辨 apt update 與 apt upgrade,用 apt 搜尋、查看、安裝、移除套件,用 apt-cache policy 查版本並安裝指定版本
  • 用 dpkg 列出已安裝的套件、套件的檔案、檔案屬於哪個套件,以及安裝與移除 .deb 檔
  • 用 apt download 下載 .deb,再用 dpkg -i 或 apt install ./檔名 安裝;說明 apt 與 apt-get 的差別

套件、套件庫與相依性

在 Windows 上裝軟體,通常是到官網下載安裝程式、一路按「下一步」。Ubuntu 的做法不同:

名詞 意思
套件(package) 打包好的軟體,Ubuntu 用的格式是 .deb,裡面有程式、設定檔、說明文件,以及「我需要哪些其他套件」的資訊
套件庫(repository) 存放大量套件的伺服器,Ubuntu 官方維護 archive.ubuntu.com、security.ubuntu.com 等
相依性(dependency) 套件 A 需要套件 B 才能執行;安裝 A 時要一起裝 B

Ubuntu 有兩層工具:

  • dpkg:處理「一個 .deb 檔」,負責解開、安裝、移除、查詢;不會自動處理相依性,也不會上網下載
  • APT(Advanced Package Tool,指令是 apt、apt-get、apt-cache):從套件庫下載套件,自動找出並安裝相依的套件,再交給 dpkg 安裝

投影片的範例以 root 執行。Ubuntu 預設用一般使用者登入,安裝、移除、更新套件都會改動系統,要在指令前加 sudo; 搜尋與查詢(apt search、apt show、dpkg -l)不需要。

套件來源設定檔

APT 從哪些套件庫下載,寫在套件來源設定檔裡。Ubuntu 24.04 起改用 deb822 格式的 /etc/apt/sources.list.d/ubuntu.sources, 舊的 /etc/apt/sources.list 只剩一行註解:

$ cat /etc/apt/sources.list
# Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources
$ cat /etc/apt/sources.list.d/ubuntu.sources
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

每一段(中間空一行隔開)是一組來源。

欄位 意思
Types deb 是編譯好的套件;deb-src 是原始碼(apt source 需要)
URIs 套件庫的網址,可以換成離你比較近的鏡像站
Suites 版本代號:noble 是 24.04 發行時的套件,noble-updates 是之後的更新,noble-security 是安全性更新
Components main(Canonical 支援的自由軟體)、restricted(硬體驅動等非自由軟體)、universe(社群維護)、multiverse(有授權限制的軟體)
Signed-By 驗證套件庫簽章用的金鑰,確認下載的套件沒有被竄改

新增套件來源(投影片 Lab01):在 /etc/apt/sources.list.d/ 另外建立一個 .sources 檔,寫上同樣格式的欄位,再執行 sudo apt update。 舊式的一行格式(deb 網址 版本代號 元件)寫在 .list 檔裡仍然可以用,很多第三方軟體的安裝說明還是這種寫法。

其他相關檔案:

  • /var/log/apt/history.log:每次 apt 安裝、移除了什麼;/var/log/apt/term.log 是當時終端機的完整輸出;/var/log/dpkg.log 是 dpkg 的紀錄
  • 學校或公司要求透過 Proxy 上網時,在 /etc/apt/apt.conf.d/ 新增一個設定檔,寫入 Acquire::http::Proxy "http://proxy.example.com:3128";(https 用 Acquire::https::Proxy)

apt 指令

指令 作用
sudo apt update 更新套件清單:向套件庫下載最新的「有哪些套件、哪些版本」,不會安裝任何東西
sudo apt upgrade 升級已安裝的套件到清單上的最新版本;要先 update,否則清單是舊的,看不到新版本
apt search 關鍵字 搜尋套件名稱與說明
apt show 套件 顯示套件的版本、大小、相依性與說明
sudo apt install 套件 安裝套件與它相依的套件
apt-cache policy 套件 列出已安裝的版本(Installed)、會安裝的版本(Candidate)與可用的版本
sudo apt install 套件=版本 安裝指定的版本,例如 sudo apt install hello=2.10-3build2
sudo apt remove 套件 移除套件,保留設定檔
sudo apt purge 套件 移除套件連同設定檔
sudo apt autoremove 移除當初因相依性自動裝進來、現在已經沒有套件需要的套件
sudo apt clean 清除下載過的 .deb 安裝檔(放在 /var/cache/apt/archives/)
apt download 套件 只下載 .deb 到目前目錄,不安裝

常見的組合是 sudo apt update && sudo apt upgrade:先更新清單、再升級。

沒加 sudo 就安裝,會看到:

$ apt install cowsay
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

are you root? 就是提醒你這個動作需要管理權限。

互動練習:用 apt 安裝與移除套件

對應投影片 Lab03。先看套件來源設定檔,再依任務更新清單、搜尋並安裝 cowsay(一隻會說話的牛)、查詢 hello 的版本並安裝指定版本,最後升級、移除與清理。每一顆按鈕都是真的指令;有一顆故意沒加 sudo,看看錯誤訊息。

dpkg:查詢與處理 .deb 檔

指令 作用
dpkg -l 列出所有已安裝的套件;開頭 ii 表示「要安裝、已安裝」
dpkg -l 套件 只看某個套件的版本
dpkg -L 套件 列出這個套件安裝了哪些檔案
dpkg -S 檔案路徑 反過來查:這個檔案屬於哪個套件
sudo dpkg -i 檔名.deb 安裝一個 .deb 檔
sudo dpkg -r 套件 移除套件,保留設定檔
sudo dpkg -P 套件 移除套件連同設定檔(purge)

dpkg -i 有兩種常見情況:

  1. dpkg -i 只認得你給的那一個檔案,缺少相依套件時會安裝失敗,並說明缺了什麼;這時執行 sudo apt install -f 讓 APT 補上
  2. 改用 sudo apt install ./檔名.deb 安裝本機的 .deb:APT 會自動處理相依性。注意要寫 ./,沒有路徑時 apt 會把它當成套件名稱去套件庫找

投影片提到的 dpkg --unpack 與 dpkg --configure 是把安裝拆成「解開」與「設定」兩步,平常用 -i 一次完成即可。

apt 與 apt-get

apt apt-get、apt-cache
對象 人在終端機裡操作 寫在腳本裡
特色 有進度條與顏色,把常用功能整合成一個指令(apt search、apt show) 輸出格式穩定,不同版本之間不容易變
注意 輸出被導到管線或檔案時,會警告 apt does not have a stable CLI interface. Use with caution in scripts. 搜尋與查詢在 apt-cache(apt-cache search、apt-cache policy)

日常操作用 apt,寫自動化腳本用 apt-get。

互動練習:下載 .deb 並用 dpkg 安裝

對應投影片 Lab02。用 dpkg 查詢已安裝的套件,再用 apt download 把 hello 的 .deb 檔下載到家目錄,用 dpkg -i 安裝(先不加 sudo 試一次)、查詢它安裝了哪些檔案,移除後改用 apt install ./檔名 再裝一次。左邊的資料夾樹會出現下載的 .deb 檔。

想一想:apt

1.

sudo apt update 與 sudo apt upgrade 的差別是?

2.

想把 nginx 連同它的設定檔一起移除,應該用?

3.

Ubuntu 24.04 的官方套件來源設定寫在哪裡?

第 1 次作答

想一想:dpkg 與 .deb 檔

1.

想知道 /usr/bin/hello 這個檔案是哪個套件安裝的,應該用?

2.

下載了 tool_1.0_amd64.deb,它還需要其他套件。用哪一個指令安裝最省事?

3.

寫一個自動安裝套件的 shell 腳本,比較建議用哪一個指令?

第 1 次作答

重點精華回顧

  • 套件是打包好的軟體(.deb),套件庫存放大量套件;APT 從套件庫下載並自動處理相依性,dpkg 處理單一個 .deb 檔。
  • Ubuntu 24.04 的套件來源在 /etc/apt/sources.list.d/ubuntu.sources(deb822 格式);新增來源就在同一個目錄加一個 .sources 檔。
  • sudo apt update 更新清單、sudo apt upgrade 升級;install、remove(留設定檔)、purge(連設定檔)、autoremove、clean。
  • apt search、apt show、apt-cache policy 查詢;套件=版本 安裝指定版本。
  • dpkg -l 列出、-L 看套件的檔案、-S 查檔案屬於哪個套件、-i 安裝、-r 移除、-P 連設定檔移除。
  • apt download 下載 .deb,用 sudo apt install ./檔名.deb 安裝可以自動補上相依套件。
  • 改動系統的操作要加 sudo;互動操作用 apt,腳本用 apt-get。
這一課完成了!
Next lesson:網路設定
你目前沒有登入,學習進度只存在這個瀏覽器。 Login 後,進度會跟著帳號,換電腦也看得到。