#!/bin/sh # Claude Code manager for macOS and Linux (claude.mad-gamer.com). # # curl -fsSL https://claude.mad-gamer.com/unix | sh # wget -qO- https://claude.mad-gamer.com/unix | sh (no curl yet) # # Installs, updates, repairs or uninstalls Claude Code, with screen reader # mode on from the very first launch. Asks what to do; pressing Enter takes # the answer in brackets. The questions are read from the terminal, so # piping the script into sh works. # # Installing: # 1. Installs what the official installer and Claude Code need, if missing: # curl, bash and CA certificates, and on Alpine (musl) also libgcc, # libstdc++ and ripgrep. It uses the system's package manager (apt, dnf, # yum, zypper, pacman or apk) and asks for sudo only for this step. A Mac # already has everything. # 2. Turns on screen reader mode ("axScreenReader": true in # ~/.claude/settings.json) before Claude Code first runs, so even its # sign-in screens are read well. Other settings in the file are kept. # 3. Runs Anthropic's official installer, https://claude.ai/install.sh. # 4. Puts ~/.local/bin on your PATH in the start-up files of bash, zsh, # fish and sh, because the official installer does not. The lines it # adds are marked, so uninstalling can take them out again. # # Uninstalling removes Claude Code the way Anthropic's documentation # describes, and also copies installed with npm, Homebrew or a system # package, if there are any. Your settings, sign-in and history stay unless # you choose to remove them too. # # Without questions (for scripts), name what to do after `sh -s --`: # # ... | sh -s -- install [latest|stable|VERSION] [--no-screen-reader] # ... | sh -s -- update # ... | sh -s -- uninstall [--everything] # ... | sh -s -- screen-reader on|off # ... | sh -s -- status # # Output is plain lines for screen readers: no colours, progress bars or # cursor movement (apart from the official installer's own few lines). # Served from /var/www/claude/unix on mad-gamer.com (see # claude.mad-gamer.com.conf). set -u MANAGER_VERSION=1.0.0 OFFICIAL_INSTALLER=https://claude.ai/install.sh MARK='# Added by the MG Claude Code installer' BIN_DIR=$HOME/.local/bin CLAUDE=$BIN_DIR/claude CONFIG_DIR=${CLAUDE_CONFIG_DIR:-$HOME/.claude} SETTINGS=$CONFIG_DIR/settings.json if [ -n "${CLAUDE_CONFIG_DIR:-}" ]; then STATE_FILE=$CLAUDE_CONFIG_DIR/.claude.json else STATE_FILE=$HOME/.claude.json fi FISH_FILE=$HOME/.config/fish/conf.d/mg-claude-code.fish INTERACTIVE=no WORK='' cleanup() { if [ -n "$WORK" ]; then rm -rf "$WORK"; fi } trap cleanup EXIT trap 'say ""; say "Stopped. Nothing more was changed."; exit 130' INT TERM say() { printf '%s\n' "$*"; } stop() { say '' say "Stopped: $*" exit 1 } work_dir() { if [ -z "$WORK" ]; then WORK=$(mktemp -d 2>/dev/null || mktemp -d -t mgclaude) || stop 'could not make a temporary folder' fi } # ─── questions (from the terminal, also when piped into sh) ─────────────── ask_line() { printf '%s ' "$1" REPLY='' IFS= read -r REPLY /dev/null 2>&1 && ldd /bin/ls 2>&1 | grep -q musl; } } check_platform() { case $OS in Darwin | Linux) ;; MINGW* | MSYS* | CYGWIN*) stop 'this is the Mac and Linux script. On Windows, run in PowerShell: irm https://claude.mad-gamer.com/win | iex' ;; *) stop "Claude Code does not support $OS." ;; esac case $(uname -m) in x86_64 | amd64 | arm64 | aarch64) ;; *) stop "Claude Code needs a 64-bit Intel, AMD or ARM processor, and this one is $(uname -m)." ;; esac } # Like the official installer: not under sudo, which would install into # root's home folder instead of yours. Plain root (a container) is fine. check_not_sudo() { if [ "$(id -u)" -eq 0 ] && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != root ]; then stop 'please run this without sudo. Claude Code installs into your own home folder; the script asks for sudo itself if it needs to install system packages.' fi } # ─── downloads ──────────────────────────────────────────────────────────── # fetch URL FILE fetch() { if command -v curl >/dev/null 2>&1; then curl -fsSL --retry 3 -o "$2" "$1" elif command -v wget >/dev/null 2>&1; then wget -q -O "$2" "$1" else return 1 fi } # ─── dependencies ───────────────────────────────────────────────────────── has_ca_certificates() { for f in /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt \ /etc/ssl/ca-bundle.pem /etc/ssl/cert.pem /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem; do if [ -s "$f" ]; then return 0; fi done return 1 } has_lib() { for d in /lib /usr/lib /lib64 /usr/lib64; do if [ -e "$d/$1" ]; then return 0; fi done return 1 } # Sets MISSING to the packages that are needed and not installed. find_missing() { MISSING='' [ "$OS" = Linux ] || return 0 command -v curl >/dev/null 2>&1 || MISSING="$MISSING curl" command -v bash >/dev/null 2>&1 || MISSING="$MISSING bash" has_ca_certificates || MISSING="$MISSING ca-certificates" if is_musl; then has_lib libgcc_s.so.1 || MISSING="$MISSING libgcc" has_lib libstdc++.so.6 || MISSING="$MISSING libstdc++" command -v rg >/dev/null 2>&1 || MISSING="$MISSING ripgrep" fi MISSING=${MISSING# } } package_manager() { for pm in apt-get dnf yum zypper pacman apk; do if command -v "$pm" >/dev/null 2>&1; then printf '%s' "$pm" return fi done } # as_root COMMAND...: runs it as root, through sudo when needed. as_root() { if [ "$(id -u)" -eq 0 ]; then "$@" elif command -v sudo >/dev/null 2>&1; then sudo "$@" elif command -v doas >/dev/null 2>&1; then doas "$@" else return 127 fi } install_dependencies() { find_missing if [ -z "$MISSING" ]; then say 'Everything Claude Code needs is already installed.' return fi pm=$(package_manager) say "Claude Code needs these, which are not installed: $MISSING." if [ -z "$pm" ]; then stop "no supported package manager was found. Install $MISSING yourself, then run this again." fi if [ "$(id -u)" -ne 0 ] && ! command -v sudo >/dev/null 2>&1 && ! command -v doas >/dev/null 2>&1; then stop "installing them needs root, and there is no sudo. As root, install $MISSING with $pm, then run this again." fi if ! ask_yes_no "Install them now with $pm? This may ask for your password." yes; then stop "Claude Code cannot be installed without $MISSING." fi say "Installing $MISSING..." # shellcheck disable=SC2086 # MISSING is a list of package names case $pm in apt-get) as_root env DEBIAN_FRONTEND=noninteractive apt-get update -qq /dev/null && as_root env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends $MISSING /dev/null ;; dnf) as_root dnf install -y -q $MISSING /dev/null ;; yum) as_root yum install -y -q $MISSING /dev/null ;; zypper) as_root zypper --non-interactive --quiet install $MISSING /dev/null ;; pacman) as_root pacman -Sy --noconfirm --needed $MISSING /dev/null ;; apk) as_root apk add --no-cache --quiet $MISSING /dev/null ;; esac || stop "$pm could not install $MISSING. Install them yourself, then run this again." find_missing if [ -n "$MISSING" ]; then stop "these are still missing after installing: $MISSING." fi say 'Installed.' } # ─── screen reader setting ──────────────────────────────────────────────── # Prints true, false or nothing: the axScreenReader value in the settings. screen_reader_setting() { [ -f "$SETTINGS" ] || return 0 awk '{ s = s $0 "\n" } END { if (match(s, /"axScreenReader"[ \t\r\n]*:[ \t\r\n]*(true|false)/)) { v = substr(s, RSTART, RLENGTH) if (v ~ /true$/) print "true"; else print "false" } }' "$SETTINGS" } # set_screen_reader true|false: sets the value, keeping the rest of the file # exactly as it is (no other key is touched or reformatted). set_screen_reader() { mkdir -p "$CONFIG_DIR" || stop "could not make the folder $CONFIG_DIR" if [ ! -f "$SETTINGS" ] || ! grep -q '[^[:space:]]' "$SETTINGS"; then printf '{\n "axScreenReader": %s\n}\n' "$1" >"$SETTINGS" || stop "could not write $SETTINGS" return fi if command -v jq >/dev/null 2>&1 && ! jq -e 'type == "object"' "$SETTINGS" >/dev/null 2>&1; then stop "$SETTINGS is not a valid settings file (it must be a JSON object). Fix or remove it, then run this again." fi work_dir if ! awk -v val="$1" '{ s = s $0 "\n" } END { if (match(s, /"axScreenReader"[ \t\r\n]*:[ \t\r\n]*(true|false)/)) { m = substr(s, RSTART, RLENGTH); start = RSTART; len = RLENGTH sub(/(true|false)$/, val, m) s = substr(s, 1, start - 1) m substr(s, start + len) } else { if (!match(s, /^[ \t\r\n]*[{]/)) exit 2 head = substr(s, 1, RLENGTH); rest = substr(s, RLENGTH + 1) if (match(rest, /^[ \t\r\n]*[}]/)) sep = "\n"; else sep = "," s = head "\n \"axScreenReader\": " val sep rest } printf "%s", s }' "$SETTINGS" >"$WORK/settings.json"; then stop "$SETTINGS is not a valid settings file (it must be a JSON object). Fix or remove it, then run this again." fi if command -v jq >/dev/null 2>&1 && ! jq -e 'type == "object"' "$WORK/settings.json" >/dev/null 2>&1; then stop "could not change $SETTINGS safely. Add \"axScreenReader\": $1 to it yourself." fi # cat keeps the file's owner, permissions and any link. cat "$WORK/settings.json" >"$SETTINGS" || stop "could not write $SETTINGS" } screen_reader_override_note() { case ${CLAUDE_AX_SCREEN_READER:-} in 0 | false) say 'Note: CLAUDE_AX_SCREEN_READER=0 is set in your environment, which turns screen reader mode off whatever the setting says. Remove it from your shell start-up file to use the setting.' ;; esac } # ─── PATH ───────────────────────────────────────────────────────────────── user_shell() { s=${SHELL:-} if [ -z "$s" ] && command -v getent >/dev/null 2>&1; then s=$(getent passwd "$(id -un)" | cut -d: -f7) fi basename "${s:-sh}" } # The start-up files that get the PATH line: those of the shells you have. path_files() { sh_name=$(user_shell) files="$HOME/.profile" for f in "$HOME/.bash_profile" "$HOME/.bash_login"; do if [ -f "$f" ]; then files="$files $f"; fi done if [ -f "$HOME/.bashrc" ] || [ "$sh_name" = bash ] || command -v bash >/dev/null 2>&1; then files="$files $HOME/.bashrc"; fi if [ -f "$HOME/.zshrc" ] || [ "$sh_name" = zsh ] || [ "$OS" = Darwin ] || command -v zsh >/dev/null 2>&1; then files="$files $HOME/.zshrc"; fi printf '%s' "$files" } add_to_path() { line='case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) export PATH="$HOME/.local/bin:$PATH" ;; esac '"$MARK" changed='' for f in $(path_files); do if [ -f "$f" ] && grep -q '\.local/bin' "$f"; then continue; fi printf '\n%s\n' "$line" >>"$f" || stop "could not write $f" changed="$changed $f" done if [ "$(user_shell)" = fish ] || [ -d "$HOME/.config/fish" ]; then if [ ! -f "$FISH_FILE" ]; then mkdir -p "$(dirname "$FISH_FILE")" && printf '%s\nfish_add_path -g $HOME/.local/bin\n' "$MARK" >"$FISH_FILE" && changed="$changed $FISH_FILE" fi fi if [ -n "$changed" ]; then say "Added ~/.local/bin to your PATH in:$changed." else say 'Your shell start-up files already put ~/.local/bin on your PATH.' fi } # Takes out only the lines this script added. remove_from_path() { removed='' work_dir for f in "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.bashrc" "$HOME/.zshrc"; do if [ -f "$f" ] && grep -qF "$MARK" "$f"; then grep -vF "$MARK" "$f" >"$WORK/rc" || true if grep -q '[^[:space:]]' "$WORK/rc"; then cat "$WORK/rc" >"$f" else rm -f "$f" # only our line was in it: the file was made for it fi removed="$removed $f" fi done if [ -f "$FISH_FILE" ]; then rm -f "$FISH_FILE" && removed="$removed $FISH_FILE"; fi if [ -n "$removed" ]; then say "Took ~/.local/bin back off your PATH in:$removed."; fi } on_path_now() { case ":$PATH:" in *":$BIN_DIR:"*) return 0 ;; esac return 1 } # ─── state ──────────────────────────────────────────────────────────────── installed_version() { if [ -x "$CLAUDE" ]; then "$CLAUDE" --version /dev/null | head -n 1 | sed 's/ (Claude Code)//' fi } # Other copies of Claude Code: sets OTHERS to a list of words # (npm, brew, brew-latest, apt, rpm, apk, legacy). find_other_installs() { OTHERS='' if command -v npm >/dev/null 2>&1 && npm ls -g --depth=0 @anthropic-ai/claude-code /dev/null | grep -q '@anthropic-ai/claude-code@'; then OTHERS="$OTHERS npm" fi if command -v brew >/dev/null 2>&1; then casks=$(brew list --cask /dev/null) if printf '%s\n' "$casks" | grep -qx 'claude-code'; then OTHERS="$OTHERS brew"; fi if printf '%s\n' "$casks" | grep -qx 'claude-code@latest'; then OTHERS="$OTHERS brew-latest"; fi fi if command -v dpkg-query >/dev/null 2>&1 && [ "$(dpkg-query -W -f='${Status}' claude-code 2>/dev/null)" = 'install ok installed' ]; then OTHERS="$OTHERS apt" elif command -v rpm >/dev/null 2>&1 && rpm -q claude-code >/dev/null 2>&1; then OTHERS="$OTHERS rpm" elif command -v apk >/dev/null 2>&1 && apk info -e claude-code >/dev/null 2>&1; then OTHERS="$OTHERS apk" fi if [ -d "$CONFIG_DIR/local" ]; then OTHERS="$OTHERS legacy"; fi OTHERS=${OTHERS# } } describe_other() { case $1 in npm) say 'a copy installed with npm (@anthropic-ai/claude-code)' ;; brew) say 'a copy installed with Homebrew (claude-code)' ;; brew-latest) say 'a copy installed with Homebrew (claude-code@latest)' ;; apt) say 'a system package installed with apt (claude-code)' ;; rpm) say 'a system package installed with dnf or yum (claude-code)' ;; apk) say 'a system package installed with apk (claude-code)' ;; legacy) say "an old local install in $CONFIG_DIR/local" ;; esac } remove_other() { case $1 in npm) npm uninstall -g @anthropic-ai/claude-code /dev/null 2>&1 || as_root npm uninstall -g @anthropic-ai/claude-code /dev/null ;; brew) brew uninstall --cask claude-code /dev/null ;; brew-latest) brew uninstall --cask claude-code@latest /dev/null ;; apt) as_root env DEBIAN_FRONTEND=noninteractive apt-get remove -y -qq claude-code /dev/null ;; rpm) if command -v dnf >/dev/null 2>&1; then as_root dnf remove -y -q claude-code /dev/null; else as_root yum remove -y -q claude-code /dev/null; fi ;; apk) as_root apk del --quiet claude-code /dev/null ;; legacy) rm -rf "$CONFIG_DIR/local" ;; esac } show_status() { v=$(installed_version) if [ -n "$v" ]; then say "Claude Code $v is installed, in $BIN_DIR." elif [ -e "$CLAUDE" ]; then say "Claude Code is in $BIN_DIR, but it does not run." else say 'Claude Code is not installed.' fi case $(screen_reader_setting) in true) say 'Screen reader mode is on.' ;; false) say 'Screen reader mode is off.' ;; *) say 'Screen reader mode is off (not set).' ;; esac screen_reader_override_note found=no for f in $(path_files) "$FISH_FILE"; do if [ -f "$f" ] && grep -q '\.local/bin' "$f"; then found=yes; fi done if [ $found = yes ]; then say 'New terminals have ~/.local/bin on the PATH.' else say 'New terminals do not have ~/.local/bin on the PATH yet.' fi find_other_installs for o in $OTHERS; do say "Also found: $(describe_other "$o")."; done } # ─── actions ────────────────────────────────────────────────────────────── do_install() { target=$1 screen_reader=$2 check_not_sudo say '' say 'Step 1 of 4: dependencies.' install_dependencies say '' say 'Step 2 of 4: screen reader mode.' if [ "$screen_reader" = yes ]; then set_screen_reader true say "Screen reader mode is on, in $SETTINGS." screen_reader_override_note else say 'Left as it is.' fi say '' say 'Step 3 of 4: the official Claude Code installer.' work_dir fetch "$OFFICIAL_INSTALLER" "$WORK/install.sh" || stop "could not download $OFFICIAL_INSTALLER. Check your internet connection." head -n 1 "$WORK/install.sh" | grep -q '^#!' || stop "$OFFICIAL_INSTALLER did not return a script. Check your internet connection." if [ -n "$target" ]; then bash "$WORK/install.sh" "$target" /dev/null)" ]; then remove_from_path elif grep -qsF "$MARK" "$HOME/.profile" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile" "$HOME/.bash_login" "$FISH_FILE"; then say 'Other programs are in ~/.local/bin, so it stays on your PATH.' fi if [ "$everything" = yes ]; then rm -rf "$CONFIG_DIR" rm -f "$STATE_FILE" say "Also removed your settings, sign-in and history ($CONFIG_DIR and $STATE_FILE)." else say "Your settings, sign-in and history are kept, in $CONFIG_DIR, so installing again picks up where you left off." fi if command -v claude >/dev/null 2>&1 && [ "$(command -v claude)" != "$CLAUDE" ]; then say "Note: another claude is still on your PATH, at $(command -v claude). It may be a shell alias or a copy installed another way." fi say 'Claude Code is uninstalled.' } usage() { say 'Usage: sh unix.sh [install [latest|stable|VERSION] [--no-screen-reader] | update | uninstall [--everything] | screen-reader on|off | status]' say 'With nothing named, it asks what to do.' } menu() { show_status say '' installed=no if [ -e "$CLAUDE" ]; then installed=yes; fi sr=$(screen_reader_setting) if [ "$sr" = true ]; then toggle='Turn screen reader mode off'; else toggle='Turn screen reader mode on'; fi if [ $installed = no ]; then ask_choice 'What would you like to do?' 'Install Claude Code' "$toggle" 'Uninstall leftovers' 'Quit' case $CHOICE in 1) CHOICE=install ;; 2) CHOICE=toggle ;; 3) CHOICE=uninstall ;; *) CHOICE=quit ;; esac else ask_choice 'What would you like to do?' 'Update Claude Code' 'Repair: install again, and check screen reader mode and the PATH' "$toggle" 'Uninstall Claude Code' 'Quit' case $CHOICE in 1) CHOICE=update ;; 2) CHOICE=install ;; 3) CHOICE=toggle ;; 4) CHOICE=uninstall ;; *) CHOICE=quit ;; esac fi case $CHOICE in install) if ask_yes_no 'Turn on screen reader mode?' yes; then sr_answer=yes; else sr_answer=no; fi do_install '' "$sr_answer" ;; update) do_update ;; toggle) if [ "$sr" = true ]; then set_screen_reader false; say 'Screen reader mode is off.' else set_screen_reader true; say 'Screen reader mode is on.'; screen_reader_override_note; fi ;; uninstall) if ! ask_yes_no 'Uninstall Claude Code?' no; then say 'Nothing was changed.'; return; fi if ask_yes_no 'Also remove your Claude Code settings, sign-in and history?' no; then all=yes; else all=no; fi do_uninstall "$all" ;; quit) say 'Nothing was changed.' ;; esac } main() { say "Claude Code manager $MANAGER_VERSION, from claude.mad-gamer.com." check_platform action=${1:-} if [ $# -gt 0 ]; then shift; fi target='' screen_reader=yes everything=no for arg in "$@"; do case $arg in latest | stable) target=$arg ;; [0-9]*.[0-9]*.[0-9]*) target=$arg ;; --no-screen-reader) screen_reader=no ;; --everything) everything=yes ;; on | off) ;; *) usage; stop "unknown option: $arg" ;; esac done case $action in '') if (exec /dev/null; then INTERACTIVE=yes menu else usage stop 'there is no terminal to ask questions in, so name what to do.' fi ;; install | repair) do_install "$target" "$screen_reader" ;; update | upgrade) do_update ;; uninstall | remove) do_uninstall "$everything" ;; status) show_status ;; screen-reader) case ${1:-} in on) set_screen_reader true; say 'Screen reader mode is on.'; screen_reader_override_note ;; off) set_screen_reader false; say 'Screen reader mode is off.' ;; *) usage; stop 'say on or off after screen-reader.' ;; esac ;; help | -h | --help) usage ;; *) usage; stop "unknown action: $action" ;; esac } main "$@"