Homebrewをインストールして、Homebrewでnodebrewをインストールして、nodebrewでNode.jsをインストールする

#1. Homebrewのインストール


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

```terminal:
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Homebrew
/usr/local/Frameworks
/usr/local/sbin
/usr/local/var

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir -p /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/var
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/var
==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown xxxxxxxxxx /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/var
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/var
==> /usr/bin/sudo /bin/mkdir -p /Users/xxxxxxxxxx/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/xxxxxxxxxx/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown xxxxxxxxxx /Users/xxxxxxxxxx/Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 106154, done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 106154 (delta 13), reused 21 (delta 11), pack-reused 106124
Receiving objects: 100% (106154/106154), 24.13 MiB | 4.62 MiB/s, done.
Resolving deltas: 100% (77488/77488), done.
From https://github.com/Homebrew/brew
 * [new branch]          master     -> origin/master
 * [new tag]             0.1        -> 0.1
 * [new tag]             0.2        -> 0.2
 * [new tag]             0.3        -> 0.3
 * [new tag]             0.4        -> 0.4
 * [new tag]             0.5        -> 0.5
 * [new tag]             0.6        -> 0.6
 * [new tag]             0.7        -> 0.7
 * [new tag]             0.7.1      -> 0.7.1
 * [new tag]             0.8        -> 0.8
 * [new tag]             0.8.1      -> 0.8.1
 * [new tag]             0.9        -> 0.9
 * [new tag]             0.9.1      -> 0.9.1
 * [new tag]             0.9.2      -> 0.9.2
 * [new tag]             0.9.3      -> 0.9.3
 * [new tag]             0.9.4      -> 0.9.4
 * [new tag]             0.9.5      -> 0.9.5
 * [new tag]             0.9.8      -> 0.9.8
 * [new tag]             0.9.9      -> 0.9.9
 * [new tag]             1.0.0      -> 1.0.0
 * [new tag]             1.0.1      -> 1.0.1
 * [new tag]             1.0.2      -> 1.0.2
 * [new tag]             1.0.3      -> 1.0.3
 * [new tag]             1.0.4      -> 1.0.4
 * [new tag]             1.0.5      -> 1.0.5
 * [new tag]             1.0.6      -> 1.0.6
 * [new tag]             1.0.7      -> 1.0.7
 * [new tag]             1.0.8      -> 1.0.8
 * [new tag]             1.0.9      -> 1.0.9
 * [new tag]             1.1.0      -> 1.1.0
 * [new tag]             1.1.1      -> 1.1.1
 * [new tag]             1.1.10     -> 1.1.10
 * [new tag]             1.1.11     -> 1.1.11
 * [new tag]             1.1.12     -> 1.1.12
 * [new tag]             1.1.13     -> 1.1.13
 * [new tag]             1.1.2      -> 1.1.2
 * [new tag]             1.1.3      -> 1.1.3
 * [new tag]             1.1.4      -> 1.1.4
 * [new tag]             1.1.5      -> 1.1.5
 * [new tag]             1.1.6      -> 1.1.6
 * [new tag]             1.1.7      -> 1.1.7
 * [new tag]             1.1.8      -> 1.1.8
 * [new tag]             1.1.9      -> 1.1.9
 * [new tag]             1.2.0      -> 1.2.0
 * [new tag]             1.2.1      -> 1.2.1
 * [new tag]             1.2.2      -> 1.2.2
 * [new tag]             1.2.3      -> 1.2.3
 * [new tag]             1.2.4      -> 1.2.4
 * [new tag]             1.2.5      -> 1.2.5
 * [new tag]             1.2.6      -> 1.2.6
 * [new tag]             1.3.0      -> 1.3.0
 * [new tag]             1.3.1      -> 1.3.1
 * [new tag]             1.3.2      -> 1.3.2
 * [new tag]             1.3.3      -> 1.3.3
 * [new tag]             1.3.4      -> 1.3.4
 * [new tag]             1.3.5      -> 1.3.5
 * [new tag]             1.3.6      -> 1.3.6
 * [new tag]             1.3.7      -> 1.3.7
 * [new tag]             1.3.8      -> 1.3.8
 * [new tag]             1.3.9      -> 1.3.9
 * [new tag]             1.4.0      -> 1.4.0
 * [new tag]             1.4.1      -> 1.4.1
 * [new tag]             1.4.2      -> 1.4.2
 * [new tag]             1.4.3      -> 1.4.3
 * [new tag]             1.5.0      -> 1.5.0
 * [new tag]             1.5.1      -> 1.5.1
 * [new tag]             1.5.10     -> 1.5.10
 * [new tag]             1.5.11     -> 1.5.11
 * [new tag]             1.5.12     -> 1.5.12
 * [new tag]             1.5.13     -> 1.5.13
 * [new tag]             1.5.14     -> 1.5.14
 * [new tag]             1.5.2      -> 1.5.2
 * [new tag]             1.5.3      -> 1.5.3
 * [new tag]             1.5.4      -> 1.5.4
 * [new tag]             1.5.5      -> 1.5.5
 * [new tag]             1.5.6      -> 1.5.6
 * [new tag]             1.5.7      -> 1.5.7
 * [new tag]             1.5.8      -> 1.5.8
 * [new tag]             1.5.9      -> 1.5.9
 * [new tag]             1.6.0      -> 1.6.0
 * [new tag]             1.6.1      -> 1.6.1
 * [new tag]             1.6.10     -> 1.6.10
 * [new tag]             1.6.11     -> 1.6.11
 * [new tag]             1.6.12     -> 1.6.12
 * [new tag]             1.6.13     -> 1.6.13
 * [new tag]             1.6.14     -> 1.6.14
 * [new tag]             1.6.15     -> 1.6.15
 * [new tag]             1.6.16     -> 1.6.16
 * [new tag]             1.6.17     -> 1.6.17
 * [new tag]             1.6.2      -> 1.6.2
 * [new tag]             1.6.3      -> 1.6.3
 * [new tag]             1.6.4      -> 1.6.4
 * [new tag]             1.6.5      -> 1.6.5
 * [new tag]             1.6.6      -> 1.6.6
 * [new tag]             1.6.7      -> 1.6.7
 * [new tag]             1.6.8      -> 1.6.8
 * [new tag]             1.6.9      -> 1.6.9
 * [new tag]             1.7.0      -> 1.7.0
 * [new tag]             1.7.1      -> 1.7.1
HEAD is now at f46eafc27 Merge pull request #4589 from reitermarkus/curl-stdout
==> Downloading https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-2.3.7.leopard_64.bottle.tar.gz
######################################################################## 100.0%
==> Pouring portable-ruby-2.3.7.leopard_64.bottle.tar.gz
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4808, done.
remote: Compressing objects: 100% (4591/4591), done.
remote: Total 4808 (delta 57), reused 853 (delta 26), pack-reused 0
Receiving objects: 100% (4808/4808), 3.90 MiB | 1.76 MiB/s, done.
Resolving deltas: 100% (57/57), done.
Tapped 2 commands and 4593 formulae (4,849 files, 12.2MB).
==> Cleaning up /Library/Caches/Homebrew...
Removing: /Library/Caches/Homebrew/automake-1.15.tar.xz... (1.4MB)
Removing: /Library/Caches/Homebrew/go-1.4.2.tar.gz... (10.4MB)
Removing: /Library/Caches/Homebrew/node-0.12.2.tar.gz... (18.4MB)
Removing: /Library/Caches/Homebrew/pcre-8.36.tar.bz2... (1.5MB)
Removing: /Library/Caches/Homebrew/pkg-config-0.28.tar.gz... (1.8MB)
Removing: /Library/Caches/Homebrew/watchman-3.1.tar.gz... (205.9KB)
Removing: /Library/Caches/Homebrew/xz-5.2.1.tar.gz... (1.4MB)
==> Migrating /Library/Caches/Homebrew to /Users/xxxxxxxxxx/Library/Caches/Homebrew...
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
Error: Could not link:
/usr/local/share/man/man1/brew.1

Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/doc/homebrew

Please delete these paths and run `brew update`.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh
```

