Visual Studio Toolbox

Bash on Windows: Getting Productive with Windows Subsystem for Linux

Bash on Windows: It's new, it's exciting, but what can you do with it? We'll show you how the magic works (including some extensions for Visual Studio developers)

Full disclosure: I love working at a proper command line interface. My long, strange trip with computers started with a Unix terminal (at the Lawrence Hall of Science computer lab in Berkeley, CA). My first years getting paid to work in software development involved lots of time on MS-DOS systems (and a little time on SGI and HP-UX systems). Even today I spend a lot of time in various terminal environments scripting up quick solutions to whatever challenges a day at work presents.

That's why I was really excited to hear the "Bash on Windows" announcement from Build 2016. Hey, there's nothing wrong with PowerShell or even ye olde Windows command prompt -- they are solid, well-known solutions and get the job done. But this was big news.

There are three reasons why a Unix-style terminal will be a terrific addition to the Windows toolset for developers. First, it brings a huge new universe of easily installed and highly configurable languages, frameworks, and tools to bear on your work within your familiar Windows environment. Second, it brings an equally huge universe of how-to expertise and code examples that you can leverage, from the most mundane shell scripts to task-specific code compilation. Third, it gives you a consistent working environment no matter what kind of system you log into: Linux servers, Mac OS terminals, Raspberry Pi...and now Windows 10.

Windows Subsystem for Linux
The official name for this new *nix environment on Windows is Windows Subsystem for Linux (WSL). It's essentially a layer of components that allows you to run native Linux ELF64 binaries without recompilation. As a practical matter, what this means for most users is a fully functional Ubuntu environment running as an application -- not a VM! – that is able to directly interact with the NT kernel and file system.

Windows Subsystem for Linux as Seen Through the Bash Prompt
[Click on image for larger view.] Figure 1: Windows Subsystem for Linux as Seen Through the Bash Prompt

For more background on WSL, I recommend starting out by watching the original Running Bash on Ubuntu on Windows! announcement and demonstration by Russ Alexander and Rich Turner from Build 2016, which is available on Channel 9. Follow up with Ubuntu on Windows -- The Ubuntu Userspace for Windows Developers, by Dustin Kirkland of Canonical, which provides a bit more detail about the implementation details of WSL from a user perspective.

If you want to learn more about under-the-hood details of WSL, see Jack Hammons' Windows Subsystem for Linux Overview, which covers the history of previous Unix subsystem support before walking down the call stack to show how the magic happens. And you definitely want to follow the Windows Command Line Tools For Developers blog for ongoing release updates and deep dives on specific facets of the technology such as WSL File System Support.

So how does one get to play with WSL in the wild? Eventually it will be baked into a regular Windows 10 update. For now, however, you'll need build 14316 or later, available through the Windows Insider Program. See How to: Enable the Windows Subsystem for Linux for a walkthrough of the update process and where to find the settings to enable WSL.

So, what can you do with this Linux command line once you get it turned on?

Installing Software in WSL
One of the nifty features of a full Ubuntu environment is the ability to use its built-in package management and huge existing library of software packages. With a brief command line instruction you can install any of thousands of common packages such as Ruby or Python, Apache or Nginx...the list goes on and on. Check out An apt-get primer from Linux.com to get a handle on the basics. (Note that this 2004 is written in the context of the Debian Linux distribution, which is core distro on which Ubuntu is based.)

Be aware that WSL is totally new and still in beta, so there are bound to be some glitches with installing some of the more complex software packages. You're operating on the bleeding edge here. If you run into problems, check online: There may be a workaround, updates to the source packages, or an update to WSL that addresses the problem.

