Installation and use of WSL2 - a new choice for Windows and Linux dual systems

Installation and use of WSL2 - a new choice for Windows and Linux dual systems

It is necessary to use the Windows system to meet daily life and collaborative office work, but also to use the Linux system to meet the development work. It is troublesome to restart the dual systems back and forth, the cost of dual hosts is high, and there are many restrictions on remote servers. WSL may be a better solution that can get rid of the above. troubled

1. What is WSL?

The full name of WSL is Windows Subsystem for Linux , which is suitable for Windows Subsystem for Linux

WSL is a Linux subsystem under the Windows system, which can be installed in the Windows system like an application, providing a Linux-based development environment for developers developing under the Windows system, and can choose different Linux distributions, with native Most of the same functions as Linux system

Microsoft describes WSL as: Windows Subsystem for Linux allows developers to 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 setups

2. Why there is WSL

Windows has an unshakable position in the field of general computer systems, but Linux has the characteristics of free, open source, high security, and high availability. In the fields of servers, embedded development, and software development, Linux is still the mainstream choice. Although the Linux system has many advantages, most people still cannot use the Windows system in daily life, so many people use Windows for daily life and Linux for development work.

The current CEO of Microsoft, Nadella, has embraced open source since he took office. Microsoft once became the largest contributor to open source projects. After acquiring GitHub, it launched a series of open source products, such as VScode, TypeScript, etc.

Microsoft released WSL in 2018 and WSL2 in 2019 as an important upgrade that greatly increased the performance of the file system, added support for full system call compatibility, and added support for using GUI-based Linux applications. support. Currently, Windows Server 2022 already supports the WSL2 Linux distribution, which also means that WSL2 has now covered Windows Desktop and Windows Server

Of course, as a commercial company, Microsoft's move is to attract more users to use Windows

3. To choose WSL2 as the development environment

3.1 What is the difference between WSL and WSL2


As can be seen from the comparison table above, the WSL2 architecture is superior to WSL1 in several ways, and WSL2 has a complete Linux kernel. For the performance of the cross-OS file system, Microsoft also gave a solution: it can be processed by storing the project file on the same operating system as the tool running when processing the project

3.2 Choice between Windows and Linux systems

Daily life requires Windows for office work, and Linux for development work. For those who need both, it is inevitable to switch back and forth between different operating systems. Although some people can meet the needs of daily office work on some Linux distributions, it is undeniable that Windows has a stronger ecology and convenience, and more importantly, everyone is working together and needs to interact. For the needs of dual systems, there are several different solutions, each with advantages and disadvantages

solution profit Disadvantages
Install Windows and Linux separately on dual hosts Independent operation, physical isolation High cost and inconvenient data transmission between different systems
Single host dual system real operating system Troublesome to use, need to restart switch
Build a remote server real operating system High cost, may be limited by performance and bandwidth
Linux system virtual machine There is basically no difference between the use and the real operating system Occupies resources and low operating efficiency
WSL2 Easy to use, seamless switching, no traditional virtual machine or dual-boot setup overhead The system function may be incomplete and unable to support some software

For those who need both Windows and Linux systems, WSL2 is a great solution. It can not only meet our daily use of Windows in office work, but also allow us to use Linux conveniently and quickly in development work.

However, after all, WSL2 is a Linux subsystem under Windows, and the upper limit of its operational stability still depends on its host Windows system. If you are looking for a dedicated server, workstation, etc. with high security and stability, you should install the Linux system directly.

WSL2 is an excellent product. If it is just for daily development work and running Demo, WSL2 is a good solution.

4. Installation of WSL2

4.1 Installation conditions

Must be running Windows 10 version 2004 and later (build 19041 and later) or Windows 11

Tips: To check the Windows version and build number, select the Windows logo key + R, then type "winver", select OK.

4.2 Enabling required Windows features

To install WSL2, you need to enable the virtual machine platform in the windows function and the Windows subsystem for Linux.
You can directly use the Windows+R shortcut key to directly execute the "Windows function" in the opened "Run" window optionalfeatures, or open it in the control panel. "Windows Features" enable the required features, then restart