動作確認

```terminal:
$ brew help
```

```terminal:
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh
```


# 2. nodebrewのインストール

```terminal:
$ brew install nodebrew
```

```terminal:
==> Downloading https://github.com/hokaccha/nodebrew/archive/v1.0.0.tar.gz
==> Downloading from https://codeload.github.com/hokaccha/nodebrew/tar.gz/v1.0.0
######################################################################## 100.0%
==> Caveats
You need to manually run setup_dirs to create directories required by nodebrew:
  /usr/local/opt/nodebrew/bin/nodebrew setup_dirs

Add path:
  export PATH=$HOME/.nodebrew/current/bin:$PATH

To use Homebrew's directories rather than ~/.nodebrew add to your profile:
  export NODEBREW_ROOT=/usr/local/var/nodebrew

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/nodebrew/1.0.0: 8 files, 38.5KB, built in 3 seconds
```


```terminal:
$ nodebrew -v
```

```terminal:
nodebrew 1.0.0

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (from binary)
    nodebrew compile <version>            Download and install <version> (from source)
    nodebrew install-binary <version>     Alias of `install` (For backword compatibility)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    nodebrew list                         List installed versions
    nodebrew ls                           Alias for `list`
    nodebrew ls-remote                    List remote versions
    nodebrew ls-all                       List remote and installed versions
    nodebrew alias <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>

Example:
    # install
    nodebrew install v8.9.4

    # use a specific version number
    nodebrew use v8.9.4
```

# Node.jsのインストール

nodebrewを使って最新版のnode.jsをインストールする

```terminal:
$ nodebrew install-binary latest
```

```terminal:
Fetching: https://nodejs.org/dist/v10.7.0/node-v10.7.0-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: /Users/xxxxxxxxxx/.nodebrew/src/v10.7.0/node-v10.7.0-darwin-x64.tar.gz: 
Warning: No such file or directory
                                                                           0.0%
curl: (23) Failed writing body (0 != 847)
download failed: https://nodejs.org/dist/v10.7.0/node-v10.7.0-darwin-x64.tar.gz
```

インストール先のディレクトリがないと警告が出たので~/.nodebrew/srcディレクトリを作成する

```terminal:
$ mkdir -p ~/.nodebrew/src
```


```terminal:
$ nodebrew install-binary latest
```

```terminal:
Fetching: https://nodejs.org/dist/v10.7.0/node-v10.7.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully
```


インストールされているnode.jsのバージョンと選択しているバージョンを確認する

```terminal:
$ nodebrew list
```

```terminal:
v10.7.0
current: none
```

使用するnode.jsのバージョンを選択する

```terminal:
$ nodebrew use  v10.7.0
```

```terminal:
use v10.7.0
```

インストールされているnode.jsのバージョンと選択しているバージョンを確認する

```terminal:
$ nodebrew list
v10.7.0
current: v10.7.0
```

nodeコマンドをターミナルから使用できるように.bashrcにプログラムへのパスを追記する

```terminal:
$ echo 'export PATH=$PATH:/Users/xxxxxxxxxx/.nodebrew/current/bin' >> ~/.bashrc
```

.bashrcを読み込む

```terminal:
if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi
```

```terminal:
$ node -v
v10.7.0
```

```terminal:
$ npm -v
6.1.0
```

このままだとターミナルを立ち上げる度に.bashrcを読み込まなければならないので、常に読み込むように.bash_profileを編集する

.bash_profileをエディタで開く

```terminal:
$ open ~/.bash_profile
```

.bashrcを読み込むように追記する

```text:
if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi
```

.bash_profileの内容を表示して確認する

```terminal:
$ cat .bash_profile
```