Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pinusm/521798bdad2eab8b0e7847bb106c75e3 to your computer and use it in GitHub Desktop.
Save pinusm/521798bdad2eab8b0e7847bb106c75e3 to your computer and use it in GitHub Desktop.
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,,}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment