dvorakmortonxd.dev/install-dependencies-deb.sh
2026-09-24 09:02:08 +02:00

131 lines
4.1 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# install-dependencies-deb.sh
#
# Installs everything needed to build and run this project on Debian/Ubuntu:
# - system packages (git, curl, build tools, ...)
# - Node.js LTS (>= 20.9.0, required by Next.js 16) + npm
# - project dependencies via `npm install`
# - a production build via `npm run build`
#
# Usage:
# ./install-dependencies-deb.sh # install deps + npm install + npm run build
# ./install-dependencies-deb.sh --no-build # install deps + npm install only
# ./install-dependencies-deb.sh --no-npm # system packages only
#
set -euo pipefail
MIN_NODE_MAJOR=20
MIN_NODE_MINOR=9
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DO_NPM=1
DO_BUILD=1
usage() {
cat <<'EOF'
install-dependencies-deb.sh — set up everything needed to build/run this project.
Installs system packages, Node.js LTS (>= 20.9.0) + npm, then runs
`npm install` and `npm run build`.
Usage:
./install-dependencies-deb.sh system deps + npm install + build
./install-dependencies-deb.sh --no-build system deps + npm install only
./install-dependencies-deb.sh --no-npm system packages only
./install-dependencies-deb.sh -h|--help show this help
EOF
}
for arg in "$@"; do
case "$arg" in
--no-npm) DO_NPM=0; DO_BUILD=0 ;;
--no-build) DO_BUILD=0 ;;
-h|--help) usage; exit 0 ;;
*)
echo "Unknown option: $arg" >&2
exit 1
;;
esac
done
log() { printf '\n\033[1;34m==>\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33mwarning:\033[0m %s\n' "$*" >&2; }
die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
# --- must be Debian/Ubuntu ------------------------------------------------
if [ ! -f /etc/os-release ]; then
die "Cannot detect the OS (missing /etc/os-release). This script is for Debian/Ubuntu."
fi
# shellcheck disable=SC1091
. /etc/os-release
case "${ID:-}${ID_LIKE:-}" in
*debian*|*ubuntu*) ;;
*) warn "Detected '${PRETTY_NAME:-unknown OS}', not Debian/Ubuntu. Continuing anyway." ;;
esac
# --- root / sudo helper ---------------------------------------------------
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
elif command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
else
die "Not root and 'sudo' is not installed. Re-run as root."
fi
# --- system packages ------------------------------------------------------
log "Updating apt package lists"
$SUDO apt-get update -y
log "Installing base system packages"
$SUDO apt-get install -y --no-install-recommends \
ca-certificates \
curl \
gnupg \
git \
build-essential \
python3 \
make \
g++
# --- node.js --------------------------------------------------------------
node_ok() {
command -v node >/dev/null 2>&1 || return 1
local major minor
major="$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)"
minor="$(node -p 'process.versions.node.split(".")[1]' 2>/dev/null || echo 0)"
if [ "$major" -gt "$MIN_NODE_MAJOR" ] || { [ "$major" -eq "$MIN_NODE_MAJOR" ] && [ "$minor" -ge "$MIN_NODE_MINOR" ]; }; then
return 0
fi
return 1
}
if node_ok; then
log "Node.js $(node -v) already satisfies >= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0"
else
if command -v node >/dev/null 2>&1; then
warn "Node.js $(node -v) is too old for Next.js 16; installing current LTS."
fi
log "Installing Node.js LTS from NodeSource"
curl -fsSL https://deb.nodesource.com/setup_lts.x | $SUDO -E bash -
$SUDO apt-get install -y nodejs
fi
node_ok || die "Node.js is still below ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0. Check 'node -v'."
command -v npm >/dev/null 2>&1 || die "npm was not installed alongside Node.js."
log "Using node $(node -v) / npm $(npm -v)"
# --- project dependencies -------------------------------------------------
if [ "$DO_NPM" -eq 1 ]; then
[ -f "$SCRIPT_DIR/package.json" ] || die "package.json not found in $SCRIPT_DIR."
log "Installing project dependencies (npm install)"
( cd "$SCRIPT_DIR" && npm install )
if [ "$DO_BUILD" -eq 1 ]; then
log "Building the project (npm run build)"
( cd "$SCRIPT_DIR" && npm run build )
fi
fi
log "Done."
printf 'Start the dev server with:\n cd %s && npm run dev\n' "$SCRIPT_DIR"