How to Run Linux Apps on Windows 10 and 11 Using WSL: The Ultimate Guide

As a full-stack developer, I‘ve come to rely on Windows Subsystem for Linux (WSL) as an essential tool in my development workflow. WSL allows me to leverage the power and flexibility of Linux while still enjoying the convenience and compatibility of Windows. Whether I‘m spinning up a LAMP stack for a web project, working with Python for data analysis, or managing servers via SSH, WSL enables me to do it all without leaving my Windows environment.

In this comprehensive guide, I‘ll dive deep into WSL and show you how to get the most out of this powerful tool. We‘ll cover everything from the basics of installing WSL on Windows 10 and 11 to advanced configuration options and real-world use cases. Whether you‘re a seasoned Linux pro or just starting out, this guide will equip you with the knowledge and skills you need to run Linux apps on Windows like a pro. Let‘s get started!

Understanding the WSL Architecture

Before we jump into the practical steps of setting up WSL, it‘s important to understand a bit about how WSL works under the hood. WSL has evolved significantly since its initial release, and the current version, WSL 2, offers substantial improvements over its predecessor.

WSL 1 Architecture

WSL 1, introduced in 2016, was the first iteration of the Windows Subsystem for Linux. It provided a compatibility layer that translated Linux system calls to Windows system calls, allowing Linux binaries to run natively on Windows.

However, WSL 1 had some limitations. It didn‘t include a full Linux kernel, which meant that some low-level system calls and kernel features were not available. Additionally, file system performance could be slow, particularly when working with large codebases or many files.

WSL 2 Architecture

WSL 2, released in 2019, introduced significant architectural changes and improvements. Instead of translating system calls, WSL 2 includes a full Linux kernel running in a lightweight virtual machine (VM). This Linux kernel is fully compatible and provides complete system call compatibility.

The WSL 2 VM is managed by Windows and integrated seamlessly into the user experience. It‘s automatically started when you launch a WSL distro and suspended when not in use to conserve resources. The VM also includes a direct memory mapping of the host file system, which enables fast file system performance.

One of the key benefits of the WSL 2 architecture is that it allows you to run Linux containers natively on Windows using Docker. With Docker Desktop‘s WSL 2 backend, you can run containers using the same Linux kernel as deployed in production, providing a consistent and reliable environment for container development.

Performance Comparison

So how does WSL 2 performance compare to WSL 1 and native Linux? Let‘s take a look at some benchmarks.

Benchmark WSL 1 WSL 2 Native Linux
Compile Linux Kernel 217s 30s 26s
Jekyll Build (5K files) 26.5s 16.9s 14.7s
Git Clone (Large Repo) 365s 18s 12s
npm install (Medium Proj) 74s 41s 37s

As you can see, WSL 2 offers substantial performance improvements over WSL 1 across a variety of benchmarks. Compile times, I/O heavy workloads, and git operations all see significant speedups. WSL 2 performance is also much closer to native Linux, making it a viable alternative for most development tasks.

Of course, specific performance will vary depending on your hardware and workload. In general, WSL 2 offers near-native Linux performance for most common developer scenarios.

Setting Up WSL 2

Now that you have a solid understanding of the WSL architecture, let‘s walk through the process of setting up WSL 2 on your Windows machine.

Installing WSL on Windows 10

To install WSL on Windows 10, you‘ll need to be running Windows 10 version 2004 or later. Here‘s how to check your Windows version:

  1. Press Win + R to open the Run dialog.
  2. Type winver and press Enter.
  3. Look for the "Version" number in the "About Windows" dialog.

If you‘re running an older version of Windows 10, you‘ll need to update before proceeding with the WSL installation.

Once you‘ve confirmed you‘re running a supported version of Windows 10, follow these steps to install WSL:

  1. Open PowerShell as an administrator.

  2. Run the following command to enable WSL:

    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  3. Restart your machine when prompted to complete the WSL installation.

With WSL enabled, you can now install your Linux distribution of choice from the Microsoft Store. Some popular options include:

  • Ubuntu
  • Debian
  • openSUSE
  • Kali Linux

Simply search for your desired distribution in the Microsoft Store, click "Install", and follow the prompts to set up your Linux environment.

Installing WSL on Windows 11

Installing WSL on Windows 11 is even easier than on Windows 10. Here‘s how to do it:

  1. Open a command prompt or PowerShell window.

  2. Run the following command:

    wsl --install

This single command enables the necessary Windows features, downloads the latest Linux kernel, sets WSL 2 as the default, and installs the Ubuntu distribution automatically.

If you want to install a different Linux distribution, you can do so using the -d flag:

wsl --install -d openSUSE-42

Configuring WSL 2

By default, WSL 2 uses a dynamic memory allocation scheme that adjusts the amount of memory available to the Linux VM based on your usage patterns. However, you can set a custom memory limit if needed.

To configure the WSL 2 memory limit:

  1. Create a new file named .wslconfig in your user directory (e.g., C:\Users\YourUserName\.wslconfig).

  2. Add the following lines to the file:

    [wsl2]
    memory=4GB

    This sets the maximum memory available to the WSL 2 VM to 4 GB. Adjust the value as needed for your specific requirements.

  3. Save the file and restart your WSL distros for the changes to take effect:

    wsl --shutdown

    The next time you start a WSL distro, it will use the specified memory limit.

You can also configure other WSL 2 settings in the .wslconfig file, such as the number of processors available to the VM or the swap file size. Consult the official WSL documentation for more details on available configuration options.

Working with WSL

With WSL installed and configured, you‘re ready to start using Linux apps on Windows. Let‘s explore some common tasks and workflows.

Accessing Windows Files from WSL

One of the handy features of WSL is the ability to access your Windows files from within the Linux environment. WSL automatically mounts your Windows drives under the /mnt directory.

For example, to access your Windows user directory from WSL:

cd /mnt/c/Users/YourUserName

Replace YourUserName with your actual Windows username.

From here, you can use standard Linux commands and tools to work with your Windows files. However, keep in mind that you should avoid modifying Windows files from within WSL, as it can lead to file corruption due to differences in file metadata handling between Windows and Linux.

Launching Linux GUI Apps

While WSL is primarily a command-line environment, it also supports running Linux GUI applications thanks to the introduction of WSLg in Windows 11 and recent Windows 10 builds.

To launch a Linux GUI app from WSL:

  1. Install the app using your distribution‘s package manager. For example, to install the GIMP image editor on Ubuntu:

    sudo apt update
    sudo apt install gimp
  2. Launch the app from the WSL command line:

    gimp

The app will open in a window managed by WSLg, just like a native Windows app.

Note that not all Linux GUI apps may work perfectly under WSLg, particularly those that rely on advanced graphics capabilities or specific desktop environment features. However, most common productivity and development tools should run without issue.

Developing with WSL

One of the primary use cases for WSL is development. With WSL, you can set up a full Linux development environment on your Windows machine, complete with all the tools and libraries you need.

For example, let‘s say you‘re a web developer working on a PHP project. With WSL, you can set up a LAMP stack (Linux, Apache, MySQL, PHP) to run your application:

  1. Install Apache, MySQL, and PHP in your WSL distro:

    sudo apt update
    sudo apt install apache2 mysql-server php libapache2-mod-php
  2. Configure Apache and MySQL as needed for your application.

  3. Place your application files in the Apache document root directory (e.g., /var/www/html).

  4. Start the Apache web server:

    sudo service apache2 start
  5. Access your application in a web browser at http://localhost.

You can use similar steps to set up other development environments, such as Python with Django or Node.js with Express.

One of the great things about using WSL for development is that you can use Windows tools and IDEs alongside your Linux environment. For example, you can use Visual Studio Code with the Remote – WSL extension to work on your Linux-based projects from a familiar Windows interface.

Managing Servers with WSL

In addition to local development, WSL is also useful for managing remote Linux servers. With WSL, you can use the same Linux tools and commands you‘re already familiar with to connect to and administer your servers.

For example, you can use the ssh command to connect to a remote server:

ssh user@hostname

Once connected, you can use standard Linux commands to manage the server, such as:

  • apt or yum to install and update packages
  • systemctl to manage system services
  • vim or nano to edit configuration files
  • rsync or scp to transfer files

