🛠️Install a Ubuntu Distribution through WSL2

wsl🤩In this note, we are going to learn how to install WSL2. If you want to learn about the differences between WSL2 and WSL1, please refer to this official docs. For more information, please refer to references.

Instructions

Installation

  1. use wsl --list --online to view available Linux distributions that can be downloaded through the online store

    wsl --list --online
  2. use wsl --install -d <distribution name> to install the certain distribution and this well a few minutes for the first time. For example, in this note, Harry installed Ubuntu-20.04, for which he used wsl --install -d Ubuntu-20.04. If you counter some errors, please refer to Additions

    Successful installation

Verification

  1. use wsl -l -v to view the installed distributions' name, state and version.

  2. Once installed, there are several ways to run a Linux distribution. Here are some recommend ways:

    1. Typing the name of the installed distribution in the Windows Start menu. For example, "Ubuntu". This opens Ubuntu in its own console window.

      search "Ubuntu" in the Windows Start menu
    2. Typing the name of the installed distribution in cmd or Windows Powershell. Type wsl or wsl.exe will open the default distribution. To change default setting, refer to this.

      NOTE: If you have installed visual studio code, you can type code . in the cli of Ubuntu to connect Linux server with VS Code server.

      connect VSC with linux server
    3. For more information, refer to this.

Quit and Stop

  • When in a WSL, you can use ctrl+D or logout to go back to powershell or cmd.

  • Use wsl --shutdown [distribution name] to stop the running WSL.

Migration

Reference: 如何对WSL2进行备份与还原

By default, WSL and Ubuntu will be installed on C:\. In this section, you will learn how to transfer these two parts to other disks.

  1. Deactivate: use wsl -l --all -v to view the states of all distributions and use wsl --shutdown to stop the running ones

  2. Backup: use wsl --export <name> <backup path>

  3. Unregister: use wsl --unregister <name>

  4. Restore: use wsl --import <name> <restore dir> <backup path> [--version 2] and you can see the VHDX file of Ubuntu present in your restore dir. (see the below figure)

    Successfully migrated
  5. Set default user: after importing and starting the Ubuntu backup, you may find the default user is root. Although you can change the default user of a distribution by <distribution name> config --default-user <user name> in cmd, it doesn't work for imported distributions. But you can do that by modifying /etc/wsl.conf in Ubuntu (see the figure below) [Reference]. Besides, you can change user temporarily by using su <user name> in Ubuntu or wsl --user <user name> in cmd

Additions

  1. If you counter error 0x800701bc, the tutorials below are recommended for your

    Error: 0x800701bc
  2. If you counter error 0x80370102, the tutorials below are recommended for your

    Error: 0x80370102

References

Last updated