$ brew install -h Usage: brew install [options] formula
Install formula. Additional options specific to formula may be appended to the command.
Unless HOMEBREW_NO_INSTALL_CLEANUP is set, brew cleanup will then be run for the installed formulae or, every 30 days, for all formulae.
-d, --debug If brewing fails, open an interactive debugging session with access to IRB or a shell inside the temporary build directory. --env If std is passed, use the standard build environment instead of superenv. If super is passed, use superenv even if the formula specifies the standard build environment. --ignore-dependencies An unsupported Homebrew development flag to skip installing any dependencies of any kind. If the dependencies are not already present, the formula will have issues. If you're not developing Homebrew, consider adjusting your PATH rather than using this flag. --only-dependencies Install the dependencies with specified options but do not install the formula itself. --cc Attempt to compile using the specified compiler, which should be the name of the compiler's executable, e.g. gcc-7 for GCC 7. In order to use LLVM's clang, specify llvm_clang. To use the Apple-provided clang, specify clang. This option will only accept compilers that are provided by Homebrew or bundled with macOS. Please do not file issues if you encounter errors while using this option. -s, --build-from-source Compile formula from source even if a bottle is provided. Dependencies will still be installed from bottles if they are available. --force-bottle Install from a bottle if it exists for the current or newest version of macOS, even if it would not normally be used for installation. --include-test Install testing dependencies required to run brew test formula. --devel If formula defines it, install the development version. --HEAD If formula defines it, install the HEAD version, aka. master, trunk, unstable. --fetch-HEAD Fetch the upstream repository to detect if the HEAD installation of the formula is outdated. Otherwise, the repository's HEAD will only be checked for updates when a new stable or development version has been released. --keep-tmp Retain the temporary files created during installation. --build-bottle Prepare the formula for eventual bottling during installation, skipping any post-install steps. --bottle-arch Optimise bottles for the specified architecture rather than the oldest architecture supported by the version of macOS the bottles are built on. -f, --force Install without checking for previously installed keg-only or non-migrated versions. -v, --verbose Print the verification and postinstall steps. --display-times Print install times for each formula at the end of the run. -i, --interactive Download and patch formula, then open a shell. This allows the user to run ./configure --help and otherwise determine how to turn the software package into a Homebrew package. -g, --git Create a Git repository, useful for creating patches to the software. -h, --help Show this message.
~ brew install go # 安装指定 go 版本 ~ brew install go@<version>
默认安装一个 go 的最新版本,也可安装指定版本。
Brew 切换 Go 版本
【方式一】brew link
安装新的版本
1
~ brew install go@<version>
移除原有的 go 版本软链
1
~ brew unlink go
指定新的版本软链
1
~ brew link go@<version>
【方式二】brew switch
使用 brew info go 命令你可以看到当前的 go 可以切换的版本。
1 2 3 4
~ brew switch go <version1> Cleaning /usr/local/Cellar/go/<version1> Cleaning /usr/local/Cellar/go/<version2> 0 links created for /usr/local/Cellar/go/<version1>
创建了零个连接,就代表着没有成功的将 go 版本指向你所需要的版本下,问题是什么呢?现将 go 版本切回 go ,你会发现可以切换并正常使用:
1 2 3 4 5 6 7
~ brew switch go <version2> Cleaning /usr/local/Cellar/go/<version1> Cleaning /usr/local/Cellar/go/<version2> 3 links created for /usr/local/Cellar/go/<version2>
~ go version go version go<version2> darwin/amd64
定位这个原因你需要看看为什么没有未给 go 版本创建软连接,首先要找一下 go 默认安装的位置,使用 go env 查看安装目录:
1
/usr/local/Cellar/go/
进入到目录之后在 go 目录下只有刚才默认安装的 版本,并没有自己安装的版本,退出父级目录看到了下载的 go@版本,由于软连接连接的是上方的路径,需要将这个目录移动至 go 目录下:
1 2 3 4 5 6 7 8
// 打开默认目录 cd /usr/local/Cellar/go/ // 退出目录 cd .. // 移动目录至 go 目录下 mv go@<version1> go/ // 重要!!! 重命名文件夹 mv go@<version1> <version1>
接下来使用切换命令 brew switch go <version> 就可以切换环境了。
brew 报错 "fatal: Could not resolve HEAD to a revision"
brew报错:`initialize': Version value must be a string; got a NilClass () (TypeError)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
brew list Traceback (most recent call last): 11: from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `<main>' 10: from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `require_relative' 9: from /usr/local/Homebrew/Library/Homebrew/global.rb:29:in `<top (required)>' 8: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require' 7: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require' 6: from /usr/local/Homebrew/Library/Homebrew/os.rb:3:in `<top (required)>' 5: from /usr/local/Homebrew/Library/Homebrew/os.rb:21:in `<module:OS>' 4: from /usr/local/Homebrew/Library/Homebrew/os/mac.rb:58:in `prerelease?' 3: from /usr/local/Homebrew/Library/Homebrew/os/mac.rb:24:in `version' 2: from /usr/local/Homebrew/Library/Homebrew/os/mac.rb:24:in `new' 1: from /usr/local/Homebrew/Library/Homebrew/os/mac/version.rb:26:in `initialize' /usr/local/Homebrew/Library/Homebrew/version.rb:368:in `initialize': Version value must be a string; got a NilClass () (TypeError)
# First check if the OS is Linux. if [[ "$(uname)" = "Linux" ]]; then HOMEBREW_ON_LINUX=1 fi
# Check if macOS is ARM if [[ "$(uname -m)" = "arm64" ]] && [[ "$(uname)" = "Darwin" ]]; then HOMEBREW_APPLE_SILICON=1 fi
# On macOS, this script installs to /usr/local only. # On Linux, it installs to /home/linuxbrew/.linuxbrew if you have sudo access # and ~/.linuxbrew otherwise. # To install elsewhere (which is unsupported) # you can untar https://github.com/Homebrew/brew/tarball/master # anywhere you like. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then HOMEBREW_PREFIX="/usr/local" HOMEBREW_REPOSITORY="/usr/local/Homebrew" HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"
STAT="stat --printf" CHOWN="/bin/chown" CHGRP="/bin/chgrp" GROUP="$(id -gn)" TOUCH="/bin/touch" fi BREW_REPO="https://github.com/Homebrew/brew"
# TODO: bump version when new macOS is released MACOS_LATEST_SUPPORTED="10.15" # TODO: bump version when new macOS is released MACOS_OLDEST_SUPPORTED="10.13"
# For Homebrew on Linux REQUIRED_RUBY_VERSION=2.6 # https://github.com/Homebrew/brew/pull/6556 REQUIRED_GLIBC_VERSION=2.13 # https://docs.brew.sh/Homebrew-on-Linux#requirements
# no analytics during installation export HOMEBREW_NO_ANALYTICS_THIS_RUN=1 export HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT=1
have_sudo_access() { local -a args if [[ -n "${SUDO_ASKPASS-}" ]]; then args=("-A") fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then if [[ -n "${args[*]-}" ]]; then /usr/bin/sudo "${args[@]}" -l mkdir &>/dev/null else /usr/bin/sudo -l mkdir &>/dev/null fi HAVE_SUDO_ACCESS="$?" fi
if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then abort "Need sudo access on macOS (e.g. the user $USER to be an Administrator)!" fi
return"$HAVE_SUDO_ACCESS" }
shell_join() { local arg printf"%s""$1" shift for arg in"$@"; do printf" " printf"%s""${arg// /\ }" done }
execute() { if ! "$@"; then abort "$(printf "Failed during: %s""$(shell_join "$@")")" fi }
execute_sudo() { local -a args=("$@") if [[ -n "${SUDO_ASKPASS-}" ]]; then args=("-A""${args[@]}") fi if have_sudo_access; then ohai "/usr/bin/sudo""${args[@]}" execute "/usr/bin/sudo""${args[@]}" else ohai "${args[@]}" execute "${args[@]}" fi }
getc() { local save_state save_state=$(/bin/stty -g) /bin/stty raw -echo IFS= read -r -n 1 -d ''"$@" /bin/stty "$save_state" }
wait_for_user() { local c echo echo"Press RETURN to continue or any other key to abort" getc c # we test for \r and \n because some stuff does \r instead if ! [[ "$c" == $'\r' || "$c" == $'\n' ]]; then exit 1 fi }
no_usable_ruby() { local ruby_exec IFS=$'\n'# Do word splitting on new lines only for ruby_exec in $(which -a ruby); do if test_ruby "$ruby_exec"; then return 1 fi done IFS=$' \t\n'# Restore IFS to its default value return 0 }
outdated_glibc() { local glibc_version glibc_version=$(ldd --version | head -n1 | grep -o '[0-9.]*$' | grep -o '^[0-9]\+\.[0-9]\+') version_lt "$glibc_version""$REQUIRED_GLIBC_VERSION" }
if [[ -n "${HOMEBREW_ON_LINUX-}" ]] && no_usable_ruby && outdated_glibc then abort "$(cat <<-EOFABORT Homebrew requires Ruby $REQUIRED_RUBY_VERSION which was not found on your system. Homebrew portable Ruby requires Glibc version $REQUIRED_GLIBC_VERSION or newer, and your Glibc version is too old. See ${tty_underline}https://docs.brew.sh/Homebrew-on-Linux#requirements${tty_reset} Install Ruby $REQUIRED_RUBY_VERSION and add its location to your PATH. EOFABORT )" fi
# USER isn't always set so provide a fall back for the installer and subprocesses. if [[ -z "${USER-}" ]]; then USER="$(chomp "$(id -un)")" export USER fi
# Invalidate sudo timestamp before exiting (if it wasn't active before). if ! /usr/bin/sudo -n -v 2>/dev/null; then trap'/usr/bin/sudo -k' EXIT fi
# Things can fail later if `pwd` doesn't exist. # Also sudo prints a warning message for no good reason cd"/usr" || exit 1
####################################################################### script if should_install_git; then abort "$(cat <<EOABORT You must install Git before installing Homebrew. See: ${tty_underline}https://docs.brew.sh/Installation${tty_reset} EOABORT )" fi
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then have_sudo_access else if [[ -n "${CI-}" ]] || [[ -w "$HOMEBREW_PREFIX_DEFAULT" ]] || [[ -w "/home/linuxbrew" ]] || [[ -w "/home" ]]; then HOMEBREW_PREFIX="$HOMEBREW_PREFIX_DEFAULT" else trapexit SIGINT if [[ $(/usr/bin/sudo -n -l mkdir 2>&1) != *"mkdir"* ]]; then ohai "Select the Homebrew installation directory" echo"- ${tty_bold}Enter your password${tty_reset} to install to ${tty_underline}${HOMEBREW_PREFIX_DEFAULT}${tty_reset} (${tty_bold}recommended${tty_reset})" echo"- ${tty_bold}Press Control-D${tty_reset} to install to ${tty_underline}$HOME/.linuxbrew${tty_reset}" echo"- ${tty_bold}Press Control-C${tty_reset} to cancel installation" fi if have_sudo_access; then HOMEBREW_PREFIX="$HOMEBREW_PREFIX_DEFAULT" else HOMEBREW_PREFIX="$HOME/.linuxbrew" fi trap - SIGINT fi HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew" fi
if [[ "$UID" == "0" ]]; then abort "Don't run this as root!" elif [[ -d "$HOMEBREW_PREFIX" && ! -x "$HOMEBREW_PREFIX" ]]; then abort "$(cat <<EOABORT The Homebrew prefix, ${HOMEBREW_PREFIX}, exists but is not searchable. If this is not intentional, please restore the default permissions and try running the installer again: sudo chmod 775 ${HOMEBREW_PREFIX} EOABORT )" fi
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then if version_lt "$macos_version""10.7"; then abort "$(cat <<EOABORT Your Mac OS X version is too old. See: ${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset} EOABORT )" elif version_lt "$macos_version""10.9"; then abort "Your OS X version is too old" elif version_gt "$macos_version""$MACOS_LATEST_SUPPORTED" || \ version_lt "$macos_version""$MACOS_OLDEST_SUPPORTED"; then who="We" what="" if version_gt "$macos_version""$MACOS_LATEST_SUPPORTED"; then what="pre-release version" else who+=" (and Apple)" what="old version" fi ohai "You are using macOS ${macos_version}." ohai "${who} do not provide support for this ${what}."
echo"$(cat <<EOS This installation may not succeed. After installation, you will encounter build failures with some formulae. Please create pull requests instead of asking for help on Homebrew\'s GitHub, Discourse, Twitter or IRC. You are responsible for resolving any issues you experience while you are running this ${what}. EOS ) " fi fi
# Keep relatively in sync with # https://github.com/Homebrew/brew/blob/master/Library/Homebrew/keg.rb directories=(bin etc include lib sbin share opt var Frameworks etc/bash_completion.d lib/pkgconfig share/aclocal share/doc share/info share/locale share/man share/man/man1 share/man/man2 share/man/man3 share/man/man4 share/man/man5 share/man/man6 share/man/man7 share/man/man8 var/log var/homebrew var/homebrew/linked bin/brew) group_chmods=() for dir in"${directories[@]}"; do if exists_but_not_writable "${HOMEBREW_PREFIX}/${dir}"; then group_chmods+=("${HOMEBREW_PREFIX}/${dir}") fi done
# zsh refuses to read from these directories if group writable directories=(share/zsh share/zsh/site-functions) zsh_dirs=() for dir in"${directories[@]}"; do zsh_dirs+=("${HOMEBREW_PREFIX}/${dir}") done
directories=(bin etc include lib sbin share var opt share/zsh share/zsh/site-functions var/homebrew var/homebrew/linked Cellar Caskroom Homebrew Frameworks) mkdirs=() for dir in"${directories[@]}"; do if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]; then mkdirs+=("${HOMEBREW_PREFIX}/${dir}") fi done
user_chmods=() if [[ "${#zsh_dirs[@]}" -gt 0 ]]; then for dir in"${zsh_dirs[@]}"; do if user_only_chmod "${dir}"; then user_chmods+=("${dir}") fi done fi
chmods=() if [[ "${#group_chmods[@]}" -gt 0 ]]; then chmods+=("${group_chmods[@]}") fi if [[ "${#user_chmods[@]}" -gt 0 ]]; then chmods+=("${user_chmods[@]}") fi
chowns=() chgrps=() if [[ "${#chmods[@]}" -gt 0 ]]; then for dir in"${chmods[@]}"; do if file_not_owned "${dir}"; then chowns+=("${dir}") fi if file_not_grpowned "${dir}"; then chgrps+=("${dir}") fi done fi
if [[ "${#group_chmods[@]}" -gt 0 ]]; then ohai "The following existing directories will be made group writable:" printf"%s\n""${group_chmods[@]}" fi if [[ "${#user_chmods[@]}" -gt 0 ]]; then ohai "The following existing directories will be made writable by user only:" printf"%s\n""${user_chmods[@]}" fi if [[ "${#chowns[@]}" -gt 0 ]]; then ohai "The following existing directories will have their owner set to ${tty_underline}${USER}${tty_reset}:" printf"%s\n""${chowns[@]}" fi if [[ "${#chgrps[@]}" -gt 0 ]]; then ohai "The following existing directories will have their group set to ${tty_underline}${GROUP}${tty_reset}:" printf"%s\n""${chgrps[@]}" fi if [[ "${#mkdirs[@]}" -gt 0 ]]; then ohai "The following new directories will be created:" printf"%s\n""${mkdirs[@]}" fi
if should_install_command_line_tools; then ohai "The Xcode Command Line Tools will be installed." fi
if [[ -t 0 && -z "${CI-}" ]]; then wait_for_user fi
if [[ -d "${HOMEBREW_PREFIX}" ]]; then if [[ "${#chmods[@]}" -gt 0 ]]; then execute_sudo "/bin/chmod""u+rwx""${chmods[@]}" fi if [[ "${#group_chmods[@]}" -gt 0 ]]; then execute_sudo "/bin/chmod""g+rwx""${group_chmods[@]}" fi if [[ "${#user_chmods[@]}" -gt 0 ]]; then execute_sudo "/bin/chmod""755""${user_chmods[@]}" fi if [[ "${#chowns[@]}" -gt 0 ]]; then execute_sudo "$CHOWN""$USER""${chowns[@]}" fi if [[ "${#chgrps[@]}" -gt 0 ]]; then execute_sudo "$CHGRP""$GROUP""${chgrps[@]}" fi else execute_sudo "/bin/mkdir""-p""${HOMEBREW_PREFIX}" if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then execute_sudo "$CHOWN""root:wheel""${HOMEBREW_PREFIX}" else execute_sudo "$CHOWN""$USER:$GROUP""${HOMEBREW_PREFIX}" fi fi
if [[ "${#mkdirs[@]}" -gt 0 ]]; then execute_sudo "/bin/mkdir""-p""${mkdirs[@]}" execute_sudo "/bin/chmod""g+rwx""${mkdirs[@]}" execute_sudo "$CHOWN""$USER""${mkdirs[@]}" execute_sudo "$CHGRP""$GROUP""${mkdirs[@]}" fi
if ! [[ -d "${HOMEBREW_CACHE}" ]]; then if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then execute_sudo "/bin/mkdir""-p""${HOMEBREW_CACHE}" else execute "/bin/mkdir""-p""${HOMEBREW_CACHE}" fi fi if exists_but_not_writable "${HOMEBREW_CACHE}"; then execute_sudo "/bin/chmod""g+rwx""${HOMEBREW_CACHE}" fi if file_not_owned "${HOMEBREW_CACHE}"; then execute_sudo "$CHOWN""$USER""${HOMEBREW_CACHE}" fi if file_not_grpowned "${HOMEBREW_CACHE}"; then execute_sudo "$CHGRP""$GROUP""${HOMEBREW_CACHE}" fi if [[ -d "${HOMEBREW_CACHE}" ]]; then execute "$TOUCH""${HOMEBREW_CACHE}/.cleaned" fi
if should_install_command_line_tools && version_ge "$macos_version""10.13"; then ohai "Searching online for the Command Line Tools" # This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools clt_placeholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress" execute_sudo "$TOUCH""$clt_placeholder"
if [[ -n "$clt_label" ]]; then ohai "Installing $clt_label" execute_sudo "/usr/sbin/softwareupdate""-i""$clt_label" execute_sudo "/bin/rm""-f""$clt_placeholder" execute_sudo "/usr/bin/xcode-select""--switch""/Library/Developer/CommandLineTools" fi fi
# Headless install may have failed, so fallback to original 'xcode-select' method if should_install_command_line_tools && test -t 0; then ohai "Installing the Command Line Tools (expect a GUI popup):" execute_sudo "/usr/bin/xcode-select""--install" echo"Press any key when the installation has completed." getc execute_sudo "/usr/bin/xcode-select""--switch""/Library/Developer/CommandLineTools" fi
if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && ! output="$(/usr/bin/xcrun clang 2>&1)" && [[ "$output" == *"license"* ]]; then abort "$(cat <<EOABORT You have not agreed to the Xcode license. Before running the installer again please agree to the license by opening Xcode.app or running: sudo xcodebuild -license EOABORT )" fi
ohai "Downloading and installing Homebrew..." ( cd"${HOMEBREW_REPOSITORY}" >/dev/null || return
# we do it in four steps to avoid merge errors when reinstalling execute "git""init""-q"
# "git remote add" will fail if the remote is defined in the global config execute "git""config""remote.origin.url""${BREW_REPO}" execute "git""config""remote.origin.fetch""+refs/heads/*:refs/remotes/origin/*"
# ensure we don't munge line endings on checkout execute "git""config""core.autocrlf""false"
if [[ ":${PATH}:" != *":${HOMEBREW_PREFIX}/bin:"* ]]; then warn "${HOMEBREW_PREFIX}/bin is not in your PATH." fi
ohai "Installation successful!" echo
# Use the shell's audible bell. if [[ -t 1 ]]; then printf"\a" fi
# Use an extra newline and bold to avoid this being missed. ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics." echo"$(cat <<EOS ${tty_bold}Read the analytics documentation (and how to opt-out) here: ${tty_underline}https://docs.brew.sh/Analytics${tty_reset} No analytics data has been sent yet (or will be during this \`install\` run). EOS ) "
ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:" echo"$(cat <<EOS ${tty_underline}https://github.com/Homebrew/brew#donations${tty_reset} EOS ) "
ohai "Next steps:" echo"- Run \`brew help\` to get started" echo"- Further documentation: " echo" ${tty_underline}https://docs.brew.sh${tty_reset}"
if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then case"$SHELL"in */bash*) if [[ -r "$HOME/.bash_profile" ]]; then shell_profile="$HOME/.bash_profile" else shell_profile="$HOME/.profile" fi ;; */zsh*) shell_profile="$HOME/.zprofile" ;; *) shell_profile="$HOME/.profile" ;; esac
cat <<EOS - Install the Homebrew dependencies if you have sudo access: ${tty_bold}Debian, Ubuntu, etc.${tty_reset} sudo apt-get install build-essential ${tty_bold}Fedora, Red Hat, CentOS, etc.${tty_reset} sudo yum groupinstall 'Development Tools' See ${tty_underline}https://docs.brew.sh/linux${tty_reset} for more information. - Configure Homebrew in your ${tty_underline}${shell_profile}${tty_reset} by running echo 'eval \$(${HOMEBREW_PREFIX}/bin/brew shellenv)' >> ${shell_profile} - Add Homebrew to your ${tty_bold}PATH${tty_reset} eval \$(${HOMEBREW_PREFIX}/bin/brew shellenv) - We recommend that you install GCC by running: brew install gcc