Windows下的仿linux環境msys2介紹

  • 2020 年 4 月 10 日
  • 筆記

msys2是一種在Windows平台上模擬Linux運行環境的技術,它的一個優點就在於利用pacman包管理器,我們可以比較輕鬆的使用Linux包管理器的方式來安裝一整套可以在Windows上運行的Linux工具。如果你只是想要在Windows上簡單運行一些Linux程序,那麼msys2是一個很好的選擇。

下載安裝msys2

到清華鏡像源網站來下載msys2,網址如下。

https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib/

然後找到適合自己系統架構的安裝包,我這裡是64位,所以選擇下面這個。

下載完成後安裝即可。第一次打開以後會看到一個比較小的窗口,可以修改一下字體、語言什麼的。如果安裝程序沒有創建快捷方式的話,可以手動去安裝目錄裏面尋找msys2.exe文件。

修改安裝源

前面提到了,msys2可以使用pacman來安裝軟件包,所以第一件事情自然是修改鏡像源的地址了。編輯鏡像源文件的方法很簡單,假設你msys2的安裝路徑是,運行下面的powershell腳本即可。如果你安裝路徑在其他位置,修改腳本即可。

function InsertConentToFileTop {      param(          [String]$filename,          [String]$content      )      $filecontent = Get-Content $filename      $s = @()      $s += $content      $s += $filecontent      $s | Out-File $filename  }    $msys2_install_dir = 'C:msys64'  InsertConentToFileTop $msys2_install_diretcpacman.dmirrorlist.mingw32 'Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686'  InsertConentToFileTop $msys2_install_diretcpacman.dmirrorlist.mingw64 'Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64'  InsertConentToFileTop $msys2_install_diretcpacman.dmirrorlist.msys 'Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch'

然後在終端中運行pacman -Syu更新系統,就像archlinux那樣。更新完成之後,就可以使用msys2的模擬Linux環境來運行一些bash腳本以及Linux程序了。很多常見的程序都可以在msys2的軟件倉庫中找到,vim、nano、git等程序都可以正常運行。