最近突然要用到mac写一些代码,打开老旧的M1 Mac一看,好家伙,里面的一些开发环境都是很多年前的配置了,而且乱七八糟的,根本不知道当年是怎么胡乱配置的。于是想着从头开始吧,直接就把这台mac恢复出厂设置了。开机—>更新系统后,从头开始吧,然后简单记录了一下(配置的差不多了把.zsh_history文件丢给LLM,让LLM整理一下直接生成一篇笔记,以供后续参考。)

系统:macOS 26.2(ARM/M1/M2/M3/M4 兼容)
目标:基础的 zsh + Homebrew + Python + Node 开发环境


📋 一、系统基础检查

ls                    # 列出当前目录
ls -all               # 列出含隐藏文件的所有内容
sw_vers               # 查看 macOS 版本
xcode-select -p       # 检查 Xcode CLI 工具路径(正常应为 /Library/Developer/CommandLineTools)
xcode-select --install # 若未安装,触发安装(编译依赖必备)
zsh --version         # 查看 Zsh 版本(macOS 默认 shell)
python --version      # 系统自带 Python(⚠️ 已弃用,勿依赖)
python3 --version     # 系统或用户安装的 Python 3
git                   # 检查 Git 是否可用(若未装会提示安装)
brew --version        # 检查 Homebrew 是否已安装

⚠️ 提示:macOS 自带 Python 在 12.3+ 已彻底移除 python 命令,仅保留 python3


🍺 二、Homebrew 安装与清华镜像配置(核心加速)

2.1 标准安装失败 → 改用清华镜像克隆安装

# 尝试官方一键安装(国内易失败)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# ✅ 推荐:清华镜像克隆安装(更稳定)
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install  # 清理临时目录

