Homebrew - 安装/使用

1.应用场景

主要用于在MacOS中借助Homebrew进行软件工具的快速安装,帮助我们解决时间,提高效率。

2.学习/操作

Homebrew logo

1.文档阅读

The Missing Package Manager for macOS (or Linux) — Homebrew

macOS(或 Linux)缺失的软件包的管理器 — Homebrew

macOS Monterey 移除了自带的 PHP

Upgrade vs. Update · Issue #13064 · Homebrew/legacy-homebrew · GitHub -- 关于brew update vs upgrade的事情

Issues · Homebrew/brew · GitHub

brew install 和 brew cask install 的区别 - 知乎

2.整理输出

前言交代

因为Mac升级OS到12.1

不再支持内置各个编程语言的运行时环境,本人又有些不像单独安装。

干脆就安装Mac的软件管理器,如果电脑感到卡,再卸载。

PHP: 使用 macOS Monterey 之前内置的 PHP - Manual

2.1 安装 homebrew

终端中执行如下命令即可

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

升级之后

➜  ~ brew -v

Homebrew 3.6.7

Homebrew/homebrew-core (git revision a99c43cbe3f; last commit 2022-10-29)

Homebrew/homebrew-cask (git revision 0441adcaf2; last commit 2022-10-29)

➜  ~

注意

有时候,需要科学上网才行,使用国内源进行安装

/bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

下载安装过程如下:

huangbaoyin@huangbaoyindeMacBook-Pro ~ % /bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

               开始执行Brew自动安装程序

              [cunkai.wang@foxmail.com]

           ['2022-08-19 16:51:27']['12.5']

        https://zhuanlan.zhihu.com/p/111014448

请选择一个下载brew本体的序号,例如中科大,输入1回车。

源有时候不稳定,如果git克隆报错重新运行脚本选择源。

1、中科大下载源

2、清华大学下载源

3、北京外国语大学下载源

4、腾讯下载源

5、阿里巴巴下载源

请输入序号: 1

  你选择了中国科学技术大学brew本体下载源

  

!!!此脚本将要删除之前的brew(包括它下载的软件),请自行备份。

->是否现在开始执行脚本(N/Y) Y

--> 脚本开始执行

Mac os设置开机密码方法:

  (设置开机密码:在左上角苹果图标->系统偏好设置->用户与群组->更改密码)

  (如果提示This incident will be reported. 在用户与群组中查看是否管理员)

==> 通过命令删除之前的brew、创建一个新的Homebrew文件夹

请输入开机密码,输入过程不显示,输入完后回车

开始执行

  ---备份要删除的/opt/homebrew到系统桌面....

   ---/opt/homebrew 备份完成

-> 创建文件夹 /opt/homebrew

运行代码 ==> /usr/bin/sudo /bin/mkdir -p /opt/homebrew

此步骤成功

运行代码 ==> /usr/bin/sudo /bin/chmod -R a+rwx /opt/homebrew

运行代码 ==> /usr/bin/sudo /usr/sbin/chown huangbaoyin /opt/homebrew

运行代码 ==> /usr/bin/sudo /usr/bin/chgrp admin /opt/homebrew

git version 2.32.1 (Apple Git-133)

下载速度觉得慢可以ctrl+c或control+c重新运行脚本选择下载源

==> 从 https://mirrors.ustc.edu.cn/brew.git 克隆Homebrew基本文件

未发现Git代理(属于正常状态)

Cloning into '/opt/homebrew'...

remote: Enumerating objects: 221996, done.

remote: Total 221996 (delta 0), reused 0 (delta 0), pack-reused 221996

Receiving objects: 100% (221996/221996), 59.40 MiB | 10.96 MiB/s, done.

Resolving deltas: 100% (164896/164896), done.

此步骤成功

--创建Brew所需要的目录

运行代码 ==> /usr/bin/sudo /bin/chmod u+rwx /opt/homebrew/bin /opt/homebrew/bin/brew

....

....

简单截图

记得重启终端窗口或者执行: source /Users/huangbaoyin/.zprofile

但是在安装PHP时,一直提示出现apr的报错

最后还是使用科学上网的方式,重新安装了homebrew,

执行brew install php 解决

