Skip to content

Instantly share code, notes, and snippets.

@IdoBar
Last active June 27, 2022 03:21
Show Gist options
  • Save IdoBar/3d5979cc5ac2bc903ac0d957fce21335 to your computer and use it in GitHub Desktop.
Save IdoBar/3d5979cc5ac2bc903ac0d957fce21335 to your computer and use it in GitHub Desktop.
A script to install R, Rstudio and Shiny-server on an Ubuntu server
#! /bin/bash
# Ask user for sudo password (to be used when needed)
read -s -p "Enter Password for sudo: " sudoPW
# update CRAN repository below if needed
REPO="'https://cran.rstudio.com/'"
# Based on the instructions on Dean Attali's website:
# https://deanattali.com/2015/05/09/setup-rstudio-shiny-server-digital-ocean/#user-libraries
# add CRAN repository to the repo list (this case for xenial)
UB_VER=$( . /etc/os-release && echo ${VERSION_CODENAME} )
echo $sudoPW | sudo -S sh -c "echo \"deb http://cran.rstudio.com/bin/linux/ubuntu $UB_VER/\" >> /etc/apt/sources.list"
echo $sudoPW | sudo -S sh -c "echo \"deb http://cran.rstudio.com/bin/linux/ubuntu ${UB_VER}-backports main restricted universe\" >> /etc/apt/sources.list"
# add public keys
gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
echo $sudoPW | sudo -S apt-key add `gpg -a --export E084DAB9`
# install nginx, R and Ubuntu dependencies
echo $sudoPW | sudo -S apt-get update
echo $sudoPW | sudo -S apt-get -y install nginx r-base-dev libcurl4-gnutls-dev libxml2-dev libssl-dev gdebi-core \
libcairo2-dev libxt-dev # these two dependencies needed for Cairo
# set default CRAN REPO
echo $sudoPW | sudo -S su - -c "echo \"options(repos=structure(c(CRAN=$REPO)))\" >> /usr/lib/R/Rprofile.site"
# add package library location
echo $sudoPW | sudo -S su - -c "echo \".libPaths(c(.libPaths(), '/usr/local/lib/R/site-library'))\" >> /usr/lib/R/Rprofile.site"
# install shiny package
echo $sudoPW | sudo -S su - -c "R -e \"install.packages('shiny')\""
# install RStudio server and shiny-server, parse recent versions from links below
# https://www.rstudio.com/products/rstudio/download-server/
RSTUDIO_DOWNLOAD=`curl https://www.rstudio.com/products/rstudio/download-server/ | grep -Eo "wget .+?rstudio-server-[0-9\.]+-amd64.deb"`
RSTUDIO_FILE=`echo $RSTUDIO_DOWNLOAD | awk -F[/] '{print $NF}'`
echo $RSTUDIO_DOWNLOAD | bash
echo $sudoPW | sudo -S gdebi -n $RSTUDIO_FILE
# https://www.rstudio.com/products/shiny/download-server/
SHINY_DOWNLOAD=`curl https://www.rstudio.com/products/shiny/download-server/ | grep -Eo "wget .+?shiny-server-[0-9\.]+-amd64.deb"`
SHINY_FILE=`echo $SHINY_DOWNLOAD | awk -F[/] '{print $NF}'`
echo $SHINY_DOWNLOAD | bash
echo $sudoPW | sudo -S gdebi -n $SHINY_FILE
# Setup user permissions
echo $sudoPW | sudo -S groupadd shiny-apps
echo $sudoPW | sudo -S usermod -aG shiny-apps $USER
echo $sudoPW | sudo -S usermod -aG shiny-apps shiny
cd /srv/shiny-server
echo $sudoPW | sudo -S chown -R $USER:shiny-apps .
echo $sudoPW | sudo -S chmod g+w .
echo $sudoPW | sudo -S chmod g+s .
# Install additional R packages
echo $sudoPW | sudo -S su - -c "R -e \"install.packages('pacman')\""
# echo $sudoPW | sudo -S su - -c "R -e \"source('https://bioconductor.org/biocLite.R'); biocLite()\"" # uncomment this one and remove "BiocManager" from next line if using R<3.5
echo $sudoPW | sudo -S su - -c "R -e \"pacman::p_install(c('devtools', 'BiocManager', 'tidyverse', 'shinydashboard', 'shinyWidgets', \
'R.utils', 'DT', 'sqldf', 'dbplyr', 'RSQLite','doFuture', 'MonetDBlite','Cairo', 'RColorBrewer'), character.only=TRUE)\""
echo $sudoPW | sudo -S su - -c "R -e \"BiocManager::install(c('Biostrings'))\""
echo "R, Rstudio-server and shiny-server installed!"
echo "Please follow nginx and shiny-server configuration instructions at https://deanattali.com/2015/05/09/setup-rstudio-shiny-server-digital-ocean/#user-libraries"
echo "To add user authentication, please read https://www.r-bloggers.com/add-authentication-to-shiny-server-with-nginx/"
@IdoBar
Copy link
Author

IdoBar commented Jun 19, 2022

@mpr1255 I haven't tried it lately, why don't you have a go and let me know if you had any issues?

@mpr1255
Copy link

mpr1255 commented Jun 27, 2022

@mpr1255 I haven't tried it lately, why don't you have a go and let me know if you had any issues?

It works! I'm going to supplement it with some of the stuff from Dean's write-up and a bunch of the packages I need, plus a bunch of the user switches... Very cool. Thanks a lot for this!!

@IdoBar
Copy link
Author

IdoBar commented Jun 27, 2022

@mpr1255 I haven't tried it lately, why don't you have a go and let me know if you had any issues?

It works! I'm going to supplement it with some of the stuff from Dean's write-up and a bunch of the packages I need, plus a bunch of the user switches... Very cool. Thanks a lot for this!!

I'm happy to hear that, share the word if it helped you...
Cheers, Ido

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