4.3 Set the default WSL version

The default version can be set via PowerShell or cmd using the following command

     wsl --set-default-version <Version>

To set the default version to WSL1 or WSL2, replace <Version> with the number 1 or 2, indicating which version of WSL you want to use by default for installing new Linux distributions, for example:


4.4 Install Linux distribution

In the Microsoft Store, you can search for relevant applications by keyword search, and then choose a desired Linux distribution to install


If the Microsoft Store does not open smoothly, you can use the following methods to install the Linux distribution

  1. You can view the installable Linux distributions with the following command
     wsl --list --online


  1. Then install the specified Linux distribution with the following command
    wsl --install -d <Distribution Name>

To install the specified Linux distribution, replace <Distribution Name> with the name of your preferred Linux distribution (eg Ubuntu-20.04)


4.5 Open the Linux distribution

We can view the installed Linux distributions with the following command

     wsl -l


  • You can find Ubuntu in all applications, and then open it, you can pin it to the taskbar or start screen for easy and quick startup



  • You can also open Ubuntu directly in the Windows terminal



When you open the Linux distribution for the first time, you need to set the user name and password . When entering the password, nothing will be displayed on the screen, which is blind typing.

4.6 Exporting and importing Linux distributions

We can export the installed Linux distribution and install it on other disks to save space on the C disk. At the same time, we can also share the exported Linux distribution with others during collaborative development to achieve a unified development environment.

  1. Take the installed Ubuntu as an example, export the Ubuntu tar file to the D drive

     wsl --export Ubuntu-20.04 D:\Ubuntu-20.04.tar
    
  2. Unregister the currently installed Linux distribution

     wsl --unregister Ubuntu-20.04
    
  3. Re-import and install the exported Linux distribution to the D drive

     wsl --import Ubuntu-20.04 D:\Ubuntu-20.04 D:\Ubuntu-20.04.tar --version 2
    
  4. Set default login user

     ubuntu2004 config --default-user <UserName>
    

    <UserName> is replaced with the username set when first opening the Linux distribution

5. Use of WSL2

5.1 File Operation

Under the Windows system, you can find the folder of the Linux subsystem in My Computer, which can be directly accessed and interoperated with the files under the Windows system


5.2 Configuration of Linux Subsystem

After the WSL2 installation is complete, the relevant system configuration is similar to the real operating system. Taking
Ubuntu as an example, as a domestic user, we can update the domestic mirror source for our convenience. For
users who use Python programming, you can install Anaconda to configure the relevant environment
. The operation will not be repeated here

5.3 Connect to WSL2 using VScode

  • VS Code leverages the Remote-WSL extension to enable you to use WSL2 as a live development environment directly from VS Code


  • Access more VS Code remote options by using the shortcut in VS Code to CTRL+SHIFT+Pbring up the Command Palette. If you then type Remote-WSL, you'll see a list of available VS Code remote options that allow you to reopen a folder in a remote session, specifying which distribution to open in


  • Optionally Remote-WSL:Open Folder in WSLopen folders in WSL


  • Then, the Python environment in WSL2 can be used to compile and run the code normally, and the WSL terminal can be operated in VS Code


  • To open a project from a Linux distribution, you can open the distribution's command line and enter:code .


5.4 Connect to WSL2 using Pycharm

In the Pycharm project, you can modify the Python Interpreter to the python environment built in WSL2 in the settings, and use the Python environment in WSL2 to compile and run the code normally, and you can operate the WSL terminal




6. Summary

As a new choice for developers, WSL satisfies the efficient use of Windows and Linux systems. Compared with dual systems and virtual machines, WSL is more convenient and efficient. With Microsoft's continuous updates and support, as far as Ubuntu is concerned, the WSL experience is very close to the real Ubuntu system. For more information about WSL, please refer to Microsoft's official documentation

Guess you like

Origin blog.csdn.net/eastking0530/article/details/126613992