插入

关于brew的命令应该熟练使用,各个命令的含义用途 -- 没有几个,而且最常用的有 红色 标记

➜  ~ brew -h

Example usage:

  brew search TEXT|/REGEX/ // 在homebrew中搜索有关所有软件信息

  brew info [FORMULA|CASK...] // 查看软件的信息「最新版本」

  brew install FORMULA|CASK... // 安装软件「默认最新版本」

  brew update // 更新homebrew版本及软件索引

  brew upgrade [FORMULA|CASK...] // 更新homebrew已安装的所有软件「如果已经是最新版,会跳过」

  brew uninstall FORMULA|CASK... // 卸载使用homebrew安装的软件

  brew list [FORMULA|CASK...] // 查看使用homebrew安装过软件列表

Troubleshooting:

  brew config

  brew doctor

  brew install --verbose --debug FORMULA|CASK

Contributing:

  brew create URL [--no-fetch]

  brew edit [FORMULA|CASK...]

Further help:

  brew commands

  brew help [COMMAND]

  man brew

  https://docs.brew.sh

➜  ~

FORMULA|CASK ==== 配方|桶

一时记不住具体的命令时 或者 根本不知道怎么使用命令时 

「很多系统,软件都支持这样」

首先,可以借助帮助命令

brew -h 

然后,具体对某一个命令进行查看

brew list -h

之后,快速扫描到目标,然后详细查看具体使用

如下:

➜  ~ brew -h

Example usage:

  brew search TEXT|/REGEX/

  brew info [FORMULA|CASK...]

  brew install FORMULA|CASK...

  brew update

  brew upgrade [FORMULA|CASK...]

  brew uninstall FORMULA|CASK...

  brew list [FORMULA|CASK...]

Troubleshooting:

  brew config

  brew doctor

  brew install --verbose --debug FORMULA|CASK

Contributing:

  brew create URL [--no-fetch]

  brew edit [FORMULA|CASK...]

Further help:

  brew commands

  brew help [COMMAND]

  man brew

  https://docs.brew.sh

➜  ~ brew list -h

Usage: brew list, ls [options] [installed_formula|installed_cask ...]

List all installed formulae and casks. // 列出所有已安装的配方和容器 -- 就是列出所有使用homebrew安装的软件

If formula is provided, summarise the paths within its current keg. If cask

is provided, list its artifacts.

      --formula, --formulae        List only formulae, or treat all named

                                   arguments as formulae.

      --cask, --casks              List only casks, or treat all named arguments

                                   as casks.

      --full-name                  Print formulae with fully-qualified names.

                                   Unless --full-name, --versions or

                                   --pinned are passed, other options (i.e.

                                   -1, -l, -r and -t) are passed to

                                   ls(1) which produces the actual output.

      --versions                   Show the version number for installed

                                   formulae, or only the specified formulae if

                                   formula are provided.

      --multiple                   Only show formulae with multiple versions

                                   installed.

      --pinned                     List only pinned formulae, or only the

                                   specified (pinned) formulae if formula are

                                   provided. See also pin, unpin.

  -1                               Force output to be one entry per line. This

                                   is the default when output is not to a

                                   terminal.

  -l                               List formulae and/or casks in long format.

                                   Has no effect when a formula or cask name is

                                   passed as an argument.

  -r                               Reverse the order of the formulae and/or

                                   casks sort to list the oldest entries first.

                                   Has no effect when a formula or cask name is

                                   passed as an argument.

  -t                               Sort formulae and/or casks by time modified,

                                   listing most recently modified first. Has no

                                   effect when a formula or cask name is passed

                                   as an argument.

  -d, --debug                      Display any debugging information.

  -q, --quiet                      Make some output more quiet.

  -v, --verbose                    Make some output more verbose.

  -h, --help                       Show this message.

➜  ~

然后,就可以使用

➜  ~ brew list   

大致浏览下,已经安装过什么软件。

如果使用的时候,不记得是否安装过,

可使用 brew list | grep 软件名

或者 brew info 软件名 // 如果没有安装,输出信息中会有提示:Not installed

以wget 和 mysql为例

2.2 安装PHP

PHP: 使用软件包 - Manual

终端中执行

brew install php

