下载并安装homebrew的sh安装包

安装命令来自https://brew.sh/, 直接复制到命令行运行就可以

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

如果上述命令执行不了,就手动下载install.sh文件,然后在命令行之行.sh文件

遇到Permission denied问题:参考https://blog.csdn.net/weixin_41770169/article/details/86664553

执行如下命令

chmod 777 install.sh

安装完成

安装完成后可能会出现warning

Warning: /opt/homebrew/bin is not in your PATH.

Instructions on how to configure your shell for Homebrew

can be found in the 'Next steps' section below.

这表示brew没有被加入到环境变量中,因此需要添加环境变量

参考https://blog.csdn.net/tao_627/article/details/104856365

首先命令行执行,打开配置文件

vim ~/.bash_profile

这是用vim方式执行修改文件的命令,想要详细了解可以去搜索vim的命令用法, 我们要做的事情是

首先切换输入法到英文,点击i(表示正在insert文字)将下述内容复制粘贴

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

首先按esc退出insert模式,然后按住shift+;输入:(表示接下来要执行命令),然后输入wq(表示执行write的命令后执行quite的命令)。这步完成后,应该退出vim模式,返回到命令行页面执行source ~/.bash_profile使命令生效执行brew -v查看是否有输出,有输出版本号表示安装完毕

遇到问题:mac每次打开命令行都找不到brew

因为mac命令行用的zsh,但我配置的是bash。参考https://www.jianshu.com/p/407f0f9d8203

命令行输入echo $SHELL,检查自己用的是什么,我的输出就是zsh

打开vim配置文件

vim ~/.zshrc

添加以下内容

if [ -f ~/.bash_profile ]; then

source ~/.bash_profile

fi

使zsh配置文件生效

source ~/.zshrc

意思就是每次调用zsh命令行的时候,都会执行一下bash_profile的配置,这样bash_profile里面的环境变量等一系列配置就都可以同步到当前命令行窗口了

修改镜像源

参考https://developer.aliyun.com/mirror/homebrew/, https://cloud.tencent.com/developer/article/1614039

查看当前源

# 查看 brew.git 当前源

# 有$打头的这一行是命令行输入

$ cd "$(brew --repo)" && git remote -v

# 下面的都表示该命令执行完成后的输出

origin https://github.com/Homebrew/brew.git (fetch)

origin https://github.com/Homebrew/brew.git (push)

# 查看 homebrew-core.git 当前源

$ cd "$(brew --repo homebrew/core)" && git remote -v

origin https://github.com/Homebrew/homebrew-core.git (fetch)

origin https://github.com/Homebrew/homebrew-core.git (push)

Bash 终端配置

# 替换brew.git:

cd "$(brew --repo)"

git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 替换homebrew-core.git:

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 应用生效

brew update

# 替换homebrew-bottles:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile

source ~/.bash_profile

Zsh 终端配置

# 替换brew.git:

cd "$(brew --repo)"

git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 替换homebrew-core.git:

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 应用生效

brew update

# 替换homebrew-bottles:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc

source ~/.zshrc

恢复默认配置 出于某些场景, 可能需要回退到默认配置, 你可以通过下述方式回退到默认配置。

首先执行下述命令:

# 重置brew.git:

$ cd "$(brew --repo)"

$ git remote set-url origin https://github.com/Homebrew/brew.git

# 重置homebrew-core.git:

$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

$ git remote set-url origin https://github.com/Homebrew/homebrew-core.git

我是懒狗直接截图了,图源自教程https://developer.aliyun.com/mirror/homebrew/

参考链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: