Macbook へ brewコマンドをインストールしてみてみた

brew コマンドで zsh-completion, iperf3, speedtest など Apple が提供していないものをインストールすることができます。
新しい Mac では 通常のインストールコマンドにいくつかコマンドをインストールする必要があったのでメモしておきます。



# ■ brewインストール
## ● Homebrew ホームページへアクセス
[Homebrew ホームページ](https://brew.sh/index_ja)へアクセスして、インストール・コマンドをコピーします。
![brewコマンドインストール.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109087/041692c2-1bd0-269f-27f8-63cd344465a7.jpeg)



## ● brewインストール
コピーしたコマンドを、ターミナルへペーストして実行します。
すると Next steps:の項目が出力されます。

```shell
user@mac-book ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    ==> Checking for `sudo` access (which may request your password)...
    Password:
    ==> This script will install:
    /opt/homebrew/bin/brew
    /opt/homebrew/share/doc/homebrew
    /opt/homebrew/share/man/man1/brew.1
    /opt/homebrew/share/zsh/site-functions/_brew
    /opt/homebrew/etc/bash_completion.d/brew
    /opt/homebrew

    Press RETURN/ENTER to continue or any other key to abort:
    ==> /usr/bin/sudo /usr/sbin/chown -R user:admin /opt/homebrew
    ==> Downloading and installing Homebrew...
    HEAD is now at 0d6d390c5 Merge pull request #13508 from Homebrew/dependabot/bundler/Library/Homebrew/sorbet-static-and-runtime-0.5.10138
    Warning: /opt/homebrew/bin is not in your PATH.
    Instructions on how to configure your shell for Homebrew
    can be found in the 'Next steps' section below.
    ==> Installation successful!

    ==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
    Read the analytics documentation (and how to opt-out) here:
    https://docs.brew.sh/Analytics
    No analytics data has been sent yet (nor will any be during this install run).

    ==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
    https://github.com/Homebrew/brew#donations

    ==> Next steps:
    - Run these two commands in your terminal to add Homebrew to your PATH:
        echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/user/.zprofile
        eval "$(/opt/homebrew/bin/brew shellenv)"
    - Run brew help to get started
    - Further documentation:
        https://docs.brew.sh
```

## ● 追加コマンド実行
brewインストールすると最後尾に Next steps: があります。
```
    ==> Next steps:
    - Run these two commands in your terminal to add Homebrew to your PATH:
        echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/user/.zprofile
        eval "$(/opt/homebrew/bin/brew shellenv)"
    - Run brew help to get started
    - Further documentation:
        https://docs.brew.sh
```

**・　Next steps: の2つのコマンドを実行**

:::note warn
Mac OS のバージョンにより、Next steps:が異なることがあります。
:::

```
user@mac-book ~ % echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/user/.zprofile
user@mac-book ~ % eval "$(/opt/homebrew/bin/brew shellenv)"
```


## ● 追加コマンド実行確認

```
user@mac-book ~ % cat /Users/user/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
```


## ● brew インストール確認
brew helpコマンドでbrewがインストールされていることを確認

```
user@mac-book ~ % brew help
    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
```

# ■ zsh-completionインストール
ZSH ではまだ利用できない新しい補完スクリプトをできるようにしてくれるツールです。
ssh の configファイルに設定したホストをTABキー補完で表示できるようにします。


## ⚫︎ brew install zsh-completion
**1) Install**
```
user@mac-book .ssh % brew install zsh-completion
	Running `brew update --auto-update`...
	==> Downloading https://formulae.brew.sh/api/formula.jws.json
	######################################################################### 100.0%
	==> Downloading https://formulae.brew.sh/api/cask.jws.json
	######################################################################### 100.0%
	==> Fetching zsh-completions
	==> Downloading https://ghcr.io/v2/homebrew/core/zsh-completions/manifests/0.34.
	######################################################################### 100.0%
	==> Downloading https://ghcr.io/v2/homebrew/core/zsh-completions/blobs/sha256:51
	######################################################################### 100.0%
	==> Pouring zsh-completions--0.34.0.all.bottle.tar.gz
	==> Caveats
	To activate these completions, add the following to your .zshrc:
	
	  if type brew &>/dev/null; then
	    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
	
	    autoload -Uz compinit
	    compinit
	  fi
	
	You may also need to force rebuild `zcompdump`:
	
	  rm -f ~/.zcompdump; compinit
	
	Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting
	to load these completions, you may need to run this:
	
	  chmod -R go-w '/opt/homebrew/share/zsh'
	==> Summary
	🍺  /opt/homebrew/Cellar/zsh-completions/0.34.0: 151 files, 1.2MB
	==> Running `brew cleanup zsh-completions`...
	Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
	Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
	==> `brew cleanup` has not been run in the last 30 days, running now...
	Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
	Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
	Removing: /Users/user/Library/Caches/Homebrew/openssl@3--3.1.1_1... (7.7MB)
	Removing: /Users/user/Library/Logs/Homebrew/openssl@3... (64B)
	Removing: /Users/user/Library/Logs/Homebrew/ca-certificates... (64B)
	Pruned 0 symbolic links and 2 directories from /opt/homebrew
```


