Using Linux Natively Within Windows 10: A Quick Guide

If you’re a Windows developer who dabbles in Linux (as I’ve done for the past decade), you might have gone down one of the following paths:

  • Install Linux on an old Windows PC, or dual-boot on the same disk.
  • Install Oracle’s free VirtualBox and then install Linux on a virtual machine (VM).
  • Install Hyper-V on Windows Pro and then install Linux on a VM.

The last two make it very easy to try different flavors of Linux, or use them for specific tasks. (I even have a second Windows 10 Pro VM for developing—don’t laugh—Delphi applications; older development in a VM is more convenient than reinstalling Delphi and all components every time I change PCs, and it’s also easier to back up.)

VMs are also a lot more convenient than fiddling with multiple PCs, monitors and keyboards. But the price of convenience is the extra RAM you need; my current PC came with 64 GB, and with a VM running I’ve seen it hit 29 GB (16 GB is probably the minimum you’ll need).

But now there’s a fourth path: WSL (or Windows Subsystem) for Linux.

Windows Subsystem for Linux

First, head to Programs and Features in the Control Panel and turn on Windows Subsystem for Linux in Windows Features, or run the following PowerShell command in an elevated PowerShell (i.e., run as administrator). In either case, you’ll need to do a reboot.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

 

Now we need Linux; I went for Ubuntu, which you can find (surprise!) in the Microsoft Store. Given the history of bad blood between Microsoft and the Linux community, it’s an odd feeling to download Linux from that storefront (and it’s a modest download—195 MB). It will only install on a System Drive, so make sure you have the space available.

After you’ve entered a new user name and password, you log in to a bash shell. The first thing I did was the typical sudo apt update, and then upgraded to bring everything up-to-date.

Working with Files

Windows and Linux/Unix have always had their differences when it comes to text files. Windows uses CR/LF (codes 13 and 10) to mark the end of files, whereas Linux uses just LF. But when it comes to files on the WSL subsystem, you must not edit or create them in Windows directly. It’s an absolute no, as it will corrupt things and necessitate a Linux reinstall.

If you wish to read those files, back up or copy beforehand. On Ubuntu, for instance, the Linux filesystem is located in Windows at:

C:\Users\%USERNAME%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\

 

You’ll see the same folders as if you did this in Linux:

ls /

 

If you are in Windows and want to run a command quickly on Linux, this is one way to do it:

dir | wsl ls /

 

The command is piped to WSL, which then runs it; so that’s the same as ls /.

Connect via SSH

Although the openSSH server works, you may experience problems connecting to it. Thankfully, someone has found a workaround for this issue.

I’ve always used WinSCP to browse Linux filesystems when connecting remotely, and after doing the workaround, it performed well for me. You can use this for copying files, as it’s not a direct connection but routed via SSH.

If you want to get to the Linux terminal from Windows, just run Bash from a command line. The exit command in Linux returns you back to the Windows command line.

Accessing Drives from Linux

The devices are stored in /mnt; browsing /mnt in WinSCP shows your drives (c, d, e, etc.). You can use the cp command to copy a PDF from Windows to your home folder on Linux, then copy it back to Windows with WinSCP; after that, you should be able to read it. You can copy files directly from Windows so long as you are in Linux and using cp.

Conclusion

This is a convenient way of accessing Linux, so long as you never copy or edit Linux files directly from Windows. Unlike with Hyper-V, RAM use with WSL is quite low, making it more suitable for running on PCs with 4 GB or 8 GB RAM.

There’s no having to fire up Hyper-V and then start and connect to a VM. I could see installing Apache, PHP and MySQL and using it as a web development staging/test server. Unless I’m mistaken, Windows is sharing its ports (as with 22 for SSH), so port 80 should be accessible.

However, I still find it easier to back up a VM than the WSL system, as the process for the latter is not very well documented. Possibly it’s just a matter of backing up the Canonical package with Ubuntu in c:\users\appname\Local\packages (that’s 1.13 GB, so not too large).

The interoperability between Windows and Linux is quite remarkable; but don’t forget that you’re restricted to non-GUI Linux. WSL doesn’t support it (though you could install an X-Server; this AskUbuntu answer suggests you could use VcXsrv, an open-source X-Server, and install Ubuntu desktop).

Related