bun_install.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. platform=$(uname -ms)
  4. if [[ ${OS:-} = Windows_NT ]]; then
  5. if [[ $platform != MINGW64* ]]; then
  6. powershell -c "irm bun.sh/install.ps1|iex"
  7. exit $?
  8. fi
  9. fi
  10. # Reset
  11. Color_Off=''
  12. # Regular Colors
  13. Red=''
  14. Green=''
  15. Dim='' # White
  16. # Bold
  17. Bold_White=''
  18. Bold_Green=''
  19. if [[ -t 1 ]]; then
  20. # Reset
  21. Color_Off='\033[0m' # Text Reset
  22. # Regular Colors
  23. Red='\033[0;31m' # Red
  24. Green='\033[0;32m' # Green
  25. Dim='\033[0;2m' # White
  26. # Bold
  27. Bold_Green='\033[1;32m' # Bold Green
  28. Bold_White='\033[1m' # Bold White
  29. fi
  30. error() {
  31. echo -e "${Red}error${Color_Off}:" "$@" >&2
  32. exit 1
  33. }
  34. info() {
  35. echo -e "${Dim}$@ ${Color_Off}"
  36. }
  37. info_bold() {
  38. echo -e "${Bold_White}$@ ${Color_Off}"
  39. }
  40. success() {
  41. echo -e "${Green}$@ ${Color_Off}"
  42. }
  43. command -v unzip >/dev/null ||
  44. error 'unzip is required to install bun'
  45. if [[ $# -gt 2 ]]; then
  46. error 'Too many arguments, only 2 are allowed. The first can be a specific tag of bun to install. (e.g. "bun-v0.1.4") The second can be a build variant of bun to install. (e.g. "debug-info")'
  47. fi
  48. case $platform in
  49. 'Darwin x86_64')
  50. target=darwin-x64
  51. ;;
  52. 'Darwin arm64')
  53. target=darwin-aarch64
  54. ;;
  55. 'Linux aarch64' | 'Linux arm64')
  56. target=linux-aarch64
  57. ;;
  58. 'MINGW64'*)
  59. target=windows-x64
  60. ;;
  61. 'Linux x86_64' | *)
  62. target=linux-x64
  63. ;;
  64. esac
  65. if [[ $target = darwin-x64 ]]; then
  66. # Is this process running in Rosetta?
  67. # redirect stderr to devnull to avoid error message when not running in Rosetta
  68. if [[ $(sysctl -n sysctl.proc_translated 2>/dev/null) = 1 ]]; then
  69. target=darwin-aarch64
  70. info "Your shell is running in Rosetta 2. Downloading bun for $target instead"
  71. fi
  72. fi
  73. GITHUB=${GITHUB-"https://github.com"}
  74. github_repo="$GITHUB/oven-sh/bun"
  75. if [[ $target = darwin-x64 ]]; then
  76. # If AVX2 isn't supported, use the -baseline build
  77. if [[ $(sysctl -a | grep machdep.cpu | grep AVX2) == '' ]]; then
  78. target=darwin-x64-baseline
  79. fi
  80. fi
  81. if [[ $target = linux-x64 ]]; then
  82. # If AVX2 isn't supported, use the -baseline build
  83. if [[ $(cat /proc/cpuinfo | grep avx2) = '' ]]; then
  84. target=linux-x64-baseline
  85. fi
  86. fi
  87. exe_name=bun
  88. if [[ $# = 2 && $2 = debug-info ]]; then
  89. target=$target-profile
  90. exe_name=bun-profile
  91. info "You requested a debug build of bun. More information will be shown if a crash occurs."
  92. fi
  93. if [[ $# = 0 ]]; then
  94. bun_uri=$github_repo/releases/latest/download/bun-$target.zip
  95. else
  96. bun_uri=$github_repo/releases/download/$1/bun-$target.zip
  97. fi
  98. install_env=BUN_INSTALL
  99. bin_env=\$$install_env/bin
  100. install_dir=${!install_env:-$HOME/.bun}
  101. bin_dir=$install_dir/bin
  102. exe=$bin_dir/bun
  103. if [[ ! -d $bin_dir ]]; then
  104. mkdir -p "$bin_dir" ||
  105. error "Failed to create install directory \"$bin_dir\""
  106. fi
  107. curl --fail --location --progress-bar --output "$exe.zip" "$bun_uri" ||
  108. error "Failed to download bun from \"$bun_uri\""
  109. unzip -oqd "$bin_dir" "$exe.zip" ||
  110. error 'Failed to extract bun'
  111. mv "$bin_dir/bun-$target/$exe_name" "$exe" ||
  112. error 'Failed to move extracted bun to destination'
  113. chmod +x "$exe" ||
  114. error 'Failed to set permissions on bun executable'
  115. rm -r "$bin_dir/bun-$target" "$exe.zip"
  116. tildify() {
  117. if [[ $1 = $HOME/* ]]; then
  118. local replacement=\~/
  119. echo "${1/$HOME\//$replacement}"
  120. else
  121. echo "$1"
  122. fi
  123. }
  124. success "bun was installed successfully to $Bold_Green$(tildify "$exe")"
  125. if command -v bun >/dev/null; then
  126. # Install completions, but we don't care if it fails
  127. IS_BUN_AUTO_UPDATE=true $exe completions &>/dev/null || :
  128. echo "Run 'bun --help' to get started"
  129. exit
  130. fi
  131. refresh_command=''
  132. tilde_bin_dir=$(tildify "$bin_dir")
  133. quoted_install_dir=\"${install_dir//\"/\\\"}\"
  134. if [[ $quoted_install_dir = \"$HOME/* ]]; then
  135. quoted_install_dir=${quoted_install_dir/$HOME\//\$HOME/}
  136. fi
  137. echo
  138. case $(basename "$SHELL") in
  139. fish)
  140. # Install completions, but we don't care if it fails
  141. IS_BUN_AUTO_UPDATE=true SHELL=fish $exe completions &>/dev/null || :
  142. commands=(
  143. "set --export $install_env $quoted_install_dir"
  144. "set --export PATH $bin_env \$PATH"
  145. )
  146. fish_config=$HOME/.config/fish/config.fish
  147. tilde_fish_config=$(tildify "$fish_config")
  148. if [[ -w $fish_config ]]; then
  149. {
  150. echo -e '\n# bun'
  151. for command in "${commands[@]}"; do
  152. echo "$command"
  153. done
  154. } >>"$fish_config"
  155. info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_fish_config\""
  156. refresh_command="source $tilde_fish_config"
  157. else
  158. echo "Manually add the directory to $tilde_fish_config (or similar):"
  159. for command in "${commands[@]}"; do
  160. info_bold " $command"
  161. done
  162. fi
  163. ;;
  164. zsh)
  165. # Install completions, but we don't care if it fails
  166. IS_BUN_AUTO_UPDATE=true SHELL=zsh $exe completions &>/dev/null || :
  167. commands=(
  168. "export $install_env=$quoted_install_dir"
  169. "export PATH=\"$bin_env:\$PATH\""
  170. )
  171. zsh_config=$HOME/.zshrc
  172. tilde_zsh_config=$(tildify "$zsh_config")
  173. if [[ -w $zsh_config ]]; then
  174. {
  175. echo -e '\n# bun'
  176. for command in "${commands[@]}"; do
  177. echo "$command"
  178. done
  179. } >>"$zsh_config"
  180. info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_zsh_config\""
  181. refresh_command="exec $SHELL"
  182. else
  183. echo "Manually add the directory to $tilde_zsh_config (or similar):"
  184. for command in "${commands[@]}"; do
  185. info_bold " $command"
  186. done
  187. fi
  188. ;;
  189. bash)
  190. # Install completions, but we don't care if it fails
  191. IS_BUN_AUTO_UPDATE=true SHELL=bash $exe completions &>/dev/null || :
  192. commands=(
  193. "export $install_env=$quoted_install_dir"
  194. "export PATH=\"$bin_env:\$PATH\""
  195. )
  196. bash_configs=(
  197. "$HOME/.bashrc"
  198. "$HOME/.bash_profile"
  199. )
  200. if [[ ${XDG_CONFIG_HOME:-} ]]; then
  201. bash_configs+=(
  202. "$XDG_CONFIG_HOME/.bash_profile"
  203. "$XDG_CONFIG_HOME/.bashrc"
  204. "$XDG_CONFIG_HOME/bash_profile"
  205. "$XDG_CONFIG_HOME/bashrc"
  206. )
  207. fi
  208. set_manually=true
  209. for bash_config in "${bash_configs[@]}"; do
  210. tilde_bash_config=$(tildify "$bash_config")
  211. if [[ -w $bash_config ]]; then
  212. {
  213. echo -e '\n# bun'
  214. for command in "${commands[@]}"; do
  215. echo "$command"
  216. done
  217. } >>"$bash_config"
  218. info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_bash_config\""
  219. refresh_command="source $bash_config"
  220. set_manually=false
  221. break
  222. fi
  223. done
  224. if [[ $set_manually = true ]]; then
  225. echo "Manually add the directory to $tilde_bash_config (or similar):"
  226. for command in "${commands[@]}"; do
  227. info_bold " $command"
  228. done
  229. fi
  230. ;;
  231. *)
  232. echo 'Manually add the directory to ~/.bashrc (or similar):'
  233. info_bold " export $install_env=$quoted_install_dir"
  234. info_bold " export PATH=\"$bin_env:\$PATH\""
  235. ;;
  236. esac
  237. echo
  238. info "To get started, run:"
  239. echo
  240. if [[ $refresh_command ]]; then
  241. info_bold " $refresh_command"
  242. fi
  243. info_bold " bun --help"