Environment Setup for Windows using WSL

OCaml Environment Setup for Windows: WSL #

This guide is intended for users of Windows systems that do not want to install a virtual machine. Here is an outline of the process:

  1. Install WSL
  2. Install a Linux Distribution (we recommend Ubuntu)
  3. Set up OCaml
  4. Set up an Xserver
  5. Set up VSCode (optional, but highly recommended)

1. Installing Windows Subsystem for Linux #

Before we start installation, we need to enable CPU virtualization. CPU virtualization is how Windows can emulate Linux. To do so:

  1. Power off your computer.
  2. Then press the specific hotkey for your computer to enter BIOS. If you don’t know it, look up your computer brand’s BIOS key on Google.
  3. Navigate to the Advanced tab, press Enter
  4. Go to Virtualization, press Enter, and select Enable.
  5. Save your changes, and resume the booting process.

If you have Windows 11 or Windows 10 version 2004 or higher #

You’re in luck! Simply open your terminal and type in

wsl --install

and you’re done! Skip to bash setup in step 2. Otherwise, keep following the instructions below.

Requirements #

To install WSL 2 on Windows you need the following things:

  • Windows 10 May 2020 (2004), Windows 10 May 2019 (1903), or Windows 10 November 2019 (1909) or later
  • A computer with Hyper-V Virtualization support. To see if the Windows 10 May 2020 Update is available on your computer go to Settings > Update & Security > Windows Update.

To check your Windows 10 Version, select Windows + R, type winver, and select OK. You will see the version listed. If you have one of the versions listed above, feel free to continue on with setup. Note that anyone on the 1903 or 1909 builds can install WSL 2 too – but must install Windows Update KB4566116 first.

Your computer also needs to support Hyper-V Virtualization to run WSL 2. You can check your Hyper-V support to be sure.

If your Windows machine does not meet the requirements listed above, you will need to setup a Linux environment in a virtual machine in lieu of WSL 2.

Enable WSL #

Open the PowerShell tool as an Administrator (this is important!) and run the command below. Copy the command to make sure there are no typos.

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Enable Virtual Machine Platform #

To enable Virtual Machine Platform on Windows 10 (2004) open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

To enable Virtual Machine Platform on Windows 10 (1903, 1909) open PowerShell as Administrator and run:

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart

After you run, restart your system.

Download the Linux kernel update package #

Download the latest package: WSL2 Linux kernel update package for x64 machines.

Run the update package downloaded in the previous step. (Double-click to run - you will be prompted for elevated permissions, select ‘yes’ to approve this installation.)

Set WSL 2 as default (optional) #

Open PowerShell as Administrator and run this command to set WSL 2 as the default version of WSL:

wsl --set-default-version 2

Note that this step is not necessary, as WSL 1 works just fine with OCaml and necessary packages. If you are running into errors, and would still like to set WSL 2 as default, consult the WSL troubleshooting page.

2. Install a Linux distribution #

Now that we have WSL installed, we need to install a Linux distribution to use for setup.

Open Ubuntu 18.04 LTS and press “Get” to install. Press “launch” after download finishes and let installation finish.

Bash setup #

After installation has finished, you will need to create a username and password for UNIX. What you choose is up to you, though be very careful to remember the password, as you will need it in the future.

After credentials are created, you will be inside a terminal. As of now, copy-and-paste is disabled in the terminal. To enable, click the orange Ubuntu icon located at the top left corner of the shell window, click Properties, and in Edit Options check “Use Ctrl_Shift_C/V as Copy/Paste”. Click “OK”.

Next, we will need to update and upgrade software inside WSL. Enter the following command into your terminal:

sudo apt update -y

Enter your password, then wait for updates to finish. Then, enter this command to upgrade all the installed packages:

sudo apt upgrade -y

File Setup (optional) #

In the past, WSL had its own filesystem distinct from Windows, which made it difficult to access WSL files from the File Explorer. Since then, updates have been made to WSL that allow for one to explore files via File Explorer. Simply type in the following command:

explorer.exe

Still, it may be helpful to be able to navigate through the files within your UNIX terminal. To do so, enter the following command into your terminal:

ln -s /mnt/c/Users/<your user name>

After this, you can navigate into your user folder by with:

cd <your username>

Note that you can still use the explorer.exe command while in your Windows directory.

3. Setting OCaml #

Installing Software #

Now that WSL and the terminal are working, we will begin to set up a functional OCaml environment (finally!). Enter the following commands into the Ubuntu terminal to finish installing OPAM.

sudo apt-get install -y gcc make patch unzip m4 git xorg libx11-dev pkg-config
sudo add-apt-repository ppa:avsm/ppa
sudo apt update
sudo apt install opam

Installing OCaml tools #

Continue with the guide to install the OCaml tools.

4. Setting up X server #

In CS51, we use graphics extensively for select assignments. WSL does not natively support graphical rendering and graphic displays, so let’s install an X server to allow compatibility between OCaml graphics and WSL.

Installing a Window Manager #

We first install a Window Manager to display our graphics. In writing this guide, it seems that Xfce works well with WSL (though recommedations are always welcome). To install, enter the command:

sudo apt install xfce4 -y

Modifying .bashrc #

Next, we need to modify our .bashrc file. This file is run everytime the bash is interactively started, so we need to define a variable to the bash that allows X server to connect to our terminal. Enter the following into your terminal:

cd ~
nano .bashrc

You should be greeted with a screen filled with code. Use your arrow keys to navigate all the way to the bottom of the page, and enter the following:

eval $(opam env)
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

After, press Control + X to exit, followed by Y to save, and enter to confirm save location.

Installing an X Server #

Next, we install X Server. VcXsrv has seemed to work well with WSL thus far, so we will use that in this guide. Navigate to the installation (found here), and press Download. Run the downloaded .exe and follow the setup.

XLaunch #

We now need to launch our X server. Go to your Start Menu and launch XLaunch. Choose “One large window”, keep Display number at -1, and click next. Check that “Start no client” is selected, and click next. Check “Disable access control” and click next. Click Finish.

Now, resume the instructions at “Verifying that OCaml graphics is working”.

5. VSCode (optional) #

Lastly, we recommend setting up VSCode with WSL, as they integrate nicely. A guide for that can be found here. Once VSCode and selected extensions are installed, install the “Remote - WSL” extension as well. This allows you to open any folder in WSL and build Linux applications. Navigate to the folder that you’d like to start within your newly configured Ubuntu terminal, and simply type:

code .z

You will see installations if run for the first time - this will not happen again unless there is an update. You should see a VSCode pop up, connected to a remote server (to check, see if there is a green bar in the lower left of your window).

6. Wrapping Up #

Congratulations on finishing setup! You now can finish problem set 0. Good luck in CS51, and have fun with OCaml!