pacman (正體中文)

From ArchWiki
Jump to: navigation, search


Tango-preferences-desktop-locale.png This article or section needs to be translated.
Notes: 仍有部分章節尚未翻譯 (Discuss)
Tango-preferences-desktop-locale.png
摘要
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 已提供了不少好用工具 (如makepkg, pactree, vercmp): 執行 pacman -Ql pacman | grep bin 可查看完整清單。

Contents

設定

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
警告: 使用 [testing] 軟體庫的人請留意!該庫的軟體處於密集開發的狀態,隨意更新可能有讓部分軟體無法運作的風險。我們鼓勵使用 [testing] 軟體庫的人訂閱 arch-dev-public 郵件論壇來了解目前開發的資訊。

軟體包的安全性

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/ 網站查看有什麼軟體群組可用。

註記: 當指定安裝一項軟體包時,就算該軟體包已被安裝過,也還是會重新安裝一次。可以利用 --needed 選項略過已安裝且為最新版本的軟體包。
警告: 在系統尚未升級過的狀況下安裝軟體包時,千萬不要重新整理軟體包清單 (即 pacman -Sy 軟體包名稱);這可能會導致相依性出現問題。請參閱#不支援部分升級https://bbs.archlinux.org/viewtopic.php?id=89328。

移除軟體包

移除單一軟體包,但不移除與其一同安裝的相依軟體包:

# pacman -R 軟體包名稱

移除單一軟體包,以及與其相依且不再被其他軟體需要的軟體包:

# pacman -Rs 軟體包名稱

移除單一軟體包,以及與其相依的軟體包;也一同移除其他受影響的軟體包:

警告: 這項操作具連帶性,進行之前必須確認不會刪掉您仍然需要的軟體包。
# pacman -Rsc 軟體包名稱

移除單一軟體包,但不移除依賴該軟體包的其他軟體包:

# pacman -Rdd 軟體包名稱

Pacman 在移除特定應用程式時,會儲存其重要設定檔,加上副檔名 .pacsave。使用 -n 選項避免這些備份檔案被建立:

# pacman -Rn 軟體包名稱
註記: Pacman 不會移除應用程式自己建立的設定檔 (例如家目錄下的「隱藏檔案」)。

升級軟體包

Pacman 只需一個指令即可更新系統所有軟體包。根據系統的軟體包版本狀態,這項操作會花上一段時間。本指令會同步資料庫更新系統的軟體包 (除了不在預定軟體庫內的「本機」軟體包):

# pacman -Syu
警告: 並不是每當更新可用時就得馬上更新。使用者必須瞭解,基於 Arch 的「無縫更新」特性,每一次大更新都可能會導向無法預期的結果。打個比方而言,在您準備一場重要的演說前一刻更新系統是相當不智的行為。最好在閒暇時刻進行更新,並準備應付更新可能引發的任何問題。

Pacman 是相當強大的軟體包管理工具,但它並不是對付各式疑難雜症的萬靈丹。若這對您造成困擾,請回頭看看 Arch 的設計哲學是怎麼說的。使用者必須有警覺心,並負起維護自己系統的重責大任。在執行系統更新時,要做好 Arch 使用者的基本功:閱讀 Pacman 輸出的所有資訊並動動腦筋。當新軟體包的版本要更新一個被使用者修改過的設定檔時,為了避免蓋掉使用者自己的設定,會將新的預設設定檔改為 .pacnew 檔案。Pacman 會提醒使用者融合新舊兩檔。這些設定檔案需要使用者手動介入,而且最好在每次軟體包更新 / 移除之後就馬上處理。更多資訊請參閱 Pacnew and Pacsave Files

提示: Pacman 的輸出訊息會記錄在 /var/log/pacman.log

升級系統之前,建議到 Arch Linux 首頁檢查最新消息 (或是訂閱 RSS feedarch-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 file://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

清理整個軟體包快取:

警告: 這將會清掉整個軟體包快取。這不是一個好動作;這樣會失去直接從快取資料夾降級軟體包的機會。使用者會被迫使用毀損軟體包的替代來源,例如 Arch Rollback Machine
# pacman -Scc
提示: 除了使用 -Sc-Scc 這兩個選項以外,也可以考慮使用 pacman 下的 paccache。此工具提供更多樣的控制,像是要刪除什麼軟體包快取、或刪除多少軟體包快取。執行 paccache -h 可獲得更多指示。

不支援部分升級

Arch Linux 採用無縫更新,函式庫若有新的版本就會馬上推進軟體庫。開發人員與受信任的使用者會將庫內所有與函式庫相關的軟體包都重新建構過。若系統內有本機安裝的軟體包 (像 AUR 軟體包) 碰到 soname bump 時,使用者就必須重新建構它們。

此時部分升級將不被支援。不要使用 pacman -Sy [軟體包] 或其他相似指令如 pacman -Sypacman -S [軟體包]。在安裝軟體包記得先將系統升級 -- 特別是當 Pacman 重整同步軟體庫之後。基於相同的原因,使用 IgnorePkgIgnoreGroup 時也請多留意。

若在部分升級的情況下,有函式庫因為找不到其他與其連結的函式庫而發生問題的話,不要試著用軟連結「修復」問題。函式庫在未向後相容的情況下會收到soname bump。只要 Pacman 還能正常運作,一個簡單的 pacman -Syu,加上與鏡像站適當地同步就可以解決此問題。

疑難排解

Q: An update to package XYZ broke my system!

A: Arch Linux is a rolling-release cutting-edge distribution. Package updates are available as soon as they are deemed stable enough for general use. However, updates sometimes require user intervention: configuration files may need to be updated, optional dependencies may change, etc.

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).

