52

I use Linux and a Mac, and I'll be teaching some command line stuff soon.

What can I install on Windows to give me a terminal window most similar to a basic Mac/Linux Bash shell?

I would like to be able to define .bash_aliases as well if possible.

migrated from stackoverflow.com Jun 15 '13 at 19:54

This question came from our site for professional and enthusiast programmers.

  • 3
    we used putty to connect to a real linux server. on windows I was always both reluctant and tempted to use cygwin, reluctant because of my clumsy install experience. But personally, i found it more convenient to use than msys and faster than a vm. once installed, cygwin turns out somehow portable simply copying it's directory is enough, however it will create a new username on each machine/user based on the windows username (by default, at least). But keep in mind that since cygwin/msys can been seen as a custom "distribution", some [optional] command switches may be missing. test them. – n611x007 Jun 15 '13 at 14:37
  • 5
    Microsoft just announced that the next update of Windows 10 will include Bash. – Dmitri Nesteruk Mar 31 '16 at 6:46
41

You could install Cygwin or MSYS/MinGW, or you could install a full Linux distribution in a virtual machine (for example, using VirtualBox).

  • I used a cygwin that was 135 mb (25 mb packed), copied from another computer (portable). it created a new username based on the new computers current user, and worked perfectly otherwise. my current cygwin is 800 mb, with a build environment, 200-300 mb source code and some stuff. – n611x007 Jun 15 '13 at 14:44
  • 2
    Note: you can now install the "Windows Subsystem for Linux" which allows you to run native user-mode Linux shell and tools on Windows. See the answer below... – MrHinsh - Martin Hinshelwood Sep 4 '16 at 8:49
48

You can now install the Windows Subsystem for Linux which allows you to run native user-mode Linux shell and tools on Windows. This allows you to run Bash shell directly on Windows and is based on Ubuntu.

This was introduced in Windows 10 with the Anniversary Update (2016) and is not enabled by default. To enable the new feature on a Windows 10 Anniversary Update machine, do the following:

  1. Navigate to Settings -> Update & Security -> For Developers and select "Developer mode"
  2. Control Panel -> Programs and Features and click "Turn Windows Features on or off" on the top left pane
  3. In the dialog box, check "Windows Subsystem for Linux" and click Ok
  4. Reboot machine
  5. Search for "bash" and click on it, it should open a command prompt and ask you if you want to install "Ubuntu on Windows", continue with "y"
  6. After installation it will ask to create a UNIX username and password
  7. You are now ready to use the bash shell

A bit of history, back in 2015 project Astoria was started to bring Android apps to Windows and they built a Linux subsystem called Windows Subsystem for Linux (WSL) for the purpose. Eventually project Astoria was dropped as it undermined their UWP (Universal Windows Platform) strategy, but some parts were retained and form the basis of bash for Windows. Over the course, Microsoft collaborated with Canonical (creators of Ubuntu Linux) to provide them with a native image with all supported tools/binaries.

More information available here,
https://blogs.msdn.microsoft.com/commandline/2016/04/06/bash-on-ubuntu-on-windows-download-now-3/
https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/
https://sec.ch9.ms/sessions/build/2016/P488.mp4 (video)

  • I like this answer better! – Peter Aug 3 '16 at 13:50
  • 1
    @Peter Thank you, tried it out briefly yesterday, was able to get gcc/g++ going, was able to get Firefox working through xserver (quite slow, maybe they cap CPU usage?), Chrome and default browser were crashing and some commands like 'screen' didn't work, things should get ironed out in the next update for bash as its still in beta. Having said this, just having bash available natively on Windows is fantastic! It opens a lot of possibilities. Kudos Microsoft! – Nemo Aug 3 '16 at 14:03
  • You can also run this in Powershell (run as admin): Enable-WindowsOptionalFeature -Online -FeatureName "Microsoft-Windows-Subsystem-Linux" And install any of the following from the Windows store: Ubuntu, Debian, OpenSUSE, Kali Linux – Zhenhir May 3 '18 at 15:55
18

Here's a list of the options available as of 2018-05 with a few updates regarding WSL during 2019-01 along with a bit of detail regarding each one.

