Mac开发环境配置--基础配置
背景
本文主要整理收集MacOS系统进行AI开发、应用开发等的基础环境配置脚本,由于众所周知的🧱原因,导致部分工具无法使用常规方式下载,因此本文打算使用一种自动化的脚本来完成一键配置的环境基础配置功能。
配置
工具 | 安装方法 | 下载地址/命令 |
---|---|---|
Homebrew | 终端执行安装脚本: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | 官网 安装指南 |
Git | 1. 官网下载安装包 2. 或通过 Homebrew 安装:brew install git | 官网 阿里镜像 |
Python | 1. 系统预装(macOS 自带 Python 2.7) 2. 安装新版:brew install python@3.11 | 官网 |
Pip | 随 Python 3.4+ 自动安装,或手动安装: curl https://bootstrap.pypa.io/get-pip.py | |
Pyenv | 通过 Homebrew 安装: brew install pyenv 需在 ~/.zshrc 添加: eval "$(pyenv init -)" | GitHub |
Oh-my-zsh | 终端执行: sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" | 官网 |
*** | *** | *** |
Sublime Text | 1. 官网下载 dmg 安装包 2. 或通过 Homebrew:brew install --cask sublime-text | 官网 |
MacDown | Homebrew 安装: brew install --cask macdown | GitHub |
Modern CSV | 1. 官网下载 dmg 文件 2. App Store 搜索安装 | 官网 功能对比 |
自动化
通过一键自动化安装配置脚本安装以上工具
#!/bin/bash
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# 网络探测函数
test_url() {
local url=$1
if curl --silent --connect-timeout 3 --head "$url" >/dev/null; then
return 0
else
return 1
fi
}
# GitHub IP探测
select_github_ip() {
declare -A github_ips=(
["主镜像IP1"]="140.82.114.4"
["主镜像IP2"]="199.232.96.133"
["备用IP1"]="151.101.72.133"
["备用IP2"]="151.101.76.133"
)
for desc in "${!github_ips[@]}"; do
ip=${github_ips[$desc]}
echo -n "测试 $desc ($ip)..."
if ping -c 1 -W 1 "$ip" &>/dev/null; then
echo -e "${GREEN}可用${NC}"
selected_ip=$ip
return 0
else
echo -e "${RED}不可用${NC}"
fi
done
echo -e "${RED}所有GitHub IP均不可用,跳过配置${NC}"
return 1
}
# Pip镜像源探测
select_pip_mirror() {
declare -A pip_mirrors=(
["清华"]="https://pypi.tuna.tsinghua.edu.cn/simple"
["阿里云"]="https://mirrors.aliyun.com/pypi/simple/"
["腾讯云"]="https://mirrors.cloud.tencent.com/pypi/simple"
["华为云"]="https://repo.huaweicloud.com/repository/pypi/simple"
)
for name in "${!pip_mirrors[@]}"; do
url=${pip_mirrors[$name]}
echo -n "测试 $name 镜像源..."
if test_url "$url"; then
echo -e "${GREEN}可用${NC}"
selected_mirror=$url
return 0
else
echo -e "${RED}不可用${NC}"
fi
done
echo -e "${RED}所有pip镜像源均不可用,跳过配置${NC}"
return 1
}
# Homebrew镜像源探测
select_brew_mirror() {
declare -A brew_mirrors=(
["清华"]="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
["中科大"]="https://mirrors.ustc.edu.cn/brew.git"
["阿里云"]="https://mirrors.aliyun.com/homebrew/brew.git"
)
for name in "${!brew_mirrors[@]}"; do
url=${brew_mirrors[$name]}
echo -n "测试 $name 镜像源..."
if test_url "$url"; then
echo -e "${GREEN}可用${NC}"
selected_brew=$url
selected_core=${url/brew.git/homebrew-core.git}
return 0
else
echo -e "${RED}不可用${NC}"
fi
done
echo -e "${RED}所有brew镜像源均不可用,跳过配置${NC}"
return 1
}
# 配置 GitHub Hosts
config_github_hosts() {
select_github_ip || return 1
echo "▶ 正在配置 GitHub Hosts (使用IP: $selected_ip)..."
sudo sed -i '' '/# GitHub Mirror Start/,/# GitHub Mirror End/d' /etc/hosts 2>/dev/null
sudo sh -c "echo -e '\n# GitHub Mirror Start\n$selected_ip github.com\n# GitHub Mirror End' >> /etc/hosts"
# 刷新DNS缓存
case "$(uname)" in
Darwin*) sudo dscacheutil -flushcache ;;
Linux*) sudo systemctl restart network-manager || sudo systemctl restart NetworkManager ;;
*) echo "⚠️ 请手动刷新DNS缓存" ;;
esac
echo "✅ GitHub Hosts 配置完成"
}
# 配置 pip 镜像源
config_pip_mirror() {
select_pip_mirror || return 1
echo "▶ 正在配置 pip 镜像源: $selected_mirror..."
PIP_CONF_FILE="${HOME}/.pip/pip.conf"
[[ ! -d ~/.pip ]] && mkdir -p ~/.pip
cat > "$PIP_CONF_FILE" <<EOF
[global]
index-url = $selected_mirror
trusted-host = $(echo $selected_mirror | awk -F/ '{print $3}')
EOF
echo "✅ pip 镜像源已配置"
}
# 配置 Homebrew 镜像源
config_brew_mirror() {
select_brew_mirror || return 1
echo "▶ 正在配置 Homebrew 镜像源..."
git -C "$(brew --repo)" remote set-url origin "$selected_brew"
git -C "$(brew --repo homebrew/core)" remote set-url origin "$selected_core"
brew update
echo "✅ Homebrew 镜像源已配置"
}
# 主菜单
show_menu() {
echo "
======== 镜像配置工具 ========
1) 配置 GitHub Hosts (含备用IP)
2) 配置 pip 多镜像源
3) 安装 Homebrew(国内镜像)
4) 配置 Homebrew 镜像源
5) 一键配置全部
0) 退出
============================
输入数字选择操作: "
}
# 执行全部配置
all_in_one() {
config_github_hosts
config_pip_mirror
config_brew_mirror
}
main() {
while true; do
show_menu
read -r choice
case $choice in
1) config_github_hosts ;;
2) config_pip_mirror ;;
3) install_homebrew_cn ;;
4) config_brew_mirror ;;
5) all_in_one ;;
0) exit 0 ;;
*) echo "无效输入,请重新选择" ;;
esac
done
}
main