macOS 下个人开发环境搭建

女朋友买了一台 mac mini 给我当生日礼物,开心 (ノ>ω<)ノ,新电脑又需要重新配置一下,这篇文章记录我这次配置的过程,作为以后配置的 checklist。
准备
mac mini 是没有键盘鼠标的,首次开机的时候没法直接连接蓝牙键盘(除非是苹果自己的妙控键盘),配置前需要准备好能够连接上的键盘,鼠标,屏幕,网络。
macOS 初始配置
- 用户名设置为
spike
,因为部分 PATH 写死了这个用户,后面看看如何优化 - 检查 icloud 配置,避免同步了不需要的文件夹,导致 icloud 存储被占用
- Mouse 中关闭 Natural scrolling
- 在 Keyboard 中找到 Keyboard Shortcuts1
- Modifier Keys 中,将 Caps Lock 换成 Control;Option 和 Command 交换
- Input Source 中,将 Select the previous input source 换成 Option + Space
- Spotlight 中,将 Show Spotlight search 关闭,因为后面会替换成 Raycast
- 在 Screenshots 中关闭 Screenshot and recording options,因为会和 Emacs 冲突
- Audio MIDI 中设置一下音频的输出采样率
网络配置
需要先配置好科学上网,不然只能用百度搜索东西,满屏的广告。
安装 Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装并配置 v2rayA
brew tap v2raya/v2raya # install brew install v2raya # run on terminal v2raya --lite # run as a brew service brew services start v2raya
安装过程中如果网络比较慢,可以考虑试试手机热点,手机热点不需要开代理。
安装软件
开发环境配置
以前配置 macOS 都是想到什么就配置什么,碰到问题就查文档,这样可能遗漏一些配置,直到碰到问题才发现,而且也比较耗费时间。
我不想每次都这么费劲,之前了解过一点 Makefile 3,这次就决定通过 Makefile 将相关环境配置好。
前阵子在 weekly#31 分享过 Ship Software That Does Nothing,里面作者建议先发布一个空的项目,将其部署起来,然后再慢慢构建功能。
这次我构建 Makefile 也是类似的,一开始只有几行我能想到的配置,然后就执行 make init
安装验证,发现缺失的再补充,最终就得到了一个相对完整的 Makefile 文件。
这样比我先安装好了,再整理成 Makefile 或者笔记要好,能够更早发现问题,验证 Makefile。
以后再初始化 macOS 配置,我只需要执行 make init
就好了。
Makefile
Makefile 中做的主要的事情:
- 通过 symbolic link ,将 dropbox 中的一些文件链接到本地目录,便于操作
- 配置 ohmyzsh,starship,tmux,volta (NodeJS 相关配置) 以及我积攒了一阵子的 dotfiles4。
Makefile
.PHONY: init link dependence install nodejs python git emacs init: link dependence install nodejs link: ln -sf ~/Dropbox/notes ~/notes ln -sf ~/Dropbox/org ~/org ln -sf ~/Dropbox/beancount ~/beancount ln -sf ~/Dropbox/init/.authinfo.gpg ~/.authinfo.gpg dependence: # 确保 /usr/local/bin 存在且有权限 sudo mkdir -p /usr/local/bin sudo chown -R $(shell whoami):admin /usr/local/bin # 检查并安装 ohmyzsh if [ ! -d "$$HOME/.oh-my-zsh" ]; then \ echo "Installing ohmyzsh..."; \ sh -c "$$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"; \ else \ echo "ohmyzsh already installed. Skipping."; \ fi # 检查并安装 starship if ! command -v starship >/dev/null; then \ echo "Installing starship..."; \ curl -sS https://starship.rs/install.sh | sh; \ else \ echo "starship already installed. Skipping."; \ fi # 安装 zsh 插件和工具 brew install zsh-syntax-highlighting echo "source $$(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> $${ZDOTDIR:-$$HOME}/.zshrc if [ ! -d "$${ZSH_CUSTOM:-$$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions" ]; then \ git clone git@github.com:zsh-users/zsh-autosuggestions $${ZSH_CUSTOM:-$$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions; \ fi # GNU related brew install coreutils brew install gnupg # tmux brew install tmux # fzf brew install fzf # mcfly, for search history brew install mcfly nodejs: # volta curl https://get.volta.sh | bash volta install node volta install pnpm volta install @antfu/ni volta install browser-sync git: git config --global user.name "Spike" git config --global user.email "l-yanlei@hotmail.com" python: # require phthon: https://www.python.org/ # aider python3 -m pip install aider-install && aider-install # beancount sudo -H python3 -m pip install beancount python3 -m pip install fava # fix: omz_urlencode:5: command not found: pygmentize python3 -m pip install Pygments emacs: sudo ln -s /Applications/Emacs.app/Contents/MacOS/Emacs /usr/local/bin/emacs sudo ln -s /Applications/Emacs.app/Contents/MacOS/bin/emacsclient /usr/local/bin install: # 安装 dotfiles cd ~/Dropbox/dotfiles && ./install.sh
配置 Git
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
GitHub
配置 GitHub SSH keys,用于拉取 GitHub 上的 Repos。
# generate ssh keys ssh-keygen # copy ssh keys pbcopy < ~/.ssh/id_ed25519.pub
配置 Emacs
下载 我的 Emacs 配置:
git clone --recurse-submodules -j8 git@github.com:Spike-Leung/emacs.d.git ~/.emacs.d
gpg
由于 Emacs 配置中涉及到读取 .authinfo.gpg
,还需要配置好 gpg 密钥。
需要在原来的电脑上导出密钥对:
gpg --export-secret-keys --armor <KeyID or Email> > my-private-key.asc
然后通过 U 盘等相对安全的方法,将密钥对复制到当前电脑中,并导入:
gpg --import my-private-key.asc
org-protocol
平时从浏览器收集阅读列表用的是 org-protocol,通过 Tampermonkey 集成了一些快捷键,看到想看的网站就执行 alt + l
,链接就会存储到我的 reading-list 笔记文件中。
font
Emacs 中有一些 unicode 符号需要字体支持才能显示,例如 ⸺ ,可以下载 Noto Sans 字体使得在 Emacs 中能正常显示。
写在最后
以上就是我使用电脑的基本配置啦,它能满足我上网、写代码和博客、记录和查阅笔记、终端的使用,基本覆盖了平时大部分的使用场景。
当然还缺失了很多软件,例如 截图工具,视频播放器,硬盘读取工具等,但相对而言无关紧要,需要的时候再慢慢安装就好了。
如果涉及到基本配置相关的配置,我还是会优先更新 Makefile,从而减少下次配置的麻烦。
另外如果你对我 Windows 上的配置感兴趣,也可以看看 Windows 下个人开发环境搭建,不过已经有段时间了,等下次组装主机,我再更新一下,也会考虑用 Makefile 整理配置。
脚注:
macOS 下如何方便地切换中英文?总是切换输入法感觉有点麻烦。不知道有没有更好的办法,像是 Windows,按一下 shift 就能切换就比较方便。原来用过 Rime,但是现在懒得配置了,能用系统自带的就用自带的。
Makefile Tutorial 是一个不错的 Makefile 入门教程。
关于 dotfiles 建议看看 Managing your Dotfiles,维护一份你的 dotfiles,便于在不同环境保持一致的体验。
安装后执行 pip 遇到 ssl certificate errors,关闭代理,进入到 /Applications/Python 3.13
目录执行 Install Certificates.command
后就好了,之后开代理也没关系。
在安装 aider 时发现 aider-install 的执行文件路径不在 $PATH
中,还需要设置一下 $PATH
,这个路径和 python 版本号耦合,可能之后安装还会变化。