Getting Rstudio Running Under Windows 11 with WSL2

R
Windows
Rstudio
Author

Alex Singleton

Published

September 7, 2022

Introduction

Since moving to Windows 11 wholesale I have done all of my R development using a Docker running an Rstudio Server instance. This post aims to illustrate the steps needed to set this up on your own machine.

Install WSL 2

From the start menu right click PowerShell “Run as Administrator”. Type into the terminal that opens:

wsl --install

Wait for the installation to complete; which also installs Ubuntu by default.

Setup Docker

Download Docker Desktop for Windows. There is a prominent button on this linked page.

When this has downloaded, double click to open and run the “Docker Desktop Installer.exe” file which should be found in your default web browser install location.

This will prompt you with a “Configuration” pop up. Make sure the option for “Use WSL 2 instead of Hyper-V (recommended)” is selected and click the “Ok” button. This will then install Docker.

When this has completed, close the dialog box and restart Windows.

Post Setup

After the reboot, you will get a terminal popup that completes the Ubuntu install. This will ask you for a username. Use something memorable and type into the prompt in all lower case letters, then press enter. You will then be asked to setup a password for the installation too; choose something you will remember, type into the prompt and press enter. You can close the Ubuntu terminal window.

You should also see a Docker Desktop icon your Desktop or Start Menu. If you double click this and accept the T&C on the popup prompt, it will run Docker Desktop. This is the interface where you can see what images you have downloaded, and what containers you currently have setup, along with their current status .. ie if they are running or not. This will all be empty at the minute as you have not yet downloaded an image or started a container.

Setup a Container for Rstudio

The next step is to download a docker image, which contains R, Rstudio and lots of useful packages. Before we do this, we will create a folder which you can use for your R projects - you can change this, but as an example we will create a new folder called “r_projects” in the root of your C: drive (e.g. “C:_projects”). You will be able to find the C: drive using the “File Explorer” from the start menu.

Next, we will now run a Docker command that will:

  • Setup a new instance called Test_R (you can change this name to whatever your project is).
  • Setup a password for Rstudio - this is “secret”, but can be anything you like.
  • Automatically downloads an image - in this case we will use rocker/geospatial.

open PowerShell again from the start menu and run the following command:

docker run -e ROOT=true --name Test_R -v C:\r_projects:/home/ -e PASSWORD=secret -p 8787:8787 rocker/geospatial

You might get a notification about Windows Defender Firewall - you will need to agree to this for Docker to run.

Run Rstudio

In the Docker Desktop Application, under “Containers” you will now see that your “Test_R” container is running. This also lists the image that it is using as “rocker/geospatial”. You can have multiple containers running from the same image; so you might want to create one for each new project; although you typically only run one at a time. On the far right hand side you will also see a stop button which can be used to stop the container running.

There is also a square button with an arrow on it; which can be used to open Rstudio. Alternatively, you can type the following URL in a web browser: http://localhost:8787/.

This will open the Rstudio interface and ask for a username and password. These will be “rstudio” and whatever password you specified. As written above this was “secret”. You will then have a full Rstudio instance running in the web browser!