Mac M4搭建后端开发环境全过程——HomeBrow、Git、JDK17、Maven、Miniconda、Docker等

4 阅读2分钟

Homebrow

官网:brew.sh/

安装

安装命令(需要梯子)

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

安装成功后会提示将它的路径添加到shell配置文件中:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc

检验安装成功:

kai@KaideMacBook-Pro ~ % brew -v
Homebrew 4.6.6

homebrow默认安装位置:/opt/homebrew

kai@KaideMacBook-Pro ~ % brew --repo
/opt/homebrew

Homebrew 配置软件源

查看当前源:

git -C "$(brew --repo)" remote -v

修改为Aliyun镜像:

# 修改 brew 源
git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 修改 homebrew-core 源(不是必要的)
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 修改 homebrew-cask 源 (可选)
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git

# 更新配置
brew update

# zsh 修改 homebrew bintray 源(适用zsh用户)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

修改为中科大源:

# 替换 brew.git
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换 homebrew-core.git(不是必要的)
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换 homebrew-cask.git(用于GUI应用)
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 更新配置
brew update

# zsh 修改 homebrew bintray 源(适用zsh用户)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

重置官方源:

# 重置 brew.git
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 重置 homebrew-cask.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git

# 更新配置
brew update

# 删除环境变量
sed -i '' '/HOMEBREW_BOTTLE_DOMAIN/d' ~/.zshrc
source ~/.zshrc

Git

安装

brew install git 

查看版本

git -v

配置

配置用户名和邮箱

git config --global user.name "用户名"
git config --global user.email "邮箱"

查看全局配置

# 列出全局的配置项
git config --list --global

生成SSH密钥

ssh-keygen -t rsa -C "邮箱"

三次回车,期间提示默认保存位置:/Users/kai/.ssh/id_rsa

如何没显示.ssh,按下 Command + Shift + .(句点)可以切换显示和隐藏文件

id_rsa 是私钥,

id_rea.pub 是公钥,配置在github或gitlab中

打开id_rea.pub,复制里面的全部内容

ssh_.png

绑定SSH密钥

登陆你的GitHub,单击头像在下拉菜单中选择settings,进入到个人设置界面

单击屏幕左侧的SSH and GPG keys,然后单击该页面的new SSH key按钮,将刚才复制的代码添加到key中,输入方便记的title,单击下方的Add按钮完成绑定。

github_ssh.png

验证

ssh git@github.com

jdk17

查看brew源中的jdk版本

brew search jdk

安装openjdk17

brew install openjdk@17

安装成功后会提示

install_success.png

需要先建立一个软链接

sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

设置环境变量,默认使用 .zshrc

echo 'export PATH="/opt/homebrew/opt/openjdk@17/bin:$PATH"' >> ~/.zshrc
echo 'export JAVA_HOME="/opt/homebrew/opt/openjdk@17"' >> ~/.zshrc
source ~/.zshrc

验证jdk是否安装成功

kai@KaideMacBook-Pro ~ % java -version
openjdk version "17.0.16" 2025-07-15
OpenJDK Runtime Environment Homebrew (build 17.0.16+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.16+0, mixed mode, sharing)

查看安装位置

kai@KaideMacBook-Pro ~ % brew --prefix openjdk
/opt/homebrew/opt/openjdk

Maven

安装

使用搜索工具去搜索maven包

brew search maven

安装

brew install maven

验证是否安装成功

mvn -v
Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
Maven home: /opt/homebrew/Cellar/maven/3.9.11/libexec
Java version: 17.0.16, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk@17/17.0.16/libexec/openjdk.jdk/Contents/Home
Default locale: zh_CN_#Hans, platform encoding: UTF-8
OS name: "mac os x", version: "15.3.1", arch: "aarch64", family: "mac"

可以看到Maven home在:/opt/homebrew/Cellar/maven/3.9.11/libexec

查看安装位置

brew --prefix maven

配置镜像源

打开 /opt/homebrew/Cellar/maven/3.9.11/libexec

如果找不到opt目录,按下快捷键: shift + command + .(句号键)显示隐藏文件目录

打开settings.xml文件,在mirrors选项卡里面添加

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

Miniconda

官网:docs.conda.io/en/latest/m…

安装

下载 .sh 安装程序

curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh

运行以下命令进行安装

bash Miniconda3-latest-MacOSX-arm64.sh

一路回车,中间会提示安装位置:PREFIX=/Users//miniconda3

刷新环境变量

source ~/.zshrc

通过运行 conda list 测试安装

conda list

conda源配置

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
 
conda config --set show_channel_urls yes

Go

使用homebrow安装go

brew install go

验证是否安装成功

go version

检查GOPATH

kai@KaideMacBook-Pro ~ % echo $GOPATH 


没有输出,手动添加 GOPATH

vi ~/.zshrc
# 添加以下内容:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin

刷新

source ~/.zshrc

Goland中设置goproxy地址

GOPROXY=https://goproxy.io

Docker

安装

下载 Docker Desktop,按提示安装即可

www.docker.com

配置镜像源

Docker 可用镜像源

toolshu.com/docker-mirr…

打开docker设置

docker_install1.png

配置 Docker Engine:

docker_install2.png

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://docker.xuanyuan.me"
  ]
}

安装 Node.js

通过 Homebrew 安装

brew install node

安装完成后验证:

node -v  # 应输出版本号(如 v20.0.0)
npm -v   # 应输出 npm 版本号(如 9.0.0)