install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP=opencode
  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., 1.0.180)
  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://opencode.ai/install | bash
  19. curl -fsSL https://opencode.ai/install | bash -s -- --version 1.0.180
  20. ./install --binary /path/to/opencode
  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. archive_ext=".zip"
  100. if [ "$os" = "linux" ]; then
  101. archive_ext=".tar.gz"
  102. fi
  103. is_musl=false
  104. if [ "$os" = "linux" ]; then
  105. if [ -f /etc/alpine-release ]; then
  106. is_musl=true
  107. fi
  108. if command -v ldd >/dev/null 2>&1; then
  109. if ldd --version 2>&1 | grep -qi musl; then
  110. is_musl=true
  111. fi
  112. fi
  113. fi
  114. needs_baseline=false
  115. if [ "$arch" = "x64" ]; then
  116. if [ "$os" = "linux" ]; then
  117. if ! grep -qi avx2 /proc/cpuinfo 2>/dev/null; then
  118. needs_baseline=true
  119. fi
  120. fi
  121. if [ "$os" = "darwin" ]; then
  122. avx2=$(sysctl -n hw.optional.avx2_0 2>/dev/null || echo 0)
  123. if [ "$avx2" != "1" ]; then
  124. needs_baseline=true
  125. fi
  126. fi
  127. fi
  128. target="$os-$arch"
  129. if [ "$needs_baseline" = "true" ]; then
  130. target="$target-baseline"
  131. fi
  132. if [ "$is_musl" = "true" ]; then
  133. target="$target-musl"
  134. fi
  135. filename="$APP-$target$archive_ext"
  136. if [ "$os" = "linux" ]; then
  137. if ! command -v tar >/dev/null 2>&1; then
  138. echo -e "${RED}Error: 'tar' is required but not installed.${NC}"
  139. exit 1
  140. fi
  141. else
  142. if ! command -v unzip >/dev/null 2>&1; then
  143. echo -e "${RED}Error: 'unzip' is required but not installed.${NC}"
  144. exit 1
  145. fi
  146. fi
  147. if [ -z "$requested_version" ]; then
  148. url="https://github.com/anomalyco/opencode/releases/latest/download/$filename"
  149. specific_version=$(curl -s https://api.github.com/repos/anomalyco/opencode/releases/latest | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
  150. if [[ $? -ne 0 || -z "$specific_version" ]]; then
  151. echo -e "${RED}Failed to fetch version information${NC}"
  152. exit 1
  153. fi
  154. else
  155. # Strip leading 'v' if present
  156. requested_version="${requested_version#v}"
  157. url="https://github.com/anomalyco/opencode/releases/download/v${requested_version}/$filename"
  158. specific_version=$requested_version
  159. # Verify the release exists before downloading
  160. http_status=$(curl -sI -o /dev/null -w "%{http_code}" "https://github.com/anomalyco/opencode/releases/tag/v${requested_version}")
  161. if [ "$http_status" = "404" ]; then
  162. echo -e "${RED}Error: Release v${requested_version} not found${NC}"
  163. echo -e "${MUTED}Available releases: https://github.com/anomalyco/opencode/releases${NC}"
  164. exit 1
  165. fi
  166. fi
  167. fi
  168. print_message() {
  169. local level=$1
  170. local message=$2
  171. local color=""
  172. case $level in
  173. info) color="${NC}" ;;
  174. warning) color="${NC}" ;;
  175. error) color="${RED}" ;;
  176. esac
  177. echo -e "${color}${message}${NC}"
  178. }
  179. check_version() {
  180. if command -v opencode >/dev/null 2>&1; then
  181. opencode_path=$(which opencode)
  182. ## Check the installed version
  183. installed_version=$(opencode --version 2>/dev/null || echo "")
  184. if [[ "$installed_version" != "$specific_version" ]]; then
  185. print_message info "${MUTED}Installed version: ${NC}$installed_version."
  186. else
  187. print_message info "${MUTED}Version ${NC}$specific_version${MUTED} already installed"
  188. exit 0
  189. fi
  190. fi
  191. }
  192. unbuffered_sed() {
  193. if echo | sed -u -e "" >/dev/null 2>&1; then
  194. sed -nu "$@"
  195. elif echo | sed -l -e "" >/dev/null 2>&1; then
  196. sed -nl "$@"
  197. else
  198. local pad="$(printf "\n%512s" "")"
  199. sed -ne "s/$/\\${pad}/" "$@"
  200. fi
  201. }
  202. print_progress() {
  203. local bytes="$1"
  204. local length="$2"
  205. [ "$length" -gt 0 ] || return 0
  206. local width=50
  207. local percent=$(( bytes * 100 / length ))
  208. [ "$percent" -gt 100 ] && percent=100
  209. local on=$(( percent * width / 100 ))
  210. local off=$(( width - on ))
  211. local filled=$(printf "%*s" "$on" "")
  212. filled=${filled// /■}
  213. local empty=$(printf "%*s" "$off" "")
  214. empty=${empty// /・}
  215. printf "\r${ORANGE}%s%s %3d%%${NC}" "$filled" "$empty" "$percent" >&4
  216. }
  217. download_with_progress() {
  218. local url="$1"
  219. local output="$2"
  220. if [ -t 2 ]; then
  221. exec 4>&2
  222. else
  223. exec 4>/dev/null
  224. fi
  225. local tmp_dir=${TMPDIR:-/tmp}
  226. local basename="${tmp_dir}/opencode_install_$$"
  227. local tracefile="${basename}.trace"
  228. rm -f "$tracefile"
  229. mkfifo "$tracefile"
  230. # Hide cursor
  231. printf "\033[?25l" >&4
  232. trap "trap - RETURN; rm -f \"$tracefile\"; printf '\033[?25h' >&4; exec 4>&-" RETURN
  233. (
  234. curl --trace-ascii "$tracefile" -s -L -o "$output" "$url"
  235. ) &
  236. local curl_pid=$!
  237. unbuffered_sed \
  238. -e 'y/ACDEGHLNORTV/acdeghlnortv/' \
  239. -e '/^0000: content-length:/p' \
  240. -e '/^<= recv data/p' \
  241. "$tracefile" | \
  242. {
  243. local length=0
  244. local bytes=0
  245. while IFS=" " read -r -a line; do
  246. [ "${#line[@]}" -lt 2 ] && continue
  247. local tag="${line[0]} ${line[1]}"
  248. if [ "$tag" = "0000: content-length:" ]; then
  249. length="${line[2]}"
  250. length=$(echo "$length" | tr -d '\r')
  251. bytes=0
  252. elif [ "$tag" = "<= recv" ]; then
  253. local size="${line[3]}"
  254. bytes=$(( bytes + size ))
  255. if [ "$length" -gt 0 ]; then
  256. print_progress "$bytes" "$length"
  257. fi
  258. fi
  259. done
  260. }
  261. wait $curl_pid
  262. local ret=$?
  263. echo "" >&4
  264. return $ret
  265. }
  266. download_and_install() {
  267. print_message info "\n${MUTED}Installing ${NC}opencode ${MUTED}version: ${NC}$specific_version"
  268. local tmp_dir="${TMPDIR:-/tmp}/opencode_install_$$"
  269. mkdir -p "$tmp_dir"
  270. if [[ "$os" == "windows" ]] || ! [ -t 2 ] || ! download_with_progress "$url" "$tmp_dir/$filename"; then
  271. # Fallback to standard curl on Windows, non-TTY environments, or if custom progress fails
  272. curl -# -L -o "$tmp_dir/$filename" "$url"
  273. fi
  274. if [ "$os" = "linux" ]; then
  275. tar -xzf "$tmp_dir/$filename" -C "$tmp_dir"
  276. else
  277. unzip -q "$tmp_dir/$filename" -d "$tmp_dir"
  278. fi
  279. mv "$tmp_dir/opencode" "$INSTALL_DIR"
  280. chmod 755 "${INSTALL_DIR}/opencode"
  281. rm -rf "$tmp_dir"
  282. }
  283. install_from_binary() {
  284. print_message info "\n${MUTED}Installing ${NC}opencode ${MUTED}from: ${NC}$binary_path"
  285. cp "$binary_path" "${INSTALL_DIR}/opencode"
  286. chmod 755 "${INSTALL_DIR}/opencode"
  287. }
  288. if [ -n "$binary_path" ]; then
  289. install_from_binary
  290. else
  291. check_version
  292. download_and_install
  293. fi
  294. add_to_path() {
  295. local config_file=$1
  296. local command=$2
  297. if grep -Fxq "$command" "$config_file"; then
  298. print_message info "Command already exists in $config_file, skipping write."
  299. elif [[ -w $config_file ]]; then
  300. echo -e "\n# opencode" >> "$config_file"
  301. echo "$command" >> "$config_file"
  302. print_message info "${MUTED}Successfully added ${NC}opencode ${MUTED}to \$PATH in ${NC}$config_file"
  303. else
  304. print_message warning "Manually add the directory to $config_file (or similar):"
  305. print_message info " $command"
  306. fi
  307. }
  308. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  309. current_shell=$(basename "$SHELL")
  310. case $current_shell in
  311. fish)
  312. config_files="$HOME/.config/fish/config.fish"
  313. ;;
  314. zsh)
  315. config_files="${ZDOTDIR:-$HOME}/.zshrc ${ZDOTDIR:-$HOME}/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  316. ;;
  317. bash)
  318. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  319. ;;
  320. ash)
  321. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  322. ;;
  323. sh)
  324. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  325. ;;
  326. *)
  327. # Default case if none of the above matches
  328. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  329. ;;
  330. esac
  331. if [[ "$no_modify_path" != "true" ]]; then
  332. config_file=""
  333. for file in $config_files; do
  334. if [[ -f $file ]]; then
  335. config_file=$file
  336. break
  337. fi
  338. done
  339. if [[ -z $config_file ]]; then
  340. print_message warning "No config file found for $current_shell. You may need to manually add to PATH:"
  341. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  342. elif [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  343. case $current_shell in
  344. fish)
  345. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  346. ;;
  347. zsh)
  348. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  349. ;;
  350. bash)
  351. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  352. ;;
  353. ash)
  354. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  355. ;;
  356. sh)
  357. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  358. ;;
  359. *)
  360. export PATH=$INSTALL_DIR:$PATH
  361. print_message warning "Manually add the directory to $config_file (or similar):"
  362. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  363. ;;
  364. esac
  365. fi
  366. fi
  367. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  368. echo "$INSTALL_DIR" >> $GITHUB_PATH
  369. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  370. fi
  371. echo -e ""
  372. echo -e "${MUTED}  ${NC} ▄ "
  373. echo -e "${MUTED}█▀▀█ █▀▀█ █▀▀█ █▀▀▄ ${NC}█▀▀▀ █▀▀█ █▀▀█ █▀▀█"
  374. echo -e "${MUTED}█░░█ █░░█ █▀▀▀ █░░█ ${NC}█░░░ █░░█ █░░█ █▀▀▀"
  375. echo -e "${MUTED}▀▀▀▀ █▀▀▀ ▀▀▀▀ ▀ ▀ ${NC}▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀"
  376. echo -e ""
  377. echo -e ""
  378. echo -e "${MUTED}OpenCode includes free models, to start:${NC}"
  379. echo -e ""
  380. echo -e "cd <project> ${MUTED}# Open directory${NC}"
  381. echo -e "opencode ${MUTED}# Run command${NC}"
  382. echo -e ""
  383. echo -e "${MUTED}For more information visit ${NC}https://opencode.ai/docs"
  384. echo -e ""
  385. echo -e ""