Set up WSL

Completed

You can install Windows Subsystem for Linux (WSL) in just a few steps. We’re using the wsl –install command. This command enables the features necessary to run WSL and install the Ubuntu distribution of Linux.

Note

You must make sure that you're running Windows 11 or Windows 10 version 2004+ (Build 19041 and higher) to run the WSL install command.

We're also going to create a Python project to later use in Visual Studio Code.

This module is focused on how we can use WSL and Visual Studio Code. However, WSL install is independent of Visual Studio Code, and you can use WSL anywhere and on its own.

Installing WSL and a Linux distribution

Screenshot of the Windows terminal and the WSL install command.

Enable the Windows Subsystem for Linux

  1. Open PowerShell as Administrator and run the following command in the terminal:

    wsl --install
    

    This command enables the necessary Windows features and installs the Ubuntu distribution by default. You can later choose to install a different Linux Distribution. Given its popularity, we're using Ubuntu for this module.

  2. Restart your computer to complete the installation and apply changes.

Set up your Linux distribution

A Linux distribution (distro) is a version of the Linux operating system which is tailored with certain software and settings for various needs or preferences. Ubuntu is a popular Linux distro known for its user-friendly interface and extensive software ecosystem, making it an accessible choice for many users.

  1. Upon restart, open the newly installed Linux distro from the Windows Start menu.

  2. Follow the prompts to create a user account and password for your Linux distro.

    Note

    If you were prompted to create a user account and password during installation, you can skip this step.

Create a Python project

Many languages work in WSL, and we selected Python for this module. It’s a great language for beginners because it has a simple and readable syntax, offers a wide range of libraries and resources, and has a large and supportive community. This ecosystem makes it easy for beginners to find help and resources when they encounter challenges.

Install Python

Python should be installed by default on your WSL distro. To verify, run python3 -–version in your WSL terminal. If you don't see a version displayed, then run the following commands:

sudo apt update
sudo apt install python3 python3-pip

The WSL file system

Now, you need to create a folder for your project. When you work in WSL, your files are stored in the WSL file system. Both your Windows and Linux files are stored on the same physical hard drive. However, they exist in separate file systems:

  • WSL File System: The files are stored in a Linux-compatible format. This system is separate from the native Windows file system and is designed to support Linux operations and tools.

  • Windows File System: The standard file system used by Windows, optimized for Windows applications and tools.

Although both file systems coexist on the same physical drive, it's important to manage files within their respective environments to maintain compatibility and performance.

WSL allows access to Windows files, but for development—especially with tools like Visual Studio Code—we recommend that you store and manage files in the WSL file system.

Create a folder in WSL

  1. To create the folder, use the following command:

    mkdir helloWorld
    
  2. To navigate to that folder, use the following command:

    cd helloWorld
    
  3. To verify the folder you're in, use the following command:

    pwd
    

    You should get a message printed on the screen showing the path of the folder you're currently in.

  4. To create a new Python file named hello.py that prints a hello message when executed, use the following command:

    echo 'print("hello from python on ubuntu on WSL!")' >> hello.py
    
  5. To run your new program, use the following command:

    python3 hello.py
    

Great, now we have a Python project to continue working with. Let's look at how we can set up the Visual Studio Code WSL extension.