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.

  2. Download the distribution. This asumes that you are running Ubuntu 20.04.

    # Ubuntu 16
    wget https://download2.rstudio.org/server/xenial/amd64/rstudio-server-1.4.1106-amd64.deb
    # Ubuntu 18 and 20
    wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.4.1106-amd64.deb
    

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

    • 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.

  3. Install the server.

    sudo dpkg -i rstudio-server-1.4.1106-amd64.deb
    

    If this generates an error (probably a dependency problem with libclang-dev) you can run the following.

    sudo apt-get --fix-broken install
    

    That should fix the dependency issue and also complete the RStudio Server install.

  4. Verify the installation.

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

  6. 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. So 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).

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

  preserve_logs true;
  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.
  }
}

:::note The timeout settings can be applied at the top level but can also be included in the server or location sections. :::

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!).