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