How to Use Windows Subsystem for Linux 2 and Windows Terminal

In this article, you’ll learn how you can use Windows Subsystem for Linux 2 to 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, you 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.

Enabling the Windows Subsystem for Linux feature

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.

Microsoft Store Linux

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:

Installing Ubuntu in the command line

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

Ubuntu usage commands

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.

VS Code WSL Terminal

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

Installing Node.js

The best way to install and configure Node.js is via the nvm. Inside the Linux shell, execute the following commands:

$ sudo apt-get install build-essential
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
$ nvm install --lts

The above commands should install the most recent LTS version of Node.js. In case you run into a problem executing nvm command, add the following line in .bashrc file:

source ~/.nvm/nvm.sh

You’ll need to restart the Linux shell or simply execute the command source ~/.bashrc before trying to execute the nvm command. Once Node.js installation is complete, try installing an npm package globally to confirm that it’s working.

You can also navigate to an existing Node.js project on your Windows drive and try running it to confirm that it’s working. A very cool feature of Windows Subsystem for Linux is that you still use localhost when you run web applications. Node.js does run completely fine on Windows. Hence, it’s not such a big benefit for Windows Subsystem for Linux users. Where it really shines is in open-source, back-end frameworks such as Django, Ruby on Rails and the Lamp Stack. Setting those up in Windows can sometimes be a challenge. For certain projects, it can be impossible due to lack of Windows support for certain open-source libraries. With WSL2, this is no longer a problem. In the next section, we’ll install the LAMP stack on Windows, which I find easier to install and configure than the WAMP stack.

Installing LAMP on Windows

Inside the Linux shell, type the following command to install Apache2, MySQL and PHP. It’s a sizable download, so give it a bit of time to complete:

$ sudo apt-get install lamp-server^

Next, let’s start and configure our MySQL database:

# Start the MySQL service
$ sudo service mysql start
# Define password for root@localhost
$ sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
exit

You can also use CTRL + D to exit from the MySQL shell. Next, let’s configure and start the Apache web server. Using vim, nano or your favorite text editor, add the following two lines to /etc/apache2/apache2.conf:

Servername localhost
AcceptFilter http none

Start Apache web server using the command sudo service apache2 start. Navigate to http://localhost to confirm that Apache has successfully started:

Apache running

Next, let’s install PhpMyAdmin:

$ sudo apt-get install phpmyadmin

Add the following line to /etc/apache2/apache2.conf:

Include /etc/phpmyadmin/apache.conf

Reload the Apache web server for the new configuration to take effect:

$ sudo service apache2 reload

To confirm that phpmyadmin has been successfully configured, navigate to the URL http://localhost/phpmyadmin. Enter the MySQL user credentials you set up earlier. (Do note that username is root). You should have a similar view:

phpmyadmin

That’s how simple it is setting up LAMP on Windows. This should be a sufficient demonstration that you can comfortably set up a Linux development environment on Windows. In the next section, we’ll briefly look at the new Windows Terminal that Microsoft is developing.

Windows Terminal

Windows Terminal is an open-source project by Microsoft that aims to deliver features requested by the community such as:

  • rich text
  • globalization
  • tabs support
  • theming and styling
  • and many others …

Implementing these features on the existing console interface was not possible without breaking compatibility with older console applications. Hence the Windows Terminal project was created that allowed developers to drastically change the architecture without worrying about backward compatibility. The project is still currently under development, so expect to see more features released in the future. Let’s take a look at how we can install and customize the new Windows Terminal.

Installing and Upgrading

Before you can install Windows Terminal, make sure you have the latest Windows 10 build on your machine. Otherwise, installation will fail with older builds. There are currently two ways you can install Windows Terminal. The first method is via the Microsoft Store:

Windows Terminal Store

The second method is using the Chocolatey tool, which is like npm for Windows applications. Once you have Chocolatey installed on your machine, you can install Windows Terminal on your machine via PowerShell like this:

choco install microsoft-windows-terminal

You should expect a similar output. In my case, I was upgrading:

Upgrade Windows Terminal Choco.png

Once the installation is complete, you can launch it from your Start Menu. The default terminal you will find is PowerShell. Whenever you click the + sign, a new tab will be created using the default terminal. Using the dropdown menu, you can select a different type of terminal.

Windows Terminal

If you click on Settings, the default editor for JSON will open. For you, it could be NotePad, NotePad++, VS Code or any other editor. I prefer VS Code or something similar because it auto-closes JSON tags when typing. If you look carefully, you’ll notice sections for:

  • Globals — global settings: e.g. default profile, tab settings, terminal size etc.
  • Keybindings — shortcut keys
  • Profiles — terminal launches: e.g. PowerShell, Command Prompt
  • Schemes — color schemes

As you can see, it’s very easy to understand and customize the settings in JSON format. One quick tip I’d like to show is for users like myself who installed an older version of Windows Terminal. Some of you have probably noticed some profiles are missing on the above screenshot. Despite updating the app, my profile settings remain the same. To quickly add the missing settings without manually doing so, all I have to do is this:

  1. Close the Windows Terminal App completely
  2. Delete profile.json (Located in %USERPROFILE%\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState)
  3. Relaunch the terminal app

Updated settings

In addition, new shortcut keys were added, which include the following:

{
    "command" : "closePane",
    "keys" :
    [
        "ctrl+shift+w"
    ]
},
{
    "command" : "copy",
    "keys" :
    [
        "ctrl+shift+c"
    ]
},
{
    "command" : "duplicateTab",
    "keys" :
    [
        "ctrl+shift+d"
    ]
},
{
    "command" : "paste",
    "keys" :
    [
        "ctrl+shift+v"
    ]
},

Let’s now learn how we can style our terminal in the next section.

Styling the Console

Styling a terminal is handled in the settings under each profile. You can customize each profile by changing any of the following attributes:

"acrylicOpacity" : 0.2,
"useAcrylic" : true
"colorScheme" : "One Half Dark",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "Consolas",
"fontSize" : 10,
"padding" : "0, 0, 0, 0",
"background":"#002B36",
"backgroundImage": "<insert image path here>"
"backgroundImageOpacity": 1,
"backgroundImageStretchMode": "none"

I believe the above settings are pretty self explanatory, except for acrylic. This is a background effect that makes the terminal’s background slightly transparent and blurry. acrylicOpacity controls the transparency. To have a completely opaque background, set useAcrylic to false. You can use GIF for the backgroundImage. The animation will play automatically. Pretty cool, right?

The colorScheme comes from the schemes sections. The ones that come by default are:

  • Campbell
  • One Half Dark
  • One Half Light
  • Solarized Dark
  • Solarized Light

Summary

Let’s wind up this quick introduction to Windows’ new development tool and platform. They’re still fresh from the oven, so expect some bugs — which will get sorted out in the near future. Let me know if you plan to switch from VM-based development to developing on Windows Subsystem for Linux 2.

Replies

  1. 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:

  2. 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.