pacman (正體中文)
| 摘要 |
|---|
| Pacman 是 Arch Linux 的軟體包管理員。軟體包管理員可以用來安裝、升級、移除軟體。本文章將說明 Pacman 的基本操作與一些疑難排解。 |
| Overview |
| Packages in Arch Linux are built using makepkg and a custom build script for each package (known as a PKGBUILD). Once packaged, software can be installed and managed with pacman. PKGBUILDs for software in the official repositories are available from the ABS tree; thousands more are available from the (unsupported) Arch User Repository. |
| Related |
| Downgrading Packages |
| Improve Pacman Performance |
| Pacman GUI Frontends |
| Pacman Rosetta |
| Pacman Tips |
| Pacman package signing |
| FAQ#Package Management |
| pacman-key |
| Pacnew and Pacsave Files |
| Resources |
| libalpm(3) Manual Page |
| pacman(8) Manual Page |
| pacman.conf(5) Manual Page |
| repo-add(8) Manual Page |
Pacman 軟體包管理員是 Arch Linux 的主要特色工具,結合了二進位軟體包格式和容易使用的建置系統。輕鬆管理軟體是 Pacman 的目標,無論這些軟體包是來自官方 Arch 軟體庫還是由使用者自建。
Pacman 會和主要伺服器同步軟體包清單,來保持系統處於最新狀態。在伺服端 / 客戶端的架構之下,使用者只要用簡單的指令就可以下載並安裝軟體包,並補足所有相依的軟體包。
Pacman 以 C 語言撰寫,使用 .pkg.tar.xz 軟體包格式。
設定
Pacman 的設定檔為 /etc/pacman.conf。使用者可以修改設定檔讓 pacman 符合需求。該設定檔的進一步資訊可以在 man pacman.conf 找到。
一般選項
一般選項的定義落在 [options] 區塊。閱讀 man 文件或查看預設 pacman.conf 以了解可調整的地方。
跳過特定軟體包的升級
若要取消特定軟體包的升級,可如下指定:
IgnorePkg=linux
多種軟體包的取消升級,可用空白分隔各軟體包名稱,或是加入若干 IgnorePkg 行。
跳過特定軟體群組的升級
取消軟體群組升級的作法跟單一軟體包相同:
IgnoreGroup=gnome
禁止特定檔案安裝至系統
若要永遠禁止特定目錄的安裝,將它們列在 NoExtract 後面。例如以下禁止 systemd 安裝新元件:
NoExtract=usr/lib/systemd/system/*
軟體庫
這一部分定義了要使用哪些軟體庫,如 /etc/pacman.conf 內所述。我們可以直接在檔案內宣告軟體庫的位置,或是從另一個檔案讀取(如 /etc/pacman.d/mirrorlist),這樣就只需要維護一項清單。鏡像站的設定請參閱這裡。
/etc/pacman.conf
#[testing] #SigLevel = PackageRequired #Include = /etc/pacman.d/mirrorlist [core] SigLevel = PackageRequired Include = /etc/pacman.d/mirrorlist [extra] SigLevel = PackageRequired Include = /etc/pacman.d/mirrorlist #[community-testing] #SigLevel = PackageRequired #Include = /etc/pacman.d/mirrorlist [community] SigLevel = PackageRequired Include = /etc/pacman.d/mirrorlist # If you want to run 32 bit applications on your x86_64 system, # enable the multilib repositories as required here. #[multilib-testing] #SigLevel = PackageRequired #Include = /etc/pacman.d/mirrorlist #[multilib] #SigLevel = PackageRequired #Include = /etc/pacman.d/mirrorlist # An example of a custom package repository. See the pacman manpage for # tips on creating your own repositories. #[custom] #SigLevel = Optional TrustAll #Server = file:///home/custompkgs
軟體包的安全性
Pacman 4 支援軟體包簽署,為軟體包的安全性多添加一層保障。預設選項 SigLevel = Required DatabaseOptional 啟用對所有軟體包的簽署認証:軟體庫設定值的 SigLevel 行 (如上面所示) 設定可蓋過預設選項。更多軟體包的簽署、簽章認證等相關資訊請參閱 pacman-key。
使用方式
以下將用一些簡單範例來說明 pacman 可執行的操作。man pacman 有更多的範例可供參考。若需要一份簡單的 Pacman 介紹則參閱 pacman - An Introduction。
安裝軟體包
安裝特定軟體包
安裝一或多個軟體包 (包含相依軟體):
# pacman -S 軟體包名稱1 軟體包名稱2 ...
有時候不同的軟體庫下會存放同一種軟體包,只是版本不同而已 (如 [extra] 和 [testing])。此時必須在軟體包名稱前面定義軟體庫名:
# pacman -S extra/軟體包名稱
安裝軟體群組
Pacman 可以同時安裝歸屬於同一群組下的軟體包,如下面範例指令:
# pacman -S gnome
Pacman 將提示您選擇 gnome 群組下要安裝那些軟體包。
某些軟體群組包含了數量眾多的軟體包,只有某一小部分的軟體包是您(不)要安裝的。您不需將所有需要的軟體包代碼一一輸入,用以下的語法可以快速選取某一範圍的軟體包,例如:
Enter a selection (default=all): 1-10 15
將會安裝從 1 到 10,以及 15 號軟體包,或者:
Enter a selection (default=all): ^5-8 ^2
除了 5 到 8、以及 2 號軟體包以外,其他軟體包將會被安裝。
執行以下指令查看哪些軟體包被歸類在 gnome 群組:
# pacman -Sg gnome
或者,到 https://www.archlinux.org/groups/ 網站查看有什麼軟體群組可用。
移除軟體包
移除單一軟體包,但不移除與其一同安裝的相依軟體包:
# pacman -R 軟體包名稱
移除單一軟體包,以及與其相依且不再被其他軟體需要的軟體包:
# pacman -Rs 軟體包名稱
移除單一軟體包,以及與其相依的軟體包;也一同移除其他受影響的軟體包:
# pacman -Rsc 軟體包名稱
移除單一軟體包,但不移除依賴該軟體包的其他軟體包:
# pacman -Rdd 軟體包名稱
Pacman 在移除特定應用程式時,會儲存其重要設定檔,加上副檔名 .pacsave。使用 -n 選項避免這些備份檔案被建立:
# pacman -Rn 軟體包名稱
升級軟體包
Pacman 只需一個指令即可更新系統所有軟體包。根據系統的軟體包版本狀態,這項操作會花上一段時間。本指令會同步資料庫並更新系統的軟體包 (除了不在預定軟體庫內的「本機」軟體包):
# pacman -Syu
Pacman 是相當強大的軟體包管理工具,但它並不是對付各式疑難雜症的萬靈丹。若這對您造成困擾,請回頭看看 Arch 的設計哲學是怎麼說的。使用者必須有警覺心,並負起維護自己系統的重責大任。在執行系統更新時,要做好 Arch 使用者的基本功:閱讀 Pacman 輸出的所有資訊並動動腦筋。當新軟體包的版本要更新一個被使用者修改過的設定檔時,為了避免蓋掉使用者自己的設定,會將新的預設設定檔改為 .pacnew 檔案。Pacman 會提醒使用者融合新舊兩檔。這些設定檔案需要使用者手動介入,而且最好在每次軟體包更新 / 移除之後就馬上處理。更多資訊請參閱 Pacnew and Pacsave Files。
升級系統之前,建議到 Arch Linux 首頁檢查最新消息 (或是訂閱 RSS feed、arch-announce 郵件論壇,或是追蹤 @archlinux Arch 的推特)。當出現 Pacman 無法自行處理、使用者必須介入的狀況時 (雖然這種情況很少見),我們會發送新的公告貼文。
要是您碰到的問題無法藉由以上的指示解決,記得到論壇搜尋看看,說不定也有其他人碰到跟您相同的問題,並提出解決辦法。
查詢軟體包資料庫
Pacman 使用 -Q 旗標查詢本機的軟體包資料庫;可參閱:
$ pacman -Q --help
並使用 -S 旗標查詢(軟體庫的)同步資料庫;可參閱:
$ pacman -S --help
Pacman 可以用來搜尋資料庫內記錄的軟體包,以軟體包名稱、描述來搜尋:
$ pacman -Ss 字串1 字串2 ...
搜尋已安裝的軟體包:
$ pacman -Qs 字串1 字串2 ...
顯示給定軟體包的詳細資料:
$ pacman -Si 軟體包名稱
系統內安裝軟體包的詳細資料:
$ pacman -Qi 軟體包名稱
傳入兩個 -i 旗標,會同時顯示備份檔案清單與它們的修改狀態:
$ pacman -Qii 軟體包名稱
獲取軟體包所安裝的檔案清單:
$ pacman -Ql 軟體包名稱
尚未安裝的軟體包則使用 pkgfile。
您也可以向資料庫查詢系統內某個檔案是屬於哪一項軟體包:
$ pacman -Qo 檔案路徑
根據相依性,列出所有不再需要的軟體包 (孤兒):
$ pacman -Qdt
列出軟體包的相依性樹狀圖:
$ pactree 軟體包名稱
使用 pkgtools 下的 whoneeds ,列出需要某個已安裝軟體包的所有軟體包:
$ whoneeds 軟體包名稱
其他指令
一行升級系統並安裝其他軟體包:
# pacman -Syu 軟體包名稱1 軟體包名稱2 ...
下載軟體包但不要安裝:
# pacman -Sw 軟體包名稱
安裝不是來自遠端軟體庫的「本機」軟體包 (如從 AUR 建構的軟體包):
# pacman -U /path/to/package/package_name-version.pkg.tar.xz
安裝非 Pacman 設定檔所指定軟體庫下的「遠端」軟體包:
# pacman -U http://www.example.com/repo/example.pkg.tar.xz
清理目前尚未安裝的軟體包快取 (/var/cache/pacman/pkg):
# pacman -Sc
清理整個軟體包快取:
# pacman -Scc
不支援部分升級
Arch Linux 採用無縫更新,函式庫若有新的版本就會馬上推進軟體庫。開發人員與受信任的使用者會將庫內所有與函式庫相關的軟體包都重新建構過。若系統內有本機安裝的軟體包 (像 AUR 軟體包) 碰到 soname bump 時,使用者就必須重新建構它們。
此時部分升級將不被支援。不要使用 pacman -Sy [軟體包] 或其他相似指令如 pacman -Sy 加 pacman -S [軟體包]。在安裝軟體包記得先將系統升級 -- 特別是當 Pacman 重整同步軟體庫之後。基於相同的原因,使用 IgnorePkg 和 IgnoreGroup 時也請多留意。
若在部分升級的情況下,有函式庫因為找不到其他與其連結的函式庫而發生問題的話,不要試著用軟連結「修復」問題。函式庫在未向後相容的情況下會收到soname bump。只要 Pacman 還能正常運作,一個簡單的 pacman -Syu,加上與鏡像站適當地同步就可以解決此問題。
疑難排解
Q: An update to package XYZ broke my system!
The most important tip to remember is to not "blindly" update Arch systems. Always read the list of packages to be updated. Note whether "critical" packages are going to be updated (linux, xorg-server, and so on). If so, it is usually a good idea to check for any news at https://www.archlinux.org/ and scan recent forum posts to see if people are experiencing problems as a result of an update.
If a package update is expected/known to cause problems, packagers will ensure that pacman displays an appropriate message when the package is updated. If experiencing trouble after an update, double-check pacman's output by looking at the log (/var/log/pacman.log).
Q: I know an update to package ABC was released, but pacman says my system is up to date!
Q: I get an error when updating: "file exists in filesystem"!
error: could not prepare transaction error: failed to commit transaction (conflicting files) package: /path/to/file exists in filesystem Errors occurred, no packages were upgraded.
Why this is happening: pacman has detected a file conflict, and by design, will not overwrite files for you. This is a design feature, not a flaw.
The problem is usually trivial to solve. A safe way is to first check if another package owns the file (pacman -Qo /path/to/file). If the file is owned by another package, file a bug report. If the file is not owned by another package, rename the file which 'exists in filesystem' and re-issue the update command. If all goes well, the file may then be removed.
If you had installed a program manually without using pacman or a frontend, you have to remove it and all its files and reinstall properly using pacman.
Every installed package provides /var/lib/pacman/local/$package-$version/files file that contains metadata about this package. If this file gets corrupted - is empty or missing - it results in "file exists in filesystem" errors when trying to update the package. Such an error usually concerns only one package and instead of manually renaming and later removing all the files that belong to the package in question, you can run pacman -S --force $package to force pacman to overwrite these files.
pacman -Syu --force.Q: I get an error when installing a package: "not found in sync db"
pacman -Syy to force a refresh of all package lists.Q: I get an error when installing a package: "target not found"
pacman -Syy to force a refresh of all package lists.It could also be that the repository containing the package is not enabled on your system, e.g. the package could be in the multilib repository, but multilib is not enabled on your system.
Q: Pacman is repeatedly upgrading the same package!
/var/lib/pacman/local/, such as two linux instances. pacman -Qi outputs the correct version, but pacman -Qu recognizes the old version and therefore will attempt to upgrade.
Solution: delete the offending entry in /var/lib/pacman/local/.
Q: Pacman crashes during an upgrade!
- Boot using the Arch install media.
- Mount your root filesystem.
- Update the pacman database via
pacman -Syy. - Reinstall the broken package via
pacman -r /path/to/root -S package.
Q: I installed software using "make install"; these files do not belong to any package!
--force switch (pacman -S --force). See Pacman Tips#Identify files not owned by any package for a script that searches the file system for disowned files.
Q: I need a package with a specific file. How do I know what provides it?
Q: Pacman is completely broken! How do I reinstall it?
Q: After updating my system, I get a "unable to find root device" error after rebooting and my system will no longer boot
--force option can be a cause). You have two options:
1. Try the Fallback entry.
- Once the system starts, run this command (for the stock linux kernel) either from the console or from a terminal to rebuild the initramfs image:
-
# mkinitcpio -p linux
2. If that does not work, from a 2012 Arch release (CD/DVD or USB stick), run:
-
# mount /dev/sdxY /mnt #Your root partition. # mount /dev/sdxZ /mnt/boot #If you use a separate /boot partition. # arch-chroot /mnt # pacman -Syu mkinitcpio systemd linux
- Reinstalling the kernel (the linux package) will automatically re-generate the initramfs image with
mkinitcpio -p linux. There is no need to do this separately.
- Afterwards, it is recommended that you run
exit,umount /mnt/{boot,}andreboot.
Q: Signature from "User <email@gmail.com>" is unknown trust, installation failed
pacman -S archlinux-keyring.Q: I keep getting
- error: PackageName: signature from "User <email@archlinux.org>" is invalid
- error: failed to commit transaction (invalid or corrupted package (PGP signature))
- Errors occured, no packages were upgraded.
# hwclock -wbefore to try to install/upgrade a package again.
Q: I keep getting a "failed to commit transaction (invalid or corrupted package)" error
*.part files (partially downloaded packages) in /var/cache/pacman/pkg and remove them (often caused by usage of custom XferCommand in pacman.conf).Q: I get an error every time I use pacman saying 'warning: current locale is invalid; using default "C" locale'. What do I do?
Q: How can I get Pacman to honor my proxy settings?
$http_proxy, $ftp_proxy etc.) are set up. If you use Pacman with sudo, you need to configure sudo to pass these environment variables to Pacman.