CodeGym /Courses /Docker SELF /Working with Linux on Windows: WSL

Working with Linux on Windows: WSL

Docker SELF
Level 1 , Lesson 2
Available

1. What is WSL?

Now we're diving into an interesting topic: how to work with Linux if you're using Windows as your primary operating system? Welcome to the world of WSL!

WSL (Windows Subsystem for Linux) is a technology from Microsoft that lets you run Linux distributions directly on Windows. There are two versions:

  • WSL1: works as compatibility at the system call level between Windows and Linux. It emulates Linux behavior through the Windows kernel.
  • WSL2: uses a full Linux kernel via a hidden lightweight virtual machine. This makes WSL2 faster and more compatible with real Linux.

WSL is like a compromise between two worlds: you get Linux tools while staying in the Windows ecosystem. For developers, this is a huge plus: you can code, test, and configure servers using your favorite Linux commands.


2. Why is WSL so valuable?

  1. Development and Testing: WSL lets developers work in a Linux environment without the need to reboot the computer or use virtual machines.
  2. Docker and Containerization: WSL2 supports Docker, which is super handy for working with containers.
  3. File System Integration: You can easily share files between Windows and Linux using a single file system.
  4. Easy to Set Up: Installing WSL literally takes just a few minutes.

How is WSL different from a virtual machine?

  • No need for virtualization. You don’t need to install VirtualBox, VMware, or Hyper-V. WSL operates at the system level.
  • Fewer resources. WSL uses less RAM and CPU time compared to a traditional virtual machine.
  • Integration with Windows. You can easily share files between Windows and Linux, run Linux and Windows commands together (!), and even use the same network interfaces.

3. Installing WSL

1. Checking system requirements

Before you start, make sure your system meets the minimum requirements:

  • Windows 10 (version 2004 and above) or Windows 11.
  • Virtualization enabled in BIOS/UEFI.

To check your Windows version, open PowerShell and run:

winver

You should see a window like this:

If you’re not sure how to open PowerShell on Windows, press Win+S and type powershell.

2. Enabling WSL

Open PowerShell as an administrator and run the following command:

wsl --install

This command will automatically install WSL, download the Linux kernel, and set WSL2 as the default version.

If for some reason the command doesn’t work, you can enable WSL manually through Windows optional features:

  1. Go to "Control Panel" → "Programs and Features" → "Turn Windows features on or off".
  2. Enable:
    • Windows Subsystem for Linux.
    • Virtual Machine Platform.

Then, restart your computer.

3. Choosing and installing a distribution

WSL allows you to install popular Linux distributions like Ubuntu, Debian, Kali Linux, and many more. To view the list of available distributions, run:

wsl --list --online

You’ll see a list of available distributions:


PS C:\Users\Admin> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.

NAME                            FRIENDLY NAME
Ubuntu                          Ubuntu
Debian                          Debian GNU/Linux
kali-linux                      Kali Linux Rolling
Ubuntu-18.04                    Ubuntu 18.04 LTS
Ubuntu-20.04                    Ubuntu 20.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
Ubuntu-24.04                    Ubuntu 24.04 LTS
OracleLinux_7_9                 Oracle Linux 7.9
OracleLinux_8_7                 Oracle Linux 8.7
OracleLinux_9_1                 Oracle Linux 9.1
openSUSE-Leap-15.6              openSUSE Leap 15.6
SUSE-Linux-Enterprise-15-SP5    SUSE Linux Enterprise 15 SP5
SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6
openSUSE-Tumbleweed             openSUSE Tumbleweed

To install, for example, Ubuntu, run:

wsl --install -d Ubuntu

After installation, launch the distribution to complete setup (like setting up a username and password).


3. Setting WSL2 as Default

To use WSL2 (which is highly recommended), make sure WSL2 is enabled. Set it as the default version:

wsl --set-default-version 2

If you have already installed a distribution with WSL1, you can upgrade it to WSL2:

wsl --set-version <Distro_Name> 2

Example:

wsl --set-version Ubuntu 2

4. WSL Integration with Windows

WSL is awesome at integrating with Windows, letting you leverage the power of both systems. Here are the key points:

1. File Sharing

WSL mounts the Windows file system in the /mnt directory. For example:

cd /mnt/c/Users/YourName

You can work with Windows files directly from Linux. Similarly, you can access WSL files in Windows through paths like \\wsl$\<Distribution_Name> in Explorer.

2. Running Windows Commands in Linux

Yep, you can run Windows commands straight from WSL. For example:

explorer.exe .

Will open the current directory in Explorer.

3. Running Linux Commands in PowerShell

WSL lets you run Linux commands directly from PowerShell. For example:

wsl ls

5. Using WSL in Real Development

Working with a Web Server

You can run an Nginx or Apache server in WSL and access it via a browser on Windows. For example:

1. Install Nginx in WSL:

   sudo apt update
   sudo apt install nginx

2. Start the server:

   sudo service nginx start

3. Open your browser and go to http://localhost.

Installing Docker

WSL2 fully supports Docker. Install Docker Desktop on Windows, and it will automatically integrate with WSL2.


6. Useful WSL Commands

  • View installed distributions:

    wsl --list --verbose
    

    Shows all installed distributions, their versions, and status.

  • Stop all distributions:

    wsl --shutdown
    
  • Remove a distribution (be careful!):

    wsl --unregister <Distribution_Name>
    
  • Open WSL in a specific directory:

    wsl ~/
    

7. Common Errors and Their Solutions

1. Error: "WSL is not enabled"

If you see a message saying that WSL is not enabled, make sure you have activated it in "Control Panel" or via PowerShell. Also, check if your processor supports virtualization (it must be enabled in BIOS).

2. Command wsl --install doesn't work

This might be because of an outdated version of Windows. Update your operating system to the latest version.

3. Issues with starting Docker

Make sure your distribution is using WSL2, not WSL1. You can check via:

wsl --list --verbose

4. Unable to connect to the server from WSL

Make sure the Windows firewall is not blocking connections.


8. Practical Task

  1. Install WSL and the Ubuntu distribution.
  2. Check the WSL version of your distribution:
    wsl --list --verbose
    
  3. Create a text file test.txt in the directory /mnt/c/Users/YourName/Documents from WSL:

    echo "Hello, WSL!" > /mnt/c/Users/YourName/Documents/test.txt
    
  4. View the contents of the file through PowerShell:

    type C:\Users\YourName\Documents\test.txt
    
  5. Bonus Task: Install Nginx, start it, and open http://localhost in the browser.

So, you just went through the entire journey from installation to actually using Linux on Windows. With this tool, you can easily and quickly set up working environments for any tasks.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION