Computational Social Science Post

Tips on R, Netlogo, and Python

RStudio Server, Linux(Ubuntu), WSL(Windows Subsystem for Linux), and Docker

 Posted on Aug.15, 2021

If I want to use RStudio Server, I need Linux as OS. Windows users like me need to set up WSL(Windows Subsystem for Linux) for RStudio Server. First of all, make sure if your Windows is the latest version.

  1. Setup WSL
Find Turn Windows features on or off in the Search of Windows.

Check Virtual Machine Platform and Windows Subsystem for Linux. Then Restart your device.

  1. Change the WSL version to 2
 # In the Windows PowerShell as Administrator

wsl --set-default-version 2 

# See https://aka.ms/wsl2kernel if you have an issue 
  1. Install Ubuntu
Find Run Linux on Windows in the Microsoft Store. Click and download Ubuntu

  1. Setup Ubuntu account
Run Ubuntu you already installed, and type username and password which you desire. These username and password will be used in the RStudio Server.

  1. Update Ubuntu
 # In the Ubuntu

sudo apt update
sudo apt upgrade -y

# You cannot use ctrl+V in the Ubuntu. Instead Use the right click 
  1. Install the latest R and RStudio Server
 # See https://cran.r-project.org/bin/linux/ubuntu/   
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/' 

sudo apt update

sudo apt install -y r-base r-base-core r-recommended r-base-dev gdebi-core build-essential libcurl4-gnutls-dev libxml2-dev libssl-dev 


## Install RStudio Server ##

wget https://rstudio.org/download/latest/stable/server/bionic/rstudio-server-latest-amd64.deb

sudo gdebi rstudio-server-latest-amd64.deb
  1. Run RStudio Server
 sudo rstudio-server start 
In the Web browser, http://localhost:8787, and type username and password you already created for the Ubuntu account.

  1. Tip and Close RStudio Server
 # Some packages may cause an install issue. You may need an dependency. 
# https://jeroen.cran.dev/V8/
sudo apt-get install -y libv8-dev
# Then install R package V8

## To Close RStudio Server ##

sudo rstudio-server stop 


  1. Docker
Now you can use RStudio Server. But every time you need to open Ubuntu and type the code. When you close it, you also need to type the code in Ubuntu. Furthermore, when you need to run multiple machines using R, you may want something efficient. Docker can be an answer. Download Docker .

Run Docker. you can find Images tab in the left panel. Docker needs an image to run instead of files. We need Rstudio image.

Go to DockerHub. Then type rocker in the Search. You can find rocker/rstudio. Copy docker pull rocker/rstudio, then paste it in the Powershell.

You can find rocker/rstudio in the image tab of docker.

Click Run for it. In the option, choose Container names. For example, "rstudio-1". Then configure 4-digit Local Host like 1001.

Now you can see rstudio-1 in the Containers/Apps tab. In the web browser, type http://localhost:1001/. Both ID and Password are rstudio which was created by image creators.

  1. Update image for your project
After creating your R environment using the rocker's image, you need to make your own image.
 # In the Powershell, check docker containers 
docker ps -a

docker commit {Container ID} {New image name}
#ex# docker commit 40b8d16b7dba rstudio-2
Now you can find rstudio-2 in the image tab. This rstudio-2 has the same environment you already created. Just run this without typing the code in Ubuntu. If you make multiple containers, you can run multiple Rs at the same time.

  1. To Stop Vmmem
 # In the Powershell
wsl --shutdown 


  1. To setup xfce4
 # In the Ubuntu
cat /etc/resolv.conf
# It will show nameserver. 

nano ~/.bashrc
# It will open nano editor. In the nano, scroll down the bottom. Then,
export DISPLAY={the nameserver above}:0
#ex# export DISPLAY=100.00.000.1:0

# Save it with Ctrl+X. In Ubuntu
source ~/.bashrc

echo $DISPLAY

# If it show the nameserver, then type
startxfce4 
However, the namesever can change. It is tiresome to change it everytime.
#  In Nano, delete "export DISPLAY={the nameserver above}:0". 
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
GDK_SCALE=1
# Then Save it.