# Setting Up a Programming Environment via Windows 10 Bash

In 2016, Microsoft added to Windows 10 (64 bit) the ability to run Ubuntu Linux in parallel with Windows. Variously referred to as “Bash on Windows”, or “Ubuntu on Windows”, and more officially as the “Windows Subsystem for Linux” (WSL), this seems to be a very useful way to work with Linux-based software development tools from a Windows 10 machine.

What it is
This provides a Linux OS running alongside Windows. Both share the same hard drive (and can access each other’s files), and the clipboard supports copy-and-paste between the two quite naturally. (A small thing, perhaps, but one that I find quite important in practice.)
What it is not
This provides a “server”-style installation of Linux. There is no full Linux desktop. The main entry point to Linux is a text-only bash shell for entering Linux commands. The ability to inter-script (i.e., to launch Windows programs from Linux or vice versa) seems quite limited.
What it can be
With VSCode running natively Windows, you get a very natural way to run Linux compilers and other development tools.

Combined with an X server running under Windows, you can launch and run GUI programs from Linux.

In this document, I’ll walk you through the process of setting up a programming environment consisting of:

• The basic Linux on Windows system.
• An X server (optional: to display Linux-based GUI programs on the Windows-managed display screen).
• Java and C++ compilers
• The VSCode IDE

Now, yes, you can install those compilers directly in Windows, but some things just seem to me to run more “smoothly” in Linux. And if your PC is a different version of Windows (or not Windows at all), those other guides are where you should go.

And there’s nothing wrong with trying out both approaches.

Personally, I favor this approach for C++ development, but will usually do Java development natively in Windows, using VSCode as my IDE in both cases.

# 1 Get the Windows 10 Subsystem for Linux

You’ll find the instructions here, but in case that page moves or disappears, the summary is:

1. Make sure you have Windows 10, 64-bit, and that you have been keeping it updated. (As of 6/1/2017, Microsoft says that you should have update 14393.0 or later).

2. Turn on Developer Mode: Open Settings -> Update and Security -> For Developers and select the “Developer mode”. Close the Settings window.

3. Enable Linux for Windows: From the taskbar, search for “Turn Windows features on or off”. Select “Windows Powershell 2.0” (if not already selected) and select “Windows Subsystem for Linux (beta)”. Click OK.

4. Back at the taskbar, search for “ubuntu”. You should see that “Ubuntu App” has been installed. Select it to run bash.

Try some simple Linux commands such as ls, cd, and pwd. You’ll find that your Windows lettered disc drives are available under ‘/mnt’. For example, your C: drive is /mnt/c.

## 1.1 sudo apt-get

Before we continue on, let’s introduce a couple of the programs that you will be using through the rest of this process.

• apt-get is the program you use to update your Linux software and to install or uninstall software packages.

apt-get, however, can only be run by a Linux administrator. Now, you are the administrator for the Linux OS you have just installed, but for safety’s sake you run most programs as an ordinary, non-administrator user.

• sudo is a command that says “run the following as the administrator”.

For example, the following sequence is how you update your Linux software:

sudo apt-get update


The first command actually fetches the latest information about what updates are available. The second installs the updates. The “sudo” in front causes them to be run as the administrator. When you give the first “sudo” command, you will be prompted for your password to prove that you really are the account owner. After that, sudo remembers your identity for a short period of time, so you can give multiple sudo commands in a row, only asked for your password once, as long as you don’t take too much time in between.

Go ahead and give those two commands now.

Try to give those two commands on a fairly regular basis so your Linux OS stays up to date. You’ll actually be notified when you start a new bash session if there are updates awaiting.

Close your bash session for now by giving the command

exit


# 2 X

Now let’s work on displaying graphics from your Linux programs.

You can skip this step if you don’t intend to run or develop GUI-based programs from Linux.

The first step is to get an X server package. Most X server packages put a heavy emphasis on connecting to remote machines, but we are going to use this one to serve programs running on the same local machine.

• I find the VcXsrv works very well in this setup.

Warning: X2Go, our favorite way to remotely log in to the CS Dept Linux servers also uses VcXsrv. This should not be a problem, but there is a bug in some versions of VcXsrv that can cause our intended use of VcXsrv to clash with X2Go.

If you are a frequent user of X2Go, I recommend using one of the other X packages listed below instead.

• XMing seems to be a good choice as well.

• Get the public domain release for “Xming” and for the “Xming-fonts”.
• Cygwin/X also works quite well, and if we were using the CygWin ports of the g++ compiler, it would be my top choice. But since we are going to get those in the Windows Subsystem for Linux, it seems unnecessary to add the entire CygWin “POSIX inside Windows” layer.

• By default, CygWin/X accepts X connections from outside, but not from programs running on the same machine. The latter is exactly what we want to use it for. When launching CygWin/X, add “-listen tcp” to the “Additional parameters for X server” box.

