Fun with Windows Terminal - WSL / Ubuntu

JJ Bussert | 04/02/2020

Previously I explained how to get started with windows terminal by setting up PowerShell Core as the default profile, but why would you want multiple terminal profiles? IMO one of the best use cases is WSL (Windows Subsystem for Linux). The ability to run a linux distro locally on Windows isn't groundbreaking, Virtual Machine tech has been around forever, but what Microsoft did with WSL makes it so convenient and easy to use compared to spinning up a traditional VM. The setup I'll go through below enables you to interact with a linux distro, in this case Ubuntu, but work against your local C drive from windows just like you would from a traditional cmd or powershell terminal. Combine that with Windows Terminal's tabbed profiles and you have the ability to bounce between different environments all running on the same system seamlessly. And even though you could set all of this up by hand... who has time for that!

If you want to use the same icons and backgrounds as me you can find them in my terminal github repository where I sync my setup to.

  1. Go to the Microsoft App Store > Search for "WSL" WSL

  2. For this walkthrough choose Ubuntu Linux Distros

  3. Install WSL Feature - Execute the following powershell command from an elevated terminal

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    
  4. Now that WSL and the Ubuntu distro are installed launch Ubuntu from the start menu Launch Ubuntu Install Ubuntu Configure Ubuntu The steps are intuitive, last one being setting up a username for yourself in this distro

  5. Launch Windows Terminal and execute code $settings > this will bring up VS Code with the Windows Terminal profile.json in it, look for a section with commandline="Windows.Terminal.Wsl" and name="Ubuntu" and update it like below.

    {
      "$schema": "https://aka.ms/terminal-profiles-schema",
    
      "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bb}",
    
      "profiles":
      {
          "defaults":
          {
              "fontFace":"Delugia Nerd Font",
              "acrylicOpacity": 0.8,
              "useAcrylic": true,
              "startingDirectory": "C:\\_"
          },
          "list":
          [
              ...
              {
                  "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                  "hidden": false,
                  "name": "Ubuntu",
                  "backgroundImage": "C:\\_\\_terminal\\ubuntu-logo.png",
                  "backgroundImageOpacity": 0.3,
                  "backgroundImageAlignment": "bottomRight",
                  "backgroundImageStretchMode": "none",
                  "source": "Windows.Terminal.Wsl",
                  "icon": "C:\\_\\_terminal\\ubuntu-icon.png"
              },
              ...
          ]
      },
    
      ...
    }
    

    Mostly repeated from part 1, let's look at the important sections:

    • [8-14] : For me I wanted to have some settings the same across profiles such as font and starting directory
    • [19-27] : In order to more easily identify the terminal type (but mostly because it's fun) I've updated the profile with and icon and background image.
  6. Now in Windows terminal you should see Ubuntu as an option for a new tab and you should see something like this: Starting Ubuntu Terminal And as great as this is, we can do better so lets add powerline

  7. Execute the following in the new Ubuntu terminal

    sudo apt-get update 
    
    sudo apt install golang-go
    go get -u github.com/justjanne/powerline-go
    
    • [1] : This will update the package repository cache, only needed for an initial install
    • [3-4] : Installs Go and uses it to install powerline-go
  8. Execute code ~/.bashrc and add the following to the bottom

    GOPATH=$HOME/go
    function _update_ps1() {
        PS1="$($GOPATH/bin/powerline-go -modules nix-shell,venv,ssh,cwd,perms,git,hg,jobs,exit,root,vgo -error $?)"
    }
    if [ "$TERM" != "linux" ] && [ -f "$GOPATH/bin/powerline-go" ]; then
        PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
    fi
    

    I'll start with launching VSCode from the ubuntu terminal, assuming this is the first time you've done this the code server will be downloaded into the terminal. This little gem will allow you to launch VS Code from your local machine against a file w/in Ubuntu. This looks trivial because of how seamlessly this is delivered but there is a lot going on under the covers here.

    Adding these lines will enable powerline-go as part of bash. One tweak I made to this code taken from the powerline-go installation page was changing the list of default modules being loaded on line [3] to remove the username@computer from the powerline.

I'd love to take credit for figuring all of this out but Scott Hanselman posted the article that I followed originally to get this setup going, these steps are my adaptation building upon his work. You can see below a screenshot of the final result.

Final Ubuntu Terminal

What do you think of WSL running Ubuntu via Windows Terminal? What is your linux distro of choice? What different customizations did you make? Share in the comments below?

Special thanks to unsplash-logoDaniel Josef for the photo used in this post!