At this point, only after ensuring there is no information available through pacman, there is no relative news on https://www.archlinux.org/, and there are no forum posts regarding the update, consider seeking help on the forum, over IRC, or downgrading the offending package.

Q: I know an update to package ABC was released, but pacman says my system is up to date!

A: Pacman mirrors are not synced immediately. It may take over 24 hours before an update is available to you. The only options are be patient or use another mirror. MirrorStatus can help you identify an up-to-date mirror.

Q: I get an error when updating: "file exists in filesystem"!

A: ASIDE: Taken from https://bbs.archlinux.org/viewtopic.php?id=56373 by Misfit138.
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.

Do not run pacman -Syu --force.

Q: I get an error when installing a package: "not found in sync db"

A: Firstly, ensure the package actually exists (and watch out for typos!). If certain the package exists, your package list may be out-of-date or your repositories may be incorrectly configured. Try running pacman -Syy to force a refresh of all package lists.

Q: I get an error when installing a package: "target not found"

A: Firstly, ensure the package actually exists (and watch out for typos!). If certain the package exists, your package list may be out-of-date or your repositories may be incorrectly configured. Try running 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!

A: This is due to duplicate entries in /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/.

Note: Pacman version 3.4 should display an error in case of duplicate entries, which should make this note obsolete.

Q: Pacman crashes during an upgrade!

A: In the case that pacman crashes with a "database write" error whilst removing packages, and reinstalling or upgrading packages fails:
  1. Boot using the Arch install media.
  2. Mount your root filesystem.
  3. Update the pacman database via pacman -Syy.
  4. 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!

A: If receiving a "conflicting files" error, note that pacman will overwrite manually-installed software if supplied with the --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.
Warning: Take care when using the --force switch because it can cause major problems if used improperly.

Q: I need a package with a specific file. How do I know what provides it?

A: Install pkgfile which uses a separate database with all files and their associated packages.

Q: Pacman is completely broken! How do I reinstall it?

A: In the case that pacman is broken beyond repair, manually download the necessary packages (openssl, libarchive, and pacman) and extract them to root. The pacman binary will be restored along with its default configuration file. Afterwards, reinstall these packages with pacman to maintain package database integrity. Additional information and an example (outdated) script that automates the process is available in this forum post.

Q: After updating my system, I get a "unable to find root device" error after rebooting and my system will no longer boot

A: Most likely your initramfs got broken during a kernel update (improper use of pacman's --force option can be a cause). You have two options:

1. Try the Fallback entry.

Tip: In case you removed this entry for whatever reason, you can always press the Tab key when the bootloader menu shows up (for Syslinux) or e (for GRUB), rename it initramfs-linux-fallback.img and press Enter or b (depending on your bootloader) to boot with the new parameters.
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:

Note: If you do not have a 2012 release or if you only have some other "live" Linux distribution laying around, you can chroot using the old fashion way. Obviously, there will be more typing than simply running the arch-chroot script.
# 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,} and reboot.
Note: If you cannot enter the arch-chroot or chroot environment but need to re-install packages you can use the command pacman -r /mnt -Syu foo bar to use pacman on your root partition.

Q: Signature from "User <email@gmail.com>" is unknown trust, installation failed

A: Follow pacman-key#Resetting all the keys. Or you can try to manually upgrade archlinux-keyring package first, i.e. 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.

A: It happens when the system clock is wrong. Set the time and run:
# hwclock -w
before to try to install/upgrade a package again.

Q: I keep getting a "failed to commit transaction (invalid or corrupted package)" error

A: Look for *.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?

A: As the error message says, your locale is not correctly configured. See Locale.

Q: How can I get Pacman to honor my proxy settings?

A: Make sure that the relevant environment variables ($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.

另請參閱