Skip to content

Instantly share code, notes, and snippets.

@ShenZhouHong
Created April 11, 2023 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShenZhouHong/2191dd62d7f18629b2912f7e5d2ba97b to your computer and use it in GitHub Desktop.
Save ShenZhouHong/2191dd62d7f18629b2912f7e5d2ba97b to your computer and use it in GitHub Desktop.
Bash script to install R, and R package dependencies on Ubuntu 22.04.
#!/usr/bin/env bash
#
# This bash script contains the minimal proceedure required to install a base R
# environment on Ubuntu 22.04. It is used as a part of the Vagrantfile
# provisioning process for a brand-new, Ubuntu 22.04 virtual machine. This
# script may also be ran as a part of a standalone process to download R on any
# Ubuntu or Debian operating system.
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# Before installation, begin with some basic sanity checks
if [ "$EUID" -ne 0 ]
then
printf '%s\n' "This script must be ran as root. Try again using sudo."
exit 1
fi
if ! command -v apt-get &> /dev/null
then
printf '%s\n' "The command apt-get is not found. This OS may not be supported"
exit 1
fi
printf '%s\n' "Beginning R-Installation Script. This may take a while."
apt-get -qq update
# Download the R Repository Package Key, Add the R apt repository
apt-get -qq install --no-install-recommends software-properties-common dirmngr
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
add-apt-repository --yes "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# Add the CRAN R Packages Repository
add-apt-repository --yes ppa:c2d4u.team/c2d4u4.0+
apt-get -qq update
apt-get -qq --allow-unauthenticated install --no-install-recommends r-base
# Install systems-level dependencies for the R devtools package
printf '%s\n' "Installing systems-level dependencies for R devtools."
apt-get -qq install build-essential libcurl4-openssl-dev libfontconfig1-dev libfreetype6-dev libfribidi-dev libharfbuzz-dev libjpeg-dev libpng-dev libssl-dev libtiff5-dev libxml2-dev
if ! command -v Rscript &> /dev/null
then
printf '%s\n' "The command Rscript is not found. This R installation may have failed."
exit 1
fi
printf '%s\n' "Using Rscript to install R devtools. This may take a while."
Rscript -e "install.packages('devtools', dependencies=TRUE)"
# Install system-level depndencies for AnalyticSystem R packages
printf '%s\n' "Installing systems-level dependencies for AnalyticSystem R packages."
apt-get -qq install libsodium-dev gfortran libglpk-dev
printf '%s\n' "Using Rscript to install R packages for AnalyticSystem. This may take a while."
Rscript -e "install.packages(c('tidyverse', 'httr', 'feather', 'googlesheets4', 'igraph', 'writexl', 'readr'), dependencies=TRUE)"
printf '%s\n' "The R installation script has been completed successfully."
printf '%s\n' "If this script was invoked as a part of a Vagrant VM, you"
printf '%s\n' "may now SSH into the VM via: `vagrant ssh`."
# Bash Commands Reference:
# apt-get: preferred for non-interactive/scripting over apt.
# apt-get -qq: suppress all output except errors
# apt-get --allow-unauthenticated: allow installation from user-added repositories
# printf: a better, POSIX-compliant version of echo
# Rscript -e: Runs R expression directly from bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment