Note

MSYS2 不属于 Shell,而是一个更大的工具集合,里面包含了终端模拟器 + Shell + 开发环境

  • 全称:Minimal SYStem 2。
  • 作用:在 Windows 上提供一个类 Unix 的开发环境,核心是 CygwinMinGW-w64
  • 组件
    1. 终端模拟器(mintty,MSYS2 默认用它来显示窗口)。
    2. Shell(通常是 bash)。
    3. 软件包管理器pacman,和 Arch Linux 一样)。
    4. 编译工具链(gcc、make 等)。

下载地址: https://www.msys2.org/

1 常用指令

1.1 安装软件

  • pacman -S 软件名: 安装软件。也可以同时安装多个包,只需以空格分隔包名即可。
  • pacman -S —needed 软件名 1 软件名 2: 安装软件,但不重新安装已经是最新的软件。
  • pacman -Sy 软件名:安装软件前,先从远程仓库下载软件包数据库 (数据库即所有软件列表)。
  • pacman -Sv 软件名:在显示一些操作信息后执行安装。
  • pacman -Sw 软件名: 只下载软件包,不安装。
  • pacman -U 软件名.pkg.tar.gz:安装本地软件包。
  • pacman -U http://www.example.com/repo/example.pkg.tar.xz: 安装一个远程包(不在 pacman 配置的源里面)。

1.2 更新系统

  • pacman -Sy: 从服务器下载新的软件包数据库(实际上就是下载远程仓库最新软件列表到本地)。
  • pacman -Su: 升级所有已安装的软件包。

pacman 可以用一个命令就可以升级整个系统。花费的时间取决于系统有多老。这个命令会同步非本地 (local) 软件仓库并升级系统的软件包:

pacman -Syu

Note

在 msys2 中 pacman -Syu 后需要重启一下 msys2(关掉 shell 重新打开即可)。

1.3 卸载软件

  • pacman -R 软件名: 该命令将只删除包,保留其全部已经安装的依赖关系
  • pacman -Rv 软件名: 删除软件,并显示详细的信息
  • pacman -Rs 软件名: 删除软件,同时删除本机上只有该软件依赖的软件。
  • pacman -Rsc 软件名: 删除软件,并删除所有依赖这个软件的程序,慎用
  • pacman -Ru 软件名: 删除软件,同时删除不再被任何软件所需要的依赖

1.4 搜索软件

  • pacman -Ss 关键字: 在仓库中搜索含关键字的软件包(本地已安装的会标记)
  • pacman -Sl : 显示软件仓库中所有软件的列表 可以省略,通常这样用:pacman -Sl | 关键字
  • pacman -Qs 关键字: 搜索已安装的软件包
  • pacman -Qu: 列出所有可升级的软件包
  • pacman -Qt: 列出不被任何软件要求的软件包 参数加 q 可以简洁方式显示结果,比如 pacman -Ssq gcc 会比 pacman -Ss gcc 显示的好看一些。

pacman -Sl | gcc 跟 pacman -Ssq gcc 很接近,但是会少一些和 gcc 有关但软件名不包含 gcc 的包。

1.5 查询软件信息

  • pacman -Q 软件名: 查看软件包是否已安装,已安装则显示软件包名称和版本
  • pacman -Qi 软件名: 查看某个软件包信息,显示较为详细的信息,包括描述、构架、依赖、大小等等
  • pacman -Ql 软件名: 列出软件包内所有文件,包括软件安装的每个文件、文件夹的名称和路径

1.6 软件包组

  • pacman -Sg: 列出软件仓库上所有的软件包组
  • pacman -Qg: 列出本地已经安装的软件包组和子包
  • pacman -Sg 软件包组: 查看某软件包组所包含的所有软件包
  • pacman -Qg 软件包组: 和 pacman -Sg 软件包组完全一样

很多人建议通过安装软件组来安装工具链,例如:

  • pacman -S mingw-w64-x86_64-toolchain
  • pacman -S mingw-w64-i686-toolchain
  • pacman -S mingw-w64-x86_64-qt5
  • pacman -S base-devel

但是这样比较浪费空间。实际上如果把 gcc, qt, clang 等安装上,msys2 就要占掉超过 10G 的硬盘空间,所以个人很少直接安装软件组。

