Skip to content

Instantly share code, notes, and snippets.

@mojaveazure
Created May 15, 2020 21:53
Show Gist options
  • Save mojaveazure/1b7927155361a4deed2a537e8015231a to your computer and use it in GitHub Desktop.
Save mojaveazure/1b7927155361a4deed2a537e8015231a to your computer and use it in GitHub Desktop.

Installing RStudio Server on the Windows Subsystem for Linux

Setting up RStudio Server on the Windows Subsystem for Linux (WSL) is an excellent way for R developers to use and interact with R on Linux while running Windows. While not perfect, it provides a nearly seamless way for Windows users to consume Linux-only packages, and for developers to test on both Windows and Linux at the same time. Moreover, the same benefits of running RStudio server, such as leaving jobs running in the background, apply to RStudio Server on WSL. These are my notes for successfully setting up and using RStudio Server on WSL

Setting up WSL

The first thing that needs to happen in enabling the WSL and installing a Linux distro. To install the WSL, open PowerShell as an administrator and run

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

Then, restart your machine. Now, we can install Linux from the Microsoft Store. I've used the following distros to run WSL for RStudio Server

Finally, launch the distribution that was just installed from the Start Menu and follow any prompts for creating a WLS user and password. You may need to update the packages on your WSL install, which can be done using the distribution's package manager. For Ubuntu and Debian, it's a simple as

sudo apt update && sudo apt upgrade -y

Note: you cannot run WSL on Windows 10 in S Mode. To switch out of S Mode, please see Microsoft's FAQ

Installing R and RStudio Server

The easiest way to install R is by using the one of the binaries provided by CRAN through your distribution's package manager. For Ubuntu or Debian, this involves adding CRAN's PPA to APT's sources; please see CRAN's notes for Debian and Ubuntu for exact details on adding the PPA.

After adding the PPA on Debian or Ubuntu, you need to refresh your package manger with

sudo apt update

R can be installed with

sudo apt install -y r-base-core r-base-dev

The r-base-dev package needs to be installed to allow R to compile packages from source.

RStudio Server can be downloaded following RStudio's instructions. For Debian and Ubuntu, note that while RStudio recommends using gdebi, you can get around this with the following

sudo dpkg -i $path_to_rstudio_deb_file
sudo apt install -f

Be sure to download the correct version of RStudio Server. At the time of writing, the Debian WSL installation is Debian 10 Buster. Ubuntu 16.04 LTS Xenial, 18.04 LTS Bionic, and 20.04 LTS Focal are all available for WSL, and (often) use different versions of the same package.

Configuring R

R on WSL works mostly out-of-the-box. There is an issue with installing packages that results in the error

mv: cannot move '/library/00LOCK/pkg' to
'/library/pkg': Permission denied
ERROR:    moving to final location failed

This happens due to the limitations of WSL (I did say "nearly seamless", right?). Thankfully, this is easy to get around; simply add

Sys.setenv(R_INSTALL_STAGED = FALSE)

to your ~/.Rprofile and packages should install successfully

Launching and Configuring RStudio Server

RStudio Server works out-of-the-box on WSL; simply open your distribution from the Start Menu and run

sudo rstudio-server start && sudo rstudio-server online

Then, open a web browser and navigate to localhost:8787. Log in with your WSL username and password, and you should be good to go.

If you want to use RStudio Server on multiple WSL distributions, then you'll need to set RStudio Server's TCP port; on Debian and Ubuntu, open /etc/rstudio/rserver.conf with your favorite text editor as root (sudo) and add the following line:

www-port=8989

Set the 8989 portion to any open TCP port. See this for a list of commonly-used and reserved TCP and UDP ports. Finally restart RStudio server with

sudo rstudio-server restart

and navigate to localhost:NEW_PORT_NUMBER for access

Using RMarkdown

RMarkdown requires pandoc, and often utilizes TeX, depending on the output format. Pandoc can easily be installed either by downloading a binary or using your distribution's package manager. On Debian and Ubuntu, this is as simple as

sudo apt install -y pandoc

The standard TeX installation used on Linux is Tex Live. However, TeX Live is quite big and bloated, especially for RMarkdown's needs. Instead, I recommend using the tinytex R package from Yihui Xie. Installing Tex through tinytex is done in R:

install.packages("tinytex")
tinytex::install_tinytex()

To get pandoc/Knitr to see the tinytex installation, the tinytex bin directory needs to be in your PATH. To get R to automatically see this, I add the following to my ~/.Rprofile

if (requireNamespace("tinytex", quietly = TRUE)) {
  Sys.setenv(PATH = paste(
    paste(
      list.dirs(path = file.path(tinytex::tinytex_root(), 'bin')),
      collapse = .Platform$path.sep
    ),
    Sys.getenv("PATH"),
    sep = .Platform$path.sep
  ))
  unloadNamespace(ns = "tinytex")
}

Once added, simply restart your R session and you should have a working, small version of TeX available for RMarkdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment