macOS 配置
全新 Mac 从零开始的完整初始化流程,涵盖 Homebrew、Fish Shell、Ghostty 终端等开发工具链配置。
全新 Mac 从零开始的完整初始化流程。基于 Apple Silicon,所有路径以 /opt/homebrew 为准。
一键部署(下文所有步骤的自动化脚本):
curl -fsSL https://gist.githubusercontent.com/conversun/cedbae05616495796052e5b1da5355ff/raw/setup-mac.sh -o /tmp/setup-mac.sh && bash /tmp/setup-mac.sh
为了保留交互能力(Git / SSH、订阅选项、系统偏好确认),这里采用“先下载再执行”,不要直接 curl | bash。以下为各步骤的详细说明。
1. 前置准备
1.1 Xcode Command Line Tools
Homebrew 和大部分开发工具的前置依赖,必须先装:
xcode-select --install
弹窗确认后等安装完成,耗时约 5-10 分钟。
1.2 Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装完成后按提示执行(Apple Silicon 路径):
eval "$(/opt/homebrew/bin/brew shellenv)"
验证安装:
brew doctor
2. 终端 — Ghostty
2.1 安装
brew install --cask ghostty
2.2 配置
配置文件路径:~/.config/ghostty/config
# 字体
font-size = 14
font-family = Monaco
# 中文字符回退到 Noto Sans SC,避免 Monaco 中文渲染问题
font-codepoint-map = U+4E00-U+9FFF=Noto Sans SC
# 主题与背景
theme = Dracula+
background = #000000
background-opacity = 0.75
background-blur-radius = 70
# 默认 shell(见第 3.1 节说明)
command = /opt/homebrew/bin/fish
Noto Sans SC 需要提前安装:brew install font-noto-sans-cjk-sc,或直接从 Google Fonts 下载。3. Shell — Fish + Fisher
3.1 安装 Fish 并设为默认 Shell
brew install fish
# 将 fish 加入系统可用 shell 列表
echo /opt/homebrew/bin/fish | sudo tee -a /etc/shells
# 设为默认 shell
chsh -s /opt/homebrew/bin/fish
注销并重新登录 macOS 后生效。仅重开终端窗口不够——chsh 修改的是系统用户数据库,但 $SHELL 环境变量在登录时才刷新,终端读到的仍是旧值。
Ghostty 用户注意:Ghostty 检测 shell 的优先级是command配置 >$SHELL环境变量 > 系统用户数据库。即使chsh生效,Ghostty 也优先读$SHELL。最可靠的做法是在 Ghostty 配置里直接指定command = /opt/homebrew/bin/fish(第 2.2 节已包含)。
3.2 安装 Fisher
Fisher 是轻量级 Fish 插件管理器,纯函数式,不侵入 config.fish:
curl -sL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher
3.3 安装插件与主题
# z — 目录跳转,根据访问频率模糊匹配
fisher install jethrokuan/z
# Hydro — 极简提示符主题,异步 Git 状态,Fisher 作者出品
fisher install jorgebucaran/hydro
Hydro 安装后即可使用,无需配置向导。通过 set --universal 自定义:
# 可选:自定义提示符号(默认 ❱)
set -U hydro_symbol_prompt ❯
# 可选:多行模式(提示符换行显示)
set -U hydro_multiline true
# 可选:自动 fetch 远程分支状态
set -U hydro_fetch true
4. 开发环境
4.1 mise — 统一版本管理
mise 统一管理 Ruby 和 Node.js 版本,替代 rbenv + fnm,一个工具搞定:
brew install mise
# 激活 mise(永久配置见第 5 节 config.fish)
mise activate fish | source
Ruby
# libyaml — Ruby 编译时依赖,缺少会导致 Psych (YAML) 模块构建失败
brew install libyaml
mise use --global ruby@3.3
# 验证
ruby --version # → ruby 3.3.x
# 常用 gem
gem install cocoapods fastlane
Node.js
mise use --global node@24
# 验证
node --version # → v24.x.x
# 常用全局包
npm i -g npm-check-updates wrangler
注意:如果 Homebrew 自带了 node(常见于 mongosh 等包的依赖),它会在 PATH 中排在 mise 前面导致版本不对。解决方法:
项目级版本锁定
在项目根目录创建 .mise.toml 即可指定该项目的语言版本:
[tools]
node = "20"
ruby = "3.2"
进入目录自动切换,无需手动操作。
4.2 Python — uv
uv 同时管理 Python 版本和包,不需要再用 mise 管 Python:
curl -LsSf https://astral.sh/uv/install.sh | sh
# 安装 Python
uv python install 3.14
# 验证
python3 --version # → Python 3.14.x
# 项目初始化(自动创建 venv + pyproject.toml)
uv init my-project
cd my-project
uv add requests # 添加依赖
4.3 Bun
curl -fsSL https://bun.sh/install | bash
验证:
bun --version # → 1.x.x
4.4 AI 编码工具
终端 AI 编码助手,理解完整代码库,直接在命令行中辅助开发。
OpenCode
开源终端 AI 编码助手,支持多模型后端(Anthropic / OpenAI / 本地模型):
curl -fsSL https://opencode.ai/install | bash
安装 oh-my-openagent 插件,提供多 Agent 协作、模型路由等增强能力:
bunx oh-my-opencode install --no-tui --claude=yes --openai=no --gemini=no --copilot=no
根据实际订阅调整 flag,支持--openai=yes、--gemini=yes、--copilot=yes等组合。完整选项见 安装文档。
进入项目目录后启动 TUI:
cd your-project
opencode
Claude Code
Anthropic 出品的终端 AI 编码工具,能跨文件协作、执行命令、操作 Git:
curl -fsSL https://claude.ai/install.sh | bash
安装后自带后台自动更新。进入项目目录后启动:
cd your-project
claude
首次运行会提示登录 Anthropic 账号。需要 Claude 订阅 或 Anthropic Console API 额度。
5. Fish 完整配置
~/.config/fish/config.fish:
set -g fish_greeting "All to be nice"
# mise — Ruby, Node.js 版本管理
mise activate fish | source
# uv — Python
fish_add_path $HOME/.local/bin
# Bun
set -gx BUN_INSTALL "$HOME/.bun"
fish_add_path $BUN_INSTALL/bin
# Homebrew
eval (/opt/homebrew/bin/brew shellenv)
if status is-interactive
alias python python3
alias pip pip3
end
# ZMX / SSH session 终端标题
function fish_title
set -l prefix ""
if set -q ZMX_SESSION
set prefix "[$ZMX_SESSION] "
else if set -q SSH_CONNECTION
set prefix "["(hostname -s)"] "
end
echo $prefix(string join " — " (string match -r '.+' $argv) (prompt_pwd))
end
配置要点:
写入后重新加载配置:
source ~/.config/fish/config.fish
6. Git + SSH
6.1 Git 全局配置
git config --global user.name "你的名字"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
git config --global pull.rebase true
6.2 SSH 密钥
# 生成 Ed25519 密钥(比 RSA 更短更安全)
ssh-keygen -t ed25519 -C "your@email.com"
一路回车使用默认路径。然后添加到 SSH Agent:
eval (ssh-agent -c)
ssh-add ~/.ssh/id_ed25519
复制公钥:
pbcopy < ~/.ssh/id_ed25519.pub
分别粘贴到:
- GitHub → Settings → SSH and GPG keys
- GitLab → Preferences → SSH Keys
6.3 多平台 SSH 配置
如果同时使用 GitHub 和自建 GitLab,配置 ~/.ssh/config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host git.socloud.ltd
HostName git.socloud.ltd
User git
IdentityFile ~/.ssh/id_ed25519
7. CLI 工具
一行装完常用命令行工具:
brew install git gh jq ripgrep fd bat eza wget httpie
8. GUI 应用
brew install --cask \
visual-studio-code \
orbstack \
google-chrome \
obsidian \
raycast \
maccy \
iina \
the-unarchiver \
tencent-lemon \
stats \
bob \
imageoptim \
sourcetree \
bruno \
feishu \
wechat \
wetype
Ghostty 已在第 2 节安装。
9. macOS 系统偏好
通过 defaults write 命令批量配置,复制粘贴即可:
# ═══ Dock ═══
defaults write com.apple.dock orientation -string "left" # Dock 放到左侧
defaults write com.apple.dock tilesize -int 48 # 图标大小
# ═══ 键盘 ═══
defaults write NSGlobalDomain KeyRepeat -int 2 # 按键重复速度(最快)
defaults write NSGlobalDomain InitialKeyRepeat -int 15 # 按键重复前延迟(最短)
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false # 关闭长按弹出重音字符,改为连续输入
# ═══ Finder ═══
defaults write com.apple.finder AppleShowAllExtensions -bool true # 显示所有文件扩展名
defaults write com.apple.finder ShowPathbar -bool true # 显示路径栏
defaults write com.apple.finder _FXShowPosixPathInTitle -bool true # 标题栏显示完整路径
# ═══ Spotlight ═══
# 已用 Raycast 替代,关闭 Spotlight 索引释放资源
sudo mdutil -a -i off
# ═══ 截图 ═══
mkdir -p ~/Pictures/Screenshots
defaults write com.apple.screencapture location ~/Pictures/Screenshots # 截图保存位置
# 重启相关服务使设置生效
killall Dock Finder
键盘设置中 ApplePressAndHoldEnabled 对开发者几乎必开——关掉后长按字母键会连续输入而不是弹出变音符选择。