jupyterhub_rstudio_install/install_v2.sh

98 lines
3.7 KiB
Bash

#!/bin/bash
# Script to install RStudio Server and then JupyterHub (via TLJH) on Ubuntu 24.04 LTS
# --- Configuration ---
# Set the desired admin username and password for JupyterHub
# !!! CHANGE THESE VALUES !!!
ADMIN_USER="your_admin_user"
ADMIN_PASSWORD="your_admin_password"
# RStudio Server .deb package URL for Ubuntu 22/24 (noble/jammy)
RSTUDIO_DEB_URL="https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.12.1-402-amd64.deb"
RSTUDIO_DEB_FILENAME="rstudio-server.deb"
# ---------------------
echo "--- Installing base prerequisites and updating system ---"
apt update
apt upgrade -y
# Install key dependencies including PAM components and gdebi
apt install -y curl git gdebi-core software-properties-common dirmngr ufw libpam-systemd libnss-systemd
# --- Configure Firewall (UFW) ---
echo "--- Configuring UFW firewall ---"
ufw allow 80/tcp # For JupyterHub/TLJH (HTTP)
ufw allow 443/tcp # For JupyterHub/TLJH (HTTPS)
ufw allow 8787/tcp # For RStudio Server
ufw --force enable
echo "Firewall configured: Ports 80, 443, and 8787 allowed."
---
## 1. Install R and RStudio Server First 📦
echo "--- Installing R and RStudio Server ---"
# 1a. Install R base and add the CRAN repository
echo "--- Adding CRAN repository and installing R ---"
# Add key for CRAN
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/cran_ubuntu_key.gpg >/dev/null
# Add CRAN repository for Ubuntu Noble (24.04)
add-apt-repository -y "deb https://cloud.r-project.org/bin/linux/ubuntu noble-cran40/"
apt update
apt install -y r-base r-base-dev
# 1b. Download and Install RStudio Server
echo "--- Downloading and installing RStudio Server ---"
wget "$RSTUDIO_DEB_URL" -O "$RSTUDIO_DEB_FILENAME"
gdebi --n "$RSTUDIO_DEB_FILENAME"
rm "$RSTUDIO_DEB_FILENAME"
# 1c. Set Explicit R Path (Crucial for stability)
echo "--- Setting RStudio R path configuration ---"
echo "rsession-which-r=/usr/bin/R" | sudo tee -a /etc/rstudio/rserver.conf
# 1d. Create and secure RStudio session directory (Crucial for preventing permission errors)
echo "--- Fixing RStudio session permissions ---"
mkdir -p /var/lib/rstudio/sessions
chown -R rstudio-server:rstudio-server /var/lib/rstudio
systemctl restart rstudio-server
echo "RStudio Server installed and configured."
---
## 2. Install The Littlest JupyterHub (TLJH)
echo "--- Installing The Littlest JupyterHub (TLJH) ---"
# TLJH installs Python, JupyterHub, the proxy, and creates the admin user
curl -L https://tljh.jupyter.org/bootstrap.py | sudo -E python3 - \
--admin "$ADMIN_USER":"$ADMIN_PASSWORD"
if [ $? -ne 0 ]; then
echo "ERROR: TLJH installation failed. Exiting."
exit 1
fi
echo "JupyterHub (TLJH) installed successfully."
---
## 3. Final Compatibility Fix (The most important step for RStudio after TLJH)
# TLJH installs its own PAM configuration. We must tell RStudio to use the standard system PAM profile for sessions.
echo "--- Applying TLJH/RStudio compatibility fix ---"
ln -s /etc/pam.d/login /etc/pam.d/rstudio
# Restart both services one last time
systemctl restart rstudio-server
systemctl restart jupyterhub
---
## 4. Final Verification and Instructions
echo ""
echo "=========================================================="
echo "✅ Installation Summary"
echo "=========================================================="
echo "JupyterHub (TLJH) is running. Access it at:"
echo " ➡️ http://<Your-Server-IP>/"
echo " Login with the admin user: **$ADMIN_USER**"
echo " Password: **$ADMIN_PASSWORD**"
echo ""
echo "RStudio Server is running on port 8787. Access it at:"
echo " ➡️ http://<Your-Server-IP>:8787/"
echo " Login with the **$ADMIN_USER** credentials created by TLJH."
echo "=========================================================="