Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Introduction Into Windows Subsystem for Linux 2 (WSL2) for Beginners | https://git.io/ilyaigpetrov-wsl | by https://git.io/ilyaigpetrov

Introduction Into Windows Subsystem for Linux 2 (WSL2) for Beginners

WSL2 allows you to use Linux programs in a Linux shell (bash) inside your Windows 10 or above. It is mostly used to run programs for terminal (textual interface, not graphical). Launching graphical Linux programs is not covered in this tutorial.

CAUTION: Most of the commands must be executed with admin privileges (Start -> type "powershell" -> press "Run as Administrator").

Install

See https://docs.microsoft.com/en-us/windows/wsl/install for install instructions. Ubuntu Linux distribution is preferable (the default).

For better experience I recommend also installing Windows Terminal.

WSL Without Virtualization

If your machine/CPU lacks virtualization (error 0x80370102) then you may use WSL1. Just switch to v1 with the command below:

wsl --set-default-version 1

Also: Comparing WSL 1 and WSL 2.

WSL2 and File System Compression or Encryption

Newly created WSL2 file system can't be kept on a compressed or encrypted area of your drive (error 0xc03a001a). You may find ways to disable these features, e.g. here: microsoft/WSL#4103 (comment). In my case I use full drive encryption on Windows 10 Pro and WSL2 installs and works without any changes to the install instructions above.

Initial Directory

By default WSL2 will open in directory /mnt/c/Users/ilyaigpetrov which is the same directory as C:\Users\ilyaigpetrov but in Linux notation:

  1. Linux uses forward slashes (/) in file paths while Windows uses backward slashes (\);
  2. Windows absolute path starts with a drive letter (C:), while in Linux absolute path starts with / which is called the root directory. In WSL2 your drives are "mounted" at /mnt/ directory (C: is /mnt/c, D: is /mnt/d, etc.). The path to the mount directory varies in different Linux distributions.
  3. Linux file paths are case-sensetive while Windows file paths are not.

In WSL2 you have also a linux user home directory accessible by inputing cd ~. cd command means "change directory" and ~ (tilda) is a contraction for /home/ilyaigpetrov directory. It is more performant to access files from your WSL2 Linux file system than from /mnt/c (Windows file system).

Visual Studio Code

You may use Visual Studio Code for editing files in WSL2 file system as well as from your Windows partition (disk C:). You should install it in Widnows. You will be able to use VS Code installed in Windows in WSL2 file system: just run code . in the terminal to open VS Code inside current directory.

Across File Systems

You may open Windows Explorer inside current direactory of Linux by running explorer.exe . in the terminal.

Introduction to Linux

A free course about Linux for beginners: https://www.reddit.com/r/linuxupskillchallenge/ (if you don't want to wait for a course launch, then open .md files on https://github.com/livialima/linuxupskillchallenge). If you installed Ubuntu Linux distribution (the default) then you may intsall new linux software via apt command (Aptitude interface for the APT package manager). The apt command must be given administrative rights via sudo apt ... (SuperUser DO) to install new software.

Installing NodeJS

The command sudo apt install nodejs installs NodeJS from your Linux distribution repositories into your linux system. However the repositories used may contain an outdated version of NodeJS. To see which version of NodeJS is available in your repositories use the following command: apt show nodejs. To install the most recent NodeJS you may use https://github.com/nodesource/distributions#deb. NodeSource usually requires you to execute 2 commands:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - # Command 1.
sudo apt-get install -y nodejs # Command 2.

Command 1 uses curl tool to download a bash script, | (pipe) to redirect output with the downloaded text to input of the command after the pipe, which executes text from input as a bash script. The executed bash script then adds custom repository from NodeSource to your system.

Command 2 installs NodeJS from the newly added repository (kept in config files).

After you have installed NodeJS you may use it as: node <filename>.mjs. First, create the file via code ./hello-node.mjs and fill it with the following content:

const greeting = 'Hello, Node!';
console.log(greeting);

Use node ./hello-node.mjs to execute it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment