为什么要在 Windows 电脑上安装 Msys2

海云青飞 为什么要在 Windows 上使用 Msys2

目前我本机使用 Windows 系统,但平时也要用到 Linux 系统,一般情况下,我要付较多出精力学习两个操作系统相关知识、技术

我的兴趣是广泛的,而精力是有限的,因此,我希望在 Windows 系统里熟悉 Linux 系统,以节省学习 Linux 系统的时间,而 Msys2 正好可以满足我的愿望。这和我学习英语同理,我宁愿把电脑、手机等界面设置成英文,也不愿意专门花大量时间学习英语

什么是 Msys2

下面介绍来自 Msys2 官网

MSYS2 不是“一个工具统治所有”,而是试图专注于它擅长的事情。它提供了一个基于开源软件的原生构建环境,并且让您在已经熟悉 Linux 时有宾至如归的感觉

MSYS2 是工具和库的集合,为您提供构建、安装和运行在本机 Windows 上的易用环境软件

它由一个名为 mintty、bash 的命令行终端、git 和 Subversion、tar 和 awk 等工具,都基于 Cygwin 的修改版本。尽管这些核心部分基于 Cygwin,MSYS2 的主要重点是提供本机 Windows 软件的构建环境,同时使用 Cygwin 的部分保持在最低限度

MSYS2 为 GCC 提供了最新的原生版本,mingw-w64、 CPython、CMake、Meson、OpenSSL、FFmpeg、Rust、Ruby 等等

我们的软件包仓库包含 3200 多个 预构建的软件包,随时可以安装

用 Scoop 安装 Msys2

执行命令:

scoop install msys2

再按照屏幕提示执行命令 msys2 完成安装

C:\Users\tuenhai.com>msys2
MSYS2 is starting for the first time. Executing the initial setup.
Copying skeleton files.
These files are for the users to personalise their msys2 experience.

They will never be overwritten nor automatically updated.

'./.bashrc' -> '/home/tuenhai.com/.bashrc'
'./.bash_logout' -> '/home/tuenhai.com/.bash_logout'
'./.bash_profile' -> '/home/tuenhai.com/.bash_profile'
'./.profile' -> '/home/tuenhai.com/.profile'
'C:\WINDOWS\system32\drivers\etc\hosts' -> '/etc/hosts'
'C:\WINDOWS\system32\drivers\etc\protocol' -> '/etc/protocols'
'C:\WINDOWS\system32\drivers\etc\services' -> '/etc/services'
'C:\WINDOWS\system32\drivers\etc\networks' -> '/etc/networks'
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
==> Generating pacman master key. This may take some time.
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/584A543AB420AB9900F72ED9B81435036D0E5E39.rev'
gpg: Done
==> Updating trust database...
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
==> Appending keys from msys2.gpg...
==> Locally signing trusted keys in keyring...
  -> Locally signed 5 keys.
==> Importing owner trust values...
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
==> Disabling revoked keys in keyring...
  -> Disabled 4 keys.
==> Updating trust database...
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
gpg: error retrieving 'alexey.pawlow@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key F40D263ECA25678A: "Alexey Pavlov (Alexpux) <alexey.pawlow@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'david.macek.0@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 790AE56A1D3CFDDC: "David Macek (MSYS2 master key) <david.macek.0@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'martellmalone@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key DA7EF2ABAEEA755C: "Martell Malone (martell) <martellmalone@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'reiter.christoph@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 755B8182ACD22879: "Christoph Reiter (MSYS2 master key) <reiter.christoph@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'icquinteiro@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 9F418C233E652008: "Ignacio Casal Quinteiro <icquinteiro@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'mingw.android@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key BBE514E53E0D0813: "Ray Donnelly (MSYS2 Developer - master key) <mingw.android@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'alexpux@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 5F92EFC1A47D45A1: "Alexey Pavlov (Alexpux) <alexpux@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'david.macek.0@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 974C8BE49078F532: "David Macek <david.macek.0@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'reiter.christoph@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key FA11531AA0AA7F57: "Christoph Reiter (MSYS2 development key) <reiter.christoph@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: no valid OpenPGP data found.
gpg: Total number processed: 0
gpg: error retrieving 'me@martellmalone.com' via WKD: No fingerprint
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 794DCF97F93FC717: "Martell Malone (martell) <me@martellmalone.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'martellmalone@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key D595C9AB2C51581E: "Martell Malone (MSYS2 Developer) <martellmalone@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg: error retrieving 'mingw.android@gmail.com' via WKD: No data
gpg: error reading key: No data
gpg: refreshing 1 key from hkps://keyserver.ubuntu.com
gpg: key 4DF3B7664CA56930: "Ray Donnelly (MSYS2 Developer) <mingw.android@gmail.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
Initial setup complete. MSYS2 is now ready to use.

