Mac开发环境配置之HomeBrew

若是你使用惯了linux,你会对软件包管理、命令行不能自拔。因为mac的底层是类unix系统,也能够配置向linux同样好用。html

安装HomeBrew

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

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

HomeBrew使用

安装成功之后,使用man brew查看命令帮助学习使用。man brew的内容不少,我下面展现brew --helpnginx

(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
复制代码

下面详细介绍具体的使用git

搜索软件包

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

(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 infojson

brew info formulaubuntu

显示这个软件的信息,后面能够接的参数有 —github, —json=version等等xcode

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内容不少,另外,扩展性和灵活性也很强大,若是感兴趣,能够去官网文档 阅读文档学习。