Installing RStudio & Shiny Servers

I did a remote install of Ubuntu Server today. This was somewhat novel because it’s the first time that I have not had physical access to the machine I was installing on. The server install went very smoothly indeed.

The next tasks were to install RStudio Server and Shiny Server. The installation process for each of these is well documented on the RStudio web site:

These are my notes. Essentially the same, with some small variations.

Base R

sudo apt update
sudo apt install r-base-core

RStudio Server

  1. Install a recent version of R. This might seem self-evident, but if you don’t have R installed the RStudio Server won’t even start.
  1. Download the distribution. 🚨 This assumes that you are running Ubuntu 20.04. I’ve had problems installing on Ubuntu 22.04 due to dependency issues.

    wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-2022.02.3-492-amd64.deb
    

    If you want to go with an older version of RStudio Server, you can try:

    • 2021.09.1-372
    • 1.4.1106
    • 1.4.1103 or
    • 1.3.1093.

    I’ve had trouble connecting to newer versions via an SSH tunnel, but 1.3.1093 works fine.

  1. Install the server.

    sudo apt-get install -y ./rstudio-server-2022.02.3-492-amd64.deb
    
  2. Verify the installation.

    sudo rstudio-server verify-installation
    
  3. RStudio Server runs on port 8787, so you should be able to access it in a browser at http://<server-ip>:8787.

  4. If RStudio Server is running on a remote machine then you should really secure communication with SSL. If you’re setting up an instance which is going to be running for some time then it will be worth your while to get an SSL certificate and use it to communicate with RStudio Server via HTTPS. If, however, you just want something that works and is secure then perhaps setting up an SSH tunnel to the remote machine is a better option.

    ssh -L 8787:127.0.0.1:8787 -N ubuntu@18.193.162.19
    

Whether or not to Start at Boot

RStudio Server does not consume an awful lot of RAM. But on a small machine, every bit of memory can be precious. Perhaps you don’t want to have RStudio Server running all the time? No problem!

# Check whether RStudio Server is running.
systemctl is-active rstudio-server

# Disable RStudio Server at boot.
sudo systemctl disable rstudio-server

# Enable RStudio Server at boot.
sudo systemctl enable rstudio-server

You can then start and stop RStudio Server as and when required.

# Start RStudio Server.
sudo systemctl start rstudio-server

# Stop RStudio Server.
sudo systemctl stop rstudio-server

# Stop and then start RStudio Server.
sudo systemctl restart rstudio-server

Configuration Options

Configuration settings are stored in /etc/rstudio/rserver.conf.

One of the most common configuration changes that I make is to change the port on which RStudio Server is running. This can be done by adding the following line to the above configuration file:

# Port for service.
www-port=80

Another thing you might want to tweak is the auth-minimum-user-id option, which determines the minimum UID which is allowed to login to RStudio Server. If you are having trouble with the shiny user logging in, then this is most likely to be the problem.

# Minimum user ID for login.
auth-minimum-user-id=950

After making any changes to the configuration you need to restart the server.

Find out more about configuring and managing the server.

Default Profile

You may want to edit the global profile in /etc/R/Rprofile.site.

One setting that I like to apply here is:

.libPaths(c(.libPaths(), "/usr/local/lib/R/site-library"))

Shiny Server

  1. Become root and install the shiny package.

    sudo su
    R -e "install.packages('shiny', repos='https://cran.rstudio.com/')"
    exit
    
  2. Download the distribution.

    wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.16.958-amd64.deb
    
  3. Install the server.

    sudo dpkg -i shiny-server-1.5.16.958-amd64.deb
    
  4. Shiny Server runs on port 3838, so you should be able to access it in a browser at http://<server-ip>:3838.

Whether or not to Start at Boot

You can also disable Shiny Server being automatically started at boot.

# Disable Shiny Server at boot.
sudo systemctl disable shiny-server

You can then start and stop Shiny Server as and when required.

# Start Shiny Server.
sudo systemctl start shiny-server

# Stop Shiny Server.
sudo systemctl stop shiny-server

# Stop and then start Shiny Server.
sudo systemctl restart shiny-server

Shiny Configuration

You can edit the configuration file, /etc/shiny-server/shiny-server.conf, to tweak the settings of the Shiny server.

run_as shiny;                          # User used to run apps.

http_keepalive_timeout 120;            # How long to wait for response
app_init_timeout 600;                  # How long to allow for startup (default: 60).
app_idle_timeout 10;                   # How long before kill idle R process (default: 5).

sockjs_heartbeat_delay 60;             # Interval between SockJS server heartbeats (default: 25).

preserve_logs true;
  
server {
  listen 3838;                         # Which port to listen on.

  location / {
    site_dir /srv/shiny-server;        # Folder for installing apps.
    log_dir /var/log/shiny-server;     # Folder for logs.
    directory_index on;                # What to do when user visits the base URL.
  }
}

Installing Apps

You should install apps into folders below /srv/shiny-server. Also ensure that all of the files in the app (source) are owned by shiny.shiny (if you don’t then the logs will simply disappear!).