The Economics of Software in eMobility: Financial Strategies & Optimal ROI

Webinar 09.05

close button
DevelopmentWSL

Windows Subsystem for Linux (WSL) explained

10 NOVEMBER 2020 • 10 MIN READ

Paweł Baczyński

Paweł

Baczyński

Windows Subsystem for Linux (WSL) explained  header picture

Introduction

No matter if you program in Java or Kotlin or with other technologies, operating on Linux has a number of advantages. One of them is access to Linux Bash with its useful commands.

However, some developers have to or prefer working on Windows. As stackoverflow.com 2020 survey showed, almost half of developers (45,8%) work in the Windows environment. There are tools that bring them closer to the Linux world like Cygwin.

The WSL provides a near-native Linux experience

Some time ago Microsoft introduced WSL, a new tool that brings Windows users even closer to the Linux experience. Here you can find gathered some frequently asked questions and our expert’s experience to help you get a better understanding of the WSL.

What is WSL?

WSL or Windows Subsystem for Linux is a part of the Windows operating system that allows running native Linux binaries. A number of distributions exist that can be installed and used.

How can I install WSL?

The first thing to do before installing any Linux distribution is to install the WSL itself.

Go to Control PanelProgramsPrograms and FeaturesTurn Windows features on and off.

Select Windows Subsystem for Windows, confirm, and restart the system.

windows install

Now you are ready to select and install a Linux distribution of your choice.

What Linux distributions are available on WSL?

To find a list of available distributions open Windows Store and search for "Linux".
The most popular are:

  • Ubuntu 18.04 and 20.04
  • SUSE Linux Enterprise Server
  • Debian
  • Fedora Remix for WSL
  • Kali Linux
windows-store-search

Is it possible to run graphical Linux applications from WSL?

Yes, it is possible. You need to install an X Window System server application on your Windows, for example Xming X Server for Windows.

Then check if your WSL system has a DISPLAY environment variable set up. If not set it to :0.

export DISPLAY=:0

After this configuration you are free to run native Linux applications with user interface.

xclock

Where is wsl.conf located?

Each WSL instance can be further configured by creating and editing a config file /etc/wsl.conf.

How to access Windows files from WSL?

All fixed drives with the NTFS or ReFS file system are automatically mounted in /mnt directory. For example a

C: drive can be accessed in /mnt/c/

The directory where drives are mounted can be specified in /etc/wsl.conf as follows:

[automount]
root=/

This setting will cause drives to be mounted under the root folder, so the C: drive would be accessible through /c folder.

How to access pendrive from WSL?

Unlike fixed drives, removable drives are not mounted automatically. To access files stored on a USB stick you need to mount it yourself. For this purpose, use these commands (assume the drive letter in Windows is F):

sudo mkdir /mnt/f
sudo mount -t drvfs H: /mnt/f

How to connect to a DVD drive from WSL?

Optical drives are not as popular nowadays as they used to be. Nonetheless, they can be accessed the same way as pendrives (assume G is the drive letter):

sudo mkdir /mnt/g
sudo mount -t drvfs G: /mnt/g

Where are WSL files stored?

WSL files are exposed through a network share \\wsl$\[distro name], for example my home directory is at \\wsl$\Ubuntu-20.04\home\pawelb.

Physically the WSL files are located at

%USERPROFILE%\AppData\Local\Packages\[distro name]

My home folder is at this rather long path

C:\Users\pawelb\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home\pawelb.

However, it is worth noting that manipulating WSL files from within Windows should be avoided as it can destroy Linux-specific file metadata. If you need to manipulate files on both WSL and Windows, store them on a Windows drive and access them from within WSL via /mnt.

Is it possible to run Windows programs from within WSL?

Yes, the WSL was built with interoperability in mind. It can run native Windows programs. However, if this feature is not needed, the user can disable it by adding enabled=false into [interop] section in wsl.conf.

[interop]
enabled=false

What terminal application to use?

Any terminal can do the job, even the good old cmd.exe. Just run the WSL command in. There is however one great application that makes running the WSL console easier. It’s Windows Terminal and it can be installed from Windows Store. It automatically detects any WSL distributions installed and adds an option to run its console. It also supports regular Windows Command Line, PowerShell, and Azure console out of the box. Also, it supports tabs and split view inside a tab.

 wsl windows-terminal

Caveats of interoperability with Windows

By default, WSL can run Windows binaries and also appends its %PATH% variable into the $PATH variable of Linux running under WSL. In most cases, this is useful or at least does no harm. However, sometimes unexpected behavior occurs.

This happened when I tried to run npm.

$ npm -v
module.js:471
    throw err;
    ^

Error: Cannot find module '\\wsl$\Ubuntu-20.04\c\Programs\nvm\v6.10.2\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

This error message is not helpful at all. What happened?

After some investigation, I found out that I had also installed npm on Windows and WSL was using this executable to run. This unfortunately failed under WSL environment.

A fix was rather simple. I disabled appending Windows %PATH% to WSL $PATH.

This can be done in two ways.

The first is to use the Registry Editor (regedit) to add a DWORD AppendNtPath with value 0 under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss. This affects all WSL distributions.

The second way is to set a property in /etc/wsl.conf. This affects only a single distribution.

[interop]
appendWindowsPath=false

The WSL is a great subsystem for Windows

With the introduction of Windows 10, a lot has changed. It seems that the system and its features are more and more thought-out and intuitive. A programmer needs a proper working environment. With WSL, Windows is keeping up with developments in this area, and looks like it is a step in the right direction. It’s worth having an eye on the development of this solution, hoping that in the future the developers will have a variety of systems that can really compete with each other. Looking for a good and stable, seamless tool, it is worth trying with the WSL.

Solidstudio develops a fleet of software products designed to facilitate EV charging network management. See more about our CPO platform and eMSP app.