linux windows wsl

Run Ubuntu Linux and Bash on Windows 10
Bash on Ubuntu on Windows

Reading time of 1805 words
9 minutes
Reading time of 1805 words ~ 9 minutes


Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee

As of release July 2016, Windows 10 includes the ability for desktop users to install and run a copy of Ubuntu terminal from within Windows! Officially called the Windows subsystem for Linux it usually goes by a slightly more memorable name of Bash on Ubuntu on Windows.

An updated revision of this guide exists for users of Windows 10 Creators Update (1703) released in April 2017.

Benefits and Caveats

Acting much like the Windows Powershell or Command Prompt this new functionality enables you to use Ubuntu’s apt-get software repository to locally download, install and use thousands of open source Linux applications within Windows. Including numerous popular programming languages, development tools and a never-ending sea of text editors. All running within a POSIX environment and without the need of silly workarounds that were traditionally used by Windows ports, think Ruby, Perl, PHP, Apache HTTP.

But it’s not all great news because as of August 2016 it is still technically in beta and the implementation could change. Besides Ubuntu on Windows is built on Ubuntu 14.04 which has been superseded by version 16.04. This means there are a number of newer standard commands and tools based on Systemd will not work in Ubuntu on Windows.

There is likewise a disconnect between Ubuntu and Windows. Directory and file sharing between the two are convoluted while the software written for Linux will have limited access to the host computer’s hardware and may inaccurately report information. System variables and other data cannot be shared between the two environments either.

There is a risk that Microsoft may abandon this pro-Linux effort in future iterations of Windows 10 or leave it to stagnate as is. This is not the first time at a POSIX compatible environment has officially been offered for Windows. Both the Microsoft POSIX subsystem and its replacement Windows Services for UNIX were both eventually abandoned.

Finally and importantly Bash on Ubuntu on Windows is not a server platform. While you can run PHP or Ruby on a dedicated Linux webserver. It can’t be used as a 24 hours, 7 days a week, always on daemon.

Instead, Ubuntu on Windows is intended as a replacement for software containers and virtual machines. Complicated tools that developers working within Windows would traditionally need to test their code when writing applications that were to be deployed on Linux servers.

Requirements

This guide assumes you have a decent understanding of the Linux terminal and Windows Command Prompt.

To install Bash on Ubuntu on Windows you need Windows 10 64-bit 1607 or newer. You can check this by typing about in the taskbar search box which I will refer to as Cortana and select About your PC.

About your PC

Install

In Cortana type and select For developers settings. In the dialogue under Use developer features check the Developer mode option.

Now in Cortana type and select Turn Windows features on or off. Check the Windows Subsystem for Linux (Beta) and press Okay.

Turn Windows features

Once installed and completed type either Bash or Ubuntu into Cortana and select Bash on Ubuntu on Windows. You should have a local Ubuntu terminal running in Windows!

Bash on Ubuntu

In the Ubuntu terminal type lsb_release -a and you will see its Linux distribution information.

lsb_release results

ll will list the content of your active directory which happens to be your Ubuntu home directory.

You also have your scripting Linux terminal staples installed and ready to use.

Bash

$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Python

$ python -V
Python 2.7.6

Perl

$ perl -v
This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi

Interacting with Windows and Ubuntu files

Ubuntu on Windows partition actually shares the NTFS directory structure used by Windows. This makes it accessible to Windows applications and also allows you to access your Windows files within Ubuntu.

Ubuntu on Windows root partition can be reached using the path below. This path can be used in Cortana, Windows File Explorer and the Command Prompt.

%localappdata%\Lxss\rootfs

list of lxss

The \home subdirectory in rootfs, where you would expect your user account directory to be is empty. Rather the home partition can be found using this path.

%localappdata%\Lxss\home\%username%

And the root user account partition.

%localappdata%\Lxss\root

Conversely, you can also access your Windows drives within Ubuntu on Windows. They can be found in the /mnt directory. The following Ubuntu command lists the content of the Windows C: drive.

ll /mnt/c

It’s easy enough to create symbolic links between your Windows and Ubuntu folders. You just need to make sure that any links you create within Ubuntu are relative symbolic links otherwise they will not work correctly.

Unfortunately, data found in environment variables are not shared between Windows and Ubuntu. In my Windows Command Prompt

echo %username% returns Ben and so my User account is located in C:\Users\Ben\.

In the Ubuntu terminal.

echo $USER returns ben but as Linux is case-sensitive the $USER result isn’t useful in this situation. So we will have to manually type the paths when creating the symlink.

Don’t forget to replace Ben in the path with your user account directory name using the correct case. While typing directory or file names tapping the Tab key on the keyboard will trigger Bash’s autocomplete function.

ln -sr /mnt/c/Users/Ben/Downloads ~/Downloads
ln -sr /mnt/c/Users/Ben/Documents ~/Documents

ls ~/Downloads
ls ~/Documents

Now you can easily access and edit files in your Windows downloads and documents directories while in Ubuntu.

ll result
Home directory with symlinks to Windows Users directories

If you want to do the same while using the Windows Command Prompt. In an administrator Command Prompt the command below will create a link named Ubuntu in your User directory, it will point to the home directory used by Ubuntu.

