Linux and windows together

Last Updated on June 16, 2024 by Arnav Sharma

The Windows Subsystem for Linux (WSL) is a revolutionary feature introduced by Microsoft, enabling users to run a Linux environment directly on a Windows machine without the need for virtualization or dual-boot setups. This article delves into the various facets of WSL, providing insights into its capabilities, installation process, integration with popular tools like VS Code, and the ability to run Linux GUI applications seamlessly on Windows.

What is Windows Subsystem for Linux (WSL)?

Introduction to WSL

Windows Subsystem for Linux (WSL) is a compatibility layer within the Windows operating system that enables you to run native Linux binaries. It provides a way to use a full Linux environment directly on Windows, without the need for a virtual machine or dual-boot setup. With WSL, you can enjoy the best of both Windows and Linux without any major disruptions to your workflow.

How does WSL work?

WSL works by implementing a Linux-compatible kernel interface on top of the Windows kernel. This allows Linux system calls to be translated and executed by the Windows kernel, enabling Linux programs to run seamlessly on the Windows operating system. WSL provides a command-line interface that allows you to interact with the Linux environment and run Linux applications directly on your Windows machine.

Benefits of using WSL

There are several benefits to using WSL:

  • Easy installation and setup: WSL can be installed directly from the Microsoft Store or through PowerShell commands.
  • Access to a wide range of Linux distributions: WSL supports various Linux distributions such as Ubuntu, Debian, and more.
  • Seamless integration with Windows: WSL allows you to access and manipulate Windows files from the Linux environment and vice versa.
  • Use familiar Windows tools: You can use Windows tools like PowerShell and Command Prompt to manage and interact with your Linux instance.
  • Develop applications using popular Linux tools: WSL provides a Linux development environment that is compatible with tools like Visual Studio.
  • Run Linux commands directly on Windows: With WSL, you can run Linux commands directly from your Windows command-line interface.

How to install WSL on Windows?

The installation process for WSL is straightforward. Users can opt for a fresh installation or update an existing WSL setup. The prerequisites include being on Windows 10 Build 19044+ or Windows 11 and having the appropriate driver for vGPU installed. Once these requirements are met, users can initiate the installation using simple commands in the PowerShell or Command Prompt.

Installing WSL 1

To install WSL 1, follow these steps:

  1. Open PowerShell as an administrator.
  2. Run the following command: wsl --install.
  3. Follow the instructions to install the required components.
  4. Restart your computer to complete the installation.

Upgrading to WSL 2

To upgrade to WSL 2, follow these steps:

  1. Ensure you have Windows 10 version 2004 or higher.
  2. Open PowerShell as an administrator.
  3. Run the following command: wsl --set-version <distribution name> 2. Replace <distribution name> with the name of your installed Linux distribution.

Choosing a Linux distribution

After installing WSL, you can choose a Linux distribution to use. The Microsoft Store provides a variety of Linux distributions that can be installed directly on your Windows machine. You can search for and install your preferred distribution from the Microsoft Store application.

Using WSL on Windows

Running Linux commands on WSL

Once you have installed WSL and a Linux distribution, you can run Linux commands directly from your Windows command-line interface. Simply open your preferred command-line tool, such as PowerShell or Command Prompt, and type in the desired Linux command.

Accessing Windows files from Linux

With WSL, you can access and manipulate your Windows files from the Linux environment. The Windows file system is mounted under the /mnt directory in the Linux file system. For example, to access your Windows C: drive in Linux, you can navigate to /mnt/c.

Interacting with Windows services from WSL

WSL allows you to interact with Windows services directly from the Linux environment. You can use the wsl command in PowerShell or Command Prompt to run Windows executables, manage Windows services, and perform various other tasks.

Managing Linux distributions on WSL

Installing additional Linux distributions

If you want to install additional Linux distributions, you can do so from the Microsoft Store. Simply search for the desired distribution, select it, and click on the “Install” button. You can have multiple Linux distributions installed side-by-side on WSL.

Switching between WSL versions

You can easily switch between WSL 1 and WSL 2 versions based on your requirements. To switch to WSL 1, run the command wsl --set-version <distribution name> 1. To switch to WSL 2, replace “1” with “2” in the command.

Updating and removing Linux distributions

You can update your installed Linux distributions by running the respective update commands within the WSL environment. For example, for Ubuntu, you can run sudo apt update and sudo apt upgrade to update the packages.

To remove a Linux distribution, you can use the following command: wsl --unregister <distribution name>. Replace <distribution name> with the name of the distribution you want to remove.

WSL Commands 

WSL Commands Table
Command Description Example
wsl Launches the default Linux distribution. wsl
wsl –list or wsl -l Lists all installed Linux distributions. wsl –list
wsl –set-version <Distro> 2 Sets a distribution to use WSL 2. wsl –set-version Ubuntu-20.04 2
wsl –set-default-version 2 Sets the default WSL version for new installations to WSL 2. wsl –set-default-version 2
wsl –shutdown Shuts down all running WSL instances and the WSL 2 lightweight utility VM. wsl –shutdown
wsl –install Installs WSL with the default Linux distribution (usually Ubuntu). wsl –install
wsl –uninstall <Distro> Uninstalls the specified Linux distribution but retains the user data. wsl –uninstall Ubuntu-20.04
wsl –export <Distro> <FilePath> Exports the specified distribution to a tar file. wsl –export Ubuntu-20.04 /path/to/exported.tar
wsl –import <Distro> <InstallLocation> <FilePath> Imports a distribution from a tar file. wsl –import NewUbuntu /path/to/install /path/to/exported.tar
wsl –set-default <Distro> Sets the default Linux distribution to the specified one. wsl –set-default Ubuntu-20.04
wsl –user <Username> Runs WSL as the specified user. wsl –user john
wsl –distribution <Distro> Runs a specific Linux distribution. wsl –distribution Ubuntu-20.04
wsl –exec <Distro> <Command> Executes a specific command in the given distribution. wsl –exec Ubuntu-20.04 ls
wsl –terminate <Distro> Terminates a running distribution. wsl –terminate Ubuntu-20.04
wsl –upgrade <Distro> Upgrades the specified distribution to WSL 2. wsl –upgrade Ubuntu-20.04

Integrating WSL with Windows

Using WSL with Windows Command Line

WSL can be used alongside the Windows Command Prompt and PowerShell. You can run WSL commands from these command-line interfaces and even pipe the output between Windows and Linux commands to perform powerful operations.

Running GUI applications on WSL

While WSL primarily focuses on command-line operations, you can also run graphical applications on WSL with the help of an X server. By installing an X server on your Windows machine and configuring WSL to use it, you can run Linux GUI applications and have them displayed on your Windows desktop.

Using WSL as a development environment

WSL provides a powerful development environment for Linux-based applications. You can utilize popular Linux development tools, compilers, and frameworks within the WSL environment, making it easier to build, test, and debug your software projects.

Integration with Visual Studio Code

One of the standout features of WSL is its seamless integration with Visual Studio Code (VS Code). With the WSL extension for VS Code, developers can:

  • Develop in a Linux-based environment.
  • Use Linux-specific toolchains and utilities.
  • Run and debug Linux-based applications on Windows.
  • Utilize VS Code’s built-in Git support for version control.
  • Edit files in Linux or mounted Windows filesystem without cross-OS challenges.

Troubleshooting WSL

Common issues and solutions

While using WSL, you may encounter some common issues such as slow performance, network connectivity problems, or compatibility issues with specific applications. The Microsoft Learn website offers extensive documentation and troubleshooting guides to help resolve these issues.

Updating WSL and Linux distributions

It’s important to keep your WSL installation and Linux distributions up to date to benefit from bug fixes, security patches, and new features. You can update WSL by installing the latest Windows updates, and update your Linux distributions using their respective package management systems.

Resetting WSL to default settings

If you encounter persistent issues with your WSL installation, you can reset it to its default settings. You can do this by opening PowerShell as an administrator, running the command wsl --unregister <distribution name> to unregister the distribution, and then reinstalling it from the Microsoft Store.

Summary of WSL features and benefits

