HomeBrew
2023年03月25日
一、认识
二、安装
1. 创建一个名为 install_homebrew.sh
的脚本
#!/bin/bash
set -e # 遇到错误时停止脚本执行
# 定义变量
BREW_INSTALL_URL="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
BREW_BIN_PATH_ARM64="/opt/homebrew/bin/brew" # Homebrew 默认安装路径 for Apple Silicon
BREW_BIN_PATH_INTEL="/usr/local/bin/brew" # Homebrew 默认安装路径 for Intel Macs
ARCH=$(uname -m)
# 检查 Homebrew 是否已安装
if command -v brew &> /dev/null; then
echo "Homebrew 已经安装。"
exit 0
fi
# 根据芯片类型设置 Homebrew 路径
if [[ "$ARCH" == "arm64" ]]; then
BREW_BIN_PATH="$BREW_BIN_PATH_ARM64"
echo "检测到 Apple Silicon (M1) 芯片,将安装 Homebrew 到 /opt/homebrew"
else
BREW_BIN_PATH="$BREW_BIN_PATH_INTEL"
echo "检测到 Intel 芯片,将安装 Homebrew 到 /usr/local"
fi
# 安装 Homebrew
echo "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL $BREW_INSTALL_URL)"
# 验证安装
if ! command -v brew &> /dev/null; then
echo "Homebrew 安装失败。"
exit 1
fi
echo "Homebrew 安装成功。"
# 确定配置文件
CONFIG_FILE=""
if [[ -f "$HOME/.zshrc" ]]; then
CONFIG_FILE="$HOME/.zshrc"
elif [[ -f "$HOME/.bashrc" ]]; then
CONFIG_FILE="$HOME/.bashrc"
elif [[ -f "$HOME/.bash_profile" ]]; then
CONFIG_FILE="$HOME/.bash_profile"
else
echo "未找到 .zshrc, .bashrc 或 .bash_profile 配置文件。请手动配置 PATH。"
exit 1
fi
# 添加 Homebrew 到 PATH
echo "Configuring PATH for Homebrew in $CONFIG_FILE..."
# 检查是否已配置
if grep -q "$BREW_BIN_PATH" "$CONFIG_FILE"; then
echo "Homebrew path already present in $CONFIG_FILE."
else
echo "export PATH=\"$BREW_BIN_PATH:\$PATH\"" >> "$CONFIG_FILE"
echo "Homebrew path added to $CONFIG_FILE."
fi
# 提示重新加载配置文件
echo "To apply the changes, please reload your shell configuration by running: source $CONFIG_FILE"
echo "Script execution completed successfully!"
# 自动重新加载配置文件(可选)
# shellcheck disable=SC1090
source "$CONFIG_FILE"
2. 赋予脚本可执行权限
chmod +x install_homebrew.sh
3. 执行脚本,安装 Docker
./install_homebrew.sh
三、操作
3.1 搜索工具
brew search nginx
3.2 安装工具
brew install nginx
3.3 卸载工具
brew uninstall nginx
3.4 更新工具
brew update
更新Homebrew
自己,它会抓取新的软件包列表,并将Homebrew
自己升级到最新版本。
brew update
brew upgrade
将所有已经安装的软件包更新到最新版本。
brew upgrade
brew upgrade [package]
只更新指定软件包的版本。
brew upgrade nginx
3.5 工具列表
brew list
3.6 工具信息
brew info nginx
3.7 安装扩展
brew
有个默认的仓库,brew tap
你可以看成是第三方的仓库。
brew tap <gihhub_user/repo>
3.8 显示扩展
brew ls-taps
3.9 切换镜像源
-
切换中科大源
# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 替换homebrew-cask.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 应用生效
brew update
# 替换homebrew-bottles:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile (或者 .zshrc)
source ~/.bash_profile (或者 .zshrc) -
切换官方源
# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
# 替换homebrew-cask.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://github.com/Homebrew/homebrew-cask.git
# 应用生效
brew update
# 删除.bash_profile变量
vim ~/.bash_profile (或者 .zshrc)
# 删除如下变量
# export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
# 执行更新
source ~/.bash_profile (或者 .zshrc) -
切换阿里源
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile (或者 .zshrc)
source ~/.bash_profile (或者 .zshrc)
四、问题
4.1 brew --version
时提示 fatal: unsafe repository ('/opt/homebrew/Library/Taps/homebrew/homebrew-core' is owned by someone else)
**
Preview
解决方法: 按提示直接输入下列命令
git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-core
git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-cask