Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

安装:1、下载脚本

  1. curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh >> brew_install

  替换脚本内容:

本地执行之前需要将BREW_REPO = "https://github.com/Homebrew/brew"替换成以下内容

BREW_REPO=“https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git”

实际上,我是将 https://github.com/Homebrew/brew 全部替换为:https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

附上文件内容:

  1. #!/bin/bash
  2. set -u
  3.  
  4. # Check if script is run non-interactively (e.g. CI)
  5. # If it is run non-interactively we should not prompt for passwords.
  6. if [[ ! -t 0 || -n "${CI-}" ]]; then
  7. NONINTERACTIVE=1
  8. fi
  9.  
  10. abort() {
  11. printf "%s\n" "$1"
  12. exit 1
  13. }
  14.  
  15. # First check OS.
  16. OS="$(uname)"
  17. if [[ "$OS" == "Linux" ]]; then
  18. HOMEBREW_ON_LINUX=1
  19. elif [[ "$OS" != "Darwin" ]]; then
  20. abort "Homebrew is only supported on macOS and Linux."
  21. fi
  22.  
  23. UNAME_MACHINE="$(uname -m)"
  24.  
  25. # Required installation paths. To install elsewhere (which is unsupported)
  26. # you can untar https://mirrors.tunA.tsinghua.edu.cn/git/homebrew/brew.git/tarball/master
  27. # anywhere you like.
  28. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  29. if [[ "$UNAME_MACHINE" == "arm64" ]]; then
  30. # On ARM macOS, this script installs to /opt/homebrew only
  31. HOMEBREW_PREFIX="/opt/homebrew"
  32. HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}"
  33. else
  34. # On Intel macOS, this script installs to /usr/local only
  35. HOMEBREW_PREFIX="/usr/local"
  36. HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
  37. fi
  38. HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"
  39. HOMEBREW_CORE_GIT_REMOTE="https://github.com/Homebrew/homebrew-core"
  40.  
  41. STAT="stat -f"
  42. CHOWN="/usr/sbin/chown"
  43. CHGRP="/usr/bin/chgrp"
  44. GROUP="admin"
  45. TOUCH="/usr/bin/touch"
  46. else
  47. # On Linux, it installs to /home/linuxbrew/.linuxbrew if you have sudo access
  48. # and ~/.linuxbrew (which is unsupported) if run interactively.
  49. HOMEBREW_PREFIX_DEFAULT="/home/linuxbrew/.linuxbrew"
  50. HOMEBREW_CACHE="${HOME}/.cache/Homebrew"
  51. HOMEBREW_CORE_GIT_REMOTE="https://github.com/Homebrew/linuxbrew-core"
  52.  
  53. STAT="stat --printf"
  54. CHOWN="/bin/chown"
  55. CHGRP="/bin/chgrp"
  56. GROUP="$(id -gn)"
  57. TOUCH="/bin/touch"
  58. fi
  59. HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tunA.tsinghua.edu.cn/git/homebrew/brew.git"
  60.  
  61. # TODO: bump version when new macOS is released or announced
  62. MACOS_NEWEST_UNSUPPORTED="12.0"
  63. # TODO: bump version when new macOS is released
  64. MACOS_OLDEST_SUPPORTED="10.14"
  65.  
  66. # For Homebrew on Linux
  67. REQUIRED_RUBY_VERSION=2.6 # https://mirrors.tunA.tsinghua.edu.cn/git/homebrew/brew.git/pull/6556
  68. REQUIRED_GLIBC_VERSION=2.13 # https://docs.brew.sh/Homebrew-on-Linux#requirements
  69.  
  70. # no analytics during installation
  71. export HOMEBREW_NO_ANALYTICS_THIS_RUN=1
  72. export HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT=1
  73.  
  74. # string formatters
  75. if [[ -t 1 ]]; then
  76. tty_escape() { printf "\033[%sm" "$1"; }
  77. else
  78. tty_escape() { :; }
  79. fi
  80. tty_mkbold() { tty_escape "1;$1"; }
  81. tty_underline="$(tty_escape "4;39")"
  82. tty_blue="$(tty_mkbold 34)"
  83. tty_red="$(tty_mkbold 31)"
  84. tty_bold="$(tty_mkbold 39)"
  85. tty_reset="$(tty_escape 0)"
  86.  
  87. have_sudo_access() {
  88. local -a args
  89. if [[ -n "${SUDO_ASKPASS-}" ]]; then
  90. args=("-A")
  91. elif [[ -n "${NONINTERACTIVE-}" ]]; then
  92. args=("-n")
  93. fi
  94.  
  95. if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
  96. if [[ -n "${args[*]-}" ]]; then
  97. SUDO="/usr/bin/sudo ${args[*]}"
  98. else
  99. SUDO="/usr/bin/sudo"
  100. fi
  101. if [[ -n "${NONINTERACTIVE-}" ]]; then
  102. ${SUDO} -l mkdir &>/dev/null
  103. else
  104. ${SUDO} -v && ${SUDO} -l mkdir &>/dev/null
  105. fi
  106. HAVE_SUDO_ACCESS="$?"
  107. fi
  108.  
  109. if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
  110. abort "Need sudo access on macOS (e.g. the user $USER to be an Administrator)!"
  111. fi
  112.  
  113. return "$HAVE_SUDO_ACCESS"
  114. }
  115.  
  116. shell_join() {
  117. local arg
  118. printf "%s" "$1"
  119. shift
  120. for arg in "$@"; do
  121. printf " "
  122. printf "%s" "${arg// /\ }"
  123. done
  124. }
  125.  
  126. chomp() {
  127. printf "%s" "${1/"$\'\n\'"/}"
  128. }
  129.  
  130. ohai() {
  131. printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
  132. }
  133.  
  134. warn() {
  135. printf "${tty_red}Warning${tty_reset}: %s\n" "$(chomp "$1")"
  136. }
  137.  
  138. execute() {
  139. if ! "$@"; then
  140. abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
  141. fi
  142. }
  143.  
  144. execute_sudo() {
  145. local -a args=("$@")
  146. if have_sudo_access; then
  147. if [[ -n "${SUDO_ASKPASS-}" ]]; then
  148. args=("-A" "${args[@]}")
  149. fi
  150. ohai "/usr/bin/sudo" "${args[@]}"
  151. execute "/usr/bin/sudo" "${args[@]}"
  152. else
  153. ohai "${args[@]}"
  154. execute "${args[@]}"
  155. fi
  156. }
  157.  
  158. getc() {
  159. local save_state
  160. save_state=$(/bin/stty -g)
  161. /bin/stty raw -echo
  162. IFS= read -r -n 1 -d \'\' "$@"
  163. /bin/stty "$save_state"
  164. }
  165.  
  166. wait_for_user() {
  167. local c
  168. echo
  169. echo "Press RETURN to continue or any other key to abort"
  170. getc c
  171. # we test for \r and \n because some stuff does \r instead
  172. if ! [[ "$c" == $\'\r\' || "$c" == $\'\n\' ]]; then
  173. exit 1
  174. fi
  175. }
  176.  
  177. major_minor() {
  178. echo "${1%%.*}.$(x="${1#*.}"; echo "${x%%.*}")"
  179. }
  180.  
  181. version_gt() {
  182. [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]]
  183. }
  184. version_ge() {
  185. [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]
  186. }
  187. version_lt() {
  188. [[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]
  189. }
  190.  
  191. should_install_command_line_tools() {
  192. if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
  193. return 1
  194. fi
  195.  
  196. if version_gt "$macos_version" "10.13"; then
  197. ! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]]
  198. else
  199. ! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]] ||
  200. ! [[ -e "/usr/include/iconv.h" ]]
  201. fi
  202. }
  203.  
  204. get_permission() {
  205. $STAT "%A" "$1"
  206. }
  207.  
  208. user_only_chmod() {
  209. [[ -d "$1" ]] && [[ "$(get_permission "$1")" != "755" ]]
  210. }
  211.  
  212. exists_but_not_writable() {
  213. [[ -e "$1" ]] && ! [[ -r "$1" && -w "$1" && -x "$1" ]]
  214. }
  215.  
  216. get_owner() {
  217. $STAT "%u" "$1"
  218. }
  219.  
  220. file_not_owned() {
  221. [[ "$(get_owner "$1")" != "$(id -u)" ]]
  222. }
  223.  
  224. get_group() {
  225. $STAT "%g" "$1"
  226. }
  227.  
  228. file_not_grpowned() {
  229. [[ " $(id -G "$USER") " != *" $(get_group "$1") "* ]]
  230. }
  231.  
  232. # Please sync with \'test_ruby()\' in \'Library/Homebrew/utils/ruby.sh\' from Homebrew/brew repository.
  233. test_ruby() {
  234. if [[ ! -x $1 ]]
  235. then
  236. return 1
  237. fi
  238.  
  239. "$1" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt -rrubygems -e \
  240. "abort if Gem::Version.new(RUBY_VERSION.to_s.dup).to_s.split(\'.\').first(2) != \
  241. Gem::Version.new(\'$REQUIRED_RUBY_VERSION\').to_s.split(\'.\').first(2)" 2>/dev/null
  242. }
  243.  
  244. no_usable_ruby() {
  245. local ruby_exec
  246. IFS=$\'\n\' # Do word splitting on new lines only
  247. for ruby_exec in $(which -a ruby); do
  248. if test_ruby "$ruby_exec"; then
  249. IFS=$\' \t\n\' # Restore IFS to its default value
  250. return 1
  251. fi
  252. done
  253. IFS=$\' \t\n\' # Restore IFS to its default value
  254. return 0
  255. }
  256.  
  257. outdated_glibc() {
  258. local glibc_version
  259. glibc_version=$(ldd --version | head -n1 | grep -o \'[0-9.]*$\' | grep -o \'^[0-9]\+\.[0-9]\+\')
  260. version_lt "$glibc_version" "$REQUIRED_GLIBC_VERSION"
  261. }
  262.  
  263. if [[ -n "${HOMEBREW_ON_LINUX-}" ]] && no_usable_ruby && outdated_glibc
  264. then
  265. abort "$(cat <<-EOFABORT
  266. Homebrew requires Ruby $REQUIRED_RUBY_VERSION which was not found on your system.
  267. Homebrew portable Ruby requires Glibc version $REQUIRED_GLIBC_VERSION or newer,
  268. and your Glibc version is too old.
  269. See ${tty_underline}https://docs.brew.sh/Homebrew-on-Linux#requirements${tty_reset}
  270. Install Ruby $REQUIRED_RUBY_VERSION and add its location to your PATH.
  271. EOFABORT
  272. )"
  273. fi
  274.  
  275. # USER isn\'t always set so provide a fall back for the installer and subprocesses.
  276. if [[ -z "${USER-}" ]]; then
  277. USER="$(chomp "$(id -un)")"
  278. export USER
  279. fi
  280.  
  281. # Invalidate sudo timestamp before exiting (if it wasn\'t active before).
  282. if ! /usr/bin/sudo -n -v 2>/dev/null; then
  283. trap \'/usr/bin/sudo -k\' EXIT
  284. fi
  285.  
  286. # Things can fail later if `pwd` doesn\'t exist.
  287. # Also sudo prints a warning message for no good reason
  288. cd "/usr" || exit 1
  289.  
  290. ####################################################################### script
  291. if ! command -v git >/dev/null; then
  292. abort "$(cat <<EOABORT
  293. You must install Git before installing Homebrew. See:
  294. ${tty_underline}https://docs.brew.sh/Installation${tty_reset}
  295. EOABORT
  296. )"
  297. fi
  298.  
  299. if ! command -v curl >/dev/null; then
  300. abort "$(cat <<EOABORT
  301. You must install cURL before installing Homebrew. See:
  302. ${tty_underline}https://docs.brew.sh/Installation${tty_reset}
  303. EOABORT
  304. )"
  305. fi
  306.  
  307. # shellcheck disable=SC2016
  308. ohai \'Checking for `sudo` access (which may request your password).\'
  309.  
  310. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  311. have_sudo_access
  312. else
  313. if [[ -n "${NONINTERACTIVE-}" ]] ||
  314. [[ -w "${HOMEBREW_PREFIX_DEFAULT}" ]] ||
  315. [[ -w "/home/linuxbrew" ]] ||
  316. [[ -w "/home" ]]; then
  317. HOMEBREW_PREFIX="$HOMEBREW_PREFIX_DEFAULT"
  318. else
  319. trap exit SIGINT
  320. if ! /usr/bin/sudo -n -v &>/dev/null; then
  321. ohai "Select the Homebrew installation directory"
  322. echo "- ${tty_bold}Enter your password${tty_reset} to install to ${tty_underline}${HOMEBREW_PREFIX_DEFAULT}${tty_reset} (${tty_bold}recommended${tty_reset})"
  323. echo "- ${tty_bold}Press Control-D${tty_reset} to install to ${tty_underline}$HOME/.linuxbrew${tty_reset}"
  324. echo "- ${tty_bold}Press Control-C${tty_reset} to cancel installation"
  325. fi
  326. if have_sudo_access; then
  327. HOMEBREW_PREFIX="$HOMEBREW_PREFIX_DEFAULT"
  328. else
  329. HOMEBREW_PREFIX="$HOME/.linuxbrew"
  330. fi
  331. trap - SIGINT
  332. fi
  333. HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
  334. fi
  335. HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core"
  336.  
  337. if [[ "${EUID:-${UID}}" == "0" ]]; then
  338. abort "Don\'t run this as root!"
  339. elif [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]]; then
  340. abort "$(cat <<EOABORT
  341. The Homebrew prefix, ${HOMEBREW_PREFIX}, exists but is not searchable.
  342. If this is not intentional, please restore the default permissions and
  343. try running the installer again:
  344. sudo chmod 775 ${HOMEBREW_PREFIX}
  345. EOABORT
  346. )"
  347. fi
  348.  
  349. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  350. # On macOS, support 64-bit Intel and ARM
  351. if [[ "$UNAME_MACHINE" != "arm64" ]] && [[ "$UNAME_MACHINE" != "x86_64" ]]; then
  352. abort "Homebrew is only supported on Intel and ARM processors!"
  353. fi
  354. else
  355. # On Linux, support only 64-bit Intel
  356. if [[ "$UNAME_MACHINE" == "arm64" ]]; then
  357. abort "$(cat <<EOABORT
  358. Homebrew on Linux is not supported on ARM processors.
  359. You can try an alternate installation method instead:
  360. ${tty_underline}https://docs.brew.sh/Homebrew-on-Linux#arm${tty_reset}
  361. EOABORT
  362. )"
  363. elif [[ "$UNAME_MACHINE" != "x86_64" ]]; then
  364. abort "Homebrew on Linux is only supported on Intel processors!"
  365. fi
  366. fi
  367.  
  368. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  369. macos_version="$(major_minor "$(/usr/bin/sw_vers -productVersion)")"
  370. if version_lt "$macos_version" "10.7"; then
  371. abort "$(cat <<EOABORT
  372. Your Mac OS X version is too old. See:
  373. ${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset}
  374. EOABORT
  375. )"
  376. elif version_lt "$macos_version" "10.10"; then
  377. abort "Your OS X version is too old"
  378. elif version_ge "$macos_version" "$MACOS_NEWEST_UNSUPPORTED" || \
  379. version_lt "$macos_version" "$MACOS_OLDEST_SUPPORTED"; then
  380. who="We"
  381. what=""
  382. if version_ge "$macos_version" "$MACOS_NEWEST_UNSUPPORTED"; then
  383. what="pre-release version"
  384. else
  385. who+=" (and Apple)"
  386. what="old version"
  387. fi
  388. ohai "You are using macOS ${macos_version}."
  389. ohai "${who} do not provide support for this ${what}."
  390.  
  391. echo "$(cat <<EOS
  392. This installation may not succeed.
  393. After installation, you will encounter build failures with some formulae.
  394. Please create pull requests instead of asking for help on Homebrew\\'s GitHub,
  395. Twitter or IRC. You are responsible for resolving any issues you experience
  396. while you are running this ${what}.
  397. EOS
  398. )
  399. "
  400. fi
  401. fi
  402.  
  403. ohai "This script will install:"
  404. echo "${HOMEBREW_PREFIX}/bin/brew"
  405. echo "${HOMEBREW_PREFIX}/share/doc/homebrew"
  406. echo "${HOMEBREW_PREFIX}/share/man/man1/brew.1"
  407. echo "${HOMEBREW_PREFIX}/share/zsh/site-functions/_brew"
  408. echo "${HOMEBREW_PREFIX}/etc/bash_completion.d/brew"
  409. echo "${HOMEBREW_REPOSITORY}"
  410.  
  411. # Keep relatively in sync with
  412. # https://mirrors.tunA.tsinghua.edu.cn/git/homebrew/brew.git/blob/master/Library/Homebrew/keg.rb
  413. directories=(bin etc include lib sbin share opt var
  414. Frameworks
  415. etc/bash_completion.d lib/pkgconfig
  416. share/aclocal share/doc share/info share/locale share/man
  417. share/man/man1 share/man/man2 share/man/man3 share/man/man4
  418. share/man/man5 share/man/man6 share/man/man7 share/man/man8
  419. var/log var/homebrew var/homebrew/linked
  420. bin/brew)
  421. group_chmods=()
  422. for dir in "${directories[@]}"; do
  423. if exists_but_not_writable "${HOMEBREW_PREFIX}/${dir}"; then
  424. group_chmods+=("${HOMEBREW_PREFIX}/${dir}")
  425. fi
  426. done
  427.  
  428. # zsh refuses to read from these directories if group writable
  429. directories=(share/zsh share/zsh/site-functions)
  430. zsh_dirs=()
  431. for dir in "${directories[@]}"; do
  432. zsh_dirs+=("${HOMEBREW_PREFIX}/${dir}")
  433. done
  434.  
  435. directories=(bin etc include lib sbin share var opt
  436. share/zsh share/zsh/site-functions
  437. var/homebrew var/homebrew/linked
  438. Cellar Caskroom Frameworks)
  439. mkdirs=()
  440. for dir in "${directories[@]}"; do
  441. if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]; then
  442. mkdirs+=("${HOMEBREW_PREFIX}/${dir}")
  443. fi
  444. done
  445.  
  446. user_chmods=()
  447. if [[ "${#zsh_dirs[@]}" -gt 0 ]]; then
  448. for dir in "${zsh_dirs[@]}"; do
  449. if user_only_chmod "${dir}"; then
  450. user_chmods+=("${dir}")
  451. fi
  452. done
  453. fi
  454.  
  455. chmods=()
  456. if [[ "${#group_chmods[@]}" -gt 0 ]]; then
  457. chmods+=("${group_chmods[@]}")
  458. fi
  459. if [[ "${#user_chmods[@]}" -gt 0 ]]; then
  460. chmods+=("${user_chmods[@]}")
  461. fi
  462.  
  463. chowns=()
  464. chgrps=()
  465. if [[ "${#chmods[@]}" -gt 0 ]]; then
  466. for dir in "${chmods[@]}"; do
  467. if file_not_owned "${dir}"; then
  468. chowns+=("${dir}")
  469. fi
  470. if file_not_grpowned "${dir}"; then
  471. chgrps+=("${dir}")
  472. fi
  473. done
  474. fi
  475.  
  476. if [[ "${#group_chmods[@]}" -gt 0 ]]; then
  477. ohai "The following existing directories will be made group writable:"
  478. printf "%s\n" "${group_chmods[@]}"
  479. fi
  480. if [[ "${#user_chmods[@]}" -gt 0 ]]; then
  481. ohai "The following existing directories will be made writable by user only:"
  482. printf "%s\n" "${user_chmods[@]}"
  483. fi
  484. if [[ "${#chowns[@]}" -gt 0 ]]; then
  485. ohai "The following existing directories will have their owner set to ${tty_underline}${USER}${tty_reset}:"
  486. printf "%s\n" "${chowns[@]}"
  487. fi
  488. if [[ "${#chgrps[@]}" -gt 0 ]]; then
  489. ohai "The following existing directories will have their group set to ${tty_underline}${GROUP}${tty_reset}:"
  490. printf "%s\n" "${chgrps[@]}"
  491. fi
  492. if [[ "${#mkdirs[@]}" -gt 0 ]]; then
  493. ohai "The following new directories will be created:"
  494. printf "%s\n" "${mkdirs[@]}"
  495. fi
  496.  
  497. if should_install_command_line_tools; then
  498. ohai "The Xcode Command Line Tools will be installed."
  499. fi
  500.  
  501. if [[ -z "${NONINTERACTIVE-}" ]]; then
  502. wait_for_user
  503. fi
  504.  
  505. if [[ -d "${HOMEBREW_PREFIX}" ]]; then
  506. if [[ "${#chmods[@]}" -gt 0 ]]; then
  507. execute_sudo "/bin/chmod" "u+rwx" "${chmods[@]}"
  508. fi
  509. if [[ "${#group_chmods[@]}" -gt 0 ]]; then
  510. execute_sudo "/bin/chmod" "g+rwx" "${group_chmods[@]}"
  511. fi
  512. if [[ "${#user_chmods[@]}" -gt 0 ]]; then
  513. execute_sudo "/bin/chmod" "755" "${user_chmods[@]}"
  514. fi
  515. if [[ "${#chowns[@]}" -gt 0 ]]; then
  516. execute_sudo "$CHOWN" "$USER" "${chowns[@]}"
  517. fi
  518. if [[ "${#chgrps[@]}" -gt 0 ]]; then
  519. execute_sudo "$CHGRP" "$GROUP" "${chgrps[@]}"
  520. fi
  521. else
  522. execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_PREFIX}"
  523. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  524. execute_sudo "$CHOWN" "root:wheel" "${HOMEBREW_PREFIX}"
  525. else
  526. execute_sudo "$CHOWN" "$USER:$GROUP" "${HOMEBREW_PREFIX}"
  527. fi
  528. fi
  529.  
  530. if [[ "${#mkdirs[@]}" -gt 0 ]]; then
  531. execute_sudo "/bin/mkdir" "-p" "${mkdirs[@]}"
  532. execute_sudo "/bin/chmod" "g+rwx" "${mkdirs[@]}"
  533. execute_sudo "$CHOWN" "$USER" "${mkdirs[@]}"
  534. execute_sudo "$CHGRP" "$GROUP" "${mkdirs[@]}"
  535. fi
  536.  
  537. if ! [[ -d "${HOMEBREW_REPOSITORY}" ]]; then
  538. execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_REPOSITORY}"
  539. fi
  540. execute_sudo "$CHOWN" "-R" "$USER:$GROUP" "${HOMEBREW_REPOSITORY}"
  541.  
  542. if ! [[ -d "${HOMEBREW_CACHE}" ]]; then
  543. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  544. execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_CACHE}"
  545. else
  546. execute "/bin/mkdir" "-p" "${HOMEBREW_CACHE}"
  547. fi
  548. fi
  549. if exists_but_not_writable "${HOMEBREW_CACHE}"; then
  550. execute_sudo "/bin/chmod" "g+rwx" "${HOMEBREW_CACHE}"
  551. fi
  552. if file_not_owned "${HOMEBREW_CACHE}"; then
  553. execute_sudo "$CHOWN" "-R" "$USER" "${HOMEBREW_CACHE}"
  554. fi
  555. if file_not_grpowned "${HOMEBREW_CACHE}"; then
  556. execute_sudo "$CHGRP" "-R" "$GROUP" "${HOMEBREW_CACHE}"
  557. fi
  558. if [[ -d "${HOMEBREW_CACHE}" ]]; then
  559. execute "$TOUCH" "${HOMEBREW_CACHE}/.cleaned"
  560. fi
  561.  
  562. if should_install_command_line_tools && version_ge "$macos_version" "10.13"; then
  563. ohai "Searching online for the Command Line Tools"
  564. # This temporary file prompts the \'softwareupdate\' utility to list the Command Line Tools
  565. clt_placeholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
  566. execute_sudo "$TOUCH" "$clt_placeholder"
  567.  
  568. clt_label_command="/usr/sbin/softwareupdate -l |
  569. grep -B 1 -E \'Command Line Tools\' |
  570. awk -F\'*\' \'/^ *\\*/ {print \$2}\' |
  571. sed -e \'s/^ *Label: //\' -e \'s/^ *//\' |
  572. sort -V |
  573. tail -n1"
  574. clt_label="$(chomp "$(/bin/bash -c "$clt_label_command")")"
  575.  
  576. if [[ -n "$clt_label" ]]; then
  577. ohai "Installing $clt_label"
  578. execute_sudo "/usr/sbin/softwareupdate" "-i" "$clt_label"
  579. execute_sudo "/bin/rm" "-f" "$clt_placeholder"
  580. execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"
  581. fi
  582. fi
  583.  
  584. # Headless install may have failed, so fallback to original \'xcode-select\' method
  585. if should_install_command_line_tools && test -t 0; then
  586. ohai "Installing the Command Line Tools (expect a GUI popup):"
  587. execute_sudo "/usr/bin/xcode-select" "--install"
  588. echo "Press any key when the installation has completed."
  589. getc
  590. execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"
  591. fi
  592.  
  593. if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && ! output="$(/usr/bin/xcrun clang 2>&1)" && [[ "$output" == *"license"* ]]; then
  594. abort "$(cat <<EOABORT
  595. You have not agreed to the Xcode license.
  596. Before running the installer again please agree to the license by opening
  597. Xcode.app or running:
  598. sudo xcodebuild -license
  599. EOABORT
  600. )"
  601. fi
  602.  
  603. ohai "Downloading and installing Homebrew..."
  604. (
  605. cd "${HOMEBREW_REPOSITORY}" >/dev/null || return
  606.  
  607. # we do it in four steps to avoid merge errors when reinstalling
  608. execute "git" "init" "-q"
  609.  
  610. # "git remote add" will fail if the remote is defined in the global config
  611. execute "git" "config" "remote.origin.url" "${HOMEBREW_BREW_GIT_REMOTE}"
  612. execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"
  613.  
  614. # ensure we don\'t munge line endings on checkout
  615. execute "git" "config" "core.autocrlf" "false"
  616.  
  617. execute "git" "fetch" "--force" "origin"
  618. execute "git" "fetch" "--force" "--tags" "origin"
  619.  
  620. execute "git" "reset" "--hard" "origin/master"
  621.  
  622. if [[ "${HOMEBREW_REPOSITORY}" != "${HOMEBREW_PREFIX}" ]]; then
  623. execute "ln" "-sf" "${HOMEBREW_REPOSITORY}/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"
  624. fi
  625.  
  626. if [[ ! -d "${HOMEBREW_CORE}" ]]; then
  627. ohai "Tapping homebrew/core"
  628. (
  629. execute "/bin/mkdir" "-p" "${HOMEBREW_CORE}"
  630. cd "${HOMEBREW_CORE}" >/dev/null || return
  631.  
  632. execute "git" "init" "-q"
  633. execute "git" "config" "remote.origin.url" "${HOMEBREW_CORE_GIT_REMOTE}"
  634. execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"
  635. execute "git" "config" "core.autocrlf" "false"
  636. execute "git" "fetch" "--force" "origin" "refs/heads/master:refs/remotes/origin/master"
  637. execute "git" "remote" "set-head" "origin" "--auto" >/dev/null
  638. execute "git" "reset" "--hard" "origin/master"
  639.  
  640. cd "${HOMEBREW_REPOSITORY}" >/dev/null || return
  641. ) || exit 1
  642. fi
  643.  
  644. execute "${HOMEBREW_PREFIX}/bin/brew" "update" "--force" "--quiet"
  645. ) || exit 1
  646.  
  647. if [[ ":${PATH}:" != *":${HOMEBREW_PREFIX}/bin:"* ]]; then
  648. warn "${HOMEBREW_PREFIX}/bin is not in your PATH."
  649. fi
  650.  
  651. ohai "Installation successful!"
  652. echo
  653.  
  654. # Use the shell\'s audible bell.
  655. if [[ -t 1 ]]; then
  656. printf "\a"
  657. fi
  658.  
  659. # Use an extra newline and bold to avoid this being missed.
  660. ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics."
  661. echo "$(cat <<EOS
  662. ${tty_bold}Read the analytics documentation (and how to opt-out) here:
  663. ${tty_underline}https://docs.brew.sh/Analytics${tty_reset}
  664. No analytics data has been sent yet (or will be during this \`install\` run).
  665. EOS
  666. )
  667. "
  668.  
  669. ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:"
  670. echo "$(cat <<EOS
  671. ${tty_underline}https://mirrors.tunA.tsinghua.edu.cn/git/homebrew/brew.git#donations${tty_reset}
  672. EOS
  673. )
  674. "
  675.  
  676. (
  677. cd "${HOMEBREW_REPOSITORY}" >/dev/null || return
  678. execute "git" "config" "--replace-all" "homebrew.analyticsmessage" "true"
  679. execute "git" "config" "--replace-all" "homebrew.caskanalyticsmessage" "true"
  680. ) || exit 1
  681.  
  682. ohai "Next steps:"
  683. if [[ "$UNAME_MACHINE" == "arm64" ]] || [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
  684. case "$SHELL" in
  685. */bash*)
  686. if [[ -r "$HOME/.bash_profile" ]]; then
  687. shell_profile="$HOME/.bash_profile"
  688. else
  689. shell_profile="$HOME/.profile"
  690. fi
  691. ;;
  692. */zsh*)
  693. shell_profile="$HOME/.zprofile"
  694. ;;
  695. *)
  696. shell_profile="$HOME/.profile"
  697. ;;
  698. esac
  699.  
  700. cat <<EOS
  701. - Add Homebrew to your ${tty_bold}PATH${tty_reset} in ${tty_underline}${shell_profile}${tty_reset}:
  702. echo \'eval \$(${HOMEBREW_PREFIX}/bin/brew shellenv)\' >> ${shell_profile}
  703. eval \$(${HOMEBREW_PREFIX}/bin/brew shellenv)
  704. EOS
  705. fi
  706.  
  707. echo "- Run \`brew help\` to get started"
  708. echo "- Further documentation: "
  709. echo " ${tty_underline}https://docs.brew.sh${tty_reset}"
  710.  
  711. if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
  712. echo "- Install the Homebrew dependencies if you have sudo access:"
  713.  
  714. if [[ $(command -v apt-get) ]]; then
  715. echo " sudo apt-get install build-essential"
  716. elif [[ $(command -v yum) ]]; then
  717. echo " sudo yum groupinstall \'Development Tools\'"
  718. elif [[ $(command -v pacman) ]]; then
  719. echo " sudo pacman -S base-devel"
  720. elif [[ $(command -v apk) ]]; then
  721. echo " sudo apk add build-base"
  722. fi
  723.  
  724. cat <<EOS
  725. See ${tty_underline}https://docs.brew.sh/linux${tty_reset} for more information
  726. - We recommend that you install GCC:
  727. brew install gcc
  728.  
  729. EOS
  730. fi

  

3、执行:切换到该文件路径

bash brew_install

4、基本上能跑,但跑到最后报错:

  1. fatal: unable to access \'https://github.com/Homebrew/homebrew-core/\': Failed 443

  

5、但这个时候,drew 已经可以执行命令了;

  1. cd "$(brew --repo)"
  2. git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
  3. cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
  4. git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

  

6、此时还不可以使用,执行 brew update,会报:error: Not a valid ref: refs/remotes/origin/master;

此时,执行命令:

  1. git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

  会报该路径已经存在,直接去把/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core 删掉,再次执行上面的命令。

7、执行过程中提示执行:

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch –unshallow

那就执行它;

8、最后brew 安装好了;

9、安装好之后,用brew安装最新的ruby;

  1. 安装ruby(这里使用Homeview来安装ruby,安装Homebrew的方法在文章开头有链接)
  2.  
  3. 1.brew install ruby(安装最新版的ruby
  4. 2.ruby --versionruby版本)
  5. 3.curl -sSL https://get.rvm.io | bash -s stable(RVM 是一个便捷的多版本 Ruby 环境的管理和切换工具.安装它,官网:https://rvm.io/)
  6. 4.rvm get stablervm更新)
  7. 5.rvm --versionrvm版本)
  8. 6.sudo gem update --systemgem更新)
  9. 7.gem sources --remove https://rubygems.org/(移除之前的源)
  10. 8.gem sources --add https://gems.ruby-china.com/(设置新的源)
  11. 9.gem sources -l(查看当前源)
  12. 10.gem --versiongem版本)

  

10、再安装cocoapods:

  1. 1.sudo gem install -n /usr/local/bin cocoapods(安装cocoapods
  2. 2.pod setup(安装本地库)
  3. 3.pod repo update(更新本地库)
  4. 4.pod --versionpod版本)

  

 

版权声明:本文为hoge66原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hoge66/archive/2004/01/13/14432750.html