RStudio Server on GCP: the right way
This quick tutorial details how to install a RStudio Server on a Google Cloud Virtual Machine (VM), and how to easily enable remote access in a secure manner through SSH port forwarding.
The right way, you say? — Having recently been looking for instructions on how to do it myself, I was surprised to see that the great majority of online tutorials uses a rather lousy method consisting of creating a firewall rule and opening the TCP port 8787 on the VM. See for example here, there, and so forth… The firewall method creates unnecessary vulnerabilities by exposing the system to MITM (man-in-the-middle) attacks. Moreover, this unsecured HTTP connection is prone to ear’s dropping as communications between the client and server are all in clear, including login credentials... Worse, those tutorials usually don’t restrict the allowed range of incoming IP’s (0.0.0.0/0).
While securing the connection with a HTTPS protocol is possible to mitigate most of the vulnerabilities, it requires the acquisition of a SSL certificate from a certificat authority, registering a domain name, etc. (see here). This longer and more complex method appears well explained in Seth Russel’s post, I might try it in the future but at the moment I want a quick and easy way.
Fortunately, there is a secure and straightforward way to enable remote access to the RStudio Server: SSH port forwarding. Let’s dive in.
NB: It is assumed that you already have a VM machine set up on GCP.
1. Connect to your Google Cloud VM and update the system package index. Optionally, upgrade all installed packages to their latest version.
sudo apt-get update
sudo apt-get -y upgrade
2. Install R and get the version to sanity-check the installation
sudo apt -y install r-base r-base-dev
3. Select the RStudio Server installation that fits your operating system. To download it directly to the Google Cloud VM, we use wget. The following commands are for Debian 10 (“buster”) since this is what I run. Note that the installation of Debian packages (.deb) requires gdebi-core, which is also installed here. Finally, a couple of supporting packages are also installed.
sudo apt-get install gdebi-core wget
sudo gdebi rstudio-server-1.3.1093-amd64.deb
sudo apt-get install libcurl4-openssl-dev libssl-dev libxml2-dev
4. Connect remotely to the RStudio Server with SSH port forwarding. Following the GCP documentation, SSH port forwarding can be accomplished as follow:
gcloud compute ssh --zone <zone> <VMname> --project <projectName> -- -L 1080:localhost:8787
Naturally, replace <zone>, <VMname>, and <projectName> with the adequate values for your VM. The supplementary arguments provided to the SSH command (after the double dash), instruct to forward the port 1080 on your local machine to the port 8787 on your VM, where RStudio Server is listening by default. This SSH connection must be up for connecting to the RStudio Server, now available via your internet browser at localhost:1080.
> For ease of use, attributing this last command as an alias might be a good idea.
> If you happen to lose the SSH connection, do not despair. The RStudio Server running on the VM machine has not been impacted. Just re-run the SSH command and resume your work.
> Handling of the RStudio Server on the GCP VM can be done with:
sudo rstudio-server start
sudo rstudio-server stop
sudo rstudio-server status