Msys2 所在位置:

C:\Users\tuenhai.com\scoop\apps\msys2\current

Msys2 怎样安装 Mingw

Msys2 官方文档 说:

You will probably want to install some tools like the mingw-w64 GCC to start compiling projects. Run the following command:

pacman -S mingw-w64-ucrt-x86_64-gcc

注意,还要 设置环境变量

For example, in the UCRT64 environment the $PATH variable starts with /ucrt64/bin:/usr/bin so you get all ucrt64 based tools as well as all msys tools

Now you can call gcc to build software for Windows.

gcc --version
gcc.exe (Rev1, Built by MSYS2 project) 14.2.0

此处的 UCRT64 可以作为 Windows 下的 C/C++ 编译环境,但不能作为 Nim 编程语言的 C 编译环境,因此 海云青飞 就不安装它了

Msys2 怎样选择 Shell 环境

Msys2 有多个 shell 环境,如:

  • MSYS2 MSYS
  • MSYS2 MINGW64
  • MSYS2 UCRT64
  • ...

上述后半部分的 MSYS、MINGW64、UCRT64 表示不同的编译环境。本文默认使用 MSYS 环境

假如你的 Msys2 是用 scoop 安装的,则所在位置类似下面:

C:\Users\tuenhai.com\scoop\apps\msys2\current

这个位置就是 Msys2 的 / 对应的 Windows 路径

按 Windows 键,输入 msys2 然后回车,实际调用的是:/msys2.exe,它的配置文件是 /msys2.ini

编辑 /msys2.ini:

#MSYS=winsymlinks:nativestrict
#MSYS=error_start:mingw64/bin/qtcreator.exe|-debug|<process-id>
#CHERE_INVOKING=1

#启用此项,就能在 MSYS 环境中继续 Windows 系统 path 环境变量,方便我们调用程序
# 否则,你就需要在 ~/.bashrc 中自定义 PATH 环境变量
MSYS2_PATH_TYPE=inherit

MSYSTEM=MSYS

编辑 Msys2 ~/.bashrc

Msys2 怎样设置环境变量 $PATH

用户目录 ~ 下有 2 个 bash 配置文件:

  • 主配置文件 .bash_profile
  • 副配置文件 .bashrc

我们可以把自定义配置写在 .bashrc 中:

  • 为了方便重复使用,建议在云同步网盘保存 .bashrc,再软链接到 ~/.bashrc

  • .bash_profile 中反注释需要的行:

    # 海云青飞 注:scoop 安装版,默认已经启用这三行
    if [ -f "${HOME}/.bashrc" ] ; then
      source "${HOME}/.bashrc"
    fi
    
    # 海云青飞 注:可能是因为我的 ~/bin 是链接,下面三行无效
    #Set PATH so it includes user's private bin if it exists
    if [ -d "${HOME}/bin" ] ; then
      PATH="${HOME}/bin:${PATH}"
    fi
    
  • .bashrc 设置环境变量示例

    ~/.bashrc:

    user=/c/Users/tuenhai.com
    
    PATH=/usr/bin:${user}/bin:$PATH
    

Msys2,~/.bashrc 使用 alias 命令

alias qdir='q-dir'
alias codenim='code --reuse-window C:/Users/tuenhai.com/nim/nim.code-workspace'

这时我们在 Msys2 的 shell 中输入 qdir 并回车就可以打开 Q-Dir。codenim 则在 VS Code 中打用于编写 Nim 代码的工作空间

Msys2 怎样安装 Git

Msys2 Git 官方文档 说:

We currently only provide a cygwin based git, which only understands and outputs Unix paths and thus behaves slightly differently compared to "Git for Windows". It also is quite a bit slower, which is especially noticeable on larger repositories

