Skip to content
On this page

MacOS环境搭建

前端环境

安装Git、SVN

macOS自带了Git可以直接使用,全局设置Git用户名和邮箱,

sh
git config --global user.name "Boor"
git config --global user.email "gugol.boor@qq.com"

svn可使用brew安装(没有brew命令的先安装HomeBrew):

sh
brew install subversion

安装终端oh-my-zsh

sh
## 安装oh my zsh
git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

## 用以下命令生成.zshrc文件:
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
# 编辑~/.zshrc文件,添加以下内容:
ZSH_DISABLE_COMPFIX=true
## 切换默认shell为zsh
chsh -s /bin/zsh

高亮插件

sh
# 下载命令高亮插件 这里下载到用户名下.zsh文件夹下
sudo git clone https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting

# 编辑~/.zshrc文件,添加以下内容:
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

命令提示插件

sh
# 下载命令提示
sudo git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions

# 编辑~/.zshrc文件,添加以下内容:
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh

刷新配置文件:source .zshrc

安装SwitchHosts

SwitchHosts 是一个免费开源的管理、切换多个 hosts的工具。

官方网站: https://swh.app/zh

谷歌翻译

sh
203.208.40.66 translate.google.com
203.208.40.66 translate.googleapis.com

Github加速

查询域名对应ip地址:https://www.ipaddress.com/

sh
140.82.114.4 github.com
185.199.109.153 assets-cdn.github.com
151.101.1.194 github.global.ssl.fastly.net

修改完之后,刷新DNS缓存:

sh
sudo killall -HUP mDNSResponder && echo macOS DNS Cache Reset

安装HomeBrew

  1. 执行以下命令使用国内镜像下载:

    sh
    /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
  2. 选择其中一个下载源,我这使用清华大学下载源

  3. 是否现在开始执行脚本? Y

    如果弹出警告:

    sh
    You have not agreed to the Xcode license agreements. You must agree to both license agreements below in order to use Xcode.

    则需要安装xcode-select ,按Q退出会自动弹出安装提示,然后安装。

安装时间有点久期间可能会需要输入开机密码。安装成功后重启终端输入:

sh
brew -v

打印出版本号说明安装成功。如果出现:

sh
fatal: detected dubious ownership in repository at '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'

按照提示的命令输入即可:

sh
git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core

TIP

Mac高版本下Homebrew安装软件时会提示权限不足:

sh
Error: Permission denied @ apply2files ...

添加root权限时又会报错危险操作:

sh
Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

解决方案

执行以下命令,改变Homebrew的路径:

