How to play the WSL

I’ve heard about WSL for a long time. As a front-end, I generally don’t need to deal with Linux after all CI can be run automatically. However, as a programmer, I think I still need to know about the running environment of our code.

One: What is WSL?

The Windows subsystem for Linux lets developers run the GNU/Linux environment as-is – including most command-line tools, utilities, and applications – without the overhead of traditional virtual machines or dual-boot setup.

You can:

  • Select your preferred GNU/Linux distribution in the Microsoft Store.
  • Run common command-line software tools (e.ggrep,sed,awk) or other ELF-64 binaries.
  • Run Bash shell scripts and GNU/Linux command-line applications, including:
    • Tools: Vim, Emacs, TMUx
    • Languages: NodeJS, Javascript, Python, Ruby, C/ C++, C# and F#, Rust, Go, etc.
    • Services: SSHD, MySQL, Apache, Lighttpd, MongoDB, PostgreSQL.
  • Install other software using your own GNU/Linux distribution manager.
  • Use a UNIx-like command line shell to invoke Windows applications.
  • Call GNU/Linux applications on Windows.

Official documentation: docs.microsoft.com/zh-cn/windo…

2: install

1. EnableWSL

Open Powershell as administrator and run the following command:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Copy the code

Here we are using the command to start or in the application to start the Windows subsystem for Linux

If you want to use WSL1 that’s fine, here we need to update WSL2

Update 2.WSL2

Open PowerShell as an administrator and run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Copy the code

These are the commands to enable the VM function

Restart your computer to complete the WSL installation and update to WSL 2

** Note: ** If your CPU does not have vm functionality enabled, then you need to go to BIOS to enable it

3. Download the Linux kernel update package

Download the Linux update and install it

X64 architecture:

Wslstorestorage.blob.core.windows.net/wslblob/wsl…

ARM64 architecture:

Wslstorestorage.blob.core.windows.net/wslblob/wsl…

4. Set theWSL2Default version

wsl --set-default-version 2
Copy the code

5. Install the Linux distribution

Go to the Microsoft Store to download

You need to set the root user name and password for the first time after installation

Three: Set the graphical desktop

After installing WSL, Linux is really just a black box, and Microsoft doesn’t have a graphical interface for it.

Those of us who are used to Windows may not be able to adapt. Is there any way to implement a graphical desktop environment?

Of course, there are two main ways:

1. Display the Ubuntu graphical desktop directly on the Desktop of Windows 10 by installing the X Server in Windows 10 and ubuntu graphical desktop components in WSL. 2. Install and start XRDP and Xubuntu-Desktop in the WSL, and use SSH to connect to the WSL as a remote server.

Here’s the second approach.

1. Modify apt sources

Its own software source in the domestic connection network speed will be more difficult, so the domestic source, similar to the mirror site.

sudo vi /etc/apt/sources.list
Copy the code

Add at the top:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
Copy the code

Update 2.apt

sudo apt update
sudo apt upgrade
Copy the code

3. The installationxrdp

sudo apt install -y xfce4 xrdp
Copy the code

XRDP is a lightweight Ubuntu desktop with applications that only meet basic needs. It’s like IE for Windows

During xFCE4 installation, a message is displayed asking you to select the management DM. You are advised to use LightDM

If you missed the wizard that appeared during the installation, you can reset the DM by executing the following command after the installation is complete

sudo dpkg-reconfigure lightdm
Copy the code

4. ModifyxrdpThe default port

sudo vim /etc/xrdp/xrdp.ini
# change the default port 3389 to another port
port=3390
Copy the code

5. Specify the login session type for the current user

$ vim ~/.xsession

# Write the following (one line)
xfce4-session
Copy the code

6. Startxrdp

sudo /etc/init.d/xrdp start # stop restart
Copy the code

7. Remote access

In Windows, run the MSTSC command to open the remote desktop connection and enter localhost:3390 for the address

ok~

Done ~

Four:Windows Terminal

WSL is automatically configured in Microsoft Store after download

The new command line works better with WSL

Five:vscodeThe connectionWSL

How can Microsoft do without vscode?

In fact, it was the WSL logo that appeared on the recently updated vscode that aroused my interest.

To connect, click on the location in the red box

Select one to access the WSL file

Six: Install the latest versionNode

As the front end, we must install Node at the first time, but apt versions are relatively low, we can use the following way to install the latest version of Node

1. Installnpm

sudo apt install npm
Copy the code

2. InstallnThe module

Note: the n module is a tool used to install various versions of Node. The -g parameter indicates global installation

sudo npm install n -g
Copy the code

3. Install the long-term support versionNode

sudo n lts
# check version
node -v
Copy the code

4. nIntroduction of the module

N module can install various versions of Node, very convenient.

  • latest– The latest version, but not necessarily stable
  • stable— The latest stable version, thanlatestOlder, has the advantage of being stable, but not long term supported (API may change)
  • lts— The latest long term support version, which is the most recommended version. Although thanstableOld, but long-supported (and stable, of course) versions. Short for Long Term support.

You can install the specified version of Node directly by using the version number

Sudo n [version number]Copy the code

You can run the use command of the N module to switch the version

Sudo n use [version]Sudo n use LTS
Copy the code

Then you can use the LTS version.

Seven: Write code ~

cd ~ && mkdir web && cd web && mkdir test && cd test && touch index.js
Copy the code

After creating index.js, initialize NPM

npm init -y
Copy the code

Initialize the git

git init
touch .gitignore
Copy the code

Add node_modules to.gitignore

Write the code

Open the current WSL directory in vscode

Let’s start with a simple question and answer

npm run serve
Copy the code

nice~

Running successfully ~

conclusion

As far as I’m concerned the experience is pretty smooth and you can play Linux on Windows without having to use a third party tool which is nice, you can mess with it and just uninstall and reinstall it foolishly.