Among my particular circle of friends and colleagues, getting a working Java development environment set up in WSL was a vexing priority...particularly since the release available in June didn't seem to install the JDK properly through a simple "apt-get install default-jdk" command. For now, if you need to install the JDK on WSL, start with rs6g10's comment on the BashOnWindows GitHub issue Unable to install Java JDK or Runtime (that's a direct link to the comment that provides a working manual installation procedure). Ongoing discussion specifically related to JDK 8 installation is covered in the issue Cannot install Oracle JDK 8.

For background information and additional manual installation of JDK 7, see [HOW TO] Install Oracle JDK 7 Manually in Ubuntu over on Xubuntu Geek, and How To Manually Install Oracle Java on a Debian or Ubuntu VPS in the DigitalOcean help knowledge base. As I mentioned previously, there's a metric boatload of excellent help available from the Linux to get you from the basics to tricky installations.

Want to run Mono directly within WSL? There's an issue on the Mono _website_ GitHub repo that covers the installation process and some troubleshooting: Mention Windows Bash? · Issue #199. This will probably get folded into the Mono documentation at some point, but support on both Mono and WSL sides is unofficial at this point.

Chrissy LeMaire has a great preview of a sample app she's brewing up for a presentation that uses not just PHP, not just Apache, but Active Directory and PHP on Apache on Bash on Ubuntu on Windows. Super cool, and she has a bunch of sample code on GitHub for you to see the details.

And then there's this: "'[WSL] doesn't include X Windows or any other graphical subsystem.' Oh, why did Microsoft have to go and make that a challenge?" Daniel Aleksandersen couldn't resist the urge to try Running Linux desktop apps on the Windows Subsystem for Linux using an external X server, and actually got a few simple programs running.

Some Related Visual Studio Extensions
WSL is handy all on its own, and yet here are some Visual Studio extensions that help integrate it right into your development workflow -- or integrate WSL-focused work into your familiar Visual Studio tools.

Our good friend Mads Kristensen has, of course, served up a handy extension in Open Command Line, which provides a menu item and keyboard shortcut for opening a command prompt at the current project path. You can configure which command prompt it opens: the Windows command prompt, PowerShell, Bash, or any other CLI installed on your system. Open Command Line also includes syntax coloring and IntelliSense for .cmd and .bat batch files.

Since you may be coding up Bash shell scripts, check out Anthony Berkow's BashSyntax extension. It provides simple syntax highlighting for commands, keywords, numbers, operators, strings, and comments. It works on .sh files, but also any plaintext file that starts with a "#!/bin/bash" line. TextHighlighterExtension2013 and TextHighlighterExtension2015, by Frederic Torres, provide syntax coloring for .bat, .cmd, .ini, .Log4j.properties, .sh, .txt, .log, and .json (2015 only) files. Coloring is completely customizable, of course. Torres has been maintaining TextHighlighter for a long time. It's in good hands.

TextHighlighter Provides Syntax Coloring for Many Terminal-Focused Files
[Click on image for larger view.] Figure 2: TextHighlighter Provides Syntax Coloring for Many Terminal-Focused Files

Finally, Kirk Woll's vs-mono allows you to write Mono-based C# projects in Visual Studio with support for building, deploying, and remote debugging code on Linux-based systems, including Raspberry Pi or Lego EV3 embedded systems. Relevant to this column: vs-mono can use WSL as its remote build server so you don't even have to set up a Linux VM and all the network routing spells that requires. Early days for this project, so watch the vs-mono GitHub project for news and issues.

What Are You Using in WSL?
This is just the beginning of an exciting new chapter for software development and devops with Bash on Windows. If you are incorporating WSL into your work -- or building tools that integrate with WSL -- please let us know about it. We'll definitely share them here, and probably use them ourselves. Ctrl+D

About the Author

Terrence Dorsey is a technical writer, editor and content strategist specializing in technology and software development. Over the last 25-plus years he has worked on developer-focused projects at ESPN, The Code Project, and Microsoft. Read his blog at http://terrencedorsey.com or follow @tpdorsey on Twitter.

comments powered by Disqus

Featured

Subscribe on YouTube