1.7 清理缓存

  • pacman -Sc:清理未安装的包文件,包文件位于 /var/cache/pacman/pkg/ 目录。
  • pacman -Scc:清理所有的缓存文件。

1.8 最常用的 pacman 命令小结

pacman 命令较多,作为新手,将个人最常用的命令总结如下:

  • pacman -Syu: 升级系统及所有已经安装的软件。
  • pacman -S 软件名: 安装软件。也可以同时安装多个包,只需以空格分隔包名即可。
  • pacman -Rs 软件名: 删除软件,同时删除本机上只有该软件依赖的软件。
  • pacman -Ru 软件名: 删除软件,同时删除不再被任何软件所需要的依赖。
  • pacman -Ssq 关键字: 在仓库中搜索含关键字的软件包,并用简洁方式显示。
  • pacman -Qs 关键字: 搜索已安装的软件包。
  • pacman -Qi 软件名: 查看某个软件包信息,显示软件简介,构架,依赖,大小等详细信息。
  • pacman -Sg: 列出软件仓库上所有的软件包组。
  • pacman -Sg 软件包组: 查看某软件包组所包含的所有软件包。
  • pacman -Sc:清理未安装的包文件,包文件位于 /var/cache/pacman/pkg/ 目录。
  • pacman -Scc:清理所有的缓存文件。

2 配置 pacman 镜像源

2.1 添加清华源和中科大源

msys64\etc\pacman.d 目录下有三个文件。

  • mirrorlist.msys
  • mirrorlist.mingw64
  • mirrorlist.mingw32

首先是 mirrorlist.msys:

##
## MSYS2 repository mirrorlist
##
 
## Primary
## msys2.org
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/
Server = http://repo.msys2.org/msys/$arch
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MSYS2/$arch
Server = http://www2.futureware.at/~nickoe/msys2-mirror/msys/$arch/

mirrorlist.mingw64:

##
## 64-bit Mingw-w64 repository mirrorlist
##
 
## Primary
## msys2.org
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/
Server = http://repo.msys2.org/mingw/x86_64
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MINGW/x86_64
Server = http://www2.futureware.at/~nickoe/msys2-mirror/x86_64/
Server = http://mirror.bit.edu.cn/msys2/REPOS/
 

mirrorlist.mingw32:

##
## 32-bit Mingw-w64 repository mirrorlist
##
 
## Primary
## msys2.org
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
Server = http://repo.msys2.org/mingw/i686
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MINGW/i686
Server = http://www2.futureware.at/~nickoe/msys2-mirror/i686/

重新运行 D:\msys64\msys2.exe。测试一下软件源是否可用:

pacman -Sy

这句命令的意思是同步本地软件数据库,如果看到了以下几句,说明没有问题:

:: 正在同步软件包数据库… mingw32 已经是最新版本 mingw64 已经是最新版本 msys 已经是最新版本

更新核心软件包。

pacman -Su

这句命令意为更新所有软件。但是似乎在第一次使用时只会更新核心软件包(待考证)。

3 安装 zsh 及配置终端

3.1 在 Windows 终端中运行 MSYS2

如果您不想使用 Mintty(MSYS2 中的默认终端应用程序),您可以改为在 Windows 终端中运行 MSYS2。

  1. 打开 Windows 终端设置。如果您使用的是 Windows 终端预览版,这将打开 GUI。您需要点击“打开 JSON 文件”来打开 settings.json
  2. MSYS2在此处 提供了 Windows 终端配置文件。复制配置文件并将其粘贴到此处 settings.json。如果您有其他配置文件,请注意不要覆盖它们。本人添加的配置如下:
 
