Skip to content

Instantly share code, notes, and snippets.

@mrkline
Last active November 4, 2020 20:36
Show Gist options
  • Save mrkline/5078528 to your computer and use it in GitHub Desktop.
Save mrkline/5078528 to your computer and use it in GitHub Desktop.
An overview of setting up *nix tools on Windows using MSYS

*nix on Windows

Options

If you would like to use *nix tools on Windows, you have two main options:

  • Cygwin offers your typical Linux toolset (bash, ls, cd, grep, and the other core utilities) and a DLL that provides some Linux system emulation (signals, etc.). However, Cygwin tends to take the "everything but the kitchen sink" approach, so I generally prefer the second option, MSYS.

  • MSYS/MinGW, which stand for "Minimal SYStem" and "Minimalist GNU for Windows", respectively, aim to be smaller, more compact setups than Cygwin. They don't offer a Linux emulation layer like Cygwin, but instead let you compile programs that run directly on Windows (for native languages like C and C++).

Installation

Setting up MSYS

  1. Grab the MinGW installer here and run it.

  2. When the installer asks you to choose between using pre-packaged repository catalogues and downloading the latest catalogues, it is essentially asking if you want to check for updates to the install. Doing so takes longer but gives more up to date packages. You can always update manually at a later time (this is discussed later in this guide).

  3. When asked to select which components to install, deselect the MinGW Compiler Suite (unless you want to do C and C++ work with gcc). This can also be installed later if you would like. Scroll down and select the "MSYS Basic System" option.

    installer screenshot

  4. Finish the installer. You should now have MSYS installed on your system!

Configuring MSYS

Before opening up a bash terminal, you may want to reconfigure where MSYS looks for your home directory. By default, MSYS creates its own internal home directory instead of using your normal Windows one (the one at C:\Users\YourUsername which contains My Documents, Music, Pictures, Desktop, etc.). To change this, open up the /etc/profile file for MSYS with a text editor (by default this is located in C:\MinGW\msys\1.0\etc) and change the block that resembles

if [ -z "$HOME" ]; then
	...
fi

to

if [ -z "$HOME" ]; then
  HOME="/c/Users/$LOGNAME"
fi

Opening a bash terminal/setting up shortcuts

You can start a bash terminal with the msys.bat script found by default in C:\MinGW\msys\1.0. Feel free to create a shortcut to this script or a shortcut to C:\MinGW\msys\1.0\bin\sh.exe --login -i for your convenience.

Using MSYS

Package management

Just like most systems have a package management system (Red Hat and Fedora have yum, Debian has apt-get, Arch has pacman, etc.), MSYS/MinGW has a package manager called mingw-get, which you run on the command line (in bash).

Installing packages

To see what packages are available, use msys-get show. You will likely want to use a pager program like less so that you can scroll through it (use msys-get show | less, type q to exit less). To install a package, the command is

msys-get install <package name(s)>

Useful packages

  • msys-openssh gives you tools like ssh so that you can log into other Linux machines remotely, and scp so you can copy files from remote machines using SFTP.

  • msys-make gives you the make utility, for use on Makefiles

  • dos2unix gives you the dos2unix and unix2dos programs for converting text files between POSIX and Windows formats.

Updating packages

Use msys-get update then msys-get upgrade to update packages.

Removing packages

Use msys-get remove <package name(s)>

Paths in MSYS

By default, MSYS mounts hard drive at /<drive letter>/, so C:\Users becomes /c/Users, etc. / is mounted to your MSYS folder (C:\MinGW\msys\1.0 by default), and /tmp is mounted to your Windows user's temporary files directory.

Your .bashrc and setting up other programs

You can set up a .bashrc file in your home directory that configures bash. Options include what your prompt looks like, command history, and so on.

Your .bashrc also gives you a convenient place to add locations to your PATH environment variable. If you have a program installed on your machine but can't run it from bash, you likely have to add its location to PATH. To do this, you do something similar to

export PATH=$PATH:"path to program here"

For example, if I wanted to add the Java compiler (javac) to PATH so I can run it from bash, I would do the following (assuming I have installed the JDK):

export PATH=$PATH:/c/Program\ Files/Java/<JDK directory>/bin/

Note two things:

  • We're appending to PATH, not totally resetting it.

  • If you don't use quotes around a directory you want to add, you need to escape spaces with a backslash.

To try to tie all of this together, below is an example .bashrc (my current one at the time of writing)

	# Ignore duplicate lines
	HISTCONTROL=ignoreboth

	# Append to history, don't overwrite it
	shopt -s histappend

	# Set history size
	HISTSIZE=1000
	HISTFILESIZE=2000

	# Color ls and grep commands
	alias ls='ls --color=auto'
	alias grep='grep --color=auto'

	# Less options (see the less man page for details)
	export LESS=-RSFX

	export PATH=$PATH:/c/lame/ # LAME MP3 encoder
	export PATH=$PATH:/c/Program\ Files/Vim/vim73/ # Vim
	export PATH=$PATH:/c/Program\ Files/flac/ # FLAC codec
	export PATH=$PATH:/c/Program\ Files/NSIS/ # Nullsoft Scriptable Install System
	export PATH=$PATH:/c/Python33/ # Python
	export PATH=$PATH:/c/MinGW/bin/ # MinGW compilers (gcc, g++)
	export PATH=$PATH:/c/Program\ Files/Git/cmd/ # Git
	export PATH=$PATH:/c/Program\ Files/Java/jdk1.7.0_15/bin/ # java and javac

	# For CS 536
	export CLASSPATH=$CLASSPATH:.:~/school/cs536/ # Java class files for school

A note on vim

Vim comes as a package in MSYS (msys-vim), but it's likely a better choice to install Vim from its website. Doing so will give you better Windows integration (gvim, Windows explorer integration, shortcuts, etc.).

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