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

chsh -s /bin/bash

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

git_status() {
    cmd=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/[((]//' -e 's/[))]//'  -e 's/* \(.*\)/(\1)/'`
    echo -en "\033[32m$cmd\033[0m"
}
proxy_status() {
    [[ -z "${http_proxy}" ]] || echo -en "\033[32m[PROXY]\033[0m"
}
PS1="\$(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

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

必备软件

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

homebrew

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

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

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

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

export PATH=/opt/homebrew/bin:$PATH

if which brew > /dev/null;then
    export HOMEBREW_NO_AUTO_UPDATE=true
    # export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=true
    # export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
    # export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
    eval "$(brew shellenv)"
fi

betterdisplay

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

brew install --cask betterdisplay

clash-verge-rev

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

brew install --cask clash-verge-rev

google-chrome

brew install --cask google-chrome

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

yabai

macos端的平铺窗口管理

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

hammerspoon

主要用于搭配 yabai 使用

brew install --cask hammerspoon

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

Localsend

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

brew install --cask localsend

inkscape

一个矢量图绘制软件

brew install --cask inkscape

wireshark

抓包工具

brew install --cask wireshark

开发软件

Emacs

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

brew install --cask railwaycat/emacsmacport/emacs-mac

Docker

brew install --cask docker

Ruby

~/.bashrc配置

export GEM_HOME=$HOME/repo/ruby/gem
export GEM_PATH=$HOME/repo/ruby/gem
export GEM_SPEC_CACHE=$HOME/repo/ruby/gem/specs
export PATH=$GEM_HOME/bin:$PATH

Python

brew install pyenv pyenv-virtualenv

~/.bashrc配置

if which pyenv > /dev/null;then
    export PYENV_ROOT=$HOME/repo/python/pyenv
    export PATH=$PYENV_ROOT/bin:$PATH
    eval "$(pyenv init -)";
fi

if which pyenv-virtualenv-init > /dev/null; then
    eval "$(pyenv virtualenv-init -)";
fi
export PYTHONUSERBASE=$HOME/repo/python
export PYTHONHISTFILE=$HOME/repo/python/history
export PATH=$HOME/repo/python/bin:$PATH

Golang

brew install go

~/.bashrc配置

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

开发依赖:

go install golang.org/x/tools/gopls@latest
go install honnef.co/go/tools/cmd/staticcheck@latest

go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init

Protobuf

brew install protobuf bufbuild/buf/buf
  • Go

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

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

Android

brew 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配置

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

Flutter

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

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

安装 rosetta

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

安装 cocoapods

export GEM_HOME=$HOME/repo/ruby/gem
export PATH=$GEM_HOME/bin:$PATH

gem install cocoapods --user-install -V

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

ERROR:  Error installing cocoapods:
    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
    securerandom requires Ruby version >= 3.1.0. The current ruby version is 2.6.10.210.

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

brew install ruby

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

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

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

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

通过使用 pod --version 查看

/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)
Checked 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
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Array#each'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Gem::Specification#activate_dependencies'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1389:in 'Gem::Specification#activate'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1423:in 'block in Gem::Specification#activate_dependencies'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Array#each'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Gem::Specification#activate_dependencies'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1389:in 'Gem::Specification#activate'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1423:in 'block in Gem::Specification#activate_dependencies'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Array#each'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1407:in 'Gem::Specification#activate_dependencies'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems/specification.rb:1389:in 'Gem::Specification#activate'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems.rb:290:in 'block in Gem.activate_bin_path'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems.rb:289:in 'Thread::Mutex#synchronize'
    from /opt/homebrew/Cellar/ruby/3.4.3/lib/ruby/3.4.0/rubygems.rb:289:in 'Gem.activate_bin_path'
    from /Users/xxxx/repo/ruby/gem/bin/pod:25:in '<main>'

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

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

~/.bashrc配置

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

Node

brew install node

~/.bashrc配置

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

开发依赖:

npm install --global yarn

Other

brew install rg
brew install tree htop

除此之外,最近很少使用的lua、node、rust等环境待后续补充吧

作者: honmaple
链接: https://honmaple.me/articles/2025/04/macoshuan-jing-pei-zhi.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论