Mac开发环境配置之HomeBrew

时间:2022-05-11
本文章向大家介绍Mac开发环境配置之HomeBrew,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

如果你使用惯了linux,你会对软件包管理、命令行不能自拔。由于mac的底层是类unix系统,也可以配置向linux一样好用。

安装HomeBrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

上面必须先有ruby以及其他开发环境支持,建议安装xcode后使用以上命令安装。

HomeBrew使用

安装成功以后,使用man brew查看命令帮助学习使用。man brew的内容很多,我下面展示brew --help

(data)  youdi@liangchangyoudeMacBook-Pro  ~  brew --help
Example usage:
  brew search [TEXT|/REGEX/]
  brew (info|home|options) [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install -vd FORMULA

Developers:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]
  https://docs.brew.sh/Formula-Cookbook.html

Further help:
  man brew
  brew help [COMMAND]
  brew home

下面详细介绍具体的使用

搜索软件包

brew search [TEXT|/REGEX/]这个是进行搜索软件包,参数支持正则表达式,

(data)  youdi@liangchangyoudeMacBook-Pro  ~  brew search nginx
==> Searching local taps...
nginx ✔
==> Searching taps on GitHub...
homebrew/nginx/accesskey-nginx-module               homebrew/nginx/xsltproc-nginx-module                homebrew/nginx/anti-ddos-nginx-module               homebrew/nginx/set-misc-nginx-module
homebrew/nginx/ajp-nginx-module                     homebrew/nginx/geoip2-nginx-module                  homebrew/nginx/array-var-nginx-module               homebrew/nginx/stream-lua-nginx-module
1. brew search, -S:
显示出本地所有可用的包,不加参数,不会去线上去查询,使用`brew search`或`brew -S`
2. brew search [--desc] (text|/text/):
   获取一个软件的短描述

3. brew search (--debian|--fedora|--fink|--macports|--opensuse|--ubuntu) text:
    获取不同平台上软件的文档信息
获取软件包信息

brew (info|home|options) [FORMULA…]

1.brew info brew info formula 显示这个软件的信息,后面可以接的参数有 —github, —json=version等等 2.brew home 通过浏览器打开软件的主页 3.brew options 显示软件包的options信息,安装时的,默认的安装信息

安装软件包

brew install FORMULA...

安装软件使用的命令,也是我们使用最频繁的。看看她的详细参数:

brew install [--debug][--env=(std|super)] [--ignore-dependencies|--only-dependencies][--cc=compiler] [--build-from-source|--force-bottle][--devel|--HEAD] [--keep-tmp][--build-bottle] formula [options …]

先来介绍最简单的使用

安装

brew install elinks

Homebrew 会将软件包安装到独立目录,并将其文件软链接至 /usr/local

$ cd /usr/local/Cellar
$ tree -L 3 elinks
elinks
└── 0.11.7_2
    ├── AUTHORS
    ├── COPYING
    ├── ChangeLog
    ├── INSTALL_RECEIPT.json
    ├── NEWS
    ├── README
    ├── TODO
    ├── bin
    └── share

3 directories, 7 files

$ cd /usr/local/bin
$ ll elinks
lrwxr-xr-x  1 youdi  admin    36B 10 15 15:32 elinks -> ../Cellar/elinks/0.11.7_2/bin/elinks

Homebrew 不会将文件安装到它本身目录之外,所以您可将 Homebrew 安装到任意位置。

你可以使用man brewbrew install --help查看具体一些参数的使用,都很简单。

升级和清理

brew update brew upgrade brew updte

会自动升级软件包和自身

brew outdate

查看过时的软件包,会有升级的版本号

brew upgrade

升级所有可以升级的

brew upgrade <formula>

升级具体的软件包

brew upgrade <formula>

停止一些软件包的更新和升级

brew unpin <formula>

将停止更新的软件包设置为允许

brew cleanup <formula>

将软件包的旧版本记录清理掉 brew cleanup 清理所有

brew cleanup -n

显示清理的内容

brew --cahce

显示缓存位置, 一般是在~/Library/Caches/Homebrew

查看安装软件包

brew list

brew list —help

brew list, ls [--full-name]:
   列出所有的安装的软件包,可以接一些参数,类似ls
    List all installed formulae. If --full-name is passed, print formulae
    with fully-qualified names. If --full-name is not passed, any other
    options (e.g. -t) are passed to ls which produces the actual output.

brew list, ls --unbrewed:
    List all files in the Homebrew prefix not installed by Homebrew.
    # diy方式安装的软件

brew list, ls [--versions [--multiple]] [--pinned] [formulae]:
    List the installed files for formulae. Combined with --verbose, recursively
    list the contents of all subdirectories in each formula's keg.

    If --versions is passed, show the version number for installed formulae,
    or only the specified formulae if formulae are given. With --multiple,
    only show formulae with multiple versions installed.

    If --pinned is passed, show the versions of pinned formulae, or only the
    specified (pinned) formulae if formulae are given.
    See also pin, unpin.

卸载软件包

brew unisntall

这个命令也是使用比较频繁的。 brew uninstall, rm, remove [--force][--ignore-dependencies] rm,remove也是卸载命令 --force 强制卸载, 如果有依赖,也强制卸载 --ignore-dependencies 忽略依赖

管理服务

brew services

brew services [-v|--verbose] [list | run | start | stop | restart | cleanup] [...]

brew services [-v|--verbose] [list | run | start | stop | restart | cleanup] [...]
    Easily start and stop formulae via launchctl.
    With -v or --verbose, print more detail.

    Integrates Homebrew formulae with macOS' launchctl manager. Services can be
    added to either /Library/LaunchDaemons or ~/Library/LaunchAgents.
    Basically, items in /Library/LaunchDaemons are started at boot, while those
    in ~/Library/LaunchAgents are started at login.

    When started with sudo, it operates on /Library/LaunchDaemons; otherwise,
    it operates on ~/Library/LaunchAgents.

    On start the plist file is generated and written to a Tempfile, and then
    copied to the launch path (existing plists are overwritten).

    [sudo] brew services list
    List all running services for the current user (or root)

    [sudo] brew services run formula|--all
    Run the service formula without starting at login (or boot).

    [sudo] brew services start formula|--all
    Start the service formula immediately and register it to launch at login (or boot).

    [sudo] brew services stop formula|--all
    Stop the service formula immediately and unregister it from launching at login (or boot).

    [sudo] brew services restart formula|--all
    Stop (if necessary) and start the service immediately and register it to launch at login (or boot).

    [sudo] brew services cleanup
    Remove all unused services.
用法
1. brew services list
打印出安装的所有服务
2. brew services run service_name
运行某个服务,没有运行,运行,运行的,重启
3. brew services start| stop| restart  formula|--all
开启,关闭,重启

另外,自己定义服务,记住/Library/LaunchDaemons, ~/Library/LaunchAgents, /Library/LaunchDaemons,修改对应文件。

总结

homebrew内容很多,另外,扩展性和灵活性也很强大,如果感兴趣,可以去官网文档 阅读文档学习。