Home Articles Categories Series
Pythonise Just now
Recommended learning

Configuring Windows Subsystem For Linux

Setting up your WSL development environment & shared directory


Article Posted on by in WSL
Julian Nash · 9 months ago in WSL

In this short guide, we're going to share how we configure our WSL installation and development environment.

First up, a few things to note. The Windows subsystem for Linux is not your typical Linux installation and doesn't work like a desktop operating system. You can't run GUI applications or anything with a window/user interface other than the bash shell, so think of it as more like a Linux server distribution embedded in your Windows machine.

Secondly, there are applications that cannot be run on the WSL, such as Docker or Kubernetes. Whilst you may be able to download and install various apps via the command line, there are some which require a native Linux installation to function properly (There are some workarounds which we'll explore later in this series)

But don't let that put you off! Most of the Linux functionality is available except for some cases where things such as virtualization is required.

Ok let's jump in to setting up your development environment.

Creating a shared WSL directory

As discussed in the first post in this series, you do not want to access of modify Linux files or directories from Windows. By default, these files are hidden and shouldn't be modified as it can cause corruption, data loss or a complete fudge of your WSL installation.

We won't go into the details of why in this article, you can Read more about it here on the Microsoft developer site.

Instead, we're going to create a new directory on our Windows machine which will become our shared folder. A safe place we can create, modify and access files from both Windows and WSL.

Pick a location on one of your windows drives where you'd like to keep all of your shared files. We're going to call our folder wsl and create it at the root of our C drive

You can do this via Windows of WSL. We'll use WSL.

Launch your bash shell from the start menu or search for your distro using Cortana. By default it's going to launch you in your Linux home directory at /home/username

Navigate to the root directory with the following command

cd /

You're now in the root directory of your Linux distro. If you run the ls command you'll see a bunch of directories & files

ls

You'll see something along the lines of below (Depending on your distro)

bin  boot  dev  etc  home  init  lib  lib64  media  mnt  opt  proc  root  run  sbin  snap  srv  sys  tmp  usr  var

We're not going to go into what all of these files & directories are in this guide. For now, just know that the mnt directory contains all of your mounted drives. Go ahead and run the following command

cd mnt

Now run the ls command

ls

You should now see a list of drives available. In our case, we're going to use the c drive but you can pick any drive you like. Run the following command to enter the drive

cd c

You're now at the root of your drive. Go ahead and run the ls command to see the contents

ls

You should now see all of the files and directories at the root of your drive!

For us, we're going to go ahead and create our shared wsl directory in the root of your c drive. To do so, run the following

mkdir wsl

It's as simple as that! We've created our wsl directory at the root of our Windows c drive that we'll use to contain all of the directories and files we want to work on and share between WSL and our Windows host machine.

Next up, let's setup some useful commands we can use in WSL to access our shared directory.

Aliases

We're going to create some useful aliases to quickly navigate to our wsl directory. Go ahead and run the following to navigate to your Linux home directory

cd ~/

If you run the ls command you'll likely see it's empty. However, running a dirrefent command will show us there are some hidden files. Run the following

ls -la

You should see a .bash_logout, .bashrc and .bash_profile file. We're going to modify the .bashrc file to create some aliases. Run the following to open up a the terminal editor nano

nano .bashrc

You'll see some comments and code which you can leave alone for now. Scroll or page down to the bottom of the file and add the following 2 lines of code (Modify them to match the location of your newly created wsl directory)

cd /mnt/c/wsl
alias wsl="cd /mnt/c/wsl"

So what are we doing here? When you launch the bash shell, it will first run the .bashrc file and execute any commands placed in there.

Firstly we're going to change directory using the cd command to enter our shared wsl directory as this is where we'll be doing most of our work. You can of course leave this line out if you don't want the shell to spawn into this directory.

Secondly, we're adding an alias which we can use from the shell to quickly change directories into our shared wsl folder.

Save and exit Nano using Ctrl+x, y, Enter

We need to reload our .bashrc file for the changes to take place. Run the following command to do so

source .bashrc

When the shell reloads you should now be in your shared wsl directory!

Let's create a directory and a file. Run the following commands

mkdir test
cd test
touch hello_world.txt
echo "Hello windows from Linux!" >> hello_world.txt
cat hello_world.txt

Ok let's quickly go over what we've done

  • Created a directory called test using the mkdir command
  • Changed directory into test using the cd command
  • Created a file called hello_world.txt using the touch command
  • Appended a line of text to hello_world.txt using the echo command
  • Printed the contents of hello_world.txt to the terminal using the cat command

Now let's open this file with an application from Windows and take a look.

Use your Windows finder to head over to wherever you created yourwsl directory. Ours was in the root of our c drive at C:\wsl

Move into the wsl folder and open up hello_world.txt with your favourite editor. You should see the lines of text we placed in it using echo

Let's add a message from Windows. Add the following lines to hello_world.txt

Hello Linux from Windows!

Save and close the file. Let's jump back into our bash shell and take a look. We're going to open the file with nano to inspect our work. Run the following

nano hello_world.txt

Congratulations! You should see the lines of text we added from Windows. You can now create directories and files in your wsl directory and safely use both Windows and Linux applications to work on them without the fear of corrupting anything or losing any data.

Wrapping up

By now you've probably noticed the default Bash terminal is pretty ugly. In our next guide we're going to show you how to quickly make your terminal pretty and a nice place to work in.

Be sure to drop any comments or feedback in the comments section below!

Last modified · 02 Feb 2019
Did you find this article useful?
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Contents
Loading...