install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP=opencode2
  4. MUTED='\033[0;2m'
  5. RED='\033[0;31m'
  6. ORANGE='\033[38;5;214m'
  7. NC='\033[0m' # No Color
  8. usage() {
  9. cat <<EOF
  10. OpenCode Installer
  11. Usage: install.sh [options]
  12. Options:
  13. -h, --help Display this help message
  14. -v, --version <version> Install a specific version (e.g., 0.0.0-next-17236)
  15. -b, --binary <path> Install from a local binary instead of downloading
  16. --no-modify-path Don't modify shell config files (.zshrc, .bashrc, etc.)
  17. Examples:
  18. curl -fsSL https://raw.githubusercontent.com/anomalyco/opencode/v2/install | bash
  19. curl -fsSL https://raw.githubusercontent.com/anomalyco/opencode/v2/install | bash -s -- --version 0.0.0-next-17236
  20. ./install --binary /path/to/opencode2
  21. EOF
  22. }
  23. requested_version=${VERSION:-}
  24. no_modify_path=false
  25. binary_path=""
  26. while [[ $# -gt 0 ]]; do
  27. case "$1" in
  28. -h|--help)
  29. usage
  30. exit 0
  31. ;;
  32. -v|--version)
  33. if [[ -n "${2:-}" ]]; then
  34. requested_version="$2"
  35. shift 2
  36. else
  37. echo -e "${RED}Error: --version requires a version argument${NC}"
  38. exit 1
  39. fi
  40. ;;
  41. -b|--binary)
  42. if [[ -n "${2:-}" ]]; then
  43. binary_path="$2"
  44. shift 2
  45. else
  46. echo -e "${RED}Error: --binary requires a path argument${NC}"
  47. exit 1
  48. fi
  49. ;;
  50. --no-modify-path)
  51. no_modify_path=true
  52. shift
  53. ;;
  54. *)
  55. echo -e "${ORANGE}Warning: Unknown option '$1'${NC}" >&2
  56. shift
  57. ;;
  58. esac
  59. done
  60. INSTALL_DIR=$HOME/.opencode/bin
  61. mkdir -p "$INSTALL_DIR"
  62. # If --binary is provided, skip all download/detection logic
  63. if [ -n "$binary_path" ]; then
  64. if [ ! -f "$binary_path" ]; then
  65. echo -e "${RED}Error: Binary not found at ${binary_path}${NC}"
  66. exit 1
  67. fi
  68. specific_version="local"
  69. else
  70. raw_os=$(uname -s)
  71. os=$(echo "$raw_os" | tr '[:upper:]' '[:lower:]')
  72. case "$raw_os" in
  73. Darwin*) os="darwin" ;;
  74. Linux*) os="linux" ;;
  75. MINGW*|MSYS*|CYGWIN*) os="windows" ;;
  76. esac
  77. arch=$(uname -m)
  78. if [[ "$arch" == "aarch64" ]]; then
  79. arch="arm64"
  80. fi
  81. if [[ "$arch" == "x86_64" ]]; then
  82. arch="x64"
  83. fi
  84. if [ "$os" = "darwin" ] && [ "$arch" = "x64" ]; then
  85. rosetta_flag=$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)
  86. if [ "$rosetta_flag" = "1" ]; then
  87. arch="arm64"
  88. fi
  89. fi
  90. combo="$os-$arch"
  91. case "$combo" in
  92. linux-x64|linux-arm64|darwin-x64|darwin-arm64|windows-x64)
  93. ;;
  94. *)
  95. echo -e "${RED}Unsupported OS/Arch: $os/$arch${NC}"
  96. exit 1
  97. ;;
  98. esac
  99. is_musl=false
  100. if [ "$os" = "linux" ]; then
  101. if [ -f /etc/alpine-release ]; then
  102. is_musl=true
  103. fi
  104. if command -v ldd >/dev/null 2>&1; then
  105. if ldd --version 2>&1 | grep -qi musl; then
  106. is_musl=true
  107. fi
  108. fi
  109. fi
  110. needs_baseline=false
  111. if [ "$arch" = "x64" ]; then
  112. if [ "$os" = "linux" ]; then
  113. if ! grep -qwi avx2 /proc/cpuinfo 2>/dev/null; then
  114. needs_baseline=true
  115. fi
  116. fi
  117. if [ "$os" = "darwin" ]; then
  118. avx2=$(sysctl -n hw.optional.avx2_0 2>/dev/null || echo 0)
  119. if [ "$avx2" != "1" ]; then
  120. needs_baseline=true
  121. fi
  122. fi
  123. if [ "$os" = "windows" ]; then
  124. ps="(Add-Type -MemberDefinition \"[DllImport(\"\"kernel32.dll\"\")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);\" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)"
  125. out=""
  126. if command -v powershell.exe >/dev/null 2>&1; then
  127. out=$(powershell.exe -NoProfile -NonInteractive -Command "$ps" 2>/dev/null || true)
  128. elif command -v pwsh >/dev/null 2>&1; then
  129. out=$(pwsh -NoProfile -NonInteractive -Command "$ps" 2>/dev/null || true)
  130. fi
  131. out=$(echo "$out" | tr -d '\r' | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
  132. if [ "$out" != "true" ] && [ "$out" != "1" ]; then
  133. needs_baseline=true
  134. fi
  135. fi
  136. fi
  137. target="$os-$arch"
  138. if [ "$needs_baseline" = "true" ]; then
  139. target="$target-baseline"
  140. fi
  141. if [ "$is_musl" = "true" ]; then
  142. target="$target-musl"
  143. fi
  144. if ! command -v tar >/dev/null 2>&1; then
  145. echo -e "${RED}Error: 'tar' is required but not installed.${NC}"
  146. exit 1
  147. fi
  148. if [ -z "$requested_version" ]; then
  149. metadata=$(curl -fsSL https://registry.npmjs.org/@opencode-ai%2fcli/next || true)
  150. specific_version=$(echo "$metadata" | sed -n 's/.*"version":"\([^"]*\)".*/\1/p')
  151. if [ -z "$specific_version" ]; then
  152. echo -e "${RED}Failed to fetch version information${NC}"
  153. exit 1
  154. fi
  155. else
  156. # Strip leading 'v' if present
  157. requested_version="${requested_version#v}"
  158. specific_version=$requested_version
  159. fi
  160. package_name="@opencode-ai/cli-$target"
  161. http_status=$(curl -s -o /dev/null -w "%{http_code}" "https://registry.npmjs.org/@opencode-ai%2fcli-$target/$specific_version" || true)
  162. if [ "$http_status" = "404" ]; then
  163. echo -e "${RED}Error: Version ${specific_version} is not available for $target${NC}"
  164. echo -e "${MUTED}Available versions: https://www.npmjs.com/package/$package_name?activeTab=versions${NC}"
  165. exit 1
  166. fi
  167. if [ "$http_status" != "200" ]; then
  168. echo -e "${RED}Failed to fetch package information${NC}"
  169. exit 1
  170. fi
  171. filename="cli-$target-$specific_version.tgz"
  172. url="https://registry.npmjs.org/$package_name/-/$filename"
  173. binary_name="$APP"
  174. if [ "$os" = "windows" ]; then
  175. binary_name="$APP.exe"
  176. fi
  177. fi
  178. print_message() {
  179. local level=$1
  180. local message=$2
  181. local color=""
  182. case $level in
  183. info) color="${NC}" ;;
  184. warning) color="${NC}" ;;
  185. error) color="${RED}" ;;
  186. esac
  187. echo -e "${color}${message}${NC}"
  188. }
  189. check_version() {
  190. if command -v "$APP" >/dev/null 2>&1; then
  191. opencode_path=$(which "$APP")
  192. ## Check the installed version
  193. installed_version=$("$APP" --version 2>/dev/null || echo "")
  194. installed_version="${installed_version##* }"
  195. installed_version="${installed_version#v}"
  196. if [[ "$installed_version" != "$specific_version" ]]; then
  197. print_message info "${MUTED}Installed version: ${NC}$installed_version."
  198. else
  199. print_message info "${MUTED}Version ${NC}$specific_version${MUTED} already installed"
  200. exit 0
  201. fi
  202. fi
  203. }
  204. unbuffered_sed() {
  205. if echo | sed -u -e "" >/dev/null 2>&1; then
  206. sed -nu "$@"
  207. elif echo | sed -l -e "" >/dev/null 2>&1; then
  208. sed -nl "$@"
  209. else
  210. local pad="$(printf "\n%512s" "")"
  211. sed -ne "s/$/\\${pad}/" "$@"
  212. fi
  213. }
  214. print_progress() {
  215. local bytes="$1"
  216. local length="$2"
  217. [ "$length" -gt 0 ] || return 0
  218. local width=50
  219. local percent=$(( bytes * 100 / length ))
  220. [ "$percent" -gt 100 ] && percent=100
  221. local on=$(( percent * width / 100 ))
  222. local off=$(( width - on ))
  223. local filled=$(printf "%*s" "$on" "")
  224. filled=${filled// /■}
  225. local empty=$(printf "%*s" "$off" "")
  226. empty=${empty// /・}
  227. printf "\r${ORANGE}%s%s %3d%%${NC}" "$filled" "$empty" "$percent" >&4
  228. }
  229. download_with_progress() {
  230. local url="$1"
  231. local output="$2"
  232. if [ -t 2 ]; then
  233. exec 4>&2
  234. else
  235. exec 4>/dev/null
  236. fi
  237. local tmp_dir=${TMPDIR:-/tmp}
  238. local basename="${tmp_dir}/opencode_install_$$"
  239. local tracefile="${basename}.trace"
  240. rm -f "$tracefile"
  241. mkfifo "$tracefile"
  242. # Hide cursor
  243. printf "\033[?25l" >&4
  244. trap "trap - RETURN; rm -f \"$tracefile\"; printf '\033[?25h' >&4; exec 4>&-" RETURN
  245. (
  246. curl --trace-ascii "$tracefile" -fsL -o "$output" "$url"
  247. ) &
  248. local curl_pid=$!
  249. unbuffered_sed \
  250. -e 'y/ACDEGHLNORTV/acdeghlnortv/' \
  251. -e '/^0000: content-length:/p' \
  252. -e '/^<= recv data/p' \
  253. "$tracefile" | \
  254. {
  255. local length=0
  256. local bytes=0
  257. while IFS=" " read -r -a line; do
  258. [ "${#line[@]}" -lt 2 ] && continue
  259. local tag="${line[0]} ${line[1]}"
  260. if [ "$tag" = "0000: content-length:" ]; then
  261. length="${line[2]}"
  262. length=$(echo "$length" | tr -d '\r')
  263. bytes=0
  264. elif [ "$tag" = "<= recv" ]; then
  265. local size="${line[3]}"
  266. bytes=$(( bytes + size ))
  267. if [ "$length" -gt 0 ]; then
  268. print_progress "$bytes" "$length"
  269. fi
  270. fi
  271. done
  272. }
  273. wait $curl_pid
  274. local ret=$?
  275. echo "" >&4
  276. return $ret
  277. }
  278. download_and_install() {
  279. print_message info "\n${MUTED}Installing ${NC}$APP ${MUTED}version: ${NC}$specific_version"
  280. local tmp_dir="${TMPDIR:-/tmp}/opencode_install_$$"
  281. mkdir -p "$tmp_dir"
  282. if [[ "$os" == "windows" ]] || ! [ -t 2 ] || ! download_with_progress "$url" "$tmp_dir/$filename"; then
  283. # Fallback to standard curl on Windows, non-TTY environments, or if custom progress fails
  284. curl -f -# -L -o "$tmp_dir/$filename" "$url"
  285. fi
  286. tar -xzf "$tmp_dir/$filename" -C "$tmp_dir"
  287. mv "$tmp_dir/package/bin/$binary_name" "$INSTALL_DIR"
  288. chmod 755 "${INSTALL_DIR}/$binary_name"
  289. rm -rf "$tmp_dir"
  290. }
  291. install_from_binary() {
  292. print_message info "\n${MUTED}Installing ${NC}$APP ${MUTED}from: ${NC}$binary_path"
  293. cp "$binary_path" "${INSTALL_DIR}/$APP"
  294. chmod 755 "${INSTALL_DIR}/$APP"
  295. }
  296. if [ -n "$binary_path" ]; then
  297. install_from_binary
  298. else
  299. check_version
  300. download_and_install
  301. fi
  302. add_to_path() {
  303. local config_file=$1
  304. local command=$2
  305. if grep -Fxq "$command" "$config_file"; then
  306. print_message info "Command already exists in $config_file, skipping write."
  307. elif [[ -w $config_file ]]; then
  308. echo -e "\n# opencode" >> "$config_file"
  309. echo "$command" >> "$config_file"
  310. print_message info "${MUTED}Successfully added ${NC}opencode ${MUTED}to \$PATH in ${NC}$config_file"
  311. else
  312. print_message warning "Manually add the directory to $config_file (or similar):"
  313. print_message info " $command"
  314. fi
  315. }
  316. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  317. current_shell=$(basename "$SHELL")
  318. case $current_shell in
  319. fish)
  320. config_files="$HOME/.config/fish/config.fish"
  321. ;;
  322. zsh)
  323. config_files="${ZDOTDIR:-$HOME}/.zshrc ${ZDOTDIR:-$HOME}/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  324. ;;
  325. bash)
  326. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  327. ;;
  328. ash)
  329. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  330. ;;
  331. sh)
  332. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  333. ;;
  334. *)
  335. # Default case if none of the above matches
  336. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  337. ;;
  338. esac
  339. if [[ "$no_modify_path" != "true" ]]; then
  340. config_file=""
  341. for file in $config_files; do
  342. if [[ -f $file ]]; then
  343. config_file=$file
  344. break
  345. fi
  346. done
  347. if [[ -z $config_file ]]; then
  348. print_message warning "No config file found for $current_shell. You may need to manually add to PATH:"
  349. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  350. elif [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  351. case $current_shell in
  352. fish)
  353. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  354. ;;
  355. zsh)
  356. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  357. ;;
  358. bash)
  359. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  360. ;;
  361. ash)
  362. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  363. ;;
  364. sh)
  365. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  366. ;;
  367. *)
  368. export PATH=$INSTALL_DIR:$PATH
  369. print_message warning "Manually add the directory to $config_file (or similar):"
  370. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  371. ;;
  372. esac
  373. fi
  374. fi
  375. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  376. echo "$INSTALL_DIR" >> $GITHUB_PATH
  377. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  378. fi
  379. echo -e ""
  380. echo -e "${MUTED}  ${NC} ▄ "
  381. echo -e "${MUTED}█▀▀█ █▀▀█ █▀▀█ █▀▀▄ ${NC}█▀▀▀ █▀▀█ █▀▀█ █▀▀█"
  382. echo -e "${MUTED}█░░█ █░░█ █▀▀▀ █░░█ ${NC}█░░░ █░░█ █░░█ █▀▀▀"
  383. echo -e "${MUTED}▀▀▀▀ █▀▀▀ ▀▀▀▀ ▀ ▀ ${NC}▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀"
  384. echo -e ""
  385. echo -e ""
  386. echo -e "${MUTED}OpenCode includes free models, to start:${NC}"
  387. echo -e ""
  388. echo -e "cd <project> ${MUTED}# Open directory${NC}"
  389. echo -e "opencode2 ${MUTED}# Run command${NC}"
  390. echo -e ""
  391. echo -e "${MUTED}For more information visit ${NC}https://opencode.ai/v2/docs"
  392. echo -e ""
  393. echo -e ""