Windows Subsystem for Linux (WSL) is a powerful tool that allows you to run a full Linux environment directly on your Windows machine. It provides seamless integration between Windows and Linux, enabling you to run Linux commands, access Windows files from Linux, and interact with Windows services. With an easy installation process, support for multiple Linux distributions, and a variety of features for developers and IT professionals, WSL is a valuable tool for anyone looking to leverage the benefits of both Windows and Linux.

FAQ – Linux on Windows

Q: What is Windows Subsystem for Linux (WSL)?

A: Windows Subsystem for Linux (WSL) is a compatibility layer provided by Microsoft to run Linux binaries on a Windows operating system. It allows developers and Linux enthusiasts to use Linux tools, utilities, and command-line applications directly on Windows without the need for a virtual machine or dual-boot setup.

Q: What are the different versions of WSL?

A: There are two major versions of WSL: WSL 1 and WSL 2. WSL 1 is based on a compatibility layer that translates Linux system calls into Windows system calls. WSL 2, on the other hand, utilizes a lightweight virtual machine to run a full Linux kernel and offers improved performance and compatibility.

Q: How can I install Linux using the WSL?

A: To install a Linux distribution using WSL, you can follow these steps: 1. Open the Microsoft Store. 2. Search for the desired Linux distribution (e.g., Ubuntu, Debian, Fedora) and select it. 3. Click on the “Get” or “Install” button to initiate the installation. 4. Once the installation is complete, you can launch the Linux distribution from the Start menu or by typing its name in the Windows command line.

Q: Can I use multiple Linux distributions with WSL?

A: Yes, you can install and use multiple Linux distributions with WSL simultaneously. Each distribution you install will be available as a separate instance that can be managed and launched independently.

Q: What are the advantages of using WSL over a virtual machine or dual-boot setup?

A: Using WSL has several advantages over traditional approaches like virtual machines or dual-boot setups, including: – Seamless integration with the Windows environment and filesystem. – Direct access to Linux tools and utilities from within the Windows command line. – Improved performance in comparison to running a full virtual machine. – Ability to use multiple Linux distributions simultaneously.

Q: How do I enable WSL on my Windows machine?

A: To enable WSL on your Windows machine, you can follow these steps: 1. Open the Windows Features dialog by typing “Turn Windows features on or off” in the Windows search bar. 2. Scroll down and locate “Windows Subsystem for Linux” or “WSL” in the list of features. 3. Check the box next to it and click the “OK” or “Apply” button. 4. Wait for Windows to install the necessary components and restart your computer if prompted.

Q: Can I use Windows services or desktop applications from within WSL?

A: Windows Subsystem for Linux is primarily designed to run command-line Linux applications and tools. It does not natively support running Windows services or desktop applications. However, you can use the Windows interop capability of WSL to execute certain Windows binaries or launch Windows applications from within the WSL environment.

Q: Is WSL available on all versions of Windows?

A: WSL is available on most modern versions of Windows, including Windows 10, Windows Server, and Windows Insider builds. However, the availability and specific features of WSL may vary depending on the version of Windows you are using.

Q: How do I use WSL with the Windows command line or Windows Terminal?

A: Once you have installed a Linux distribution using WSL, you can access the Linux command line by launching the “Windows Terminal” or any other appropriate terminal emulator on Windows. From there, you can use the installed Linux distribution as you would on a traditional Linux environment.

Q: Can I install software or packages from Linux distributions on WSL?

A: Yes, you can use the package management system of the installed Linux distribution to install software or packages directly from their respective repositories. You can also compile and run Linux software on WSL.

Q: Compare WSL 1 to WSL 2

A: WSL 1 and WSL 2 are both iterations of the Windows Subsystem for Linux, but they differ fundamentally in their architecture. While WSL 1 uses a translation layer to interpret Linux system calls, WSL 2 employs a genuine Linux kernel within a lightweight virtual machine. This distinction makes WSL 2 more performant and compatible, especially for tasks requiring full Linux system capabilities.

keywords: microsoft windows subsystem for linux 2 run on windows for wsl distribution available on windows side of linux installation using wsl 2 inside the windows

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.