Installing RStudio via Docker in Ubuntu 22.04 using the Windows Subsystem for Linux (WSL) on Windows 11

Introduction

This tutorial describes how to install Rstudio Server on a Windows 11 host system by installing it as a Docker image in Ubuntu 22.04 LTS as virtual machine as a guest system using the Windows Subsystem for Linux (WSL2). This way the complete functionality of the Linux operating system will be available for Rstudio Server in the web browser (eg, MS Edge)  under Windows 11 (see [7] for using WSL in an enterprise environment).

Activate Virtualization Technology

  • Activate Virtualization Technology (VTx, VTd) in the BIOS which is required for Docker under Windows 11.
  • In the following a tutorial for a HP Workstation Z4G4 (HP Computer Setup, System BIOS, version P61) is given [0].
  • Steps:
    • Start Computer
    • While booting, repeatedly press escape (or F7 or space depending on BIOS settings) till Startup (BIOS) menu is shown.
    • Navigate to settings below: Startup Menu > BIOS Setup > Advanced > System Security:
      • Tick (enable) “Virtualization Technology (VTx)”
      • Tick (enable) “Virtualization Technology for Directed I/O (VTd)”
  • You have to find out how to do it for your own computer and its BIOS.

Figure 1: Screenshot of BIOS after enabling Virtualization Technologies (VTx, VTd) to run Docker containers in the Windows Subsystem for Linux (WSL2)

Configure WSL

  •  Create file C:\.wslconfig with a text editor (eg, notepad) and enter the following configs after adapting them to your system. The configs below are for my workstation (Windows 11, 32GB, XEON  W2125 CPU with 4 cores) allocating 2GB of memory and 2 of 4 cores to WSL. If WSL is not configured, WSL will use the maximum amount of RAM (here: 20GB) which will stall the system and make downloads via docker extremely slow.
[wsl2]
memory=4GB #Limits VM memory in WSL 2 to 4GB
processors=2 #Makes the WSL 2 VM use two virtual processors

Install Linux using WSL

  • Start CMD with administrative privileges.
  • WinCMD> Execute “wsl –install” command to install Ubuntu 22.04 LTS as default
  • WinCMD> Enter “wsl” to start Ubuntu 22.04 LTS
  • LinBASH> nano .bashrc and add the following and after existing configs. This will change to your home directory in the linux filesystem after you start Linux. Otherwise the working directory will default to a mounted NTFS drive (ie, /mnt/c/users/username/) in the Windows host system, which will slow down the docker downloads:
cd ~
  • LinBASH> nano ~/.docker/config.json and modify to. The default “desktop.exe” will make your WSL system very slow.
{
"credsStore": ""
}
  • Shutdown WSL and restart for changes to become effective:
WinCMD> wsl --shutdown

WinCMD> wsl

Update Linux

  • Update Linux [3]:
LinBASH> sudo apt-get update

LinBASH> sudo apt-get upgrade

Install Docker

  • Add Docker’s official GPG key [3]:
LinBASH> sudo apt-get install ca-certificates curl
LinBASH> sudo install -m 0755 -d /etc/apt/keyrings
LinBASH> sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
LinBASH> sudo chmod a+r /etc/apt/keyrings/docker.asc
  • Add the repository to Apt sources [3]:
LinBASH> echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
LinBASH> sudo apt-get update
LinBASH> sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • Run test [3]:
LinBASH> sudo docker run hello-world

Install rocker/r-base image

  • Install rocker/r-base image [4]:
LinBASH> sudo docker run --rm -ti r-base

Install rocker/rstudio image

  •  Install rocker/rstudio image [4]:
LinBASH> sudo docker run --rm -ti -e PASSWORD=yourpassword -p 8787:8787 rocker/rstudio

Use Rstudio server on Windows 11

  • User Rstudio server [4]:
    • Start browser on Windows 11
    • Enter “http://localhost:8787”
    • Login to Rstudio with username “rstudio” and “yourpassword”

Note: The rocker/rstudio container on port 8787 may conflict with an existing RStudio Server installation on Windows 11. In this case, it this recommended to change the default port of the RStudio Server installation on Windows 11, for example, to port 8788 and access Rstudio Server accordingly in the browser at http://localhost:8788 (see details, [6, page  22, 4.1  Network port and address], eg configure ./rstudio/rserver.conf with “www-port=8788”). You have to restart the RStudio Server again for changes to become effective.

Exporting and importing WSL Images

The created Linux image can be exported and imported to Windows Subsystem for Linux (WSL) between machines and on the same machine [5].

References

[0] https://support.hp.com/us-en/document/ish_5637142-5637191-16

[1] https://techcommunity.microsoft.com/t5/windows-11/how-to-install-the-linux-windows-subsystem-in-windows-11/m-p/2701207

[2] https://stackoverflow.com/questions/62154016/docker-on-wsl2-very-slof

[3] https://docs.docker.com/engine/install/ubuntu/

[4] https://rocker-project.org/

[5] https://4sysops.com/archives/export-and-import-windows-subsystem-for-linux-wsl/

[6] https://s3.amazonaws.com/rstudio-server/rstudio-server-pro-0.98.507-admin-guide.pdf

[7] https://medium.com/codex/using-wsl-2-in-enterprises-d9cef1f60c73