Using zsh on Windows (2024)

Using zsh on Windows (1)

This is part of the "Setting Up a Cross-Platform Development Experience" series, where I simplify my own user experience across my macOS and Windows machines for general use and software development.

Table of Contents

  • Prerequisties
  • Install zsh on WSL
  • Replace Git Bash with zsh
    • Restore pacman in Git for Windows
  • Extras
    • Using both Git Bash and zsh on Windows
  • References
    • Additional Resources

One of the long-time arguments that favor macOS over Windows for developers is that macOS Terminal can run Unix commands while Windows Command Prompt cannot. However, since most servers worldwide are UNIX-based, it is common advice for developers to be familiar with Unix commands as much as possible.

Today, this argument holds little weight as there are plenty of ways Windows users can install a Unix shell like bash easily withGit Bash and Windows Subsystem for Linux (WSL).

As macOS has replaced bash with zsh since Catalina, I decided to use zsh on Windows too, so that I have an unified experience across 2 different OS, with the same (or similar) .zshrc config.

It turns out it is not as straightforward as I initially thought.

TLDR: My advice is to stick with git bash for Windows as it is good enough for most cases. (See my 1-minute guide on How to add Git Bash to Windows Terminal) If you are as experimental as I am, please proceed ahead.

There are 2 options to install zsh on Windows. We can either install zsh on WSL or replace the git bash shell with zsh. See Git Bash vs WSL.

Prerequisties

You may need these software installed on your Windows machine to follow this guide.

Install zsh on WSL

Use this method if you are already using WSL for most of your stuff.

This is the easier option; we just have to run the following commands in the Ubuntu shell.

sudo apt updatesudo apt install zsh -y# install oh-my-zsh to set zsh as default shell and install pluginssh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

You are good to go at this point. You may continue to watch the following video or read the companion blog post by Christian for more tips on this method.

Replace Git Bash with zsh

This is my preferred choice as git bash is easier for me to maintain due to the differences in the file directory with WSL. Most of my files are stored in the Windows file system and I use Chocolatey to manage my installation packages.

For this method to work, we need pacman but it is not included with Git for Windows by default.

At this point, we have 2 options:

  1. Install Git for Windows SDK
  2. or, download pacman separately

Git for Windows SDK has a huge disk footprint of 5.33 GB as compared to a typical installation of Git for Windows which is only about 840 MB.

Hence, restoring pacman is a much more reasonable option.

Restore pacman in Git for Windows

Huge thanks to Michael Chen

  1. Run the following commands to download /etc/pacman.conf and 3 packages: pacman, pacman-mirrors and msys2-keyring
curl https://raw.githubusercontent.com/msys2/MSYS2-packages/7858ee9c236402adf569ac7cff6beb1f883ab67c/pacman/pacman.conf -o /etc/pacman.conffor f in pacman-5.2.2-4-x86_64 pacman-mirrors-20201028-1-any msys2-keyring-1~20201002-1-any; do curl https://repo.msys2.org/msys/x86_64/$f.pkg.tar.xz -o ~/Downloads/$f.pkg.tar.xz;done
  1. Unpack them at the root then restore pacman with these commands
cd /tar x --xz -vf ~/Downloads/msys2-keyring-1~20201002-1-any.pkg.tar.xz usrtar x --xz -vf ~/Downloads/pacman-mirrors-20201028-1-any.pkg.tar.xz etctar x --xz -vf ~/Downloads/pacman-5.2.2-4-x86_64.pkg.tar.xz usrmkdir -p /var/lib/pacmanpacman-key --initpacman-key --populate msys2pacman -Syu
  1. The next two commands restore all matching metadata. The second command is multi-line but still safe to cut and paste (be patient):
URL=https://github.com/git-for-windows/git-sdk-64/raw/maincat /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. Refresh pacman keys (to avoid this issue)
pacman-key --refresh-keys
  1. Install zsh!
pacman -S zsh
  1. Add the following line to .bashrc to switch from git bash to zsh whenever executing git bash.
bash -c zsh
  1. Install oh-my-zsh, theme, plugins and all the good stuff!

Extras

Update: added this section after a week of using zsh on Windows.

Using both Git Bash and zsh on Windows

After setting up zsh on Windows Terminal, I noticed that zsh is extremely slow within VSCode and freezes from time to time. As there isn't much documentation on this, so I have decided to fall back to using git bash in VSCode for the time being.

However, it seems that there is no simple way to load different .bashrc files so that I can switch between git bash and zsh easily.

This is a little hack to make this work.

  1. comment out the line mentioned above in my .bashrc to switch to zsh
# bash -c zsh
  1. Add a switch-to-zsh.sh script into my user directory
#!/bin/bashbash -c zsh
  1. Execute switch-to-zsh.sh only with Windows Terminal. by adding "commandline": "C:/Program Files/Git/bin/bash.exe --login switch-to-zsh.sh" to my Windows Terminal settings.json.

  2. Viola! I get to enjoy the speed of running git bash within VS Code and use zsh on Windows Terminal!

Using zsh on Windows (2)

One caveat of this setup is that I have to maintain both .bashrc and .zshrc and make sure my alias and path variables are set up in both files. This is a fairly small cost for me to enjoy both shells on my machine.

References

Additional Resources

Using zsh on Windows (2024)
Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6024

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.