**2) .zshrc 設定**
```
user@mac-book ~ % cat ~/.zshrc
    if type brew &>/dev/null; then
        FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

        autoload -Uz compinit
        compinit
    fi
```

**3) .zcompdump再構築**
```
user@mac-book ~ % rm -f ~/.zcompdump; compinit
```

## ⚫︎ 動作確認

**1) ターミナル再起動**
.zshrcを読み込むためターミナルを再起動します。

**2) ssh config host情報確認**
```
user@mac-book ~ % cat ~/.ssh/config | grep host
    host linux-osaka-inst  
    host linux-tokyo-inst
```
**3) ssh Host 補完表示**
```
user@mac-book ~ % ssh exa- [Tabキー押下]
    exa-osaka-inst  exa-tokyo-inst
```

# ■ iperf3インストール
brewコマンドでiperf3をインストールしてみてみます。

## ● iperf3インストール

```
user@mac-book ~ % brew install iperf3
    ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2022-04-26
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:c05a44feba2a630de2e1cefba90d3aa3f74e4d57146c0117858f648c4
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:c05a44feba2a630de2e1cefba90d3aa3f74e4d57146c011785
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1p
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/blobs/sha256:47a1bfb4e090a4a21320e809be8744dccc43baf63f251d1501bf8976903bd
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:47a1bfb4e090a4a21320e809be8744dccc43baf63f251d1501
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/iperf3/manifests/3.11
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/iperf3/blobs/sha256:07f43ee08140b9ed415ef4c84d58f0a0242ca4eb11bd5d9691e55066e341dbfd
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:07f43ee08140b9ed415ef4c84d58f0a0242ca4eb11bd5d9691
    ######################################################################## 100.0%
    ==> Installing dependencies for iperf3: ca-certificates and openssl@1.1
    ==> Installing iperf3 dependency: ca-certificates
    ==> Pouring ca-certificates--2022-04-26.all.bottle.tar.gz
    ==> Regenerating CA certificate bundle from keychain, this may take a while...
    🍺  /opt/homebrew/Cellar/ca-certificates/2022-04-26: 3 files, 215.6KB
    ==> Installing iperf3 dependency: openssl@1.1
    ==> Pouring openssl@1.1--1.1.1p.arm64_monterey.bottle.tar.gz
    🍺  /opt/homebrew/Cellar/openssl@1.1/1.1.1p: 8,097 files, 18MB
    ==> Installing iperf3
    ==> Pouring iperf3--3.11.arm64_monterey.bottle.tar.gz
    🍺  /opt/homebrew/Cellar/iperf3/3.11: 11 files, 503.2KB
    ==> Running `brew cleanup iperf3`...
    Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
    Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```


## ● iperf3コマンド実行テスト

```
user@mac-book ~ % iperf3 -c 10.2.0.2 -u -b 125M
    Connecting to host 10.2.0.2, port 5201
    [  5] local 192.168.200.7 port 65421 connected to 10.2.0.2 port 5201
    [ ID] Interval           Transfer     Bitrate         Total Datagrams
    [  5]   0.00-1.00   sec  14.9 MBytes   125 Mbits/sec  11670
    [  5]   1.00-2.00   sec  14.9 MBytes   125 Mbits/sec  11678
    [  5]   2.00-3.00   sec  14.9 MBytes   125 Mbits/sec  11678
    [  5]   3.00-4.00   sec  14.9 MBytes   125 Mbits/sec  11685
    [  5]   4.00-5.00   sec  14.9 MBytes   125 Mbits/sec  11676
    [  5]   5.00-6.00   sec  14.9 MBytes   125 Mbits/sec  11673
    [  5]   6.00-7.00   sec  14.9 MBytes   125 Mbits/sec  11679
    [  5]   7.00-8.00   sec  14.9 MBytes   125 Mbits/sec  11683
    [  5]   8.00-9.00   sec  14.9 MBytes   125 Mbits/sec  11670
    [  5]   9.00-10.00  sec  14.9 MBytes   125 Mbits/sec  11679
    - - - - - - - - - - - - - - - - - - - - - - - - -
    [ ID] Interval           Transfer     Bitrate         Jitter    Lost/Total Datagrams
    [  5]   0.00-10.00  sec   149 MBytes   125 Mbits/sec  0.000 ms  0/116771 (0%)  sender
    [  5]   0.00-10.00  sec   148 MBytes   124 Mbits/sec  0.960 ms  568/116767 (0.49%)  receiver

    iperf Done.
```



# ■ Speedtest CLIインストール
Speedtest CLI を brew を使用してインストールしてみてみます。

https://www.speedtest.net/apps/cli

## ● Speedtestインストール
Speedtest CLIのページにbrewを使用したインストールコマンドが記載されています。
![51_MacBookインストール.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109087/711980c8-2730-cd17-01cc-a4f671710426.jpeg)


### 1) brew tap teamookla/speedtest実行
```
user@mac-book ~ % brew tap teamookla/speedtest
	Running `brew update --auto-update`...
	==> Auto-updated Homebrew!
	Updated 2 taps (homebrew/core and homebrew/cask).
	==> New Formulae
	cloudflare-wrangler2              httm                              libfyaml                          psysh                             txt2man
	dufs                              keploy                            nap                               secp256k1                         wikibase-cli
	==> New Casks
	devsquadron                       duplicacy-cli                     unraid-usb-creator                vero                              wooshy

	You have 5 outdated formulae installed.
	You can upgrade them with brew upgrade
	or list them with brew outdated.

	==> Tapping teamookla/speedtest
	Cloning into '/opt/homebrew/Library/Taps/teamookla/homebrew-speedtest'...
	remote: Enumerating objects: 64, done.
	remote: Counting objects: 100% (21/21), done.
	remote: Compressing objects: 100% (16/16), done.
	remote: Total 64 (delta 7), reused 13 (delta 5), pack-reused 43
	Receiving objects: 100% (64/64), 10.52 KiB | 10.52 MiB/s, done.
	Resolving deltas: 100% (18/18), done.
	Tapped 1 formula (13 files, 17KB).
```

### 2) brew update実行
```
	user@mac-book ~ % brew update
	Already up-to-date.
```

### 3) brew install speedtest実行
```
user@mac-book ~ % brew install speedtest --force
	==> Fetching teamookla/speedtest/speedtest
	==> Downloading https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-macosx-universal.tgz
	######################################################################## 100.0%
	==> Installing speedtest from teamookla/speedtest
	🍺  /opt/homebrew/Cellar/speedtest/1.2.0: 4 files, 2.7MB, built in 3 seconds
	==> Running `brew cleanup speedtest`...
	Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
	Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```

### 4) speedtestインストール確認
```
user@mac-book ~ % cd /opt/homebrew/Cellar/speedtest/1.2.0/bin
user@mac-book bin % ls -l
	total 5528
	-r-xr-xr-x  1 user  admin  2828368 Jul 28 10:35 speedtest
```

### 5) Accept the license
```
user@mac-book bin % ./speedtest -s 21569
==============================================================================

You may only use this Speedtest software and information generated
from it for personal, non-commercial use, through a command line
interface on a personal computer. Your use of this software is subject
to the End User License Agreement, Terms of Use and Privacy Policy at
these URLs:

	https://www.speedtest.net/about/eula
	https://www.speedtest.net/about/terms
	https://www.speedtest.net/about/privacy

==============================================================================

Do you accept the license? [type YES to accept]: YES
License acceptance recorded. Continuing.
```

## ● speedtest実行
### 1) peedtest　Help確認
```
user@mac-book bin % ./speedtest -h
Speedtest by Ookla is the official command line client for testing the speed and performance of your internet connection.

Version: speedtest 1.2.0.84

Usage: speedtest [<options>]
  -h, --help                        Print usage information
  -V, --version                     Print version number
  -L, --servers                     List nearest servers
  -s, --server-id=#                 Specify a server from the server list using its id
  -I, --interface=ARG               Attempt to bind to the specified interface when connecting to servers
  -i, --ip=ARG                      Attempt to bind to the specified IP address when connecting to servers
  -o, --host=ARG                    Specify a server, from the server list, using its host's fully qualified domain name
  -p, --progress=yes|no             Enable or disable progress bar (Note: only available for 'human-readable'
                                    or 'json' and defaults to yes when interactive)
  -P, --precision=#                 Number of decimals to use (0-8, default=2)
  -f, --format=ARG                  Output format (see below for valid formats)
      --progress-update-interval=#  Progress update interval (100-1000 milliseconds)
  -u, --unit[=ARG]                  Output unit for displaying speeds (Note: this is only applicable
                                    for ‘human-readable’ output format and the default unit is Mbps)
  -a                                Shortcut for [-u auto-decimal-bits]
  -A                                Shortcut for [-u auto-decimal-bytes]
  -b                                Shortcut for [-u auto-binary-bits]
  -B                                Shortcut for [-u auto-binary-bytes]
      --selection-details           Show server selection details
      --ca-certificate=ARG          CA Certificate bundle path
  -v                                Logging verbosity. Specify multiple times for higher verbosity
      --output-header               Show output header for CSV and TSV formats

 Valid output formats: human-readable (default), csv, tsv, json, jsonl, json-pretty

 Machine readable formats (csv, tsv, json, jsonl, json-pretty) use bytes as the unit of measure with max precision

 Valid units for [-u] flag:
   Decimal prefix, bits per second:  bps, kbps, Mbps, Gbps
   Decimal prefix, bytes per second: B/s, kB/s, MB/s, GB/s
   Binary prefix, bits per second:   kibps, Mibps, Gibps
   Binary prefix, bytes per second:  kiB/s, MiB/s, GiB/s
   Auto-scaled prefix: auto-binary-bits, auto-binary-bytes, auto-decimal-bits, auto-decimal-bytes
```

### 2) speedtest Servers確認
```
user@mac-book bin % ./speedtest -L
	Closest servers:

		ID  Name                           Location             Country
	==============================================================================
	21569  i3D.net                        Tokyo                Japan
	28910  fdcservers.net                 Tokyo                Japan
	38241  Enzu.com                       Tokyo                Japan
	20976  GLBB Japan                     Tokyo                Japan
	24333  Rakuten Mobile, Inc            Tokyo                Japan
	50686  GSL Networks                   Tokyo                Japan
	50959  Osaka Electro-Communication University | Communication Research Club Tokyo                Japan
	50467  Verizon                        Tokyo                Japan
	48463  IPA CyberLab 400G              Tokyo                Japan
	14623  IPA CyberLab                   Bunkyo               Japan
```

### 3) speedtes測定実行
```
user@mac-book bin % ./speedtest -s 21569

   Speedtest by Ookla

      Server: i3D.net - Tokyo (id: 21569)
         ISP: VECTANT
Idle Latency:     5.24 ms   (jitter: 0.53ms, low: 5.08ms, high: 6.09ms)
    Download:    92.23 Mbps (data used: 45.2 MB)
                 21.36 ms   (jitter: 1.73ms, low: 7.12ms, high: 25.11ms)
      Upload:    92.74 Mbps (data used: 112.5 MB)
                103.72 ms   (jitter: 31.01ms, low: 6.33ms, high: 604.53ms)
 Packet Loss:     2.7%
  Result URL: https://www.speedtest.net/result/c/8136414f-70a1-4b8a-9dfd-79f85875b1aa
```

# ■ 参考
　・[Homebrew](https://brew.sh/index_ja)
　・[Homebrew Formulae: ipserf3](https://formulae.brew.sh/formula/iperf3)
　・[zsh-completions](https://github.com/zsh-users/zsh-completions)
　・[Speedtest CLI](https://www.speedtest.net/apps/cli)