Windows Subsystem For Linux
Tags: devops, linux, productivity, wsl
Published: 2022-12-10 18:28:10

## Windows
There are different types of **Operating Systems (OS)**, however the most popular and commonly used OS after android is **Windows**. Windows dominates the industry because of its **simplicity**, **user-friendly** nature and its modern **Graphical User Interface (GUI)**. This makes it more **versatile** and powerful because it is packed with a ton of **preinstalled** tools that helps the users in their day-to-day activities. It has everything that an average user would do. But with all its salient features, it comes with a price and since windows is the most dominating OS in the global market, many hackers try to find **vulnerabilities** and try to **exploit** them.

## Linux
Other than windows there exists another OS called **Linux**. Linux is an **Open-Source OS** meaning that the entire code for the kernel is available online and it can be viewed and installed for **free**. Additionally, there is a wide community that contributes towards its **development**, **testing** and **debugging**. So that any vulnerabilities are fixed as soon as they are released. Moreover, Linux is just like a **Do It Yourself (DIY)** kit. When one say’s they use Linux, they just don’t go and directly use Linux, they use a customized version of it, or what it is commonly referred as **distro** or **distribution** that is built upon the Linux kernel.

### Different distributions of Linux
![The different distributions of Linux, each is made for its own purpose.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6leunjk5h6sob5g5hejm.png)
<figcaption>Fig. 1. The different distributions of Linux, each is made for its own purpose.</figcaption>
<figcaption><br></figcaption>
There are many distros made for different purposes, examples are **Ubuntu** which has similarities and is user friendly as windows, **Kali** which is made for software penetration and discovering vulnerabilities and **Arch** which is like the DIY distro where anything can practically be customized within it. Linux is mostly developer oriented, because of all the tools it provides and less bloating software which is not required and would otherwise hinder user productivity. But the downside of Linux is, it is **tough** to find and install software that one can easily install on windows and sometimes there may not even be a Linux port for that application.

## Using Windows and Linux together
Linux system uses very less resources and comes with a bunch of **developer tools** hence providing a great developer experience. Windows have a large set of **applications** many of which cannot work on Linux, so if there is a need of both systems instead of buying a new machine it can be done in a few ways.

1. ### Dual-Boot (Multi-Boot)
Dual Boot is a method that allows the system to **boot** into two operating systems that is installed on the system. This allows to run both the operating systems without deleting either one and having the need to use a new machine. If any OS gets infected the files in the other will be unharmed unless it was mounted with write access. This is the best method in order to experience the **full functionality** of the OS. The problem with Dual Boot, is that only one OS can be run at a time, so in order to use any application from the other OS one has shut down and boot into that OS.

2. ### Virtual Machine (VM)
Virtual Machine’s use a concept called **virtualization** which was a big step in the industry. Virtualization is the process of **simulating** a **physical component** such as hard disk, operating system and so many others, into a **virtual** version of it. VM’s help to run or rather simulate an entire OS upon the actual host OS. That means if a user runs a Linux VM, they can also use their windows application **parallelly**. This is something dual boot isn’t able to do. The VM works by running in an isolated environment created by the **Hypervisor**, which means it should always request access for storage and memory from the host system and in order to transfer files across the systems, a **hosted file server** must be created.

3. ### WSL (Windows Subsystem for Linux)
WSL the integrated VM, solves the drawbacks of the other two methods. WSL is a feature that enables the functionality to run a Linux system **directly** on the windows environment. In comparison to a full-fledged VM, WSL has very **little overhead** and starts up within seconds. It comes inbuilt within Windows and created was by Microsoft.

A traditional VM can be slow to boot up, will consume a lot of resources, cannot easily share files between the host and VM and some configuration must be done to setup and manage it.
In comparison to a full-fledged VM, WSL it runs using a **lightweight** virtual machine, it has a very **fast boot time**, a **very small resource footprint** and there is no need to setup or manage the virtual machine. 

