How to install WSL 2 + Ubuntu 20.04 LTS on Windows and open Visual Studio Code from the terminal

Mwatsefu Mwarika
8 min readJul 26, 2023

--

Ubuntu + Windows

Windows Subsystem for Linux (WSL) allows you to install a complete Ubuntu terminal environment in minutes on your Windows machine, allowing you to develop cross-platform applications without leaving Windows.

In this tutorial, I’ll show you how to get up and running with Ubuntu on WSL. These instructions will work on both Windows 10 or Windows 11.

Check That your Windows system is Compatible

When working on software, your computer and the software you’re using must be compatible. In this step, I’ll ensure that you are able to install all the tools that you will need to complete the program.

Make sure your laptops has at least 8gb of Ram and 1GB free storage space

Install Visual Studio Code (VS Code) on Windows

Download Visual Studio Code

Visual Studio Code (VS Code) is a code editor redefined and optimized for building and debugging modern applications. It is a text editor that provides some really useful extensions for developers. One such tool is an integrated terminal! This means that you can edit your code and use your terminal in the same window. Follow the steps below to get both set up

Action item: download and install Visual Studio Code

  1. Open the Visual Studio Code Download webpageLinks to an external site. https://code.visualstudio.com/DownloadLinks to an external site. in your browser
  2. Click on the Windows download option
  3. Download and run the Visual Studio Code installer
  4. Complete the installer
  5. If you can open the “Visual Studio Code” application using the “Start” menu, continue below.

Install the “Ubuntu” Application from the Microsoft Store

Ubuntu application on Microsoft Store

You are ready to install and set up the Ubuntu operating system! Ubuntu is a Linux-based operating system and this is the application we’ll use to run the remainder of the environment setup.

Action Item: Install the “Ubuntu” Application

  1. Open the “Microsoft Store” application using the “Start” menu
  2. Search for “Ubuntu” (note: do not choose “Ubuntu 16.04 LTS”, “Ubuntu 18.04 LTS”, or “Ubuntu 20.04LTS”. Instead, choose the option that has no number next to it.)
  3. Click “Get” and “Install” and wait for the application installation to complete
  4. Open the “Ubuntu” application

If you can open the “Ubuntu” application using the “Start” menu, do not type anything into the window, and continue to the next section, Setting up WSL2.

Setting Up WSL2

Windows Subsystem Linux (WSL) is not automatically enabled on windows. So, to start, we need to enable it!

Enable the Windows Subsystem for Linux (WSL) and Virtual Machine Platform

Now that we know your computer is ready for the rest of the environment setup, we need to enable two more settings before moving on. We’ll be using the Windows Subsystem for Linux (WSL) and the Virtual Machine Platform to create a space on our computer for our Ubuntu operating system.

Action Item: Enable Windows Subsystem for Linux (WSL) and Virtual Machine Platform Settings

  1. Open the “Turn windows features on and off” application using the “Start” menu
  2. Check the box next to “Virtual Machine Platform”
  3. Check the box next to “Windows Subsystem for Linux”
  4. Click “OK” to save your work; your computer should restart

If you can check both of those checkboxes and your computer restarts, continue below. Otherwise, reach out to your teaching team for next steps.

Set up the “Ubuntu” Application

After you have installed the “Ubuntu” application from the Microsoft Store and enabled Windows Subsystem for Linux (WSL) and the Virtual Machine Platform, we have to set up the Ubuntu operating system.

Action Item

  1. Open the “Ubuntu” application using the “Start” menu
  2. When it says “Enter new UNIX username:” add a simple username and press <Enter> (Note: usernames may not start with a number, usernames may not include capital letters)
  3. Where it says “New password:” add a simple password and press <Enter> (Note: you will not see any text when you are typing your password.)
  4. Where it says “Retype new password:” retype the same password from before and press <Enter> (Note: store this password somewhere safe. You will need it to be able to run commands in the future)
  5. The terminal should output “Installation successful!” and then print about 50 lines that you can ignore

Now, the last line in your “Ubuntu” application should say your username + “@DESKTOP” + some random numbers and letters. If you see that, continue below.

Update the Windows Subsystem for Linux (WSL) to WSL 2

Now that we have the Windows Subsystem for Linux (WSL) enabled and we have the “Ubuntu” application installed, we can update WSL to version 2 and update the “Ubuntu” application to use WSL 2.

Action Item

  1. Download the WSL Update file by visiting the following webpage in your browser: https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msiLinks to an external site.
  2. Open the file, follow the prompts, allow the program to make changes to your device, and click “Finish”
  3. Search for the “Command Prompt” application using the “Start” menu
  4. Select “Run as administrator” from the right side of the search window
  5. Allow the program to make changes to your device and wait for the “Command Prompt” application to open
  6. Type wsl --set-default-version 2 into the terminal and press <Enter> (Note: you should see a message starting with "For information on key differences…")
  7. Type wsl --set-version Ubuntu 2 into the terminal and press <Enter>
  8. Wait for the “Conversion complete” or “This distribution is already the requested version” message in the terminal

If you saw the “Conversion complete” or “This distribution is already the requested version” message in the “Command Prompt” application, close the “Command Prompt” application and continue below. Note: If you encounter an error message that you need to enable the Virtual Machine Platform, but you’ve already enabled it, you may not be able to use WSL2. However, you may still be able to use WSL1. Run wsl — set-default-version 1, then run wsl — set-version Ubuntu 1. Wait for the “Conversion complete” or “This distribution is already the requested version” message in the terminal, then continue on with these instructions.

Configure VS Code to Work with WSL

Action Item

  1. Open the “Visual Studio Code” application using the “Start” menu
  2. Click “View” in the toolbar, then click “Extensions” in the dropdown menu, or use the shortcut <Control> + <Shift> + X
  3. Search for “Remote — WSL” and click on the item in the list with the same name (Note: the description should start with “Open any folder in the Windows Subsystem for Linux (WSL) …”)
  4. Click the “Install” button near the top of the page
  5. Click “Terminal” in the toolbar, then click “New Terminal” (Note: a new terminal should appear at the bottom of your VS Code window)
  6. Click on the dropdown in the terminal that says “1: powershell” and choose “Select Default Profile”
  7. A dropdown should appear at the top of your VS Code window
  8. Click on “Ubuntu (WSL)” to enable VS Code to display your Ubuntu terminal
  9. Close the “Visual Studio Code” application
  10. Open the “Ubuntu” application using the “Start” menu
  11. Type code and press <Enter>

If the “Visual Studio Code” application opens when you type code in the "Ubuntu" application, continue to the next section, Installing Node on WSL2.

Installing Node.js on WSL2

Install Node Version Manager (NVM) on Ubuntu

Node Version Manager (NVM) is a tool that will allow you to download and install multiple versions of Node.js, one of the environments for the JavaScript programming language. Installing NVM is the first step in installing Node.js on your Ubuntu operating system.

Action Item

  1. Open the “Ubuntu” application using the “Start” menu
  2. Type curl -o- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash and press <Enter>
  3. Type curl -sSL https://raw.githubusercontent.com/flatiron-school/dotfiles/master/nvm-shell-script >> ~/.bashrc and press <Enter>
  4. Close the “Ubuntu” application
  5. Reopen the “Ubuntu” application
  6. Type nvm and press <Enter>

Check Your Work

If you see a message ending with “Note: to remove, delete, or uninstall nvm”, continue below. Note: If nvm is not recognized, or you are seeing new errors when restarting the “Ubuntu” application, check out the troubleshooting steps in the Verify and Troubleshoot your WSL2 Environment Setup lesson later on in this module.

Install Node.js on Ubuntu

If Node Version Manager (NVM) has been successfully installed, you can quickly install Node.js with a couple of commands.

Action Item

  1. Open the “Ubuntu” application using the “Start” menu
  2. Type nvm install node and press <Enter>
  3. Type nvm list and press <Enter>

If you see a message starting with “-> v14.13.0” (or any higher number, like “-> v14.13.10” or “-> v15.0.0”), continue

Configuring Git and GitHub on WSL2

Create a GitHub Account

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere. You will need to sign up for a GitHub account if you don’t already have one.

Action Item

  1. Open the GitHub signup webpageLinks to an external site. at https://github.com/joinLinks to an external site.
  2. Fill out the form and create your account
  3. Verify the email address connected to your GitHub account

If you were able to verify your email address, continue below.

Configure Git and GitHub

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. To use Git without signing in every time, you can create a Secure Shell (SSH) key and associate that to your GitHub account. Lastly, you will want to run a few commands to make sure that when you use Git, you get the proper credit for your work. This step will ask you to do work both in your browser and your terminal.

Action Item: Update Git

  1. Open the “Ubuntu” application using the “Start” menu
  2. Type sudo add-apt-repository ppa:git-core/ppa and press <Enter> to add a package repository for downloading the latest version of Git. Follow the prompts in the terminal.
  3. Type sudo apt update and press <Enter> to update your local repository cache
  4. Type sudo apt install git and press <Enter> to install the latest version of Git

You can check your work by typing git --version in the terminal. You should see a version greater than 2.30.0.

Action Item: Configure Git

  1. Open the “Ubuntu” application using the “Start” menu
  2. Type git config --global color.ui true and press <Enter>
  3. Type git config --global user.name + <Space> + your name and press <Enter> (Note: this should be your full name, not your GitHub username, in quotes.)
  4. Type git config --global user.email + <Space> + the email address you used to sign up to GitHub and press <Enter>
  5. Type git config --global init.defaultBranch main to update the default branch nameLinks to an external site. to main
  6. Type ssh-keygen and press <Enter>, for each prompt do not type anything, just continue to press <Enter>. It's particularly important that you do not enter a passphase and leave the passphrase empty when prompted; otherwise, you'll have to enter that passphrase any time you interact with GitHub (which will happen a lot); and you may run into issues submitting assignments later.
  7. Type cat ~/.ssh/id_rsa.pub | clip.exe and press <Enter>. This will copy your SSH key to your clipboard
  8. Open the GitHub New SSH key formLinks to an external site. (https://github.com/settings/ssh/newLinks to an external site.) (Note: you need to be logged in to GitHub to access that link.)
  9. Type “My personal PC” in the “Title” input field
  10. Paste what’s on your clipboard from step seven in the “Key” input field
  11. Click “Add SSH Key”

If you see your new SSH key beneath the “SSH keys” heading, all is successful.

--

--