#!/bin/sh # Install mm, Modelmathics in the terminal, on macOS or Linux. # Usage: curl -fsSL https://modelmathics.com/install.sh | sh # It installs uv when it is missing, then the newest mm as a uv tool, all in your home folder and without sudo. # Running it again installs the newest release over the old one. # It installs a release only when the release key signed its latest.json, which OpenSSH's ssh-keygen checks. # Every download goes over https, redirects included. # MM_DOWNLOAD names another https folder with latest.json, its signature and the wheels, for tests. # Everything runs from main on the last line, so a download cut short runs nothing. set -eu # The key that signs latest.json, as an allowed-signers line for ssh-keygen -Y verify. # Its private half stays on the owner's laptop, never on the server, and scripts/dev/release-mm.sh signs with it. # cli/src/modelmathics/update.py holds the same line, and release-mm.sh refuses to release until both hold the real key. RELEASE_KEY='release@modelmathics.com namespaces="modelmathics-release" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILrUB+s9EHPghfJIE7OUSydub01vHIi5guURtLevW18d' # uv's installer for one pinned release, which runs only when it matches this sha256. # That installer carries the sha256 of each uv download in turn. UV_INSTALLER=https://github.com/astral-sh/uv/releases/download/0.11.32/uv-installer.sh UV_INSTALLER_SHA256=43aff33a967fe40e8c17949d8c85c65bc43f3b5c94742393c957f56ab5ba80f4 say() { printf '%s\n' "$*" } die() { printf 'mm install: %s\n' "$*" >&2 exit 1 } # The sha256 of a file, with the tool Linux or macOS has. checksum() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 else die "neither sha256sum nor shasum is installed, so the download cannot be checked." fi } # Download an https URL to a file, and say plainly why when it fails. # curl refuses plain http, after a redirect too, and its own error line stays hidden, so the learner reads one message. fetch() { failed=0 code="$(curl --proto '=https' --proto-redir '=https' -fsL -o "$2" -w '%{http_code}' "$1" 2>/dev/null)" || failed=$? case "$failed:$code" in 0:200) return 0 ;; *:000 | *:200) die "could not reach $1. Check your connection and run this line again." ;; *:404) die "nothing is published at $1 yet. Try again later." ;; *:3??) die "$1 moved to an address that is not https, so nothing was installed." ;; *) die "$1 answered with HTTP $code. Try again later." ;; esac } # One string field of latest.json, which keeps one key per line. field() { sed -n "s/^[[:space:]]*\"$1\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" "$2" | head -n 1 } # Say when the course's builds cannot run here, since its pinned PyTorch has no build for this machine (B1-1). # mm itself and its lessons work everywhere, so this only warns. # sysctl names Apple silicon even when this shell runs under Rosetta, where uname -m says x86_64. warn_platform() { problem="" case "$(uname -s)" in Darwin) if [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" != 1 ]; then problem="an Intel Mac" else mac="$(sw_vers -productVersion 2>/dev/null || true)" case "${mac%%.*}" in '' | *[!0-9]*) ;; *) [ "${mac%%.*}" -ge 14 ] || problem="macOS $mac" ;; esac fi ;; Linux) case "$(uname -m)" in x86_64 | amd64 | aarch64 | arm64) ;; *) problem="Linux on $(uname -m)" ;; esac ;; esac if [ -n "$problem" ]; then say "Note: the course's builds need PyTorch, which has no build for $problem." say "Builds run on macOS 14 or newer on Apple silicon, Linux, and Windows through WSL." say "mm and its lessons work here; take the builds on one of those." say "" fi } # Find uv on the PATH or where Astral's installer puts it, and put its folder on this script's PATH. find_uv() { if command -v uv >/dev/null 2>&1; then return 0 fi for dir in "${XDG_BIN_HOME:-}" "$HOME/.local/bin" "$HOME/.cargo/bin"; do if [ -n "$dir" ] && [ -x "$dir/uv" ]; then PATH="$dir:$PATH" export PATH return 0 fi done return 1 } main() { original_path="$PATH" base="${MM_DOWNLOAD:-https://modelmathics.com/dl}" base="${base%/}" case "$(uname -s)" in Linux | Darwin) ;; *) die "mm runs on macOS and Linux; on Windows, run this inside WSL." ;; esac warn_platform command -v curl >/dev/null 2>&1 || die "curl is not installed. Install curl with your package manager, then run this line again." command -v ssh-keygen >/dev/null 2>&1 || die "ssh-keygen is not installed, and it checks that a release is signed. Install OpenSSH 8.1 or newer (openssh-client with apt, openssh-clients with dnf), then run this line again." case "$base" in https://*) ;; *) die "MM_DOWNLOAD must be an https address, not $base." ;; esac if [ "$(id -u)" = 0 ]; then say "You are running this as root; mm installs for the user who runs it, so run it without sudo." fi work="$(mktemp -d 2>/dev/null || mktemp -d -t mm-install)" trap 'rm -rf "$work"' EXIT INT TERM # latest.json is read only once the release key's signature on it checks, and before anything is installed. fetch "$base/latest.json" "$work/latest.json" fetch "$base/latest.json.sig" "$work/latest.json.sig" printf '%s\n' "$RELEASE_KEY" >"$work/allowed_signers" ssh-keygen -Y verify -f "$work/allowed_signers" -I release@modelmathics.com -n modelmathics-release \ -s "$work/latest.json.sig" <"$work/latest.json" >/dev/null 2>&1 \ || die "$base/latest.json is not signed with the Modelmathics release key, so nothing was installed. Checking it needs OpenSSH 8.1 or newer." version="$(field version "$work/latest.json")" wheel="$(field wheel "$work/latest.json")" sha="$(field sha256 "$work/latest.json")" [ -n "$version" ] && [ -n "$wheel" ] && [ -n "$sha" ] || die "$base/latest.json does not name a release." case "$wheel" in */* | *..*) die "$base/latest.json names a strange file: $wheel." ;; esac if ! find_uv; then say "Installing uv, the Python tool manager mm runs on..." fetch "$UV_INSTALLER" "$work/uv-installer.sh" [ "$(checksum "$work/uv-installer.sh")" = "$UV_INSTALLER_SHA256" ] || die "uv's installer does not match its checksum, so nothing was installed. Run this line again." # uv's installer talks on stderr too, so its output is kept aside and shown only when it fails. # Its stdin is empty, since under curl | sh this script's own stdin is the rest of the script. if ! env UV_NO_MODIFY_PATH=1 INSTALLER_PRINT_QUIET=1 sh "$work/uv-installer.sh" "$work/uv.log" 2>&1; then cat "$work/uv.log" >&2 die "uv could not be installed. See https://docs.astral.sh/uv/ and run this line again." fi find_uv || die "uv was installed but cannot be found. Open a new terminal and run this line again." fi say "Installing mm $version..." fetch "$base/$wheel" "$work/$wheel" [ "$(checksum "$work/$wheel")" = "$sha" ] || die "$wheel does not match its checksum, so nothing was installed. Run this line again." # Without a Python 3.11 or newer, uv downloads one first, which takes a moment. if ! uv python find '>=3.11' >/dev/null 2>&1; then say "Downloading Python for mm (about 30 MB)..." fi uv tool install --force --quiet "$work/$wheel" || die "uv could not install mm." # The folder uv puts tool commands in, which must be on the PATH of every new terminal. bin="$(uv tool dir --bin)" # The script's own PATH already has uv's folder, so update-shell gets the PATH the terminal had. shell_ok=yes PATH="$original_path" "$(command -v uv)" tool update-shell >/dev/null 2>&1 || shell_ok=no # Only a terminal opened after this one sees a PATH that changed. case ":$original_path:" in *":$bin:"*) on_path=yes ;; *) on_path=no ;; esac say "" say "mm $version is installed." say "" if [ "$on_path" = yes ]; then say "Once your invitation arrives, create your account at app.modelmathics.com/signup." say "Then sign in, and open the course:" elif [ "$shell_ok" = no ]; then say "Your shell was not recognised, so add this line to its startup file and open a new terminal:" say "" say " export PATH=\"$bin:\$PATH\"" say "" say "Once your invitation arrives, create your account at app.modelmathics.com/signup." say "Then sign in, and open the course:" else say "Open a new terminal first, so it finds mm in $(printf '%s' "$bin" | sed "s|^$HOME/|~/|")." say "Once your invitation arrives, create your account at app.modelmathics.com/signup." say "Then sign in, and open the course:" fi say "" say " mm login" say " mm" say "" say "No account yet? Modelmathics is invite-only during the private beta." say "Request access at https://modelmathics.com/join?from=mm" say "" } main "$@"