sh
sudo chown -R `whoami` /usr/local/Homebrew/
sudo chown -R $(whoami) $(brew --prefix)/*
sudo mkdir /usr/local/Frameworks
sudo chown -R `whoami` /usr/local/Frameworks/

之后就可以正常安装软件了!

安装Nodejs

前往node官网 下载node安装包,常用版本14.x 或者16.x

验证是否安装成功:

sh
node -v
# 查看npm版本
npm -v

pnpm、yarn安装

个人比较喜欢使用pnpm或者yarn,可使用brew安装:

sh
brew install pnpm
brew install yarn
# 查看是否安装成功
pnpm -v

安装VSCode

前往VSCode官网 下载安装包

Flutter环境

以下步骤大都需要科学上网的环境。

  1. Flutter官网下载最新安装包

  2. 终端输入以下命令:

    sh
    cd ~
    mkdir development
    cd ~/development
    # 这里需要换成自己下载下来的sdk路径
    unzip ~/Downloads/flutter_macos_2.10.4-stable.zip
  3. 配置环境变量

    输入open ~/.zshrc 打开配置文件(bash终端使用的是open ~/.bash_profile),将以下代码复制到里面:

    sh
    # flutter 国内镜像
    export PUB_HOSTED_URL=https://pub.flutter-io.cn
    export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
    # 添加flutter相关工具命令到path中
    export PATH= ~/development/flutter/bin:$PATH

    然后输入以下命令:

    sh
    # 刷新当前终端配置
    source $HOME/.zshrc
    # 验证目录是否在已经在PATH中
    echo $PATH
  4. 运行flutter doctor命令查看是否需要安装其它依赖项来完成安装

安装失败可以参考 https://zhuanlan.zhihu.com/p/480002331 排查问题。

IOS模拟器

打开模拟器:

sh
open -a Simulator
sh
sudo chmod -R 777 *

后端环境

javaSDK下载

前往官网下载java17安装包

IntelliJ IDEA 安装

前往官网下载安装包

MySQL安装

前往官网下载地址 选择 MySQL Community Server 下载社区版本mysql安装包,inter的选择x86版本,M芯片选择ARM版本

下载成功后打开安装程序一直下一步,直到configuration步骤,选择 Use Legacy Password Encryption ,然后下一步,输入mysql密码后点击完成就可以安装成功了

安装成功后终端没有mysql命令

输入命令:

sh
# 以zsh为例,打开终端配重文件
open ~/.zshrc

打开文件后在最底部添加以下代码后保存:

sh
# mysql命令
PATH=$PATH:/usr/local/mysql/bin
export

保存后刷新配置文件:

sh
source .zshrc
# 刷新之后在试下mysql命令是否有效
mysql

Maven

安装

打开Mevan官方网站 下载 压缩包,解压到/Users/boor/development 文件夹下

配置环境变量

打开终端配置:

sh
vim ~/.zshrc

添加2行代码:

sh
# Maven环境安装目录
export MAVEN_HOME=/Users/boor/development/apache-maven-3.9.1
export PATH=$MAVEN_HOME/bin:$PATH

保存之后刷新配置,查看是否配置成功:

sh
# 刷新配置
source ~/.zshrc
# 查看Maven版本
mvn -v

配置文件

打开 /Users/boor/development/apache-maven-3.9.1/conf/settings.xml,编辑以下内容:

xml
<!-- 添加配置本地仓库路径 -->
<localRepository>/Users/boor/development/apache-maven-3.9.1/repository</localRepository>
<!-- 修改镜像为阿里云 -->
<mirror>
  <id>aliyunmaven</id>
  <mirrorOf>*</mirrorOf>
  <name>阿里云公共仓库</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>

阿里云Maven配置

其它工具

安装nginx

打开终端

sh
#安装nginx
brew install nginx

打开nginx安装目录:

sh
open /usr/local/Cellar/nginx

配置使用nginx

打开nginx配置文件目录,里面有servers目录以及nginx.conf的配置文件

sh
open /usr/local/etc/nginx/

在server文件夹内创建一个server.conf文件

sh
server {
	#设置监听端口
    listen       80;
    #设置域名
    server_name  www.xxx.com;

    location / {
    	# 设置本地项目路径
        root   /Users/boor/Desktop/top_line/;
        # 设置访问默认文件名
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

配置hosts文件

打开终端,输入:sudo vim /private/etc/hosts

然后输入密码之后弹出:

vim
E325: ATTENTION
Found a swap file by the name "/private/etc/.hosts.swp"
          owned by: root   dated: Wed Nov 13 23:16:00 2019
         file name: /private/etc/hosts
          modified: YES
         user name: root   host name: qi.local
        process ID: 3394
While opening file "/private/etc/hosts"
             dated: Thu Nov 14 01:02:30 2019
      NEWER than swap file!

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r /private/etc/hosts"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/private/etc/.hosts.swp"
    to avoid this message.

Swap file "/private/etc/.hosts.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

按E键再按I就可以进行编辑

添加一项配置

vim
127.0.0.1    www.xxx.com

编辑完成之后 control + C退出编辑,再按Shift + : 输入 :wq! 强制修改hosts!!!

直接命令行输入 nginx启动nginx

在浏览器打开www.xxx.com验证是否成功

nginx使用命令

启动: nginx

修改配置后重新加载生效: sudo nginx -s reload

重新打开日志文件: sudo nginx -s reopen

快速停止nginx: sudo nginx -s stop

完整有序的停止nginx/优雅关闭(先服务完已打开的连接): sudo nginx -s quit

判断配置文件是否有问题: sudo nginx -t

停止nginx服务的其他的方法:

查看nginx进程,进程号,停止的话可以杀进程

查看nginx进程:

sudo kill -QUIT 主进程号

sudo kill -TERM 主进程号

命令行终端美化

安装oh-my-zsh

sh
## 安装oh my zsh
$ git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

## 如果丢失.zshrc文件用以下命令生成:
$ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

## 切换默认shell为zsh
$ chsh -s /bin/zsh

高亮插件

sh
# 下载命令高亮插件 这里下载到用户名下.zsh文件夹下
$ sudo git clone https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting
 
# 编辑~/.zshrc文件,添加以下内容:
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

命令提示插件

sh
# 下载命令提示
$ sudo git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
 
# 编辑~/.zshrc文件,添加以下内容:
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh

Mac搭建环境问题

1.安装软件打开后提示文件已损坏

原因是高版本的MacOS在

设置>安全性与隐私>允许从以下位置下载应用

中没有了任何来源,解决方法:

sh
# 终端输入以下命令
sudo spctl --master-disable