LoginSignup
62

More than 5 years have passed since last update.

[Mac, Homebrew] Node.jsのバージョン管理ツール、nodebrew導入手順

Last updated at Posted at 2017-03-27

Node.jsは、nodebrewを使ってインストールするとバージョンの切り替えができて便利です。本記事では、Homebrew経由でのnodebrewの導入手順をまとめます

環境

  • OS X El Capitan (v10.11)

パッケージでインストールしたNode.jsをアンインストール

既にNode.jsをパッケージからインストールしている場合は、アンインストールします。ターミナルから$ node -vを実行し、バージョン情報が出力される場合は、以下の記事を参考にアンインストールしてください

MacにpkgでインストールしたNode.jsをアンインストールする手順

Homebrewをインストール

Homebrewのインストールをしていない場合は、公式ページの「Install Homebrew」を見てインストールしてください

nodebrewのインストール手順

基本的にはREADME通りに進めれば大丈夫ですが、今回はHomebrew経由でインストールするのでそこだけ注意です

1. nodebrewのインストール

まずはnodebrewのインストールです


$ brew install nodebrew

2. nodebrewのセットアップ(忘れずに!)

次に、nodebrew setupでnodebrewのセットアップをします


$ nodebrew setup
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

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

これを怠ると、ディレクトリがなくてNode.jsのインストールに失敗します(しましたorz)


$ nodebrew install-binary v6.10.1
Fetching: https://nodejs.org/dist/v6.10.1/node-v6.10.1-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: /Users/xxxxxxx/.nodebrew/src/v6.10.1/node-v6.10.1-darwin-x64.tar.gz:
Warning:  No such file or directory

curl: (23) Failed writing body (0 != 941)
download failed: https://nodejs.org/dist/v6.10.1/node-v6.10.1-darwin-x64.tar.gz

3. Node.jsをインストール

nodebrew ls-remoteで、インストール可能なNode.jsのバージョン一覧を見ることができます


$ nodebrew ls-remote
v0.0.1    v0.0.2    v0.0.3    v0.0.4    v0.0.5    v0.0.6    

v0.1.0    v0.1.1    v0.1.2    v0.1.3    v0.1.4    v0.1.5    v0.1.6    v0.1.7
...

インストールは、nodebrew install-binary <バージョン番号>で行います。試しにv6.10.1をインストールしてみます


$ nodebrew install-binary v6.10.1
Fetching: https://nodejs.org/dist/v6.10.1/node-v6.10.1-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

インストールしたバージョン一覧は、nodebrew lsで見ることができます


$ nodebrew ls
v6.10.1

current: none

latestで最新版、stableで安定版をインストールすることもできます


$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v7.7.4/node-v7.7.4-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

4. 使用するバージョンを指定

使用したいバージョンのインストールができたら、使用するバージョンをnodebrew use <バージョン番号>で指定します(lateststable指定もできます)


$ nodebrew use v6.10.1
use v6.10.1

これにより、v6.10.1がcurrentに設定されました


$ nodebrew ls
v6.10.1
v7.7.4

current: v6.10.1

5. PATHの設定

2.のセットアップ時の出力にもある通り、PATHを通しましょう。PATHを通さないと、nodeコマンドが実行できません


$ node -v
-bash: node: command not found

~/.bash_profile~/.bashrcに記述します


$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile

~/.bash_profile(または~/.bashrc)を再読み込みします
source ~/.bash_profile

これでnodeコマンドが実行できるようになりました


$ node -v
v6.10.1

npmコマンドも実行できることを確認してください


$ npm -v
3.10.10

その他

インストールしたバージョンのアンインストールは、nodebrew uninstall <バージョン番号>でできます


$ nodebrew uninstall v7.7.4
v7.7.4 uninstalled

*アンインストールでは、lateststable指定はできません


$ nodebrew uninstall latest
latest is not installed

参考

nodebrew
MacにpkgでインストールしたNode.jsをアンインストールする手順
Macにnode.jsをインストールする手順。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62