"profiles": {
	  "list":
	  [
	        // ...
            {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64",
                "guid": "{0754eb02-fc96-433a-9db0-6f104caaaafb}",
                "hidden": false,
                "icon": "C:\\msys64\\mingw64.ico",
                "name": "MINGW64 / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },
            {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -msys",
                "guid": "{cda14367-012b-4734-be33-532d8b384f72}",
                "hidden": false,
                "icon": "C:\\msys64\\msys2.ico",
                "name": "MSYS / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },
            {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -ucrt64",
                "guid": "{6a1c820f-d1dc-479a-acea-f631ba89a0d6}",
                "hidden": false,
                "icon": "C:\\msys64\\ucrt64.ico",
                "name": "UCRT64 / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },
            {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -clang64",
                "guid": "{ffa3bd81-487c-40f1-ada1-ab7564607dac}",
                "hidden": false,
                "icon": "C:\\msys64\\clang64.ico",
                "name": "CLANG64 / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },
		    // ...
	  ]
}
  1. 如果您希望 MSYS2 成为 Windows 终端打开时启动的默认配置文件,请将 MSYS / MSYS2 的 guid(当前为 {71160544-14d8-4194-af25-d05feeac7233})设置为 defaultProfile

3.2 安装 zsh 并配置

pacman -S zsh

添加或修改 C:\msys64\home\zhang.zshrc 内容为:

#################################### zsh & zinit (begin)   ###############################################
 
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
 
 
### Added by Zinit's installer
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
    print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
    command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
    command git clone https://github.com/zdharma-continuum/zinit "$HOME/.local/share/zinit/zinit.git" && \
        print -P "%F{33} %F{34}Installation successful.%f%b" || \
        print -P "%F{160} The clone has failed.%f%b"
fi
 
source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit
 
# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \
    zdharma-continuum/zinit-annex-as-monitor \
    zdharma-continuum/zinit-annex-bin-gem-node \
    zdharma-continuum/zinit-annex-patch-dl \
    zdharma-continuum/zinit-annex-rust
 
### End of Zinit's installer chunk
 
 
autoload -Uz compinit
zstyle ':completion:*' menu select
fpath+=~/.zfunc
 
 
# Powerlevel10k 主题
zinit ice depth=1; zinit light romkatv/powerlevel10k
 
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
 
 
# zsh 套件四天王
zinit ice blockf
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
#zinit light zsh-users/zsh-history-substring-search
#zinit light zdharma-continuum/fast-syntax-highlighting
 
# Oh My Zsh 功能
zinit snippet OMZL::completion.zsh
zinit snippet OMZL::history.zsh
zinit snippet OMZL::key-bindings.zsh
zinit snippet OMZL::theme-and-appearance.zsh
#zinit snippet OMZL::git.zsh
#zinit snippet OMZP::sudo/sudo.plugin.zsh
#zinit snippet OMZP::common-aliases/common-aliases.plugin.zsh
#zinit snippet OMZP::command-not-found/command-not-found.plugin.zsh
#zinit snippet OMZP::colored-man-pages/colored-man-pages.plugin.zsh
 
zinit wait lucid for \
  atinit"zicompinit; zicdreplay"  zdharma-continuum/fast-syntax-highlighting \
  				  zsh-users/zsh-history-substring-search \
  as"completion"                  OMZP::pip/_pip \
  			          OMZL::git.zsh \
				  OMZP::git/git.plugin.zsh \
				  OMZP::common-aliases/common-aliases.plugin.zsh
 
# 其他
#zinit load djui/alias-tips
zinit light agkozak/zsh-z
#zinit light Aloxaf/fzf-tab
#zinit load asdf-vm/asdf
 
WORDCHARS=$'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
 
#source "${XDG_CONFIG_HOME:-$HOME/.config}/asdf-direnv/zshrc"
 
#################################### zsh & zinit (eng)   ###############################################
 
#################################### 开发环境 (begin) ###############################################
 
 
#################################### 开发环境 (end)   ###############################################
 

3.3 设置 zsh 为默认的 MSYS2 shell

  1. 在 Windows 中找到 MSYS2 安装文件夹。我这里是 C:\msys64
  2. 打开 msys2_shell.cmd
  3. 找到行 set "LOGINSHELL=bash" 并将其更改为 set "LOGINSHELL=zsh"

现在每次打开 MSYS2 时,它都会 zsh 作为默认 shell 加载。

3.4 VSCode 设置 MSYS2 shell 为默认集成终端

settings.json 中添加如下内容:

  "terminal.integrated.defaultProfile.windows": "MINGW64 / MSYS2",
  "terminal.integrated.fontFamily": "MesloLGS NF",
  "terminal.integrated.fontSize": 12,
  "terminal.integrated.profiles.windows": {
	# ...
    "MINGW64 / MSYS2": {
      "icon": "terminal-bash",
      "path": [
        "C:\\msys64\\msys2_shell.cmd"
      ],
      "args": [
        "-defterm",
        "-mingw64",
        "-no-start",
        "-here"
      ]
    },
    # ...
  },

Note

推荐使用 MesloLGS NF 字体(p10k 主题推荐的字体).

4 Mingw64/MSYS2 开发环境后续配置

4.1 安装 mingw64 开发环境

# 安装开发工具和库:
pacman -S base-devel mingw-w64-x86_64-toolchain git cmake ninja
 
# 安装 clang 生态工具: clang-format,clangd, clang-tidy 等 
pacman -S mingw-w64-x86_64-clang-tools-extra
 

Faq

pacman 安装包时, clang 、 mingw-w64-clang-x86_64-clang、mingw-w64-x86_64-clang 有何区别,推荐安装哪个

包名所属环境安装位置clang 编译目标用途说明
clangMSYS 环境工具/usr/bin/clangMSYS(POSIX)仅用于构建 MSYS 环境工具本身,非用户项目,不要用来编译实际软件
mingw-w64-clang-x86_64-clangMINGW-w64/Clang 工具链/clang64/bin/clangMINGW-w64(Win32 ABI)针对 Windows 原生程序(Clang + libc++),推荐使用于 clang64 环境
mingw-w64-x86_64-clangMINGW-w64/GCC 工具链/mingw64/bin/clangMINGW-w64(Win32 ABI)Clang 但链接的是 GCC 工具链(libstdc++),与 clang64 不同

4.2 将 MSYS2 中安装的软件包公开给 Windows

为使 msys2 的一些工具如 git,cmake,clang-format 等全局可见,需将如下路径加入到环境变量 Path (注意不是 .zshrc 中的 PATH):

C:\msys64\usr\bin
C:\msys64\usr\local\bin
C:\msys64\mingw64\bin

Attention

  1. 为避免 msys2 的应用覆盖系统层级的应用,上述路径应放在用户环境变量 Path 的最末尾.
  2. 同理,安装系统层级的应用时,其应用路径应尽量添加到系统环境变量 Path.

Danger

后续实践发现,msys2 的应用会污染 windows 原有的指令,导致一些意料之外的错误,谨慎起见还是避免将上述路径加入 Path 以免污染 Path,msys2 的工具还是老老实实在 msys2 环境中使用吧

4.3 关于 cmake, git, ninja 等开发工具

如果之前基于 msvc 构建过项目,则应该安装过 cmake,git,ninja 等开发工具(或通过官网下载安装包安装,或通过其他软件管理工具如 winget,scoop 等 ,详见 Windows 下的软件包管理工具),这些工具都是系统层级的(后续称其为原生工具),而 msys2 安装的工具全位于 C:\msys64 下。

大部分情况下,原生工具与 msys2 工具不能混用,因此如果需要保留两种工具链,则需要这些工具共存。可以通过上面按一定顺序设置 Path 的方式保证在 msys2 之外的环境下调用的是原生工具,而在 msys2 之内的环境调用的是 msys2 pacman 安装的工具。

Info

原生工具和 msys2 工具一个典型差异就是对路径的处理,前者是 windows 下的路径,后者是 unix 路径,因此混用肯定会出问题。

4.4 关于 Python

如果之前通过 Python 官方 的安装包安装过 Python,其路径类似如下:

C:\Users\zhang\AppData\Local\Programs\Python\Python311\python.exe

msys2 下的 python 安装 python 包非常不方便,但有的软件又依赖 msys2 下的 python,取折中方案就是用系统的 Python 覆盖 msys2 的 Python,具体做法是在 ~/.zshrc 下加:

# 确保系统安装的python优先级最高
export PATH="/c/Users/zhang/AppData/Local/Programs/Python/Python311:/c/Users/zhang/AppData/Local/Programs/Python/Python311/Scripts:$PATH"

以防使用 msys2 的 pip 安装 python 包,应将其移除:

pacman -R mingw-w64-x86_64-python-pip

Note

类似的还有 Nodejs,Golang 等, 这些软件包本身就跨平台做得很好,不需要通过 mingw64 中间层来与 Windows 系统交互,又需要管理自己的包,因此在系统层面维护会更好。

5 为什么不是 WSL?Cygwin?

MSYS2 团队提供了 MSYS2 与其他项目的比较


6 参见: