in

[Explained] How to Use Linux Commands on Windows

As a fellow developer and Linux enthusiast, you have likely found yourself needing to leverage the strengths of both the Windows and Linux ecosystems. Constantly dual booting or spinning up virtual machines can be extremely tedious. Luckily, Microsoft has provided an invaluable built-in feature in Windows called the Windows Subsystem for Linux (WSL) that allows us to run our favorite Linux tools and commands directly within Windows!

In this comprehensive guide, I‘ll explain everything you need to know about utilizing WSL to dramatically enhance your workflow by unlocking the power of Linux right within your Windows environment.

What Exactly is the Windows Subsystem for Linux?

The Windows Subsystem for Linux (WSL) is an incredibly innovative compatibility layer developed by Microsoft that enables native execution of Linux binary executables on Windows.

At a high level, WSL provides a real Linux kernel interface that runs on top of the foundational Windows NT kernel. This Linux kernel component allows Linux ELF64 binary files to run unmodified on Windows. It performs translation of Linux system calls into Windows system calls, allowing seamless integration between the two environments.

In simpler terms, WSL provides an entire Linux userspace experience directly within Windows, giving you that beloved Linux terminal environment coupled with all your favorite tools, utilities, commands, and packages – all while still using your familiar Windows desktop!

Why Use WSL Over Traditional Virtualization?

While traditional virtualization like VirtualBox and VMware has allowed us to run Linux on top of Windows for years, WSL provides substantial improvements in a number of areas:

Performance: By running Linux binaries natively rather than emulating hardware, WSL has significantly lower overhead compared to virtual machines. Linux tools and commands run markedly faster. Filesystem operations are also extremely performant due to the deep integration between the Windows and Linux file systems.

Resource efficiency: WSL consumes far fewer system resources than a traditional VM, saving memory and CPU cycles. It has a much smaller storage footprint as well.

No need for separate VMs: With WSL, you don‘t need to manage cumbersome virtual machines that need configuring, updating and securing independently. Linux runs natively within Windows.

Tighter integration: There is seamless integration between Windows and Linux environments when using WSL, with the ability to access Windows files from Linux and vice versa. Commands executed on one side have instant effects on the other.

Simpler setup: Installing a Linux distribution via WSL is as easy as installing an app from the Microsoft Store. No need to manually partition drives or configure virtual hardware.

Flexibility: WSL supports switching between multiple Linux distros easily, all from within Windows. Want to jump from Ubuntu to Debian to Kali? No problem!

Development focused: WSL provides an amazing Linux development experience while still providing full access to the Windows ecosystem. Developers can build and debug cross-platform apps with tremendous flexibility.

For productivity focused power users and developers needing tight integration between Windows and Linux tools, WSL delivers incredible advantages over traditional virtualization approaches.

A Quick Look Under the Hood

Understanding how WSL actually works helps appreciate the technical elegance of its implementation.

At its core, WSL provides a Linux kernel built by Microsoft that runs directly on top of the Windows NT kernel, rather than in a separate VM. This Linux kernel handles interfacing with the Linux binaries, and translates Linux system calls into Windows NT system calls.

However, the WSL Linux kernel itself does not contain a full Linux userspace or distro. Instead, actual Linux distributions like Ubuntu are installed on top of the WSL kernel, providing the userspace, packages, tools and commands. These distro images are deployed and tightly integrated within Windows just like a Windows app.

So in summary, the base WSL component developed by Microsoft provides the Linux kernel on Windows NT kernel, which lets you install actual Linux distros on top of it. The distro provides the userspace, while the kernel interfaces with Windows.

This architecture delivers the best of both worlds – the Linux environment runs natively for unmatched performance, but integrates tightly with Windows for interoperability.

WSL Architecture Summary

Now that we understand what WSL is and why it‘s useful, let‘s look at how to install and use it effectively.

Installing WSL the Right Way

To utilize WSL, you need to be running Windows 10 version 2004 and above, or Windows 11. The installation process is straightforward:

  1. Open PowerShell as Administrator and run:

    wsl --install

    This will install all necessary components for WSL to function properly.

  2. Restart your computer when prompted so Windows can switch to the proper hypervisor configuration.

  3. After restarting, verify WSL is ready by entering:

    wsl -l -v

    You should see Windows Subsystem for Linux with the WSL version number.

Now we are ready to add a Linux distro. I recommend Ubuntu or Debian to start.

  1. Open the Microsoft Store app and search for your Linux distro of choice. Click Install.

    This will download the distro image and deploy it integrated within Windows. Be patient as this can take some time.

  2. Once installed, you can launch the distro from the Start Menu. A terminal window will open, providing you with a fully functional Linux shell within Windows!

That‘s all there is to it – you now have a Linux terminal ready for use within Windows thanks to the magic of WSL!

Unleashing the Linux Command Line on Windows

Once set up, using Linux tools and commands on Windows via WSL is a seamless experience.

For example, after installing Ubuntu let‘s launch the terminal:

Ubuntu Terminal

I can now run Linux commands like ls, grep, package managers like apt, and any other Linux tools.

Some examples:

  • List files in home: ls ~

  • Search file contents: grep "text" *

  • Install packages: sudo apt update && sudo apt install git

  • Check disk usage: df -h

WSL provides full fledged Linux command line functionality. You can install packages, edit files with Vim, use Perl/Python/Ruby, compile code – anything you can do on Linux.

To run Linux GUI apps, install an X server like VcXsrv or Xming. Then launch GUI apps with the DISPLAY variable set.

One of WSL‘s biggest strengths is seamless interoperability between Windows and Linux. You can access Windows files from Linux using the /mnt directory.

For example, to list files on your C drive:

ls /mnt/c/Users/MyName/Documents

You can also run Windows programs from Linux using the .exe extension:

./myapp.exe

This bidirectional interoperability opens up incredibly powerful workflows.

Tips for Productivity with WSL

After using WSL extensively, I‘ve discovered many useful tips for being more productive:

  • Access Windows files using /mnt/c/ instead of /mnt/c for better compatibility.

  • Run explorer.exe . from WSL to open the current Linux folder in File Explorer.

  • Convert Windows paths to WSL format with wslpath ‘C:\Files‘.

  • Use multiple distros and switch between them easily – wsl --list and wsl -d <name>.

  • Make a distro default with wsl --set-default <name>.

  • Integrate your favored editor like VSCode for cross-platform development.

  • Share environment variables between Windows and WSL.

Mastering these tips will let you seamlessly integrate WSL into your workflow.

Alternative Options

While WSL is the best way to natively run Linux commands on Windows 10 and 11, there are a couple alternatives if WSL is not an option:

Cygwin – Provides Linux-like tools and Bash terminal for Windows. Good option for command line utilities but lacks WSL‘s deeper integration.

Virtual Machine – VirtualBox, VMware, etc can run full Linux desktops. More resource heavy but allows Linux GUI apps.

For most developers, WSL hits the sweet spot between convenience and functionality. But the alternatives provide options if needed.

WSL is a Game-Changer for Windows/Linux Workflows

For both developers and power users who need access to the powerful Linux toolchain on Windows, the Windows Subsystem for Linux is an absolute game-changer.

WSL enables installing real Linux distros natively on Windows with incredible interoperability between the environments. This unlocks tremendous productivity enhancements and flexible cross-platform workflows, while avoiding the downsides of virtual machines.

Microsoft has shown strong commitment to rapidly evolving WSL with new features and performance enhancements. As someone who uses both Windows and Linux extensively, WSL has become an indispensable part of my toolkit. I hope this guide helps you also take advantage of using Linux commands on Windows!

Let me know if you have any other questions!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.