Get Shiny Server on DigitalOcean

Get Shiny Server on DigitalOcean (or any Ubuntu Server)
TL;DR: Deploy R Shiny apps to the web by installing open-source Shiny Server on a Linux machine. Covers R installation, server config, and how to host multiple apps or per-user directories.
Why Host Your Own?
Services like shinyapps.io are great, but hosting your own Shiny Server offers:
- Cost savings: $5-10/month VPS vs. expensive pro tiers.
- Control: No limits on active hours or instance size.
- Privacy: Keep data on your own controlled infrastructure.
- Custom domains: Use
apps.yourlab.comeasily.
Part 1: Prerequisites
Start with a fresh Ubuntu 22.04 or 24.04 server (see Set up a remote server).
1. Install R
Use the official CRAN repository for the latest R version (Ubuntu default is often old).
# Update indices
sudo apt update -qq
# Install helper to add repo
sudo apt install -y --no-install-recommends software-properties-common dirmngr
# Add CRAN key and repo
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# Install R
sudo apt install -y r-base
2. Install Shiny Package
Install system dependencies first (for httpuv, openssl, etc.).
sudo apt install -y libcurl4-gnutls-dev libcairo2-dev libxt-dev libssl-dev
# Install Shiny package (as root, so it's available globally)
sudo su - -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""
Part 2: Install Shiny Server
Download the latest version from RStudio (Posit).
# Check latest version at: https://posit.co/download/shiny-server/
wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.22.1017-amd64.deb
# Install
sudo gdebi shiny-server-1.5.22.1017-amd64.deb
Verify it's running:
sudo systemctl status shiny-server
It should say active (running).
Open Firewall:
Shiny Server runs on port 3838 by default.
sudo ufw allow 3838
Visit http://your-server-ip:3838 in your browser. You should see the "Welcome to Shiny Server!" page.
Part 3: Deploying Apps
Location 1: System-wide Apps (/srv/shiny-server)
This is the default location.
Delete sample apps:
sudo rm -rf /srv/shiny-server/*Deploy your app:
Use git to pull your app code.cd /srv/shiny-server sudo git clone https://github.com/yourname/my-shiny-app.gitInstall App Dependencies:
If your app usesggplot2ordplyr, install them system-wide:sudo su - -c "R -e \"install.packages(c('ggplot2', 'dplyr'))\""
Your app is now at: http://your-server-ip:3838/my-shiny-app/
Location 2: Per-User Apps (~/ShinyApps)
Allow users to host apps in their home directories (e.g., http://server:3838/miana/app1).
Edit Config:
sudo nano /etc/shiny-server/shiny-server.confAdd
user_dirs:run_as :HOME_USER:; server { listen 3838; location / { user_dirs; # Enable ~/ShinyApps directory_index on; } }Create Directory:
mkdir ~/ShinyAppsRestart Server:
sudo systemctl restart shiny-server
Part 4: Production Best Practices
1. Run on Port 80/443 (Nginx Reverse Proxy)
Don't make users type :3838. Use Nginx to forward traffic.
sudo apt install nginx
Edit config: /etc/nginx/sites-available/shiny
server {
listen 80;
server_name apps.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3838;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Enable it:
sudo ln -s /etc/nginx/sites-available/shiny /etc/nginx/sites-enabled/
sudo systemctl restart nginx
2. Secure with HTTPS (Let's Encrypt)
Never run apps over HTTP.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d apps.yourdomain.com
3. Log Files
Troubleshooting? Check the logs:
- System logs:
/var/log/shiny-server.log - App logs:
/var/log/shiny-server/app-name-timestamp.log
Troubleshooting
"An error has occurred" (Grey Screen)
The generic error message hides details.
- Check the logs in
/var/log/shiny-server/. - Ensure all R packages used in
library()calls are installed system-wide (usingsudo R).
"Disconnected from server"
Cause: App timeout or WebSocket issue.
Fix: Increase timeout in shiny-server.conf:
app_idle_timeout 300; # 5 minutes
Permission Denied
Shiny Server runs as user shiny (by default). Ensure it can read your files:
sudo chown -R shiny:shiny /srv/shiny-server
See also:
Originally posted on WeChat Official Account: bioinfo_studyroom