既然这样,海云青飞 用 scoop 安装 git,git 的安装空间占用 300 多MB,包含 git GUI、git bash 等我不需要的东西,因此我安装的是 mingit,占用空间 100 多 MB

$ scoop install mingit
Installing 'mingit' (2.46.2) [64bit] from 'main' bucket
Starting download with aria2 ...
Download: Download Results:
Download: gid   |stat|avg speed  |path/URI
Download: ======+====+===========+=======================================================
Download: 1f9b6f|OK  |   5.8MiB/s|C:/Users/tuenhai.com/scoop/cache/mingit#2.46.2#680fc1b.zip
Download: Status Legend:
Download: (OK):download completed.
Checking hash of MinGit-2.46.2-64-bit.zip ... ok.
Extracting MinGit-2.46.2-64-bit.zip ... done.
Linking ~\scoop\apps\mingit\current => ~\scoop\apps\mingit\2.46.2
Creating shim for 'git'.
'mingit' (2.46.2) was installed successfully!
Notes
-----
To get Git to recognise OpenSSH, you will need to run

scoop install openssh
[environment]::setenvironmentvariable('GIT_SSH', (resolve-path (scoop which ssh)), 'USER')

and then restart powershell

2024-09-18 我为了测试一下,卸载 scoop 安装的 git,再安装 Msys2 的 git,然后执行 github 的 push,它竟然提示找不到 host。看来,这 Msys2 版的 git 真的不能用

另外,git for Windows 与 Msys2 存在兼容性问题。我在 ~/.ssh/config 中用了 /etc/fstab 映射后的短路径,用 git 向 github 提交更新时会报错:

no such identity: /folder/rsa_file: No such file or directory

用包含软链接的路径或全路径 如 /c/folder/rsa_file 就没有问题

Msys2 安装其他软件

  • which tar 得到 /usr/bin/tar,就不用再安装 tar 了

  • which ssh 可知 Windows 系统本来就有 openssh,但经 海云青飞 测试,它与 Msys2 不兼容

    安装 openssh:

    pacman --sync openssh
    

    在 PATH 环境变量中,/usr/bin 要前置,以确保 which ssh 的结果是 /usr/bin/ssh

Msys2 /etc/fstab 配置目录映射

/etc/fstab 配置文件目录映射的方法,在 /etc/fstab 加入类似下面的代码,然后退出所有使用 /etc/fstab 的程序,再重启 terminal 就可以了

/etc/fstab

# 目录路径中不能有空格。如果目录路径中有空格请使用转义字符"\040"代替
# 路径中不能有 ~,如 ~/.ssh 这种写法是无效的
C:\Users\tuenhai.com /tuenhai.com
C:\Users\tuenhai.com\Downloads /downloads
C:\Users\tuenhai.com\Documents /documents
C:\Users\tuenhai.com\Music /music
C:\Users\tuenhai.com\Videos /videos
C:\Users\tuenhai.com\Pictures /pictures

C:\Users\tuenhai.com/cloud/backup /backup

fstab 的映射目录只是在 Msys2 的命令行中起作用,并不显示在 Windows 资源管理器中

fstab 中的目录映射可以理解为隐式路径替换。上述命令配置完成后,在终端 start /tuenhai.com 后,实际打开的是 C:\Users\tuenhai.com 目录

因为配置 /etc/fstab 比创建目录链接方便,海云青飞 建议把 Windows 系统的常用目录都映射到 msys2 / 目录下

Windows 下字符不区分大小写,/tuenhai.com/Tuenhai.com 效果一样

在 Msys2 用户目录 ~ 下创建链接

请参考文章:Windows 10 使用 Msys2 ln 创建链接,取代 mklink

可以基于 fstab 的目录映射创建链接。假设重要配置文件备份在 C:/Users/tuenhai.com/cloud/backup,映射为 msys2 的 /backup。下面是创建文件软链接的示例:

ln -s /backup/msys2/.ssh/config ~/.ssh/config
ln -s /backup/msys2/.bashrc ~/.bashrc

相关文档内容

2024-09-12


独立思考最难得,赞赏支持是美德!(微信扫描下图)