2.2 配置全链路镜像(写入 ~/.zprofile

# 参考:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/
# 写入 ~/.zprofile(仅需各 1 次)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zprofile
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zprofile
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zprofile
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
echo 'export HOMEBREW_PIP_INDEX_URL="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"' >> ~/.zprofile

# 立即生效(当前终端)
eval "$(/opt/homebrew/bin/brew shellenv)"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

✅ 效果:brew install/update/search 全部走清华源,速度提升 5–10 倍
🔍 验证:brew update && brew search zsh


🐚 三、Zsh 与终端体验增强

3.1 升级 Zsh 并设为默认 shell

brew install zsh
which zsh                    # 应为 /opt/homebrew/bin/zsh
sudo vi /etc/shells          # 添加 /opt/homebrew/bin/zsh 到文件末尾
chsh -s /opt/homebrew/bin/zsh  # 切换默认 shell
echo $SHELL                  # 验证:输出 /opt/homebrew/bin/zsh

3.2 Oh My Zsh(OMZ)安装与插件(后因性能问题卸载)

# 安装 OMZ
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 插件:自动建议 + 语法高亮
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# 主题:Powerlevel10k(需后续 `p10k configure`)
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

⚠️ 后期执行 uninstall_oh_my_zsh —— 因发现 OMZ 加载拖慢终端启动(实测 >800ms)


⚙️ 四、Shell 性能优化(重点实践)

4.1 测速 & 分析

time zsh -i -c exit   # 查看启动耗时(优化前常 >1s)
# 在 ~/.zshrc 开头加:
# zmodload zsh/zprof
zprof                 # 查各阶段耗时(插件/主题/路径是瓶颈)

4.2 卸载 OMZ + 改用轻量方案(✅ 推荐最终方案)

uninstall_oh_my_zsh     # 卸载 OMZ
rm -f ~/.zcompdump*     # 清理补全缓存
rm -f ~/.p10k.zsh       # 删除 P10K 缓存配置

4.3 使用 Homebrew 安装 Zsh 插件(更稳定)

brew install zsh-autosuggestions zsh-syntax-highlighting zsh-history-substring-search
# 其他:brew install zsh-completions # 这个包提供了对多种常见命令(如 Git、npm、kubectl 等)的更丰富补全脚本。

并在 ~/.zshrc 中添加(无 OMZ 依赖):

# Zsh 插件(brew 安装版)
source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source $(brew --prefix)/share/zsh-history-substring-search/zsh-history-substring-search.zsh

# 启用方向键历史子串搜索(按 ↑ 输入 "git" 可搜 git 命令)
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

✅ 优化后实测:time zsh -i -c exit50–80ms


🐍 五、Python 环境:多方案管理(uv )

5.1 Homebrew Python(备用)

brew install python          # 安装 Python 3.x
brew link --overwrite python # 若多版本冲突,强制 link
python3 --version            # 验证
pip3 --version

5.2 uv—— 超快包/虚拟环境工具(主力推荐)

brew install uv
uv --version
uv python list               # 列出所有可用 Python 解释器

创建 & 使用虚拟环境(FastAPI 项目实例)

mkdir backend && cd backend
uv venv --python 3.11        # 创建 .venv(⚠️ 卸载不是 `uv venv remove`!应 `rm -rf .venv`)
source .venv/bin/activate    # 激活
python --version             # 应为 3.11.x

安装依赖(替代 pip install

uv add fastapi uvicorn sqlalchemy psycopg2-binary python-dotenv alembic passlib python-jose
# 查看:
uv pip list

运行 FastAPI

uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

uvpip 快 10–100 倍,支持 .toml 配置(~/.config/uv/uv.toml):

index-url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
python-preference = "only-managed"

🟩 六、Node.js & 前端工程化

6.1 直接 brew install node(弃用 nvm)

brew install node
node --version
npm --version

# 配置淘宝 NPM 镜像
npm config set registry https://registry.npmmirror.com
# 或写入 ~/.npmrc:
# registry=https://registry.npmmirror.com

6.2例子:创建 Vue 3 前端项目(Vite + Tailwind + DaisyUI)

npm create vite@latest frontend -- --template vue
cd frontend
# 安装 Tailwind CSS(指定兼容版本)
npm install -D tailwindcss@3.4.17 postcss autoprefixer
npx tailwindcss init -p  # 生成 tailwind.config.js + postcss.config.js
# 安装 DaisyUI(Tailwind 组件库)
npm install daisyui
# 启动开发服务器
npm run dev

⚠️ 注意:曾 npm uninstall tailwindcss 后重装固定版本 → 避免新版本破坏性更新。


🐍 七、Miniforge(Conda)—— AI/数据科学专用

# 下载并安装 Miniforge3(conda-forge 社区版,ARM 优化)
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-*.sh  # 按提示安装(建议不自动 init conda)
# 设置conda源参考:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
# 关闭 base 自动激活(推荐)
conda config --set auto_activate_base false

创建虚环境 与jupyter使用

# 1. 在 base 装 Jupyter Lab
# 确保处于 base 环境
conda activate base
# 安装 Jupyter Lab(比 Notebook 更现代)
conda install -c conda-forge jupyterlab
# 或仅安装 Notebook(轻量)
# conda install -c conda-forge notebook
jupyter --version   # 验证安装成功

# 2. 创建并配置 AI 环境
conda create -n ai_env python=3.13 -y
conda activate ai_env
conda install -c conda-forge -y ipykernel pandas numpy matplotlib jupyter

# 3. 注册ipykernel(已装 nb_conda_kernels,会自动注册,此步可省!)
# python -m ipykernel install --user --name ai_env --display-name "AI (3.13)"

# 4. 启动jupyter:回到 base 环境启动(环境干净)
conda activate base
jupyter lab   # 或 jupyter notebook
#打开浏览器 → 新建 Notebook → 顶部菜单 Kernel > Change kernel即可看到 Python 3.11 (ai_env),选择后即使用该环境的所有包!

# 5. 其他操作(建议在base环境中操作)
jupyter kernelspec list # 检查已注册内核
## 导出环境(不含 build 信息,更跨平台)
conda env export -n ai_env --from-history > environment.yml
conda env create -f environment.yml # 使用导出配置创建虚环境
## 启用代码自动补全(Jupyter Lab 必装)
conda install -c conda-forge jupyterlab-lsp python-lsp-server
##  安装 nb_conda_kernels —— 自动发现所有 conda 环境
conda install -c conda-forge nb_conda_kernels
### 无需手动 python -m ipykernel install
### 所有含 ipykernel 的 conda 环境自动出现在 Jupyter 内核列表,环境删除后自动消失(更干净)
### ⚠️ 注意:它只是「发现器」,各环境仍需安装 ipykernel。

📌 环境隔离建议:

  • Web 后端 → uv 虚拟环境
  • 数据/AI → conda 环境
  • 避免混用 /opt/homebrew/bin/python 与 conda Python

🔧 八、其他关键配置

Git 全局设置

git config --global user.name "xxxxxx"
git config --global user.email "xxxxxx@xx.com"

安装Nerd Fonts字体(Powerlevel10k 推荐)

# Nerd Fonts是一组专为开发者设计的打补丁(patched)字体集合,它本身不是一种新字体,而是将数百个开源字体(如 Fira Code、JetBrains Mono、Hack、Cascadia Code 等)批量注入大量编程/终端专用图标(glyphs) 的成果。
# 它的核心优势在于:让终端、编辑器、Status Bar 能稳定、美观地显示各类 DevOps/AI 工具所需的图标,而无需手动配置或担心乱码。
brew install font-hack-nerd-font
# 安装后需在终端设置中选用 Hack Nerd Font

✅ 最佳实践总结

模块 推荐工具 理由
包管理 Homebrew + 清华镜像 稳定、ARM 优化、国内快
Shell Zsh + brew 插件(无 OMZ) 启动 <100ms,功能全
Python uv(Web) + conda(AI) 速度最快 + 科学计算生态
Node brew install node + npmmirror 简单直接,避免 nvm 路径问题