关键信息:

To enable PHP in Apache add the following to httpd.conf and restart Apache:

    LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>

        SetHandler application/x-httpd-php

    </FilesMatch>

Finally, check DirectoryIndex includes index.php

    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:

    /usr/local/etc/php/8.1/

To restart php after an upgrade:

  brew services restart php

Or, if you don't want/need a background service you can just run:

  /usr/local/opt/php/sbin/php-fpm --nodaemonize

查看PHP版本

➜  php-test-temp php -v

PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)

Copyright (c) The PHP Group

Zend Engine v4.1.1, Copyright (c) Zend Technologies

    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

➜  php-test-temp

查看扩展文件目录

➜  php-test-temp php-config --extension-dir

/usr/local/Cellar/php/8.1.1/pecl/20210902

➜  php-test-temp

查看配置文件目录

➜  8.1 ls

conf.d       pear.conf    php-fpm.conf php-fpm.d    php.ini

➜  8.1 pwd

/usr/local/etc/php/8.1

➜  8.1

➜  php-test-temp php-config --ini-dir      

/usr/local/etc/php/8.1/conf.d

补充

安装不同的PHP版本,以及使用PHP版本

1. brew search php // 查看homebrew源支持的PHP本本

2.  安装PHP另一版本[项目需要] === PHP 7.2

这里已经安装了PHP 8.1

➜  ~ brew install php@7.2

出现报错

解决方法

homebrew - Error: php@7.3 has been disabled because it is a versioned formula - Stack Overflow

brew tap shivammathur/php

brew install shivammathur/php/php@7.2

截图

输出的关键信息

...

To enable PHP in Apache add the following to httpd.conf and restart Apache:

    LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>

        SetHandler application/x-httpd-php

    </FilesMatch>

Finally, check DirectoryIndex includes index.php

    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:

    /usr/local/etc/php/7.2/

php@7.2 is keg-only, which means it was not symlinked into /usr/local,

because this is an alternate version of another formula.

If you need to have php@7.2 first in your PATH, run:

  echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc

  echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.2 you may need to set:

  export LDFLAGS="-L/usr/local/opt/php@7.2/lib"

  export CPPFLAGS="-I/usr/local/opt/php@7.2/include"

To restart shivammathur/php/php@7.2 after an upgrade:

  brew services restart shivammathur/php/php@7.2

Or, if you don't want/need a background service you can just run:

  /usr/local/opt/php@7.2/sbin/php-fpm --nodaemonize

==> Summary

🍺  /usr/local/Cellar/php@7.2/7.2.34_4: 515 files, 75.0MB

==> Running `brew cleanup php@7.2`...

Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.

Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

➜  ~

由于信息可知,安装目录在 /usr/local/opt/php@7.2

使用PHP 7.2启动Web服务

/usr/local/opt/php@7.2/bin/php -S localhost:8000 -t ./web

使用PHP 8.1启动Web服务

/usr/local/opt/php@8.1/bin/php -S localhost:8000 -t ./web

/usr/local/opt/php/bin/php -S localhost:8000 -t ./web

php -S localhost:8000 -t ./web

至于切换不同PHP的工具,TBD

后续补充

...

3.问题/补充

1. 迁移mac导致homebrew 无法使用

原因应是旧Mac是inter版本,新Mac是M1版本

解决办法:

Cannot install in Homebrew on ARM processor in Intel default prefix - 简书

2. 安装brew install wget 「安装其他软件也是一样」

先是编辑~/.zshrc

添加如下内容到尾部

# HomeBrew

export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles

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

export PATH="/opt/homebrew/sbin:$PATH"

# HomeBrew END

执行

source ~/.zshrc

然后开始安装软件 wget

同时执行

wget -c baidu.com

可以看到下载了index.html

cat index.html

3.  补充,在linux下升级软件

这是Ubuntu阵营

使用apt-get升级库或者软件时,会遇到是用update或者upgrade的问题,原则是:

update
update 是同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。


upgrade
upgrade 是升级已安装的所有软件包,升级之后的版本就是本地索引里的,因此,在执行 upgrade 之前一定要执行 update, 这样才能是最新的。

4.参考

参见文档阅读

后续补充

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值