mklink /d %homepath%\Ubuntu %localappdata%\Lxss\home\%username%
symbolic link created for \Users\Ben\Ubuntu <> C:\Users\Ben\AppData\Local\Lxss\home\Ben

Now list the Ubuntu home directory in the Command Prompt.

dir %homepath%\Ubuntu
dir result
List of Ubuntu’s home directory in the Command Prompt

You can then pin that symbolic link to the Quick access pane in File Explorer and even give it a Ubuntu icon located at %localappdata%\lxss\bash.ico.

file explorer result
Quick access in File Explorer

Disabling the terminal bell

One thing I find very annoying is the Windows 10 terminal bell sound. As is often triggered after I tap the Tab key and the Bash autocomplete has too many or zero options. So let’s disable it.

sudo nano ~/.inputrc

Paste in the following.

# ~/.inputrc - See http://ss64.com/bash/syntax-inputrc.html for more options

# do not bell on tab-completion
set bell-style none
# set bell-style visible

Save, exit, then close the Bash on Ubuntu terminal. Re-launch it and it should now remain silent!

Run a simple web server

In Ubuntu, you can quickly serve the current directory over the local web using Python. Here we will serve our user home directory.

cd ~
python -m SimpleHTTPServer
python SimpleHTTPServer

Now in Cortana or in the address bar of your Windows web browser type localhost:8000.

localhost:8000 result

To quit Python’s Simple HTTP Server tap Ctrl+C together in Ubuntu.

Updating Bash on Ubuntu on Windows

Let’s update the Ubuntu distribution.

If you’re outside of the USA you may want to change the sources list that Ubuntu uses to check for and download updates from to somewhere geographically closer.

sudo nano -B /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse

Change http://archive.ubuntu.com/ubuntu To http://(country code).archive.ubuntu.com/ubuntu

So for me to use nearby Australian mirrors I switched the the links to the following.

deb http://au.archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb http://au.archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse

Note the http://security.ubuntu.com/ubuntu URL doesn’t offer any mirrors so it can be ignored.

Save and exit nano.

sources.list

Now let’s update Ubuntu.

sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade
sudo apt-get -y autoremove

And you’re done.

Creating a Bash script in Ubuntu on Windows

We will create a simple Bash script to run the apt-get update and upgrade processes.

Ubuntu has a number of directories where you can create and store system-wide accessible scripts but I keep them in /usr/local/bin

cd /usr/local/bin
sudo nano upgrade.sh

Paste in the following script.

#!/usr/bin/env bash
# /usr/local/bin/upgrade.sh
# Refreshes the APT repository applies any package updates.
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade
sudo apt-get -y autoremove

Save and exit.

Make the script executable.

sudo chmod +x upgrade.sh
sudo ln -s upgrade.sh upgrade
ll
ll result

Now we can run the script from any location within terminal and those 4 apt-get commands should run in sequence.

cd /
sudo upgrade

Install a simple web server

It’s very easy to install and run a dedicated web server in Ubuntu on Windows.

sudo apt-get install lighttpd

sudo service lighttpd status
* lighttpd is not running

sudo service lighttpd start
* Starting web server lighttpd

sudo service lighttpd status
* lighttpd is running

Type http://localhost/ into Cortana or your web browser to view the Lighttpd Placeholder page.

Lighttpd web content is stored in /var/www/

It’s configuration file can be viewed.

nano /etc/lighttpd/lighttpd.conf

It’s manual can be found at http://redmine.lighttpd.net/projects/lighttpd/wiki

Note lighttpd will stop running when the Bash on Ubuntu on Windows terminal is closed.

If you prefer, other more widely used web servers are available such as Apache HTTP and Nginx.

sudo apt-get install apache2
sudo apt-get install nginx

Compiling Linux source code on Windows

I use nano as my go to text editor and the included version in Ubuntu 14.04 is a bit out of date, so let’s update it. To do this we can download, compile and install the most recent nano source code onto our Ubuntu on Windows environment.

$ nano -V
nano, version 2.2.4

But first, we need to update our sources.list to allow apt-get access to the source code repositories. This should only ever have to be done once.

sudo nano /etc/apt/sources.list

Append the following to the bottom of the file.

deb-src http://archive.ubuntu.com/ubuntu/ trusty main

You may instead include a country code mirror such as this example for Australia.

deb-src http://au.archive.ubuntu.com/ubuntu/ trusty main

Save and exit then update.

sudo apt-get update

Remove the default nano install.

sudo apt-get remove nano

Install the packages we will need to compile our updated nano program.

sudo apt-get build-dep nano
sudo apt-get install libmagic-dev

Download and extract the nano source code. You can check for the latest source code gzipped version at the nano editor site.

cd ~
wget https://www.nano-editor.org/dist/v2.8/nano-2.8.4.tar.gz
tar -xf nano-2.8.4.tar.gz
cd nano-2.8.4

Now configure, compile the source code and then install the program.

./configure --enable-utf8
make
sudo make install
$ source ~/.bashrc
$ nano -V
nano, version 2.8.4
nano -V result

Brilliant you have just compiled GNU free software written for Linux on Windows 10!

Used and useful resources

Written by Ben Garrett

Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee