Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A very basic, no-frills, setup of RStudio on WSL2.

VERY Basic WSL2 RStudio Install

Begin with a plain Ubuntu distro from the Windows App Store. If one is already installed and you want to start from scratch simply:

wsl -t Ubuntu
wsl --unregister Ubuntu
Ubuntu

Then enter the user name and password and exit again.

Download the basic-wsl2-rstudio.sh script to your Windows system and execute

wsl -d Ubuntu -u root -- ./basic-wsl2-rstudio.sh

Wait a few minutes (just under 5 on my system) and then

Ubuntu
rstudio &

If everything worked out you should be able to install binary packages from RStudio without issue. If you create a new markdown document then RStudio will prompt to download what you need.

Have fun!

#/bin/bash
cd ~
locale-gen en_US.UTF-8
/usr/sbin/update-locale LANG=en_US.UTF-8
MKRUNUSERDIR_PATH="/usr/sbin/wsl-user-mk-runuserdir"
cat > ${MKRUNUSERDIR_PATH} << \EOF
### User runtime dir
# Executed as part of 00-wsl-user-env.sh
RUNUSER_DIR="/run/user"
UID=$(id -u ${SUDO_USER})
sudo mkdir -m 0700 -p ${RUNUSER_DIR}/${UID}
sudo chown ${UID}:${UID} ${RUNUSER_DIR}/${UID}
EOF
chmod +x ${MKRUNUSERDIR_PATH}
echo "%sudo ALL = NOPASSWD: ${MKRUNUSERDIR_PATH}" >\
"/etc/sudoers.d/$(basename wsl-user-mk-runuserdir)"
cat > /etc/profile.d/00-wsl-user-env.sh << \EOF
### Global GUI app support ENV.
# Adjust SCALE_FACTOR as needed.
set -a
DISPLAY="$(ip r l default | cut -d\ -f3):0.0"
XDG_RUNTIME_DIR=/run/user/${UID}
GDK_SCALE="1.2"
NO_AT_BRIDGE="1"
QT_SCALE_FACTOR="1.2"
QT_X11_NO_MITSHM="1"
set +a
sudo /usr/sbin/wsl-user-mk-runuserdir
EOF
### R install
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
apt-add-repository "deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/"
apt update
apt -y upgrade
packages=(
# With recommends
gdebi
libcurl4-gnutls-dev
libnss3
libssl-dev
libxml2-dev
littler
r-base
)
apt -y install ${packages[@]}
### Rstudio install and binary package repo setup.
URL=https://rstudio.org/download/latest/preview/desktop/bionic/rstudio-latest-amd64.deb
wget -O rstudio.deb ${URL}
gdebi -n rstudio.deb
rm -f rstudio.deb
RPROFILE_SITE=/etc/R/Rprofile.site
cat >> ${RPROFILE_SITE} << \EOF
# Use RStudio Package Manager Binaries, try nexus proxy repository first.
local({
r <- getOption("repos")
r["CRAN"] <- "https://packagemanager.rstudio.com/all/__linux__/focal/latest"
options(repos = r)
options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version$platform, R.version$arch, R.version$os)))
})
EOF
### Git Config
# Use Window's git user settings.
CRED_DIR="/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"
GIT_USER=$(git.exe config user.name)
GIT_USER_EMAIL=$(git.exe config user.email)
git config --system credential.helper "${CRED_DIR}"
git config --system user.name "${GIT_USER,,}"
git config --system user.email "${GIT_USER_EMAIL,,}"
@rdinnager

This comment has been minimized.

Copy link

@rdinnager rdinnager commented Aug 21, 2020

Hi, just wanted to say thanks for this. I was able to get Rstudio up and running on my WSL2 with this, with a few extra steps. I thought I would post them here in case anyone else is having troubles.

  1. The first thing I had to do was change the $DISPLAY environmental variable, because ip r l default returned the wrong ip (it returned my ethernet ip, whereas I needed my wifi ip). I had to find this manually in Windows with ipconfig, I think because I am using a USB wifi adaptor which WSL is not picking up.

  2. After fixing 1, I ended up with a window with a black screen when I ran rstudio. This turned out to be an issue with opengl. This was fixed by changing rstudio's settings to use 'software' rendering, instead of opengl. This is accomplished by opening ~/.config/RStudio/desktop.ini and adding a line like this:

[General]
desktop.renderingEngine=software

This solution is discussed in this rstudio issue: rstudio/rstudio#3185

I should also mention that I am using VcXsrv as my X server.

After I did this, everything is working great now. Thanks so much for the gist Thell!

@Thell

This comment has been minimized.

Copy link
Owner Author

@Thell Thell commented Aug 22, 2020

Glad to hear it was of use! Thanks for leaving that information too, I'm sure it will come in useful to others as well.

I've been using a variety of settings in my build testing too...

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