Skip to content

Instantly share code, notes, and snippets.

@CypherpunkSamurai
Last active May 3, 2025 15:08
Show Gist options
  • Save CypherpunkSamurai/ea80f4b04e2d0da13769e726ffd7bc38 to your computer and use it in GitHub Desktop.
Save CypherpunkSamurai/ea80f4b04e2d0da13769e726ffd7bc38 to your computer and use it in GitHub Desktop.
Msys2 Fresh Portable Install

Msys2 Refresh

Create a refreshed msys2 installation

  1. place the file in C:\Tools
    or run Invoke-WebRequest [url from the above script] -OutFile msys2.ps1 (or iwr for short)
  2. run the script.
  3. open mingw64.exe from the msys2 folder or msys2_shell.cmd -defterm -here -no-start -mingw64
iwr "https://gist.githubusercontent.com/CypherpunkSamurai/ea80f4b04e2d0da13769e726ffd7bc38/raw/msys2_refresh.ps1" | iex

Windows Terminal Config

Add this to windows terminal json. Replace C:\Tools\msys2 with your msys2 folder and -mingw64 with your runtime name. Refer to [1] and [2].

{
 "guid": "{17da3cac-b318-431e-8a3e-7fcdefe6d114}",
 "name": "MINGW64 / MSYS2",
 "commandline": "C:/Tools/msys2/msys2_shell.cmd -defterm -here -no-start -mingw64",
 "startingDirectory": "C:/Tools/msys2/home/%USERNAME%",
 "icon": "C:/Tools/msys2/mingw64.ico",
 "font": {
  "face": "Lucida Console",
  "size": 9
 }
}
# Refresh Msys2 Installation
# Paths
$msys2_extract_dir = "$PWD\msys2"
# Variables
$msys2_repository = "msys2/msys2-installer"
$release_api = "https://api.github.com/repos/$msys2_repository/releases/latest"
$msys2_nightly_file = "msys2-nightly.sfx.exe"
# Check if Msys2 is already extracted
if (Test-Path $msys2_extract_dir) {
Write-Host "MSYS2 is already installed to $msys2_extract_dir"
$reinstall = Read-Host "Do you want to reinstall MSYS2? (y/n)"
if ($reinstall -ne 'y') {
Exit 1
}
Remove-Item $msys2_extract_dir -Recurse -Force
}
# Check if Msys2 installer is already downloaded
if (!(Test-Path $msys2_nightly_file)) {
# Fetch the Latest Url
# Parse [0].assets[] where "name" ends with ".sfx.exe"
$release_url_nightly = (Invoke-RestMethod -Uri $release_api)[0] | Select-Object -ExpandProperty assets | Where-Object {$_.name -like "*.sfx.exe"} | Select-Object -ExpandProperty browser_download_url
# Print Download Link
Write-Host "Downloading MSYS2 Nightly from: $release_url_nightly"
# Download File
Invoke-WebRequest -Uri $release_url_nightly -OutFile $msys2_nightly_file
# Notify download completion
Write-Host "MSYS2 Nightly download complete: $msys2_nightly_file"
} else {
Write-Host "MSYS2 Nightly already downloaded: $msys2_nightly_file"
}
# Extract SFX Archive
Write-Host "Extracting $msys2_nightly_file to Current Directory..."
# & ".\$msys2_nightly_file" -y -o"$msys2_extract_dir"
& ".\$msys2_nightly_file" -y -o"$PWD"
Write-Host "Moving msys64 to $msys2_extract_dir"
Move-Item "$PWD\msys64" $msys2_extract_dir
# * Msys2 Post Install
# $msys2_distro_path = Join-Path $msys2_extract_dir "msys64"
# Distro path is now moved to msys2_extract_dir
$msys2_distro_path = $msys2_extract_dir
Write-Host "Configuring MSYS2 environment..."
# Set Env PATH
$env:PATH="$msys2_distro_path\usr\bin;$env:PATH"
# Initialize Msys2
& "$msys2_distro_path\usr\bin\bash.exe" --login -c "echo 'Setting up MSYS2 environment...'"
# Clean GnuPG keys and Update
Remove-Item "$msys2_distro_path\etc\pacman.d\gnupg\*" -Force -Recurse -ErrorAction SilentlyContinue
# Pacman Get Keys
# rm -r /etc/pacman.d/gnupg/ && pacman-key --init && pacman-key --populate msys2 && pacman-key --refresh-keys
# use "bash pacman-key" as pacman-key is a script
& "$msys2_distro_path\usr\bin\bash.exe" "$msys2_distro_path\usr\bin\pacman-key" --init
& "$msys2_distro_path\usr\bin\bash.exe" "$msys2_distro_path\usr\bin\pacman-key" --populate msys2
& "$msys2_distro_path\usr\bin\bash.exe" "$msys2_distro_path\usr\bin\pacman-key" --refresh-keys
# Rank Mirrors
foreach ($repo in @("clang32", "clang64", "mingw", "mingw32", "mingw64", "msys", "ucrt64")) {
Write-Host "Creating Backup of $repo mirrorlist..."
Copy-Item "$msys2_distro_path\etc\pacman.d\mirrorlist.$repo" "$msys2_distro_path\etc\pacman.d\mirrorlist.$repo.bak" -ErrorAction SilentlyContinue
if (Test-Path "$msys2_distro_path\etc\pacman.d\mirrorlist.$repo.bak") {
Write-Host "Ranking mirrors for $repo repository..."
& "$msys2_distro_path\usr\bin\bash.exe" "$msys2_distro_path\usr\bin\rankmirrors" -n 6 "$msys2_distro_path\etc\pacman.d\mirrorlist.$repo.bak" > "$msys2_distro_path\etc\pacman.d\mirrorlist.$repo"
}
}
# Run with custom shell
# msys2_shell.cmd -shell fish
# basic runtime developer tools
pacman -S --noconfirm --needed base-devel msys2-runtime-devel
# Install Requirements
pacman -S --noconfirm --needed git zsh make diffutils patchutils grep which wget curl openssh openssl openssl-devel mingw-w64-x86_64-toolchain
pacman -S --noconfirm --needed make yasm nasm pkg-config mingw-w64-x86_64-make mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja autotools mingw-w64-x86_64-autotools
pacman -S --noconfirm --needed mingw-w64-x86_64-clang
pacman -S --noconfirm --needed mingw-w64-x86_64-bat
# ESP
# https://raw.githubusercontent.com/espressif/esp-idf/cc5440f6a2df826dc9a9ded571ad20b5c30a4b89/tools/windows/windows_install_prerequisites.sh
# pacman -S --noconfirm gettext-devel git make ncurses-devel flex bison gperf vim zip unzip winpty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment