京橋のバイオインフォマティシャンの日常

南国のビーチパラソルの下で、Rプログラムを打ってる日常を求めて、、Daily Life of Bioinformatician in Kyobashi of Osaka

macOS用パッケージマネージャー Homebrewのやり方をいつも微妙に忘れてしまう件

はじめに

大学時代には、MacPortsを使っていたが、 ある後輩に勧められ、Homebrewを使うようになった。

特に、MacPortsがどうとかいうわけではないが、 それ以降、Macでのパッケージ・インストールは、Homebrew で行っている*1

Homebrew は、macOS X用のパッケージ管理システムである。 macOS X用のメジャーなパッケージ管理システムには、Fink、MacPorts、Homebrewと3つある。

下表にまとめると、Homebrewが一番新しく、あと、Homebrew実行時には確かにRubyが走る。

管理システム リリース 言語
Fink 2000年 Perl
MacPorts 2002年 Tcl
Homebrew 2007年 Ruby

Homebrewにおける唯一のコマンドは「brew」であり、brewに各種のオプション(install、info、searchとか)を付与して実行することになる。

「brew」とは「ビールを醸造する」事らしく、Homebrewは「自家醸造」という意味になる*2。 ちなみに、celler というディレクトリも作成されるが、これは、ビール貯蔵庫(インストール後の保存先)を意味する。

実際のHomebrewの環境構築について概説する。

Homebrew のインストール・環境構築

今回のローカル環境は以下の通りである。

macOS Catalina (10.15.4)
MacBook Pro (12-inch, 2019, Four Thunderbolt 3 ports)
プロセッサ: 2.8GHz クアッドコアIntel Core i7
メモリ: 16GB 2133 MHz LPDDR3

手順は、1〜3を順に実行する。

Terminal を起動する

Terminal.appは、/System/Applications/Utilities/Terminal.appにある。 あと、Terminalは、Dockに出しておくのが望ましい。

Terminalに以下のコマンドをコピペして実行する

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

※ Downloading Command Line Tools for Xcode も実行される

Homebrewの事始めに確認しておくこと

brewのパスを確認しておく

which brew
#/usr/local/bin/brew

brewに問題がないかをチェックしておく

brew doctor

出力が「Your system is ready to brew.」なら、次に進む。

ただ、以下のような、出力になるときがしばしばある。

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/libtcl8.6.dylib
  /usr/local/lib/libtk8.6.dylib

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/fakemysql.h
  /usr/local/include/fakepq.h
  /usr/local/include/fakesql.h
  /usr/local/include/itcl.h
  /usr/local/include/itcl2TclOO.h
  /usr/local/include/itclDecls.h
  /usr/local/include/itclInt.h
  /usr/local/include/itclIntDecls.h
  /usr/local/include/itclMigrate2TclCore.h
  /usr/local/include/itclTclIntStubsFcn.h
  /usr/local/include/mysqlStubs.h
  /usr/local/include/odbcStubs.h
  /usr/local/include/pqStubs.h
  /usr/local/include/tcl.h
  /usr/local/include/tclDecls.h
  /usr/local/include/tclOO.h
  /usr/local/include/tclOODecls.h
  /usr/local/include/tclPlatDecls.h
  /usr/local/include/tclThread.h
  /usr/local/include/tclTomMath.h
  /usr/local/include/tclTomMathDecls.h
  /usr/local/include/tdbc.h
  /usr/local/include/tdbcDecls.h
  /usr/local/include/tdbcInt.h
  /usr/local/include/tk.h
  /usr/local/include/tkDecls.h
  /usr/local/include/tkPlatDecls.h

Warning: Unbrewed .pc files were found in /usr/local/lib/pkgconfig.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .pc files:
  /usr/local/lib/pkgconfig/tcl.pc
  /usr/local/lib/pkgconfig/tk.pc

Warning: Unbrewed static libraries were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected static libraries:
  /usr/local/lib/libtclstub8.6.a
  /usr/local/lib/libtkstub8.6.a

その際には、以下のrmコマンドを実行しておく。

rm -rf \
  /usr/local/lib/libtcl8.6.dylib \
  /usr/local/lib/libtk8.6.dylib \
  /usr/local/include/fakemysql.h \
  /usr/local/include/fakepq.h \
  /usr/local/include/fakesql.h \
  /usr/local/include/itcl.h \
  /usr/local/include/itcl2TclOO.h \
  /usr/local/include/itclDecls.h \
  /usr/local/include/itclInt.h \
  /usr/local/include/itclIntDecls.h \
  /usr/local/include/itclMigrate2TclCore.h \
  /usr/local/include/itclTclIntStubsFcn.h \
  /usr/local/include/mysqlStubs.h \
  /usr/local/include/odbcStubs.h \
  /usr/local/include/pqStubs.h \
  /usr/local/include/tcl.h \
  /usr/local/include/tclDecls.h \
  /usr/local/include/tclOO.h \
  /usr/local/include/tclOODecls.h \
  /usr/local/include/tclPlatDecls.h \
  /usr/local/include/tclThread.h \
  /usr/local/include/tclTomMath.h \
  /usr/local/include/tclTomMathDecls.h \
  /usr/local/include/tdbc.h \
  /usr/local/include/tdbcDecls.h \
  /usr/local/include/tdbcInt.h \
  /usr/local/include/tk.h \
  /usr/local/include/tkDecls.h \
  /usr/local/include/tkPlatDecls.h \
  /usr/local/lib/pkgconfig/tcl.pc \
  /usr/local/lib/pkgconfig/tk.pc \
  /usr/local/lib/libtclstub8.6.a \
  /usr/local/lib/libtkstub8.6.a

*ここで「\ (バックスラッシュ)」は次のコマンド行をつなげて(連結して)実行することを意味する

brew formulaのアップデートをしておく

brew update

ここまでで、brewのインストール・確認は完了である。

次に、brewの基本コマンドについて

(同上)brewに問題がないか調べるコマンド

brew doctor

Homebrew formulaのアップデート、再ビルド

brew update       # (同上)formula更新
brew upgrade      # 再ビルド

インストール済みのformulaリストの表示

brew list

古いバージョンのformulaを削除する*3

brew cleanup

Mac / brewの設定確認*4

brew --config       # 実行環境での出力結果

HOMEBREW_VERSION: 2.2.14
ORIGIN: https://github.com/Homebrew/brew
HEAD: e2bc0fa6879ce4c74ea2d8f8c654c9812f776b43
Last commit: 4 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 6f0b61d66923272ad55845a0aef6f110b94ac801
Core tap last commit: 3 hours ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_DISPLAY: /private/tmp/com.apple.launchd.Ykw99TwIYo/org.macosforge.xquartz:0
HOMEBREW_MAKE_JOBS: 8
CPU: octa-core 64-bit kabylake
Homebrew Ruby: 2.6.3 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
Clang: 11.0 build 1103
Git: 2.24.2 => /Library/Developer/CommandLineTools/usr/bin/git
Curl: 7.64.1 => /usr/bin/curl
Java: 12.0.2, 1.8.0_91
macOS: 10.15.4-x86_64
CLT: 11.4.1.0.1.1586360307
Xcode: N/A
XQuartz: 2.7.11 => /opt/X11

brewコマンドを使って、実際にパッケージをインストールしてみる

Mac版wget*5のインストール手順を例示する。

wget formulaの検索(これはスキップ可能)

brew search wget

==> Formulae
wget   wgetpaste

左側のwgetが、今回インストールしたパッケージである。

wget の情報を調べる

brew info wget

wget: stable 1.20.3 (bottled), HEAD
Internet file retriever
https://www.gnu.org/software/wget/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/wget.rb
==> Dependencies
Build: pkg-config ✔
Required: libidn2 ✔, openssl@1.1==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 107,484 (30 days), 316,193 (90 days), 1,353,032 (365 days)
install-on-request: 104,307 (30 days), 305,522 (90 days), 1,278,011 (365 days)
build-error: 0 (30 days)

*DependenciesがちゃんとOKになっていることを見ておく

wget のインストール実行

brew install wget

==> Installing dependencies for wget: gettext, libunistring, libidn2 and openssl@1.1
==> Installing wget dependency: gettext
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.20.2_1.catalina.b
==> Downloading from https://akamai.bintray.com/71/71f4ded03e8258b5e6896eebb00d2
######################################################################## 100.0%
==> Pouring gettext-0.20.2_1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/gettext/0.20.2_1: 1,923 files, 18.6MB
==> Installing wget dependency: libunistring
==> Downloading https://homebrew.bintray.com/bottles/libunistring-0.9.10.catalin
==> Downloading from https://akamai.bintray.com/ce/ce746662b98d93511b86920011b5c
######################################################################## 100.0%
==> Pouring libunistring-0.9.10.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libunistring/0.9.10: 54 files, 4.4MB
==> Installing wget dependency: libidn2
==> Downloading https://homebrew.bintray.com/bottles/libidn2-2.3.0.catalina.bott
==> Downloading from https://akamai.bintray.com/09/0908585cca518a83f101b2edc0417
######################################################################## 100.0%
==> Pouring libidn2-2.3.0.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libidn2/2.3.0: 70 files, 727.8KB
==> Installing wget dependency: openssl@1.1
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.1g.catalina
==> Downloading from https://akamai.bintray.com/19/1926679569c6af5337de812d86f4d
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.1g.catalina.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.sh_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1g: 8,059 files, 18MB
==> Installing wget
==> Downloading https://homebrew.bintray.com/bottles/wget-1.20.3_2.catalina.bott
==> Downloading from https://akamai.bintray.com/ef/ef65c759c5097a36323fa9c777564
######################################################################## 100.0%
==> Pouring wget-1.20.3_2.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/wget/1.20.3_2: 50 files, 4.0MB
==> Caveats
==> openssl@1.1
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.sh_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

ちゃんとwgetのパスが通っているか確認しておく

which wget
#/usr/local/bin/wget

これで、wgetのインストールは完了である。

もしformula(パッケージ)をアンインストールする際には、、、

brew uninstall wget    # or $ brew remove wget

#Uninstalling /usr/local/Cellar/wget/1.20.3_2... (50 files, 4.0MB)

最後に

PCの買い替えのたびに*6、同じことをいつもやっているが、Homebrewのセットアップの仕方を微妙に忘れている。このメモが次回役立つことを願う。

補足

Homebrewでインストールできるパッケージリストについて

Homebrewの全Formulaeは、ここで確認もできる。

Homebrew-cask

Homebrewでインストールできるパッケージ群は、主にCUIツールなのだが、 コマンドでアプリケーション(GUIツール)を入れたい場合には、Homebrew-caskをインストールして使うことになる*7*8

Homebrew のアンインストール・コマンド

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

「sudo」を入れた方が良いかも。。

出力されるメッセージに従い、関連フォルダを削除する

このときは、以下のフォルダを削除した。

sudo rm -rf \
/usr/local/bin/ \
/usr/local/Caskroom/ \
/usr/local/etc/ \
/usr/local/lib/ \
/usr/local/remotedesktop/ \
/usr/local/share/ \
/usr/local/var/

#OR

sudo rm -rf \
/usr/local/Frameworks/ \
/usr/local/Homebrew/ \
/usr/local/bin/ \
/usr/local/etc/ \
/usr/local/include/ \
/usr/local/lib/ \
/usr/local/man/ \
/usr/local/opt/ \
/usr/local/sbin/ \
/usr/local/share/ \
/usr/local/texlive/ \
/usr/local/var/

#OR

rm -rf /opt/homebrew

アドバイス: いつも最小限覚える

使い方は、いつも最小限覚えるのが望ましい。必要な時に覚えていくのが良い。

パスの優先度の変更(重要!!)

変更するファイルは、/etc/paths である。

sudo vim /etc/paths

というように、vimコマンドを実行する。

そこで、

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

となっているところを

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

というように、/usr/local/binを一番上に変更する。 これで、/usr/local/binにあるファイルが先に読まれることになる。

CLTのインストールコマンド【追記】

xcode-select --install

を実行すると、ポップアップで、「command line toolをダウンロードしますか」が表示される。

参考記事

MacPortsの紹介 #MacOSX - Qiita

GitHub - Homebrew/install: 📥 Homebrew (un)installer

homebrewとは何者か。仕組みについて調べてみた #Mac - Qiita

Homebrew使い方まとめ #homebrew - Qiita

はてなで使えるMarkdown記法まとめ - 開発メモ

*1:最近はHomebrewの方が資料が多いかも

*2:ビール工場のことをbrewery(ブリュワリー)と言う

*3:定期的に実行するのが良い

*4:Mac設定の確認するのに、いろんな場面で結構役に立つので、覚えておくと良い。

*5:ウェブサーバからコンテンツを取得するダウンローダである。

*6:最近は年2回近くのペースで変わって、タイムマシーンのエラーにもウンザリ。。

*7:Homebrew-caskについてはまた次回にまとめることにする

*8:Homebrew Cask provides a friendly CLI workflow for the administration of macOS applications distributed as binaries.