MacOS环境配置

阅读约 13 分钟 macos

最近实在没忍住,以 2944 的价格入手了一台 mac mini m4 丐版,而我之前一直使用的是一台2019年 Intel 芯片的 macbook pro,因为刚开始使用,开发环境还没配置好,所以还没感觉到明显地提升,但至少我的桌面倒是干净了不少。

此次入手 mac mini 后我使用 迁移助理 对旧数据进行迁移,不幸的是,由于是 Intel 迁移至 Apple Silicon,所以出现了各种各样的问题,尤其是 Bad CPU type in executable 错误,导致 git、python等二进制文件全部不可用,最后只能重置系统并重头开始配置(注:重置系统只需要到 设置 -> 通用 -> 传输与还原 -> 抹掉所有内容和设置,不用长按开机键进入选项抹掉磁盘)

设置

App Store

因为我一般都是美区ID+国区商店,所以打开 App Store,在左上方的菜单栏中找到 商店 -> 退出登录,并登入国区ID

终端

我还是习惯使用 bash

1chsh -s /bin/bash

并且有使用了十多年的PS1配置

1git_status() {
2    cmd=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/[((]//' -e 's/[))]//'  -e 's/* \(.*\)/(\1)/'`
3    echo -en "\033[32m$cmd\033[0m"
4}
5proxy_status() {
6    [[ -z "${http_proxy}" ]] || echo -en "\033[32m[PROXY]\033[0m"
7}
8PS1="\$(proxy_status)\[\e[0;37m\]\342\224\214\342\224\200 \[[\e[0;33m\]\u\[\e[0;37m\]]\342\224\200[\[\e[1;33m\]\w\[\e[0;37m\]]\$(git_status)\n\[\e[0;37m\]\342\224\224\342\224\200\342\224\200\342\225\274\[\e[0m\] "

如果 ~/.bashrc 不生效,需要添加文件 ~/.bash_profile

1if [ -r $HOME/.bashrc ]; then
2    source $HOME/.bashrc
3fi

必备软件

微信、QQ、QQ音乐、网易云音乐、Localsend等软件可以在商店里直接下载

homebrew

这应该是每个Mac用户的必备软件了吧

1/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

如果安装速度太慢或是下载部分内容时完全卡住,可以先行安装科学上网工具

然后添加以下内容到 ~/.bashrc

1export PATH=/opt/homebrew/bin:$PATH
2
3if which brew > /dev/null;then
4    export HOMEBREW_NO_AUTO_UPDATE=true
5    # export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=true
6    # export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
7    # export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
8    eval "$(brew shellenv)"
9fi

betterdisplay

由于我使用的是一个2K显示器,所以需要开启HIDPI

1brew install --cask betterdisplay

clash-verge-rev

如果之前是通过dmg文件安装的,需要先卸载,然后重新使用brew安装,方便升级和管理

1brew install --cask clash-verge-rev

folo

RSS信息聚合阅读器

1brew install --cask folo

google-chrome

1brew install --cask google-chrome

如果在登陆账号时总是提示请求取消,并且登陆失败,开启全局代理即可

yabai

macos端的平铺窗口管理

1brew install koekeishiya/formulae/yabai
2yabai --start-service

hammerspoon

主要用于搭配 yabai 使用

1brew install --cask hammerspoon

我的配置在 https://github.com/honmaple/dotfiles/tree/master/macos

Localsend

一个内网文件传输工具,可以在商店直接下载,或者使用

1brew install --cask localsend

inkscape

一个矢量图绘制软件

1brew install --cask inkscape

wireshark

抓包工具

1brew install --cask wireshark

字体

  • 终端字体

    1brew install --cask font-maple-mono-nf

其它

1brew install rg
2brew install tree htop
3brew install --cask visual-studio-code

开发软件

Emacs

emacs永存,虽然我是邪恶的evil用户

1brew install --cask railwaycat/emacsmacport/emacs-mac
  • quelpa 依赖

    1brew install gnu-tar
  • toml 格式化工具

    1brew install taplo

Docker

1brew install --cask docker

Ruby

~/.bashrc配置

1export GEM_HOME=$HOME/repo/ruby/gem
2export GEM_PATH=$HOME/repo/ruby/gem
3export GEM_SPEC_CACHE=$HOME/repo/ruby/gem/specs
4export PATH=$GEM_HOME/bin:$PATH

Python

1brew install pyenv pyenv-virtualenv

~/.bashrc配置

1if which pyenv > /dev/null;then
2    export PYENV_ROOT=$HOME/repo/python/pyenv
3    export PATH=$PYENV_ROOT/bin:$PATH
4    eval "$(pyenv init -)";
5fi
6
7if which pyenv-virtualenv-init > /dev/null; then
8    eval "$(pyenv virtualenv-init -)";
9fi
1export PYTHONUSERBASE=$HOME/repo/python
2export PYTHONHISTFILE=$HOME/repo/python/history
3export PATH=$HOME/repo/python/bin:$PATH

开发依赖:

1python3 -m pip install --user python-lsp-server
2python3 -m pip install --user python-lsp-ruff

Golang

1brew install go

~/.bashrc配置

1export GOPROXY=https://goproxy.cn,direct
2export GOPATH=$HOME/repo/golang
3export PATH=$HOME/repo/golang/bin:$PATH

开发依赖:

1go install golang.org/x/tools/gopls@latest
2go install honnef.co/go/tools/cmd/staticcheck@latest
3
4go install golang.org/x/mobile/cmd/gomobile@latest
5gomobile init

Protobuf

1brew install protobuf bufbuild/buf/buf
  • Go

    1go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    2go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
    3go install github.com/favadi/protoc-go-inject-tag@latest
    4go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
    5go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
  • Dart

    1dart --disable-analytics
    2dart pub global activate protoc_plugin 21.1.2

Android

1brew install --cask android-studio

然后打开 android-studio, 并配置 HTTP手动代理,设置sdk目录为 $HOME/repo/android/sdk,设置完成后到达 Welcome to Android Studio 界面,点击 More Actions -> SDK Manager

  • SDK Platforms: 勾选 Android 12.0

  • SDK Tools: 勾选 NDK(Side by side)Android SDK Command-line Tools (latest)

~/.bashrc配置

1export ANDROID_HOME=$HOME/repo/android/sdk
2export NDK_HOME="$ANDROID_HOME/ndk/26.2.11394342"
3export JAVA_HOME='/Applications/Android Studio.app/Contents/jbr/Contents/Home'
4export PATH=$ANDROID_HOME/platform-tools:$PATH

Flutter

下载 flutter_macos_arm64_3.29.3-stable.zip 并解压到 ~/repo

1unzip ~/Downloads/flutter_macos_arm64_3.29.3-stable.zip-d ~/repo/

安装 rosetta

1sudo softwareupdate --install-rosetta --agree-to-license

安装 cocoapods

1export GEM_HOME=$HOME/repo/ruby/gem
2export PATH=$GEM_HOME/bin:$PATH
3
4gem install cocoapods --user-install -V

因为系统内置的ruby版本是 2.6.10,这导致 cocoapods 的部分依赖无法安装,并出现类似下面的错误

1ERROR:  Error installing cocoapods:
2    The last version of securerandom (>= 0.3) to support your Ruby & RubyGems was 0.3.2. Try installing it with `gem install securerandom -v 0.3.2` and then running the current command again
3    securerandom requires Ruby version >= 3.1.0. The current ruby version is 2.6.10.210.

本来还想按照提示处理,但处理完一个又来一个,处理完一个又来一个,太过麻烦。还有一种方法是安装新版本的ruby

1brew install ruby

然后使用新版本ruby的gem安装 cocoapods

1/opt/homebrew/Cellar/ruby/3.4.3/bin/gem install cocoapods -V

这时使用 flutter doctor 会出现下面错误

1[!] Xcode - develop for iOS and macOS (Xcode 16.3)
2    ✗ CocoaPods installed but not working.
3        You appear to have CocoaPods installed but it is not working.
4        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
5        This can usually be fixed by re-installing CocoaPods.
6      For re-installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation
7[✓] Chrome - develop for the web

通过使用 pod --version 查看

 1/opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1421:in 'block in Gem::Specification#activate_dependencies': Could not find 'nkf' (>= 0) among 100 total gem(s) (Gem::MissingSpecError)
 2Checked in 'GEM_PATH=/Users/xxxx/repo/ruby/gem' at: /Users/xxxx/repo/ruby/gem/specifications/CFPropertyList-3.0.7.gemspec, execute `gem env` for more information
 3    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Array#each'
 4    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Gem::Specification#activate_dependencies'
 5    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1389:in 'Gem::Specification#activate'
 6    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1423:in 'block in Gem::Specification#activate_dependencies'
 7    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Array#each'
 8    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Gem::Specification#activate_dependencies'
 9    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1389:in 'Gem::Specification#activate'
10    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1423:in 'block in Gem::Specification#activate_dependencies'
11    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Array#each'
12    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Gem::Specification#activate_dependencies'
13    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1389:in 'Gem::Specification#activate'
14    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems.rb:290:in 'block in Gem.activate_bin_path'
15    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems.rb:289:in 'Thread::Mutex#synchronize'
16    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems.rb:289:in 'Gem.activate_bin_path'
17    from /Users/xxxx/repo/ruby/gem/bin/pod:25:in '<main>'

尝试安装 nkf 终于解决了 cocoapods 的安装问题

1/opt/homebrew/Cellar/ruby/3.4.3/bin/gem install nkf -V

~/.bashrc 配置

1alias flutter='flutter --no-version-check'
2export PUB_HOSTED_URL=https://pub.flutter-io.cn
3export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
4export PATH=$HOME/repo/flutter/bin:$PATH

如果需要使用 fvm

  • 安装

    1dart pub global activate fvm
  • 卸载

    1dart pub global deactivate fvm
  • ~/.bashrc 配置

    1export FVM_CACHE_PATH=$HOME/repo/flutter/fvm
  • 查看可使用的版本

    1fvm releases
  • 切换版本 在flutter项目下执行

    1fvm use 3.39.3
    2fvm use 3.38.10
  • 设置全局版本

    1fvm global 3.29.3

    然后修改 .bashrc

    1export PATH=$HOME/repo/flutter/fvm/default/bin:$PATH

Node

1brew install node

~/.bashrc配置

1export NODE_PATH=$HOME/repo/npm/lib/node_modules
2export PATH=$HOME/repo/npm/bin:$PATH

开发依赖:

1npm install -g yarn
2npm install -g pnpm
3npm install -g typescript-language-server typescript
4npm install -g @vue/language-server
5npm install -g @tailwindcss/language-server
6npm install -g emmet-ls
7npm install -g prettier
8npm install -g js-beautify

设置 pnpm 缓存路径,注意: pnpm 配置文件存储在 ~/Library/Preferences/pnpm/rc

1pnpm config set store-dir ~/repo/pnpm

其它:

1npm install -g @quasar/cli

Rust

1brew install rustup
2rustup-init

~/.bashrc配置

1export CARGO_HOME=$HOME/repo/rust/cargo
2export CARGO_TARGET_DIR=$HOME/repo/rust/cargo/build
3
4export RUSTUP_HOME=$HOME/repo/rust/rustup
5export RUSTUP_DIST_SERVER=https://rsproxy.cn
6export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
7
8export PATH=$HOME/repo/rust/cargo/bin:$PATH

AI软件

  • opencode

    1- brew install opencode
    2+ brew install anomalyco/tap/opencode
    

    如果安装时报错

    1It seems that your package manager failed to install the right version of the opencode CLI for your platform. You can try manually installing the "opencode-darwin-arm64" package

    则可以使用 npm install -g opencode-ai

    桌面版本

    1brew install --cask opencode-desktop
  • claude-code

    1brew install claude-code --cask
  • trae

    1brew install trae --cask
  • codex

    1brew install codex --cask
    2brew install codex-app --cask