WSL is particularly handy for managing servers when you need to work with both Windows and Linux tools. For example, you might use WSL to connect to a server and perform some maintenance tasks, then switch to a Windows tool like PuTTY or WinSCP for more advanced configuration or file transfers.

Advanced WSL Configuration

While WSL works well out of the box for most use cases, there are several advanced configuration options that can help you optimize your WSL environment for your specific needs. Let‘s take a look at a few examples.

Using a Different Default Shell

By default, WSL uses the bash shell as the default login shell. However, you can change this to any other shell installed in your WSL distro.

For example, to set zsh as your default shell:

  1. Install zsh in your WSL distro:

    sudo apt install zsh
  2. Set zsh as your default shell:

    chsh -s $(which zsh)
  3. Log out and log back in to your WSL distro for the changes to take effect.

You can use a similar process to set other popular shells like fish or tcsh as your default shell.

Customizing the WSL Prompt

The default WSL prompt is fairly basic, showing just the current directory and a dollar sign ($). However, you can customize the prompt to include additional information, such as the Git branch for the current directory or the exit status of the last command.

To customize the WSL prompt, you‘ll need to modify the PS1 environment variable in your shell configuration file (e.g., ~/.bashrc for bash or ~/.zshrc for zsh).

For example, to set a prompt that shows the current directory, Git branch, and exit status:

PS1=‘\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(git_prompt_info)\$ ‘

This uses ANSI color codes to set the colors for different parts of the prompt, such as green for the username and host, and blue for the current directory.

You can find many more examples of custom prompts online, or create your own using the available prompt escape sequences for your shell.

Enabling Persistent SSH Connections

If you frequently use SSH to connect to remote servers from WSL, you may find it inconvenient to enter your SSH passphrase each time you connect. You can enable persistent SSH connections to avoid this.

To enable persistent SSH connections in WSL:

  1. Install the keychain package in your WSL distro:

    sudo apt install keychain
  2. Add the following lines to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc):

    eval `keychain --eval --agents ssh id_rsa`

    Replace id_rsa with the name of your SSH key file if different.

  3. Source your shell configuration file or restart your WSL distro for the changes to take effect:

    source ~/.bashrc

    or

    source ~/.zshrc

With these changes, keychain will start automatically when you log in to your WSL distro and prompt you for your SSH passphrase. It will then store the passphrase securely and use it for subsequent SSH connections, so you don‘t have to enter it each time.

Conclusion

Windows Subsystem for Linux has revolutionized the way developers work on Windows, providing a powerful and flexible Linux environment directly integrated into the Windows experience. With WSL 2‘s full Linux kernel compatibility and improved performance, it‘s now possible to run most Linux workloads on Windows with near-native speed and reliability.

As a full-stack developer, I‘ve found WSL to be an indispensable tool in my workflow. Whether I‘m setting up a local development environment, managing remote servers, or working with Linux-based tools and utilities, WSL enables me to do it all from the comfort of my Windows machine.

In this guide, we‘ve covered the essentials of getting started with WSL, from understanding the WSL architecture to installing and configuring WSL on Windows 10 and 11. We‘ve also explored some of the key features and workflows that make WSL so useful for developers, such as accessing Windows files from Linux, running GUI apps, and managing servers via SSH.

Of course, we‘ve only scratched the surface of what‘s possible with WSL. As you dive deeper into using WSL for your own projects and workflows, you‘ll discover countless more ways to customize and optimize your environment to suit your needs.

I encourage you to experiment with different Linux distributions, development tools, and configuration options to find the setup that works best for you. And don‘t be afraid to dive into the Linux command line and explore the vast ecosystem of open-source tools and utilities available to you.

With WSL, the power and flexibility of Linux is just a few keystrokes away on your Windows machine. Whether you‘re a seasoned Linux veteran or just starting out, WSL provides a welcoming and accessible entry point into the world of Linux development.

So what are you waiting for? Fire up a WSL terminal and start exploring the possibilities! And if you have any questions or run into any issues along the way, don‘t hesitate to reach out to the vibrant WSL community for support and guidance.

Happy coding!

Similar Posts