#!/data/data/com.termux/files/usr/bin/bash set -e # ───────────────────────────────────────────────────────── # OpenCode Termux Installer # https://github.com/superman-enamy/opencode-termux-installer # ───────────────────────────────────────────────────────── # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' BOLD='\033[1m' DIM='\033[2m' RESET='\033[0m' # Helpers info() { echo -e "${BLUE}[INFO]${RESET} $1"; } success() { echo -e "${GREEN}[SUCCESS]${RESET} $1"; } warn() { echo -e "${YELLOW}[WARN]${RESET} $1"; } error() { echo -e "${RED}[ERROR]${RESET} $1"; } step() { echo -e "\n${CYAN}━━━ Step $1 ━━━${RESET}"; } # Banner echo "" echo -e "${BOLD}${CYAN}" echo " ┌──────────────────────────────────────────────┐" echo " │ │" echo " │ ⚡ OpenCode for Termux ⚡ │" echo " │ AI Coding Agent on Android │" echo " │ │" echo " └──────────────────────────────────────────────┘" echo -e "${RESET}" # ── Pre-flight checks ────────────────────────────────── step "1/4 Pre-flight checks" # Check architecture ARCH=$(uname -m) if [ "$ARCH" != "aarch64" ]; then error "Unsupported architecture: $ARCH" echo " This installer only supports Android aarch64 (64-bit ARM)." exit 1 fi info "Architecture: $ARCH ✓" # Check if running in Termux if [ -z "$PREFIX" ]; then error "Not running in Termux." echo " Please run this script inside the Termux app." exit 1 fi info "Termux detected ✓" # ── Install dependencies ─────────────────────────────── step "2/4 Installing dependencies" pkg update -y pkg install -y ripgrep dpkg curl unzip success "Dependencies installed" # ── Download OpenCode ────────────────────────────────── step "3/4 Downloading OpenCode" info "Fetching latest release from GitHub..." DEB_URL=$(curl -s https://api.github.com/repos/guysoft/opencode-termux/releases/latest \ | grep -o '"browser_download_url": "[^"]*_aarch64\.deb"' \ | head -1 \ | cut -d'"' -f4) if [ -n "$DEB_URL" ]; then info "Found .deb package — downloading..." cd ~ curl -# -LO "$DEB_URL" DEB_FILE=$(ls -t opencode_*_aarch64.deb 2>/dev/null | head -1) info "Installing $DEB_FILE..." dpkg -i "$DEB_FILE" || true # Fix any dependency issues apt-get install -f -y 2>/dev/null || pkg install -f -y 2>/dev/null || true rm -f "$DEB_FILE" success "Installed via .deb package" else warn ".deb not found — falling back to .zip method..." ZIP_URL=$(curl -s https://api.github.com/repos/guysoft/opencode-termux/releases/latest \ | grep -o '"browser_download_url": "[^"]*android-aarch64\.zip"' \ | head -1 \ | cut -d'"' -f4) if [ -z "$ZIP_URL" ]; then error "Could not find any download URL." echo " Check your internet connection and try again." exit 1 fi info "Downloading from: $ZIP_URL" cd ~ curl -# -LO "$ZIP_URL" ZIP_FILE=$(ls -t opencode-*-android-aarch64.zip 2>/dev/null | head -1) info "Extracting $ZIP_FILE..." # Extract ALL files — the wrapper + .so files MUST stay together unzip -o "$ZIP_FILE" -d "$PREFIX/bin/" chmod +x "$PREFIX/bin/opencode" "$PREFIX/bin/opencode.bin" # Debug binary (if present) [ -f "$PREFIX/bin/opencode-debug" ] && chmod +x "$PREFIX/bin/opencode-debug" "$PREFIX/bin/opencode-debug.bin" rm -f "$ZIP_FILE" success "Installed via .zip archive" fi # ── Verify ───────────────────────────────────────────── step "4/4 Verifying installation" echo "" if command -v opencode &>/dev/null; then VERSION=$(opencode --version 2>/dev/null || echo "unknown") success "OpenCode is installed! ${DIM}(version: $VERSION)${RESET}" else warn "opencode command not found in PATH." echo " Try restarting Termux or run: ${BOLD}source ~/.bashrc${RESET}" fi echo "" echo -e "${BOLD}${GREEN} ┌──────────────────────────────────────────────┐${RESET}" echo -e "${BOLD}${GREEN} │ Installation Complete! 🎉 │${RESET}" echo -e "${BOLD}${GREEN} └──────────────────────────────────────────────┘${RESET}" echo "" echo -e " ${BOLD}Quick start:${RESET}" echo -e " 1. Set your API key:" echo -e " ${DIM}export ANTHROPIC_API_KEY=sk-ant-...${RESET}" echo "" echo -e " 2. Launch OpenCode:" echo -e " ${DIM}cd ~/your-project && opencode${RESET}" echo "" echo -e " ${DIM}Docs: https://opencode.ai${RESET}" echo ""