1. Overview
Windows Subsystem for Linux (WSL) is a compatibility layer that enables users to run Linux binary executables natively on the Windows operating system. Initially released in 2016, WSL provides us with a powerful Linux environment within Windows, allowing us to run Linux command-line tools, utilities, and programs straight from our Windows PC. This seamless connectivity streamlines the workflow for developers who work in both Windows and Linux systems.
In this tutorial, we’ll look at the fundamentals of WSL, including installation, functionality, and usage.
2. What Is WSL?
Windows Subsystem for Linux (WSL) is simply a Windows feature that allows you to install a whole Linux distribution on your Windows PC. It connects Windows and Linux by delivering a complete Linux kernel and user space under Windows. WSL comes in two versions:
- WSL 1: This version employs a translation layer to connect Linux system calls to Windows system calls.
- WSL 2: This version incorporates a full Linux kernel created by Microsoft and uses a lightweight virtual machine to enable full system call compatibility and enhanced performance.
Additionally, WSL provides a seamless experience by combining the capability of Linux and the comfort of Windows.
3. Benefits of WSL
Most developers use WSL primarily for the benefits it provides. Some of the most notable benefits includes:
- WSL enables users to run Linux programs alongside Windows applications, resulting in a smooth workflow. We can view Windows files from Linux, and vice versa, making it simple to transfer between systems.
- WSL provides access to a wide variety of Linux software and development tools. We can run Bash scripts, use Linux command-line tools, and install software packages using a Linux package manager, such as APT in Ubuntu.
- WSL streamlines the development workflow for developers that need to test software on both Windows and Linux platforms. Additionally, it enables the use of Linux-specific development tools and libraries straight from a Windows system.
Furthermore, WSL has the advantage of not requiring a dual-boot system or separate hardware to run Linux, saving both time and resources.
4. Installing WSL
To get started with using a Linux environment on our Windows machine, we can install WSL in a few basic steps.
4.1. Enable WSL Feature
First, on Windows, let’s enable the WSL feature. We’ll do this by launching PowerShell as an administrator and executing the wsl command:
$ wsl --install
Furthermore, this command enables WSL capability and installs the default Linux distribution, which is normally Ubuntu.
4.2. Set WSL 2 as the Default Version
WSL 2 provides considerable performance gains and full system call compatibility. Let’s make WSL 2 the default version by running another PowerShell command:
$ wsl --set-default-version 2
Additionally, this ensures that any new Linux distribution installed uses WSL 2 by default.
4.3. Install a Linux Distribution
We can install more Linux distributions from the Microsoft Store. For example, to install Ubuntu, we can launch the Microsoft Store, search for Ubuntu, choose our preferred Ubuntu version, and click Install.
Thereafter, we can run Ubuntu to complete the setup and create a new user account.
4.4. Check if WSL Is Installed
We can also use PowerShell to ensure that WSL is correctly installed and to examine the installed distributions:
$ wsl --list --verbose
Moreover, this will show a list of all installed distributions, along with respective WSL versions.
5. Using WSL
Now that WSL and a Linux distribution have been installed, let’s see how to carry out some simple activities in WSL.
5.1. Running Basic Linux Commands
After installing Ubuntu, we can launch it by entering Ubuntu into the Windows search box. This will open a terminal window from where we can execute Linux commands:
$ pwd
/home/baeldung
$ echo "Hello, Baeldung! welcome to WSL"
Hello, Baeldung! welcome to WSL
Additionally, WSL offers us that native Linux terminal experience.
5.2. Accessing Windows Files From WSL
WSL enables us to access Windows files directly from the Linux environment. Windows drives are mounted under the /mnt directory:
$ cd /mnt/c
$ ls
The cd command navigates to the C: disk, while the ls command displays its contents.
5.3. Compatibility Between Windows and Linux
One of WSL’s most powerful characteristics is its ease of use with Windows and Linux systems.
Additionally, we can easily run Windows executables directly from WSL. For instance, to launch Notepad from WSL:
$ notepad.exe
This command would launch the Notepad application.
Finally, we can call Linux binaries from Windows Command Prompt or PowerShell, thereby improving communication between the two platforms.
6. Disadvantages of WSL
While WSL provides various benefits, there are also some limitations that we should be aware of.
6.1. Limited Hardware Access
WSL has less access to hardware resources than a full Linux installation. For example, GPU access is limited, although WSL 2 has made progress in this area by supporting GPU acceleration in select cases.
6.2. Compatibility Issues
Some Linux apps may not function properly under WSL because of differences in the underlying architecture.
Furthermore, although WSL 2 has substantially improved compatibility, it’s not necessarily an ideal replacement for a full Linux environment.
6.3. Network Configuration
WSL 2 operates in a virtualized environment, which can occasionally cause network configuration complications, particularly when working with port forwarding and network interfaces.
Additionally, several networking functions seen on native Linux systems may not work properly within WSL.
7. Conclusion
Windows Subsystem for Linux (WSL) bridges the gap between the Windows and Linux environments, giving developers a strong tool for working with both operating systems.
In this article, we looked at the fundamentals of WSL, including installation, functionality, and usage.
Finally, WSL increases productivity and simplifies workflow by allowing Linux command-line tools and software to be used directly on Windows, making it a useful tool for developers and IT professionals.