5  Windows Setup for WSL2

5.1 Check and Update Microsoft Account:

Ensure you have a Microsoft Account for better synchronization and access to various Microsoft services. If you don’t have one, you can create it at https://account.microsoft.com/.

5.2 Open File Explorer and make File extensions visible:

Open File Explorer. To see file extensions in File Explorer, follow these steps:

  • Go to the “View” tab in the File Explorer menu.
  • Check the “File name extensions” box to display file extensions.

5.3 Install Windows Subsystem for Linux (WSL) 2:

It is recommended to use the enhanced WSL (version 2) for setting up RStudio (local/server) on Windows 10 or higher. You can check your Windows version by pressing the Windows logo key + R, typing winver, and hitting Enter.

To enable the use of Linux command-line tools on Windows, you can install WSL2. Here’s how:

  • Open PowerShell as an administrator (search for “PowerShell” in the Start menu, right-click, and select “Run as administrator”).
  • Run the following command to enable WSL:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • Enable the “Virtual Machine Platform” feature by running the following command in the PowerShell:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  • Restart your computer when prompted.
  • Open “Windows PowerShell” with admin privileges again.
  • Set WSL 2 as your default version using the following command in the PowerShell:
wsl --set-default-version 2
  • Download and install the latest WSL2 Linux kernel if you see the following message:

WSL2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel.

5.4 Install the Ubuntu Linux Distribution:

WSL2 allows you to run various Linux distributions. To install Ubuntu, run the following command in Windows PowerShell:

wsl --install

We recommend using Ubuntu 20.04 which is compatible with the RStudio Server which can be found here: https://aka.ms/wslubuntu2004

5.5 Set Up Your Linux Distribution:

Once the WSL instance prompts for a username, provide a username followed by password that will be used for both your WSL distribution and RStudio Server authentication later. You can now close the WSL instance by typing the following command:

exit

5.5.1 Update and Upgrade:

In your WSL terminal, update and upgrade the installed packages using the following commands:

sudo apt update
sudo apt upgrade

The apt update command updates the list of available packages and their versions, but it does not install or upgrade any packages. The apt upgrade command installs newer versions of the packages you have.

5.5.2 Install Git:

To install Git, use the package manager of your Linux distribution. For example, with Ubuntu, run:

sudo apt install git

5.6 Install Visual Studio Code (VS Code):

You can download and install VS Code from the official website at https://code.visualstudio.com/.

5.7 Optional Steps

5.7.1 Configure Windows Terminal Profiles:

Customize your Windows Terminal profiles to include WSL distributions for a better command-line experience.

5.7.2 Using bash on Windows Terminal

Windows Terminal supports bash as a default shell. To set up bash as the default shell in Windows Terminal, you need to create a new profile or modify an existing one. Here are the steps:

  • Open Windows Terminal by searching for it in the Start menu.
  • Click on the drop down menu icon in the title bar of the Windows Terminal and select “Settings”. Alternatively, you can press Ctrl +, to open the settings.
  • In the Settings, find the “Profiles” section on the left sidebar. Here, you’ll see a list of profiles for different shells.
  • Scroll down to the “list” section under “profiles” and add a new profile for bash. Here is an example profile configuration for bash:
{
    "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
    "hidden": false,
    "name": "Ubuntu",
    "source": "Windows.Terminal.Wsl",
    "startingDirectory": "//wsl$/Ubuntu-20.04/home/yourusername",
    "icon": "C:\\Path\\To\\Your\\Icon\\ubuntu.png",
    "colorScheme": "One Half Dark",
    "fontFace": "Consolas",
    "fontSize": 12
}

Replace yourusername with your actual WSL username.

You can change other settings like the icon, color scheme, font face, and size according to your preferences.

  • Save the changes to the settings file.
  • Close and reopen Windows Terminal for the changes to take effect.

Note: The actual GUID, startingDirectory, and other settings may vary based on your specific WSL distribution and preferences. Adjust them accordingly.

5.7.3 Set Up an SSH Key through WSL terminal (if needed):

If you use Git or need to connect to remote servers securely, set up an SSH key using the ssh-keygen command within WSL.

5.7.4 Verify Your Setup:

Make sure everything is working as expected by testing your CLI commands, Git, and any other tools you’ve installed.