Options that stand out

Cygwin and Babun

Cygwin provides the following:

  1. a bash shell and the Mintty terminal,
  2. an extensive range of common Linux utilities compiled to run on Windows
  3. a comprehensive compatibility layer that enables Linux programs to run on Windows.

Cygwin aims to make porting Linux applications on Windows easy. If some C program was designed for Unix and expects to use features such as pipes, Unix-style file and directory access e.t.c. then you can compile it for Cygwin and Cygwin will act as a compatibility layer between the code and the foreign OS (Windows). This allows the same C program to work on both Linux and Windows with little or no modification. A huge list of Linux programs have been thus complied and can be run when you install Cygwin. However you, the user of these programs, will probably have to be familiar with Unix. For example you may need to pass arguments like /cygdrive/c/autoexec.bat instead of c:\autoexec.bat and be ready to deal with output with Unix line termination (\n) instead of Windows line termination (\r\n).

The collection of all Cygwin programs comprise a repository and although Cygwin doesn't have a traditional Linux package manager you can add, remove and update programs from this repository at any time with a very counter-intuitive way (you run Cygwin's setup.exe at any time after the installation has completed)

Babun is Cygwin with a full-featured package manager (pact), oh-my-zsh for its shell, git, automatic updates and a plug-in oriented architecture. Although the default shell is zsh you can easily switch to bash. It seems like a nice upgrade from vanilla Cygwin if you don't mind the extra weight.

Mingw-w64

Mingw-w64 provides the following:

  1. a bash shell,
  2. a fair amount of Linux utilities,
  3. a SW development toolchain to build apps that will run without a Linux compatibility layer (or with a minimal one).

MinGW is primarily a software distribution and a building platform for Windows. In particular it's a Windows port of the GNU compiler tools, such as GCC, make, bash, and so on. It includes a fair amount of GNU tools and a minimal Unix compatibility layer.

The main difference between Mingw-w64 and Cygwin is on priorities: Cygwin aims to provide a complete POSIX compatibility layer comprising a full implementation of all major Unix system calls and libraries; performance is secondary to compatibility. MinGW aims for performance so it will not provide certain POSIX APIs which cannot be implemented easily and/or fast enough under Windows. Also MinGW's main target are S/W developers. Nevertheless casual users may also enjoy its shell and the general purpose included utilities.

MSYS2 and Git for Windows

MSYS2 provides the following:

  1. a bash shell and the Mintty terminal,
  2. a fair amount of Linux utilities,
  3. a full featured command line package manager to install OS programs
  4. parts from both Cygwin and MinGW to support building applications either with or without a comprehensive Linux compatibility layer.
  5. a software repository and a package manager (pacman) which makes it easy to install, use, build and port software on Windows.

MSYS2 is also primarily a software distribution and a building platform for Windows like MinGW but it combines both the way of Cygwin and the way of MinGW. You can use MSYS2 to either create "MinGW programs" or "MSYS2 programs". The later link to the compatibility layer of MSYS2 (msys-2.dll) which itself closely follows Cygwin's compatibility layer development with a few additions/modifications. The repositories of the 2 projects are completely unrelated however. So although both MSYS2 and Cygwin provide grep.exe they may have a completely different version.

Git for Windows is either just MSYS2 with git installed or at least heavily based on MSYS2. It provides the same shell (bash), terminal (Mintty) and package manager (pacman). It appears to be the most popular way to get the dominant Git running on Windows and along with it you get a nice Unix environment.

Windows Subsystem for Linux(WSL)