1. So, go to one of those X server websites, download the installer, and run it to install an X server.

Then run the Xlaunch program to launch the server.

Get in the habit of running this X server just before you open a Windows bash session. It won’t do much of anything when you run it, other than add a new icon in the task bar tray. It is a server, a program that sits quietly and waits until some other client program calls upon it.

2. Run Windows ubuntu.

3. We’re going to test your ability to run X GUI applications from within Linux and to see the results in Windows. So first we need to install an X application. In bash, give the command

sudo apt-get install xterm


When the installation is complete, test it by giving the command

DISPLAY=:0 xterm &


(For CygWin/X, you may need to change the “:0” to “:0.0”.)

You should see a new window open up. This is another window in which you can issue Linux commands. It is, however, a Linux/X application, using X to render its general appearance, menus, etc. You can see this by holding the Ctrl key and then left- or right- clicking on the xterm window. Each mouse button will pop up a different menu.

4. The DISPLAY= part of the earlier command is the way that we tell an X _client" program (in that case, xterm) where to find an X server to handle drawing things and on which of many possible screens we want to draw (from among many that server might be managing). In this case, the DISPLAY value is pretty simple, because we are not connecting to a remote machine and we’re using using the default screen.

Still, we don’t want to have to add that to every command. So let’s set that as the default for our Linux applications.

Do

nano ~/.bashrc


Add the following line to the end:

export DISPLAY=:0


Save that and exit from nano.

Exit from bash. And then restart it. Now

xterm &


should open an xterm window without your needing to supply the DISPLAY preamble.

# 3 Installing the Compilers

Now we’re ready to start installing some real software. You will need

• The Java compiler and run-time system.
• The g++ compiler for C++.
• The gdb debugger.
• The make project build system.

If you are only interested in C++ or only interested in Java, you can skip over the steps for the other language.

We’ll install all of these with the following apt-get commands:

sudo apt-get install default-jdk
sudo apt-get install g++
sudo apt-get install gdb
sudo apt-get install make


Once those are done, verify your installation by typing the following in bash:

java -version
g++ --version
gdb --version
make --version


Each should respond with an identifying message making clear that the software is installed and running.

# 4 Installing VSCode

Although VSCode can be installed in Linux, I recommend installing it as a Windows application and using its remote development features to connect to your WSL Linux.

1. Get VSCode here and install it in Windows.

2. Run VSCode. In a freshly installed state, it does not know how to work with C++ or Java, and it does not know how to do remote development. We’ll fix these limitations by immediately installing some extensions.

Click on the button to enter the list of extensions.

3. Use the text box at the top of the left column to search for “C++”. Locate and install the following extensions:

• Remote - WSL
• C/C++
• C++ Intellisense
• Better C/C++ Syntax
• Java Extension Pack

(If you already had VSCode on your Windows PC, you may have already installed some of these extensions. However, after adding the Remote - WSL extension, you will want to check that each of the other extensions says that it is “installed globally” or “installed in WSL”. If not, reinstall it.)

# 5 Try it Out

1. In a WSL bash window, create a convenient directory and cd into it.
wget https://www.cs.odu.edu/~zeil/cs252/sum21/Public/make/makeTry.zip
unzip makeTry.zip
ls

3. You should have a new makeTry directory containing a simple project.

4. Launch vsCode from that directory:

cd makeTry
code .


The code command signals Windows to open an instance of VSCode looking at the current directory (.).

Try looking at the directory from within VSCode. Click on some of the files to load them into the editor.

5. Follow these instructions to tell VSCode to use “make” to compile this project.

Try building and running the project.

6. Follow these instructions to configure the debugger to run one of the executables for this project.

# 6 Optional packages

The main purpose of this document was to set up your programming environment. But since you now have a working Linux installation running under Windows 10, here’s a few optional packages you might want to consider installing.

Each of the packages named here can be installed via the apt-get command. All of these programs, unless sated otherwise, are GUI based and will require you to be running X.

• scite: a simple, intuitive GUI-based editor (described here that is lightweight (in disk requirement) but still offers syntax highlighting for many programming languages.

There are others that are more popular (e.g., gedit, but many of those rely on a host of other libraries that are normally provided by the full Linux desktop, and since you aren’t running a Linux desktop, apt-get would need to download and install many tens of megabytes of those other libraries).

sudo apt-get install scite

• emacs: Definitely not lightweight, but if you have gotten used to it…

sudo apt-get install emacs

• File Explorers: If you really dislike cding around just to examine your files and directories, you can have a file explorer without installing a full desktop, or even an appreciable fraction of the libraries needed to support a desktop.

• mc: short for Midnight Commander, this is very lightweight and actually runs in text mode - it does not open in a separate X window.

sudo apt-get install mc

• xfe: a more conventional, GUI-based (X windows) file explorer, but still very lightweight. It is described here.

sudo apt-get install xfe