0%

Git for Windows 安装 pacman 包管理器

Git for WindowsGit 官方的 Windows 版本,它是基于 MSYS2-Mingw-w64 的,所以 Git for Windows 也是一个微型的 MSYS2

但是 Git for Windows 安装后却没有 MSYS2 的包管理器 pacman,可以考虑手动安装。

网上大部分博客的描述都已经过时,有的需要去外网下载依赖。折腾之后记录一种简易的安装方式。

此方案问题太多,不推荐。建议反过来,装一个 MSYS2 环境之后再安装 git,更丝滑。
传送门:MinGW-64 & MSYS2 指北

下载软件包

pacman

去到 MSYS2 官网下载 pacman
主页:https://packages.msys2.org/package/pacman
地址:https://mirror.msys2.org/msys/x86_64/pacman-6.1.0-4-x86_64.pkg.tar.zst
镜像:https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/x86_64/pacman-6.1.0-4-x86_64.pkg.tar.zst

依赖

pacman 页面介绍中发现需要许多依赖:

  • base
  • base-devel
  • pacman-contrib
  • pactoys
  • pacutils
  • pacutils-devel
  • pkgfile
  • python-pyalpm (make)

不过其中大部分的依赖都已经存在了,只需要补充两个必选的子依赖:

安装

将上面的三个软件包下载到本地,然后需要进行“安装”。

使用压缩软件打开上面的三个压缩软件包,将其中的文件夹解压到 Git for Windows 的安装目录下(比如我的安装目录是:C:\Program Files\Git\)。

可以使用 7-ZipBandizip 等支持 zstd 压缩格式的软件。

正常情况完成操作后打开 Git bash,可以运行 pacman -Sy 命令,并且会报错:

$ pacman -Sy
warning: Public keyring not found; have you run 'pacman-key --init'?
error: clangarm64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: mingw32: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: mingw64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: ucrt64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: clang32: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: clang64: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: msys: key "5F944B027F7FE2091985AA2EFA11531AA0AA7F57" is unknown
error: keyring is not writable
error: database 'clangarm64' is not valid (invalid or corrupted database (PGP signature))
error: database 'mingw32' is not valid (invalid or corrupted database (PGP signature))
error: database 'mingw64' is not valid (invalid or corrupted database (PGP signature))
error: database 'ucrt64' is not valid (invalid or corrupted database (PGP signature))
error: database 'clang32' is not valid (invalid or corrupted database (PGP signature))
error: database 'clang64' is not valid (invalid or corrupted database (PGP signature))
error: database 'msys' is not valid (invalid or corrupted database (PGP signature))

初始化

错误提示提示密钥库没有初始化,需要初始化密钥库。

以管理员身份打开 Git bash,运行以下命令完成密钥库初始化:

  1. pacman-db-upgrad
    $ pacman-db-upgrade
    
  2. pacman-key --init
    $ pacman-key --init
    gpg: /etc/pacman.d/gnupg/trustdb.gpg: trustdb created
    gpg: no ultimately trusted keys found
    gpg: starting migration from earlier GnuPG versions
    gpg: porting secret keys from '/etc/pacman.d/gnupg/secring.gpg' to gpg-agent
    gpg: migration succeeded
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
    gpg: Generating pacman keyring master key...
    gpg: directory '/etc/pacman.d/gnupg/openpgp-revocs.d' created
    gpg: revocation certificate stored as '/etc/pacman.d/gnupg/openpgp-revocs.d/    4B00C615A704DD5CD68B86F457741B3C5937E708.rev'
    gpg: Done
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
    gpg: marginals needed: 3  completes needed: 1  trust model: pgp
    gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
    
  3. pacman-key --populate msys2
    $ pacman-key --populate msys2
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
      -> Locally signed 5 keys.
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
    gpg: setting ownertrust to 4
    gpg: setting ownertrust to 4
    gpg: setting ownertrust to 4
    gpg: setting ownertrust to 4
    gpg: setting ownertrust to 4
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
      -> Disabled 4 keys.
    /usr/bin/pacman-key: line 631: /usr/bin/gettext: No such file or directory
    ==>
    gpg: marginals needed: 3  completes needed: 1  trust model: pgp
    gpg: depth: 0  valid:   1  signed:   5  trust: 0-, 0q, 0n, 0m, 0f, 1u
    gpg: depth: 1  valid:   5  signed:   7  trust: 0-, 0q, 0n, 5m, 0f, 0u
    gpg: depth: 2  valid:   4  signed:   2  trust: 4-, 0q, 0n, 0m, 0f, 0u
    gpg: next trustdb check due at 2024-10-07
    

然后就可以使用 pacman 命令了。

问题记录

权限问题

需要注意:pacman 命令会读写 Git for Windows 的安装目录,操作这个目录有可能是需要使用管理员权限的,所以需要以管理员身份打开 Git bash

这是没有给管理员权限时候的报错:

$ pacman -Syu
:: Synchronizing package databases...
error: failed to synchronize all databases (unable to lock database)

这是正常情况下的执行结果:

$ pacman -Syu
:: Synchronizing package databases...
 clangarm64 is up to date
 mingw32 is up to date
 mingw64 is up to date
 ucrt64 is up to date
 clang32 is up to date
 clang64 is up to date
 msys is up to date
:: Starting core system upgrade...
 there is nothing to do
:: Starting full system upgrade...
 there is nothing to do

报错文件存在

由于 Git for Windows 自带的软件程序文件不是经过 pacman 安装的,所以后续安装软件的时候,尝尝会因为软件包覆盖安装报错文件已经存在:

$ echo "Y" | pacman -S bash
resolving dependencies...
looking for conflicting packages...

Packages (1) bash-5.2.026-1

Total Installed Size:  11.73 MiB

:: Proceed with installation? [Y/n] Y
(1/1) checking keys in keyring                     [#####################] 100%
(1/1) checking package integrity                   [#####################] 100%
(1/1) loading package files                        [#####################] 100%
(1/1) checking for file conflicts                  [#####################] 100%
error: failed to commit transaction (conflicting files)
bash: /usr/bin/bash.exe exists in filesystem
bash: /usr/bin/bashbug exists in filesystem
bash: /usr/bin/sh.exe exists in filesystem
Errors occurr

这个时候可以考虑强制覆盖安装,加上 --overwrite="*" 参数:

$ echo "Y" | pacman -S --overwrite="*" bash
warning: bash-5.2.026-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...

Packages (1) bash-5.2.026-1

Total Installed Size:  11.73 MiB
Net Upgrade Size:       0.00 MiB

:: Proceed with installation? [Y/n] Y
(1/1) checking keys in keyring                                           [#######################################] 100%
(1/1) checking package integrity                                         [#######################################] 100%
(1/1) loading package files                                              [#######################################] 100%
(1/1) checking for file conflicts                                        [#######################################] 100%
(1/1) checking available disk space                                      [#######################################] 100%
warning: could not get file information for usr/share/doc/
warning: could not get file information for usr/share/doc/bash/
warning: could not get file information for usr/share/doc/bash/bash.html
warning: could not get file information for usr/share/doc/bash/bashref.html
warning: could not get file information for usr/share/doc/bash/CHANGES
warning: could not get file information for usr/share/doc/bash/COMPAT
warning: could not get file information for usr/share/doc/bash/FAQ
warning: could not get file information for usr/share/doc/bash/INTRO
warning: could not get file information for usr/share/doc/bash/NEWS
warning: could not get file information for usr/share/doc/bash/POSIX
warning: could not get file information for usr/share/doc/bash/RBASH
warning: could not get file information for usr/share/doc/bash/README
warning: could not get file information for usr/share/info/
warning: could not get file information for usr/share/info/bash.info.gz
:: Processing package changes...
(1/1) reinstalling bash                                                  [#######################################] 100%

初始化系统

为了避免日后使用 pacman 冲突,可以考虑“重装”基本系统:

$ echo "Y" | pacman -S --overwrite="*" base

注意这条命令最后可能会一直报错,关闭窗口即可。