#!/bin/bash # Script to install the Porter CLI on macOS, Linux, and WSL osname="" arch=$(arch) check_prereqs() { command -v curl >/dev/null 2>&1 || { echo "[ERROR] curl is required to install the Porter CLI." >&2; exit 1; } command -v unzip >/dev/null 2>&1 || { echo "[ERROR] unzip is required to install the Porter CLI." >&2; exit 1; } } # Sets download_error to a readable reason when the download fails. fetch() { local url="$1" local output_file="$2" shift 2 local http_code http_code=$(curl -L --fail --silent --connect-timeout 5 --max-time 600 --speed-limit 10240 --speed-time 30 \ --write-out '%{http_code}' "$@" "${url}" --output "${output_file}") local status=$? if [[ $status -eq 0 ]]; then return 0 fi # Some curl builds report an HTTP/2 error status as exit code 56, not 22. if [[ "$http_code" == "404" ]]; then download_error="file not found (HTTP 404)" return 1 elif [[ "$http_code" -ge 400 ]]; then download_error="the server returned HTTP ${http_code}" return 1 fi case $status in 6) download_error="could not resolve the host name, check your DNS or network connection" ;; 7) download_error="could not connect to the server" ;; 23) download_error="could not write ${output_file}, check the disk space and permissions of $(pwd)" ;; 28) download_error="the download timed out or stalled" ;; 35 | 51 | 58 | 60) download_error="the TLS connection failed, a proxy or firewall may intercept HTTPS traffic" ;; *) download_error="curl exited with code ${status}" ;; esac return 1 } download_file() { local filename="$1" local porter_version="$2" local output_file="$3" local download_url="https://install.porter.run/download/v${porter_version}/${filename}" local releases_url="https://releases.porter.run/releases/v${porter_version}/${filename}" local github_url="https://github.com/porter-dev/releases/releases/download/v${porter_version}/${filename}" local download_error="" echo "[INFO] Downloading ${filename}..." # The install server redirects to a presigned R2 URL, which is free egress. if fetch "${download_url}" "${output_file}"; then echo "[INFO] Downloaded ${filename}" return 0 fi echo "[WARNING] Download from install.porter.run failed: ${download_error}. Trying releases.porter.run..." if fetch "${releases_url}" "${output_file}"; then echo "[INFO] Downloaded ${filename}" return 0 fi echo "[WARNING] Download from releases.porter.run failed: ${download_error}. Trying GitHub..." if fetch "${github_url}" "${output_file}" --retry 2 --retry-connrefused --retry-delay 2; then echo "[INFO] Downloaded ${filename} from GitHub" return 0 fi echo "[ERROR] Could not download ${filename} from any source. The last error was: ${download_error}." >&2 if [[ "$download_error" == *"HTTP 404"* ]]; then echo "[ERROR] Porter v${porter_version} may not have a build for ${osname}/${arch}." >&2 fi echo "[ERROR] You can download it manually from https://github.com/porter-dev/releases/releases/tag/v${porter_version}" >&2 return 1 } download_and_install() { check_prereqs echo "[INFO] Since the Porter CLI gets installed in /usr/local/bin, you may be asked to input your password." echo "[INFO] Please make sure /usr/local/bin is included in your PATH." porter_version="0.69.15" if [[ -n "$PORTER_VERSION" ]]; then echo "[INFO] Using $PORTER_VERSION override instead of $porter_version" porter_version="$PORTER_VERSION" fi download_file "porter_${porter_version}_${osname}_${arch}" "${porter_version}" "porter-cli-executable" || exit 1 chmod +x ./porter-cli-executable sudo mv ./porter-cli-executable /usr/local/bin/porter command -v porter >/dev/null 2>&1 || { echo "[ERROR] There was an error installing the Porter CLI. Please try again." >&2; exit 1; } # only for legacy support (switchboard required) download_file "docker-credential-porter_${porter_version}_${osname}_${arch}" "${porter_version}" "docker-credential-porter" || exit 1 chmod +x ./docker-credential-porter sudo mv ./docker-credential-porter /usr/local/bin/docker-credential-porter echo "[INFO] Porter CLI installed. Run 'porter auth login' to get started." print_agent_notice exit } # curl | bash has no usable stdin, so this informs rather than prompts print_agent_notice() { cat <<'NOTICE' Using an AI coding agent (Claude Code, Codex, Cursor, ...)? Porter also ships an MCP server so your agent can deploy and manage apps for you: curl -fsSL https://agents.porter.run | sh Docs: https://docs.porter.run/mcp/overview NOTICE } if [[ "$arch" == "x86_64" ]]; then arch="amd64" elif [[ "$arch" == "aarch64" ]]; then arch="arm64" fi if [[ "$OSTYPE" == "linux-gnu"* ]]; then if uname -a | grep -q '^Linux.*Microsoft'; then echo "[WARNING] WSL support is experimental and may result in crashes." fi osname="linux" download_and_install elif [[ "$OSTYPE" == "darwin"* ]]; then osname="darwin" download_and_install fi echo "[ERROR] Unsupported operating system." exit 1