bun_install.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. case "$target" in
  66. 'linux'*)
  67. if [ -f /etc/alpine-release ]; then
  68. target="$target-musl"
  69. fi
  70. ;;
  71. esac
  72. if [[ $target = darwin-x64 ]]; then
  73. # Is this process running in Rosetta?
  74. # redirect stderr to devnull to avoid error message when not running in Rosetta
  75. if [[ $(sysctl -n sysctl.proc_translated 2>/dev/null) = 1 ]]; then
  76. target=darwin-aarch64
  77. info "Your shell is running in Rosetta 2. Downloading bun for $target instead"
  78. fi
  79. fi
  80. GITHUB=${GITHUB-"https://github.com"}
  81. github_repo="$GITHUB/oven-sh/bun"
  82. # If AVX2 isn't supported, use the -baseline build
  83. case "$target" in
  84. 'darwin-x64'*)
  85. if [[ $(sysctl -a | grep machdep.cpu | grep AVX2) == '' ]]; then
  86. target="$target-baseline"
  87. fi
  88. ;;
  89. 'linux-x64'*)
  90. # If AVX2 isn't supported, use the -baseline build
  91. if [[ $(cat /proc/cpuinfo | grep avx2) = '' ]]; then
  92. target="$target-baseline"
  93. fi
  94. ;;
  95. esac
  96. exe_name=bun
  97. if [[ $# = 2 && $2 = debug-info ]]; then
  98. target=$target-profile
  99. exe_name=bun-profile
  100. info "You requested a debug build of bun. More information will be shown if a crash occurs."
  101. fi
  102. bun_version=BUN_VERSION
  103. if [[ $# = 0 ]]; then
  104. bun_uri=$github_repo/releases/download/bun-v$bun_version/bun-$target.zip
  105. else
  106. bun_uri=$github_repo/releases/download/$1/bun-$target.zip
  107. fi
  108. install_env=BUN_INSTALL
  109. bin_env=\$$install_env/bin
  110. install_dir=${!install_env:-$HOME/.bun}
  111. bin_dir=$install_dir/bin
  112. exe=$bin_dir/bun
  113. if [[ ! -d $bin_dir ]]; then
  114. mkdir -p "$bin_dir" ||
  115. error "Failed to create install directory \"$bin_dir\""
  116. fi
  117. curl --fail --location --progress-bar --output "$exe.zip" "$bun_uri" ||
  118. error "Failed to download bun from \"$bun_uri\""
  119. unzip -oqd "$bin_dir" "$exe.zip" ||
  120. error 'Failed to extract bun'
  121. mv "$bin_dir/bun-$target/$exe_name" "$exe" ||
  122. error 'Failed to move extracted bun to destination'
  123. chmod +x "$exe" ||
  124. error 'Failed to set permissions on bun executable'
  125. rm -r "$bin_dir/bun-$target" "$exe.zip"
  126. tildify() {
  127. if [[ $1 = $HOME/* ]]; then
  128. local replacement=\~/
  129. echo "${1/$HOME\//$replacement}"
  130. else
  131. echo "$1"
  132. fi
  133. }
  134. success "bun was installed successfully to $Bold_Green$(tildify "$exe")"
  135. if command -v bun >/dev/null; then
  136. # Install completions, but we don't care if it fails
  137. IS_BUN_AUTO_UPDATE=true $exe completions &>/dev/null || :
  138. echo "Run 'bun --help' to get started"
  139. exit
  140. fi
  141. refresh_command=''
  142. tilde_bin_dir=$(tildify "$bin_dir")
  143. quoted_install_dir=\"${install_dir//\"/\\\"}\"
  144. if [[ $quoted_install_dir = \"$HOME/* ]]; then
  145. quoted_install_dir=${quoted_install_dir/$HOME\//\$HOME/}
  146. fi
  147. echo
  148. case $(basename "$SHELL") in
  149. fish)
  150. # Install completions, but we don't care if it fails
  151. IS_BUN_AUTO_UPDATE=true SHELL=fish $exe completions &>/dev/null || :
  152. commands=(
  153. "set --export $install_env $quoted_install_dir"
  154. "set --export PATH $bin_env \$PATH"
  155. )
  156. fish_config=$HOME/.config/fish/config.fish
  157. tilde_fish_config=$(tildify "$fish_config")
  158. if [[ -w $fish_config ]]; then
  159. {
  160. echo -e '\n# bun'
  161. for command in "${commands[@]}"; do
  162. echo "$command"
  163. done
  164. } >>"$fish_config"
  165. info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_fish_config\""
  166. refresh_command="source $tilde_fish_config"
  167. else
  168. echo "Manually add the directory to $tilde_fish_config (or similar):"
  169. for command in "${commands[@]}"; do
  170. info_bold " $command"
  171. done
  172. fi
  173. ;;
  174. zsh)
  175. # Install completions, but we don't care if it fails
  176. IS_BUN_AUTO_UPDATE=true SHELL=zsh $exe completions &>/dev/null || :
  177. commands=(
  178. "export $install_env=$quoted_install_dir"
  179. "export PATH=\"$bin_env:\$PATH\""
  180. )
  181. zsh_config=$HOME/.zshrc
  182. tilde_zsh_config=$(tildify "$zsh_config")
  183. if [[ -w $zsh_config ]]; then
  184. {
  185. echo -e '\n# bun'
  186. for command in "${commands[@]}"; do
  187. echo "$command"
  188. done
  189. } >>"$zsh_config"
  190. info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_zsh_config\""
  191. refresh_command="exec $SHELL"
  192. else
  193. echo "Manually add the directory to $tilde_zsh_config (or similar):"
  194. for command in "${commands[@]}"; do
  195. info_bold " $command"
  196. done
  197. fi
  198. ;;
  199. bash)
  200. # Install completions, but we don't care if it fails
  201. IS_BUN_AUTO_UPDATE=true SHELL=bash $exe completions &>/dev/null || :
  202. commands=(
  203. "export $install_env=$quoted_install_dir"
  204. "export PATH=\"$bin_env:\$PATH\""
  205. )
  206. bash_configs=(
  207. "$HOME/.bashrc"
  208. "$HOME/.bash_profile"
  209. )
  210. if [[ ${XDG_CONFIG_HOME:-} ]]; then
  211. bash_configs+=(
  212. "$XDG_CONFIG_HOME/.bash_profile"
  213. "$XDG_CONFIG_HOME/.bashrc"
  214. "$XDG_CONFIG_HOME/bash_profile"
  215. "$XDG_CONFIG_HOME/bashrc"
  216. )
  217. fi
  218. set_manually=true
  219. for bash_config in "${bash_configs[@]}"; do
  220. tilde_bash_config=$(tildify "$bash_config")
  221. if [[ -w $bash_config ]]; then
  222. {
  223. echo -e '\n# bun'
  224. for command in "${commands[@]}"; do
  225. echo "$command"
  226. done
  227. } >>"$bash_config"
  228. info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_bash_config\""
  229. refresh_command="source $bash_config"
  230. set_manually=false
  231. break
  232. fi
  233. done
  234. if [[ $set_manually = true ]]; then
  235. echo "Manually add the directory to $tilde_bash_config (or similar):"
  236. for command in "${commands[@]}"; do
  237. info_bold " $command"
  238. done
  239. fi
  240. ;;
  241. *)
  242. echo 'Manually add the directory to ~/.bashrc (or similar):'
  243. info_bold " export $install_env=$quoted_install_dir"
  244. info_bold " export PATH=\"$bin_env:\$PATH\""
  245. ;;
  246. esac
  247. echo
  248. info "To get started, run:"
  249. echo
  250. if [[ $refresh_command ]]; then
  251. info_bold " $refresh_command"
  252. fi
  253. info_bold " bun --help"