|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# GitHub Copilot CLI Installation Script |
| 5 | +# Usage: curl -fsSL https://gh.io/copilot-install | bash |
| 6 | +# or: wget -qO- https://gh.io/copilot-install | bash |
| 7 | +# Use | sudo bash to run as root and install to /usr/local/bin |
| 8 | +# Export PREFIX to install to $PREFIX/bin/ directory (default: /usr/local for |
| 9 | +# root, $HOME/.local for non-root), e.g., export PREFIX=$HOME/custom to install |
| 10 | +# to $HOME/custom/bin |
| 11 | + |
| 12 | +echo "Installing GitHub Copilot CLI..." |
| 13 | + |
| 14 | +# Detect platform |
| 15 | +case "$(uname -s || echo "")" in |
| 16 | + Darwin*) PLATFORM="darwin" ;; |
| 17 | + Linux*) PLATFORM="linux" ;; |
| 18 | + *) |
| 19 | + if command -v winget >/dev/null 2>&1; then |
| 20 | + echo "Windows detected. Installing via winget..." |
| 21 | + winget install GitHub.Copilot |
| 22 | + exit $? |
| 23 | + else |
| 24 | + echo "Error: Windows detected but winget not found. Please see https://gh.io/install-copilot-readme" >&2 |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | + ;; |
| 28 | +esac |
| 29 | + |
| 30 | +# Detect architecture |
| 31 | +case "$(uname -m)" in |
| 32 | + x86_64|amd64) ARCH="x64" ;; |
| 33 | + aarch64|arm64) ARCH="arm64" ;; |
| 34 | + *) echo "Error: Unsupported architecture $(uname -m)" >&2 ; exit 1 ;; |
| 35 | +esac |
| 36 | + |
| 37 | +DOWNLOAD_URL="https://github.com/github/copilot-cli/releases/latest/download/copilot-${PLATFORM}-${ARCH}.tar.gz" |
| 38 | +echo "Downloading from: $DOWNLOAD_URL" |
| 39 | + |
| 40 | +# Download and extract with error handling |
| 41 | +TMP_TARBALL="$(mktemp)" |
| 42 | +if command -v curl >/dev/null 2>&1; then |
| 43 | + curl -fsSL "$DOWNLOAD_URL" -o "$TMP_TARBALL" |
| 44 | +elif command -v wget >/dev/null 2>&1; then |
| 45 | + wget -qO "$TMP_TARBALL" "$DOWNLOAD_URL" |
| 46 | +else |
| 47 | + echo "Error: Neither curl nor wget found. Please install one of them." |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# Check that the file is a valid tarball |
| 52 | +if ! tar -tzf "$TMP_TARBALL" >/dev/null 2>&1; then |
| 53 | + echo "Error: Downloaded file is not a valid tarball or is corrupted." >&2 |
| 54 | + rm -f "$TMP_TARBALL" |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# Check if running as root, fallback to non-root |
| 59 | +if [ "$(id -u 2>/dev/null || echo 1)" -eq 0 ]; then |
| 60 | + PREFIX="${PREFIX:-/usr/local}" |
| 61 | +else |
| 62 | + PREFIX="${PREFIX:-$HOME/.local}" |
| 63 | +fi |
| 64 | +INSTALL_DIR="$PREFIX/bin" |
| 65 | +if ! mkdir -p "$INSTALL_DIR"; then |
| 66 | + echo "Error: Could not create directory $INSTALL_DIR. You may not have write permissions." >&2 |
| 67 | + echo "Try running this script with sudo or set PREFIX to a directory you own (e.g., export PREFIX=\$HOME/.local)." >&2 |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +# Install binary |
| 72 | +if [ -f "$INSTALL_DIR/copilot" ]; then |
| 73 | + echo "Notice: Replacing copilot binary found at $INSTALL_DIR/copilot." |
| 74 | +fi |
| 75 | +tar -xz -C "$INSTALL_DIR" -f "$TMP_TARBALL" |
| 76 | +chmod +x "$INSTALL_DIR/copilot" |
| 77 | +echo "✓ GitHub Copilot CLI installed to $INSTALL_DIR/copilot" |
| 78 | +rm -f "$TMP_TARBALL" |
| 79 | + |
| 80 | +# Check if install directory is in PATH |
| 81 | +case ":$PATH:" in |
| 82 | + *":$INSTALL_DIR:"*) ;; |
| 83 | + *) |
| 84 | + echo "" |
| 85 | + echo "Warning: $INSTALL_DIR is not in your PATH" |
| 86 | + echo "Add it to your PATH by adding this line to your shell profile:" |
| 87 | + echo " export PATH=\"\$PATH:$INSTALL_DIR\"" |
| 88 | + ;; |
| 89 | +esac |
| 90 | + |
| 91 | +echo "" |
| 92 | +echo "Installation complete! Run 'copilot help' to get started." |
0 commit comments