WSL solves the same problem using a completely different method. You run the exact same code that you would run on a Linux system on WSL which intercepts kernel calls and translates them to windows kernel calls (it's the opposite of Wine). Of course the translation can't be perfect but Microsoft droped the beta warning. Because WSL wasn't available on Windows Server before 2018 I haven't looked into it that much. Anyway you do get a nice bash shell with most CLI goodies you'd expect to find on a stock Ubuntu installation. Microsoft officially states that "WSL does not aim to support GUI desktops or applications" however there are unofficial instructions on how to do it. It seems that as things stand at the moment (early 2018) the main drawbacks of WSL are the following: a) rendering the screen and disk access are very slow b) you are not supposed to edit WSL(Linux) files from Windows c) it only runs on 64bit Windows. Here's an old but nice comparison with cygwin (note that Rich Turner is a member of the WSL team and he has posted a lot of useful info regarding WSL)

Common ground

Except from WSL most of the above tools share a lot of common technology (libraries, executables, concepts). For example the bash shell that comes with MinGW depends on msys-2.dll which itself is a fork of cygwin.dll. So yes there's plenty of room for confusion :-)

More options

Cmder provides just a nice terminal and a bash-like shell for Windows. It's main component is Conemu (the terminal). On top of that it adds Clink which provides Powerful Bash-style command line editing, a custom prompt layout and the Monokai color scheme. It is highly (or maybe fully) compatible with native windows console programs.

Scoop provides a command line package manager for many well known cross-platform programs including many GNU tools. It downloads pre-compiled packages. It does not provide neither a shell nor terminal but instead runs under windows' cmd.exe (with all its limitations but also with its full compatibility with native windows console programs). It also includes no compiler suite (but of course compilers and development tools are typical packages you can install with scoop). A lot of the programs that Scoop installs either come directly from the MinGW/MSYS project, or were built using their tools.

Even more options

The solutions bellow don't seem active and I never tested them but they seem to do the job for others:

Gow (Gnu On Windows) is the lightweight alternative to Cygwin without a shell. It uses a convenient Windows installer that installs about 130 extremely useful open source Linux applications compiled as native win32 binaries and available through windows' cmd.exe. It is designed to be as small as possible (about 10 MB).

UnxUtils & GnuWin32: UnxUtils is a collection of ports of common GNU Unix-like utilities to native Win32, with executables only depending on the Microsoft C-runtime msvcrt.dll. You must download the main part and a few updates. GnuWin32 has later versions than UnxUtils, but requires supporting files (e.g. DLLs)

MSYS & MinGW seem to have been obsoleted by MSYS2 & MinGW-W64 so I never looked at them.

Some common issues on Cygwin & MSYS2

Regarding the terminal

Mintty is the terminal used in Cygwin, MSYS2 and their derivatives. One should be aware that if you're running windows native console programs it is not a painless replacement for the Windows Command Prompt. While programs with simple text output usually work fine, interactive and full screen ones often have problems. Read more on Mintty's home page and also read the entry "Some native console programs don't work when run from Git Bash" on Git for Windows FAQ. That entry has this recommendations to make when you face this kind of problems:

There are several methods for working around these problems:

  • Run programs that have problems using the winpty utility. This allows you to keep using the nicer mintty terminal, but can become unwieldy if you need the workaround for many programs.
  • [use cmd.exe] and configure it for "Quick Edit", reasonable size and scroll-back and suitable unicode font. You'll still have to live with the other quirks of [cmd.exe].
  • Install and use Conemu.

Other issues

The two most important things to keep in mind are these:

  1. there's a trade-of between power and hard to debug issues. The more powerful the solution the more stuff can go wrong even in ways that will initially seem unrelated to your Linux-layer.

  2. if you don't have plenty of Linux experience don't use the more powerful solutions like Cygwin/MSYS2/MinGW

UnxUtils and GnuWin32 have the least power but also the least potential of causing you headaches. Cygwin and similar will top the bar in both power and headaches. Cmder is somewhere at the middle. So when you need the most powerful solutions take a good sleep and be fully concentrated. Avoid using powerful solutions while testing something new and possibly unstable. Also don't forget that medium complexity solutions like cmder are not spotless.

The issue of Linux commands shadowing windows ones is a common cause of hard to debug trouble in solutions like Cygwin/MSYS2/MinGW. Here's an example: I had a .bat file that was using the timeout command. When run under Cygwin it would fail because timeout is also a Linux command but with different syntax. After I've spotted the issue I found out I could add a PATH=... at the top of the .bat file to make sure the windows command had priority. But then I got an even more cryptic "Input redirection is not supported" error and settled for a workaround before finding the reason.

Here's an example of an issue with cmder. I was running the Windows version of unison under Cmder and under some special conditions it was hanging with absolutely no error message. The same command was always running perfectly under cmd.exe. The funny thing is that it was also running perfectly under Cygwin (in fact if you have Unicode filenames a Cygwin terminal is better than cmd.exe because cmd.exe often displays Unicode text malformed).

About this guide

During 2017-05 I got sick of not understanding the differences of the above tools although I was using some of them since long ago (mainly CygWin and cmder but never for development purposes). So I spent a few hours to grok the landscape. This guide is the result of the notes I kept. I'm not an expert on the field but I've tried hard to find all relevant information, read it with a critical eye and present the most important parts clearly. Please comment if somethings seems wrong and I'll do my best to correct it.

Finally an apology for some terminology issues: In this text I sometimes use the term Linux in place of UNIX or POSIX. I know they are not the same but it's very difficult to differentiate between them when approaching a topic from such a long distance. Even when it's not difficult it takes time that I don't have ;-)

  • 2
    This is a superb answer, thank you for sharing your findings and experience. – Daniel Sokolowski Dec 2 '17 at 19:50
  • Comprehensive answer, but it's pacman, not packman – Adrian Günter May 5 '18 at 16:54
  • How could this not be the accepted answer. Thank you for detailing all of this. – 42. Mar 9 at 2:33
10

I would suggest you download and install a minimal set of MinGW and also minimal set of MSYS. The MinGW installer gives you an easy GUI to chose what packages you want installed. If all you want is BASH, then the installation won't take more than few megabytes.

UPDATE: I moved to the MSYS2 which is far more advanced, and has port of ArchLinux pacman tool for package management. MSYS2 installer has everything you need, plus it uses mintty which is absolutely perfect .

10

There's also Clink available at GitHub. It integrates itself into windows command prompt using hook libraries.

From the project description:

Clink combines the native Windows shell cmd.exe with the powerful command line editing features of the GNU Readline library, which provides rich completion, history, and line-editing capabilities.

(so it does not provide bash-as-a-programming-language features or any other Unix goodies)

I use Linux at work and find myself sometimes tabbing around in cmd which does not act like I expect it from Bash. With Clink I can overcome some of these shortcomings.

Lately, I also discovered a promising, but not yet 'can-do-all' project called flinux which emulates Linux system functions and provides some kind of a system on top of it (currently based on Arch Linux)

  • Does Clink actually change the language, or it only improves something in cmd.exe? This is not entirely clear from the answer. – Sarge Borsch May 31 '15 at 19:35
  • It enables you to use some of the usual *nix shell features, such as reverse search using Ctrl+R, a PATH searching command completion etc. – Andreas Jun 1 '15 at 12:13
  • So it is not a bash implementation, right? – Sarge Borsch Jun 1 '15 at 13:20
  • Erm, no. An extension to cmd.exe, just as described on the site ;) – Andreas Jun 1 '15 at 13:32
7

In addition to what faffaffaff said, Git for Windows includes Bash and some common Linux command-line programs (e.g. Vim, sed, and gawk). It's based on MSYS. The version of the programs is old though, for example Bash is version 3.1. Cygwin would have more recent versions of the utilities.

I also use ConEmu as terminal emulator (mainly for syntax highlighting in Vim, tab pages, and easier copy-pasting). console2 is also another popular terminal emulator though I haven't tried it.

  • BTW: ConEmu also recognizes MSYS2 installations and allows for some Clink integration. – Andreas Jun 1 '15 at 12:15
2

This is one of the first things I do when I set up a new Windows machine:

A functional and sleek console in Windows

  • 3
    Make sure to add an excerpt from the link to prevent the effects of link rot. – oldmud0 Jun 16 '15 at 23:05
1

I just tried with Babun, which integrated the Cygwin internally, and tastes good to me : )

The download file slightly big, around 250 MB. But installation is straightforward; there is no need to configure anything and it just works!

  • Babun includes whole cygwin, it's really insanely huge. The installation process is a bit hacky (currently the zip archive is blocked by chrome as a dangerous file). But so far best terminal replacement I've found. – Tombart Dec 17 '15 at 8:41
  • Babun was really slow for me. I switched to using conemu and git shell – wordsforthewise Mar 23 '17 at 23:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.