## Integration of Windows and Linux
WSL comes with a **seamless integration** with both the Windows and Linux environments. Via WSL, the Windows filesystem can be accessed within Linux and the Linux filesystem from windows and also commands from one OS can be executed in the other.

![Using Linux commands within Windows using the project Windows Bash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xxp6ie26x1i5cxuvle6.png)
<figcaption>Fig. 2. Using Linux commands within Windows (using the project <a href="https://github.com/ajratnam/windowsBash">Windows Bash</a>)</figcaption>
<figcaption><br></figcaption>

It also significantly improves the performance of containerization software’s like docker. Technologies like **docker engine** can now run Linux Containers can run **natively** without **emulation** and using the help of WSL, it can perform a cold boot within seconds which offers a massive **performance boost**.
<br>
## Hope for the future
Initially WSL was just a compatibly layer between Windows and Linux and the **File I/O** speeds were very slow, but since the release of WSL2 it had grown to become more popular. The difference between the two versions is way more than a simple version update, the entire process of running the kernel was entirely **reworked** and is now almost 20 times better.

### Cross Working Applications
Since the introduction of **WSLg (WSL GUI)** within WSL2, it is now possible to run **graphical** Linux apps within windows. Before in WSL1 an external server had to be setup in order run GUI apps. 
By using WSL, it is possible to use **existing** applications on windows to edit or run files within Linux instead of reinstalling them and vice-versa.
 
![Using Windows application to edit a Linux file in Windows Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eby3wbh3w3sfjxefqpwn.png)
<figcaption>Fig. 3. Using Windows application to edit a Linux file in Windows Terminal.</figcaption>
<figcaption><br></figcaption>

## Installation of WSL
To install WSL, there used to be a lot of steps but through the progress in software now it can be installed with just one command inside command prompt `wsl --install`. Yes, that’s it!! That command automatically now downloads WSL2 with WSLg support and an Ubuntu distro. By default, it downloads Ubuntu, but other distros can be also specified using the command `wsl --install -d <distro>` ex-`wsl --install -d ubuntu`, the list of available distros can be found on the Microsoft Store.
<figcaption><br></figcaption>
If the command doesn’t work that’s maybe because the wsl version is outdated. It can be updated using the `wsl --update` command. 
<figcaption><br></figcaption>
The first launch of the subsystem will take quite an amount of time but the subsequent will happen within seconds. 
<figcaption><br></figcaption>
A new app called **Ubuntu** will be installed which is the one used to launch and operate the subsystem. It is recommended to use the **pre-installed** application called **Windows Terminal** which is a modern replacement for command prompt, by installing WSL it automatically creates a profile for Ubuntu inside windows terminal. 
<figcaption><br></figcaption>
Also, WSL makes it so that the entire Linux subsystem’s file system can be **accessed directly** from file explorer. Which is maybe preferably better than viewing and modifying from the command line for some people.

![Accessing the Linux filesystem as if it was part of the native windows environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p14iiih8aufb9v3rybw0.png)
<figcaption>Fig. 4. Accessing the Linux filesystem as if it was part of the native windows environment.</figcaption>
<figcaption><br></figcaption>

Now the Windows Subsystem for Linux has been installed on the system and can be used to run and explore Linux commands.

## Conclusion
WSL is a tool for running Linux and is not a replacement for running VM’s. It just is a **lightweight adaptation**, which is highly integrated with windows and **highly optimized** to use only minimal overhead. A full-fledged VM can do more things than what WSL can and as such a real Linux operating system can do much more than what a VM can but they all come with their own downsides.
<figcaption><br></figcaption>
Also, there is a new subsystem for android (**WSA**) introduced in windows 11 which is a bit **unstable** right now. It allows to run android apps natively from windows without any emulator. These subsystems **inspire** developers to make **cross-platform** interconnected applications. This technology isn’t something revolutionary as of now, but there may be hope for it to be the foundation of something new.

I thank everyone who took the time to read my article and hope got inspiration to start using WSL.