Skip to content

Instantly share code, notes, and snippets.

@selfawaresoup
Last active November 2, 2023 07:52
Show Gist options
  • Save selfawaresoup/0b5ca73d9a296d88c961f4ecf956e708 to your computer and use it in GitHub Desktop.
Save selfawaresoup/0b5ca73d9a296d88c961f4ecf956e708 to your computer and use it in GitHub Desktop.
Rstudio server setup on ubuntu

How to set up an Rstudio server on Ubuntu

This procedure was tested on Ubuntu 23.04 but should be very similar or identical on other verions.

Note that this will likely not install the latest version of R but the one that is included in the Ubuntu package repository. Installing a newer version can be tricky since the R project doesn't always support all recent releases of major Linux distributions. For most use cases, especially for education, the slightly older version of R will be prefectly fine though.

All commands in this guide need to be run as user root or with sudo.

1 Prepare the machine

1.1 Install updates

apt update
apt upgrade

1.2 Create admin user with sudo rights

This user should be used for admin work, not in RStudio

useradd -m admin
usermod -a -G sudo admin
passwd admin # set strong password!

1.3 Disable root login and require public keys for SSH

Make sure at least one sudo capable account exists before this step! Otherwise you might lock yourself out.

in /etc/ssh/sshd_config set the following values:

PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no

1.4 Set firewall rules

ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow http
ufw allow https
ufw show added # double check the changes before applying
ufw enable

2 Install R

apt install r-base r-base-dev

2.1 Install dependecies for common R packages

apt install libxml2-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev libcurl4-openssl-dev libssl-dev

2.2 Install common R packages

R
install.packages("tidyverse")

2.3 Install RStudio Server

Check https://posit.co/download/rstudio-server/ for current builds of rstudio-server. There may be unavailable dependencies, but the nightly builds might fix that: https://dailies.rstudio.com

apt install gdebi-core
wget https://s3.amazonaws.com/rstudio-ide-build/server/jammy/amd64/rstudio-server-2023.12.0-daily-161-amd64.deb
gdebi rstudio-server-2023.12.0-daily-161-amd64.deb

Check server status:

systemctl status rstudio-server

3 Set up webserver

(replace rstudio.example.org with your domain)

3.1 Install NGINX

apt install nginx

3.2 Certbot and SSL certificates

apt install certbot python3-certbot-nginx
certbot certonly --nginx -d rstudio.example.org

3.3 Configure NGINX reverse proxy and SSL termination

Write /etc/nginx/sites-available/rstudio:

server {
   listen 80;
   server_name rstudio.example.org;
   return 301 https://rstudio.example.org$request_uri;
 }

server {
   listen 443 ssl;
   server_name rstudio.example.org;
   ssl_certificate  /etc/letsencrypt/live/rstudio.example.org/fullchain.pem;
   ssl_certificate_key  /etc/letsencrypt/live/rstudio.example.org/privkey.pem; 
   ssl_prefer_server_ciphers on;

   location / {
        proxy_pass http://localhost:8787;

        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
   }
}

Test NGINX config:

nginx -t

Restart NGINX:

systemctl restart NGINX

Open https://rstudio.example.org in your browser and login with a regular linux user account.

RStudio server is now ready to use. User's files (e.g. R PRojects, etc) are kept in their home directories.

4 Adding more users

useradd -m [name]
passwd [name]
@robingarcia
Copy link

Thank you very much for this tutorial.
I did it in a similar way.
I am wondering, whether 2FA is possible for login (e.g. TOTP)?

@selfawaresoup
Copy link
Author

Haha, thanks. It's not really a tutorial but more like my personal notes so I don't forget things next time :D

I don't think RStudio server has any user management stuff built-in since it just uses the OS' user accounts. Another issue is how to create those accounts in the first place, for example in a university setting where students probably have an email account from the institution but that doesn't translate into a system account on the RStudio server

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