Skip to content

Instantly share code, notes, and snippets.

@delphym
Forked from fworks/install-zsh-windows-git-bash.md
Last active November 30, 2023 23:36
Show Gist options
  • Save delphym/56571f4dde96046e2f459ee04fd337cb to your computer and use it in GitHub Desktop.
Save delphym/56571f4dde96046e2f459ee04fd337cb to your computer and use it in GitHub Desktop.
Zsh / Oh-my-zsh on Windows Git Bash

Steps:

  1. Download the latest zsh package: https://packages.msys2.org/package/zsh?repo=msys&variant=x86_64

Example:

zsh-5.7.1-1-x86_64.pkg.tar.xz

Update:

The package now is compacted using zstd, so now we need some "special" extractor.
So, in my case, I've downloaded this file
https://repo.msys2.org/msys/x86_64/zsh-5.8-5-x86_64.pkg.tar.zst And extracted it using the Peazip.
https://peazip.github.io/zst-compressed-file-format.html

  1. Extract the content to your git bash installation dir:

Usually C:\Program Files\Git

  1. Test it and config zsh:

Open git bash and type:

zsh

So, this step is important, it seems zsh will ask a few configurations, like the tab completion, history, etc.
Please read the options and set that according to your use.

  1. Installing oh-my-zsh, execute the following cmd on git bash
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  1. Configuring zsh as default shell

Edit the ~/.bashrc file. (create it if it doesn't exist)

Add the following lines at the end of the file

# Launch Zsh
if [ -t 1 ]; then
exec zsh
fi

Close and open again the git bash.

Optional steps

  1. Installing the Honukay theme
curl -fsSL https://raw.githubusercontent.com/oskarkrawczyk/honukai-iterm/master/honukai.zsh-theme -o ~/.oh-my-zsh/custom/themes/honukai.zsh-theme
  1. Set it
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="honukai"/g' ~/.zshrc

Instructions to replace Git Bash with zsh and Restore pacman in Git for Windows

These instructions are based on:

They are mainly exact commands I had to run to make it work.

Make sure you run Git Bash in Admin mode, all the time.

Steps to restore PACMAN

  1. Download and install latest Git for Windows
  2. Download PeaZip to extract ZSTD compressed archives
  3. Download and extract the foloowing with the help of PeaZip (to have only tar archives available)
  1. Run following sequence of commands to restore pacman
    cd /
    curl https://raw.githubusercontent.com/msys2/MSYS2-packages/master/pacman/pacman.conf -o /etc/pacman.conf
    tar -xvf ~/Downloads/msys2-keyring-1_20221024-1-any.pkg.tar usr
    tar -xvf ~/Downloads/pacman-mirrors-20221016-1-any.pkg.tar etc
    tar -xvf ~/Downloads/pacman-6.0.1-25-x86_64.pkg.tar usr
    mkdir -p /var/lib/pacman
    pacman-key --init
    pacman-key --populate msys2
    pacman -Syu
  2. Restore all matching metadata (It's a multiline command, but still safe to cut and paste)
URL=https://github.com/git-for-windows/git-sdk-64/raw/main
cat /etc/package-versions.txt | while read p v; do d=/var/lib/pacman/local/$p-$v;
 mkdir -p $d; echo $d; for f in desc files install mtree; do curl -sSL "$URL$d/$f" -o $d/$f;
 done; done
  1. Try to install something pacman -S mc

Steps to upgrade MSYS2/MINGW environment

Iterate through the below script, untill you reach satisfied outcome of having all packages updated.

DISCLAMER! the "Git Bash may (and for sure will) freeze several times on trhe attempt of updating the core packages like bash, mingw-w64-x86_64*, filesystem, etc. Just keep closing the Git Bash terminal. The script has to be modified (at least uncomment the --overwrite command and file names should be change through the iteration.) and feded with the right input file

cantupd="/etc/package-cantupd.txt"
canupd="/etc/package-canupd.txt"
canupd_force="/etc/package-canupd_force.txt"
cantupd_force="/etc/package-cantupd_force.txt"
echo > $cantupd
echo > $canupd
echo > $canupd_force
echo > $cantupd_force
cat /etc/package-versions1.txt | while read p v; do echo -e "\nREINSTALLING" $p;
yes | pacman -S $p;
if [ $? -eq 1 ]; then
    echo "Trying to force the reinstall of "$p
    echo "$p $v" >> $cantupd
    #yes | pacman -S --overwrite "*" $p;
    if [ $? -eq 0 ]; then
        echo "$p $v" >> $canupd_force
    else
        echo "$p $v" >> $cantupd_force
    fi
else
    echo "$p $v" >> $canupd
fi;
done;
echo -e "\n\nThis is a list of`wc -l ${cantupd}` packages which didn't update successfully:\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cat $cantupd
echo -e "\n\nSuccessfully reinstalled`wc -l ${canupd} packages`:\n~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $canupd
echo -e "\n\nThis is a list of`wc -l ${canupd_force}` forcefully installed packages:\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cat $canupd_force
echo -e "\n\nFailed reinstalled`wc -l ${cantupd_force} packages`:\n~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $cantupd_force

Steps to sync DB, get everything up todate and install ZSH

pacman -Syy
pacman -Syu

pacman -S zsh

Install oh-my-zsh

You may need to install curl or wget prior that like this: pacman -S curl wget Then follow instructions from oh-my-zsh home page

Troubleshooting

Those are just commands which may help you to resolve various issues:

pacman-db-upgrade -r /../
rm /var/lib/pacman/sync/*.sig
vim /etc/pacman.conf -> SigLevel = Never
pacman -Qq /usr/bin/bash.exe

You may also face issue with write permission to ~/.ssh when using git, the symptomps were:

Could not create directory '/home/$my_user/.ssh' (No such file or directory). Failed to add the host to the list of known hosts (/home/$my_user/.ssh/known_hosts). git@github.com: Permission denied (publickey). Could not read from remote repository.

Not sure what does it cause and what makes it go away. Sometime it resolve itselve, sometime it returns. The way how to fix it permanently is actually create that directory like this: mkdir -p /home/$my_ser/.ssh, it will create that missing .ssh folder, where you need to copy your private and public keys. Alternativelly you can create symlink(s) to your desired $HOME folder or just .ssh folder location, e.g. mklink /D "c:\Program Files\Git\home\myUser" %userprofile%. Prior that you still need to make /home folder either from your confused Git Bash/ZSH or in Windows under the root of installation path of Git for Windows.

Alternatives

I have just discovered alternative to oh-my-zsh -> oh-my-posh. It seems promissing and may give a try next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment