天天看點

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 brew

brew 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内容很多,另外,擴充性和靈活性也很強大,如果感興趣,可以去

官網文檔

閱讀文檔學習。