Install and configure WSL2 on Windows 10

[Pages:16]Install and configure the Windows Subsystem for Linux 2 (WSL2) on Windows 10

By Philippe Beraud, Microsoft France

This walkthrough covers all the steps required to configure a Windows 10 machine with the WSL2 feature installed on it.

(Web) developers, DevOps, Systems admins, University students, and many more asked Microsoft over the past few years for a better command line interface (CLI) experience on Windows. Related expectations notably comprise a Bash shell that leverages CLI muscle memory (ls, grep, cat, etc.) and runs shell scripts, along with a SSH client.

As a first answer to such expectations, Microsoft unveiled during the Microsoft Build 2016 the Windows Subsystem for Linux (WSL) to somehow put Linux on Windows 10. WSL is a feature of Windows 10.

As such, WSL is a genuine Linux Bash shell on Windows. It lets you run a GNU/Linux environment -- including most command-line tools, utilities, and applications (ssh, sed, grep, awk, tail, ls, vim, htop, etc.) -- directly on Windows 10, unmodified, without the overhead of a virtual machine (VM).

Moreover, WSL allows you to use your Linux distro(s) of choice directly from your Windows 10 machine. It indeed provides a multi-distro support (Ubuntu, openSUSE, SLES, Fedora, etc.). The Windows Store provides reliable downloads for theses distro with the ability to install multiple distros side-by-side.

All the installed distros can run simultaneously. In addition, WSL allows you to invoke Windows exe's from within Linux, and conversely invoke Windows apps from within Linux. You can also run background services, daemons.

WSL, with all the above, already represents a more than welcome connected experience for everyone using the Windows 10 platform for cross-platform development, DevOps, and administration.

Note

For more information, see the WSL documentation1, or the WSL learning resources page2.

The next version of WSL, i.e. WSL2, is about to arrive later this year. It has been announced at the Microsoft Build 2019 conference last year.

Note

For more information, see blog posts ANNOUNCING WSL23, and WSL2 POST BUILD FAQ4.

WSL2 is a new version of the architecture in WSL (e.g. WSL 1) that changes how Linux distros interact with Windows, allowing Linux applications to have full access to their normal system calls and Linux containers to run natively, all of that without emulation. See Deliver an improved Linux experience on Windows 10 for developers and beyond.

1 WINDOWS SUBSYSTEM FOR LINUX DOCUMENTATION: 2 LEARN ABOUT WINDOWS CONSOLE & WINDOWS SUBSYSTEM FOR LINUX (WSL): 3 ANNOUNCING WSL 2: 4 WSL 2 Post BUILD FAQ:

One should note that WSL2 also contains some experience differences compared to WSL 1. The most important are the fact that you will need to store you Linux files in your Linux root file system to have increased file system performance.

Note

For more information, see article USER EXPERIENCE CHANGES BETWEEN WSL 1 AND WSL25.

As of this writing, testing WSL2 requires Windows 10 20H1 preview builds as part of the Windows Insiders program (see below).

Note

For more information, see blog post WSL2 IS NOW AVAILABLE IN WINDOWS INSIDERS6.

Let's see how to:

1. Setup WSL2, 2. Use Visual Studio Code with WSL2, 3. Leverage Docker Desktop WSL 2 backend.

This is the purpose of the next sections.

Setting up Windows Subsystem for Linux (WSL) 2

To get started, if not already present on your local Windows 10 machine, install WSL2 Preview along with the Ubuntu18.04 distro from the Windows Store and the WSL2 Docker Desktop Tech Preview.

To install WSL2 preview, you will need to fulfill the following requirements:

1. Windows 10 build 18932 or higher. You can check your Windows version by opening a Windows command prompt and running the ver command. To make sure you are using build 18932 or higher please join the Windows Insider Program and select the Fast ring.

WSL2 requires Windows 10 build 18917 or higher. However, Docker WSL2 Tech Preview requires Windows 10 build 18932 or higher. See below

Important note

The above means that you should only use these bits for NOT production environments.

2. A machine that supports the Hyper-V virtualization technology.

If so, perform the following steps:

Note

For more information, see article INSTALLATION INSTRUCTIONS FOR WSL27.

1. Install WSL 1. Open a PowerShell command prompt as an Administrator and run the following command:

PS C:\> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

5 USER EXPERIENCE CHANGES BETWEEN WSL 1 AND WSL 2: 6 WSL 2 IS NOW AVAILABLE IN WINDOWS INSIDERS: 7 INSTALLATION INSTRUCTIONS FOR WSL 2:

Restart your computer when prompted. Press Y to do so when invited.

Note

For specific instructions, see article WINDOWS SUBSYSTEM FOR LINUX INSTALLATION GUIDE FOR WINDOWS 108.

2. Install a default distribution based on Ubuntu 18.04 from the Microsoft Store9.

Once installation is complete, you will be prompted to create a new user account (and its password).

3. The Ubuntu distro as most distros ship with an empty/minimal package catalog. It's strongly recommended to regularly update your package catalog, and upgrade your installed packages using your distro's preferred package manager. Run the following commands from the Bash prompt:

$ sudo apt update && sudo apt upgrade

8 WINDOWS SUBSYSTEM FOR LINUX INSTALLATION GUIDE FOR WINDOWS 10: 9 Ubuntu 18.04:

4. Once completed, now enable the 'Virtual Machine Platform' optional component. Open a PowerShell command prompt as an Administrator and run the following command:

PS C:\> Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

After these changes are enabled you will need to restart your computer. Press Y to do so when you will be asked for a confirmation. 5. Run the following command to retrieve the name of your current distro. Copy this actual name of your distro: Ubuntu-18.04. PS C:\> wsl -l -v

6. Set your current distro as the one to be backed by WSL2 using the following command in PowerShell:

PS C:\> wsl --set-version Ubuntu-18.04 2

You can change back to WSL 1 at any time by running the same command as above but replacing the '2' with a '1'. Depending on your environment, you may end up with the following error after a couple of seconds:

To resolve this error, open a Windows command prompt with administrative privileges and run the following two commands:

C:\> fsutil behavior set disableencryption 1 C:\> fsutil behavior set disablecompression 1

When creating the VHD's, those will ensure that compression and encryption are both disabled on them. Restart your machine.

Note

For more information, see article FSUTIL BEHAVIOR10.

If this workaround doesn't solve the issue, go to the Ubuntu directory in your profile folder, i.e. something like %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited..., right-click on LocalState, and select Properties.

10 FSUTIL BEHAVIOR:

Click on Advanced and uncheck Compress contents to save space and Encrypt contents as well if that's checked. Click on OK twice. When a dialog asks whether you want to apply to just this folder or to all subfolders and files, you can say "just this folder", because all you're doing is clearing that "compress" flag. Restart your machine. After that, the wsl --set-version command should work.

Note

For more information, see issue WSL2: VIRTUAL HARD DISK FILES MUST BE UNCOMPRESSED AND UNENCRYPTED AND MUST

NOT BE SPARSE #410311 on GitHub.

7. Set Ubuntu 18.04 as the default distribution:

PS C:\> wsl -s Ubuntu-18.04

Additionally, if you want to make WSL2 your default architecture you can do so with this command:

PS C:\> wsl --set-default-version 2

This will make any new distro that you install be initialized as a WSL2 distro. 8. Verify what versions of WSL your distros are using:

PS C:\> wsl --list --verbose

11 WSL2: VIRTUAL HARD DISK FILES MUST BE UNCOMPRESSED AND UNENCRYPTED AND MUST NOT BE SPARSE #4103:

The distro that you've chosen above should now display a '2' under the 'version' column.

Important note

As outlined in blog post WSL2 IS NOW AVAILABLE IN WINDOWS INSIDERS12, "make sure to put the files that

you will be accessing frequently with Linux applications inside of your Linux root file system to enjoy the file performance

benefits. To enjoy the faster file system access in WSL2 these files must be inside of the Linux root file system."

Note

It's possible for Windows applications to access the Linux root file system like File Explorer! Try running

explorer.exe / in the home directory of your Linux distro and see what happens.

Let's now see how to use Visual Studio Code in conjunction.

Using Visual Studio Code with WSL2

Visual Studio Code is a powerful, lightweight code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code supports almost every major programming language. Common web-based languages like JavaScript, TypeScript, CSS, and HTML ship in the box, and rich extensions from the Visual Studio marketplace provide completions, linting, debugging, and refactoring support for hundreds more.

It benefits from the IntelliSense technology, i.e. smart code completions based on variable types, function definitions, and imported modules. It allows you to easily navigate large code bases with features like Go To Definition and Find All References, to debug code right in the editor using breakpoints, a full call stack, and an interactive console, etc.

Visual Studio Code is free and Open Source.

To install Visual Studio Code and connect it with WSL2, perform the following steps:

1. Install Visual Studio Code or Visual Studio Code ? Insiders13.

2. Launch Visual Studio Code.

12 WSL 2 IS NOW AVAILABLE IN WINDOWS INSIDERS: 13 Visual Studio Code ? Insiders:

3. Install the Remote Development extension pack14. This pack notably includes the Visual Studio Code Remote - WSL15 extension that allows you to get a Linux-powered development experience from the comfort of Windows 10 by opening any folder in WSL2.

Note

For details on setting up and working with the WSL extension, see article DEVELOPING IN WSL1617. For

troubleshooting tips and tricks, see article REMOTE DEVELOPMENT TIPS AND TRICKS18.

4. To connect Visual Studio Code to WSL2, open a WSL2 terminal window and launch Visual Studio Code from here:

a. Open a WSL2 terminal window using the Start Menu item or by typing "wsl" from the command prompt.

b. Navigate to a folder you'd like to open in Visual Studio Code (including, but not limited to, Windows filesystem mounts like the folder /mnt/c for the C drive on your Windows 10 local

machine.

c. Type in the terminal the following command:

$ code .

Note

When doing this for the first time, you should see Visual Studio Code fetching components needed to run in WSL2. This should only take short while and is only needed once.

If this command does not work, you may not have added VS Code to your path when it was installed.

14 Remote Development Extension Pack: 15 Visual Studio Code Remote ? WSL: 16 DEVELOPING IN WSL: 17 DEVELOPING INSIDE A CONTAINER: 18 REMOTE DEVELOPMENT TIPS AND TRICKS:

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download