How to Use Windows Subsystem for Linux 2 and Windows Terminal

Originally published at:

In this article, you’ll learn how you can set up and run a local Linux shell interface in Windows without using a virtual machine. This not like using terminals such as Git Bash or cmder that have a subset of UNIX tools added to $PATH. This is actually like running a full Linux kernel on Windows that can execute native Linux applications. That’s pretty awesome, isn’t it?

If you’re an experienced developer, you already know that Linux is the best platform on which to build and run server-based solutions using open-source technologies. While it’s possible to run the same on Windows, the experience is not as great. The majority of cloud hosting companies offer Linux to clients to run their server solutions in a stable environment. To ensure software works flawlessly on the server machine just like on the local development machine, you need to run identical platforms. Otherwise, you may run into configuration issues.

When working with open-source technologies to build a project, you may encounter a dependency that runs great on Linux but isn’t fully supported on Windows. As a result, Windows will be required to perform one of the following tasks in order to contribute to the project:

  • Dual Boot Windows and Linux (switch to Linux to contribute code)
  • Run a Linux virtual machine using a platform such as Vagrant, VirtualBox, VMWare etc.
  • Run the project application inside a Docker container

All the above solutions require several minutes from launch to have a full Linux interface running. With the new Windows Subsystem for Linux version 2 (WSL2), it takes a second or less to access the full Linux shell. This means you can now work on Linux-based projects inside Windows with speed. Let's look into how we can set up one in a local machine.

Installing Ubuntu in Windows

First, you'll need to be running the latest version of Windows. In my case, it's build 1903. Once you've confirmed this, you'll need to activate the Windows Subsystem for Linux feature. Simply go to Control-Panel -> Programs -> Turn Windows feature on or off. Look for "Windows Subsystem for Linux" and mark the checkbox. Give Windows a minute or two to activate the feature. Once it's done, click the restart machine button that appears next.

Next, go to the Windows Store and install Ubuntu. The first Ubuntu option will install the latest versions. Other Ubuntu options allow you to install an older supported version.

Once the installation is complete, you'll need to launch it from the menu. Since this is the first time, you’ll need to wait for the Ubuntu image to be downloaded and installed on your machine. This is a one-time step. The next time you launch, you’ll access the Linux Shell right away.

Once the image installation is complete, you’ll be prompted to create a new root user account inside this shell:

After you’ve created your credentials, feel free to type any Linux command to confirm you’re truly accessing a native Linux shell:

You’ll be pleased to note that git, python3, ssh, vim, nano, curl, wget and many other popular tools are available out of the box. In a later section, we'll use sudo apt-get command to install more frameworks. First, let's look at several ways we can access this new Linux shell terminal interface. It's probably a good idea to upgrade currently installed packages:

$ sudo apt-get update && sudo ap-get upgrade

Accessing Linux Shell Interface

The are several interesting ways of accessing the Linux shell interface.

  1. Go to Windows Menu Start > type "Ubuntu". You can pin it to Start for quicker access

  2. Open Command Prompt or Windows PowerShell and execute the command bash

  3. In Windows explorer, SHIFT + right-mouse click a folder to open a special context menu. Click Open Linux shell here.

  4. In Windows explorer, navigate to any folder you desire, then in the address bar type wsl, then press enter.

  5. In Visual Studio Code, change the default terminal to wsl.

If you come across new ways, please let me know. Let's set up Node.js in the following section.

1 Like

So, here I am evaluating whether SitePoint is the right resource to point my undergraduate students at.

Short answer NO!

If this was my first detailed look at SitePoint (BTW: Your home page is blank when viewed with Firefox) it would also be the last. Good thing is, I examined SitePoint before and saw some potential, so there will be an opportunity for “second chances”.

I’m not sure who the target audience for this article was. It certainly didn’t hit the mark for an experienced Linux user who has to suffer Windows. It also didn’t hit the mark for millennials addicted to their mobile phones, that are trying to get into development.

SitePoint - here’s a suggestion… Identify your audience first, then focus the message at that audience. Articles like this target no-one and deliver value to very few (if any) readers.

BTW: I can see that a considerable amount of effort was put into this article, which is appreciated. The problem is with the identification of the audience and the focus on their needs. +1 for effort.

If you do point them here, please be sure to tell them the forums are here to help, not answer their homework for them.

The Forums very quickly learn professors’ curriculum based on repeated asking of the same, very specific, example problems… :wink:

I’ve never seen the home page blank before in any browser, and it works for me in a clean Firefox install. Feel free to provide more details such as Firefox version and installed extensions and we’ll look into it.

I personally enjoyed the article, although I would have preferred to see more information on the changes made in WSL 2 and what new functionality this brings to the table. For example, how does version 2 change/improve/integrate with tools like Docker and Vagrant? What are the networking configuration options? Can we easily compile custom kernel modules now from within WSL 2 images, or is the kernel a very specific build for Windows and doesn’t work that way? Is this now a complete replacement for VirtualBox for people that only care about GNU/Linux guests?

Even though I rarely use Windows, I do find the technology somewhat interesting since Windows feels too frustrating to use without the full power of a GNU user-land. WSL for 1 was a good effort, but overall just had too many issues since it wasn’t a real Linux kernel.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.