LoginSignup
1
2

More than 5 years have passed since last update.

Debian(Wheezy)にnvmでnode.jsを導入する

Last updated at Posted at 2015-11-21

はじめに

Debian Wheezyに最新のnode.jsを入れたくてnvm(Node Version Manager)に環境を移行したのでメモ。
nvmの公式はこちら

手順

  1. nvmの導入
  2. nvmの動作確認
  3. node.jsの導入
  4. スクリプトの修正

nvmの導入

基本はDebian/UbuntuでNode.jsをインストールする(nvm)でOK。
最新版は公式通りにcurl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bashで入る。

nvmの動作確認

nvmコマンドで以下のusageが表示される。

Node Version Manager

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

Usage:
  nvm help                                  Show this message
  nvm --version                             Print out the latest released version of nvm
  nvm install [-s] <version>                Download and install a <version>, [-s] from source. Uses .nvmrc if available
    --reinstall-packages-from=<version>     When installing, reinstall packages installed in <node|iojs|node version number>
  nvm uninstall <version>                   Uninstall a version
  nvm use [--silent] <version>              Modify PATH to use <version>. Uses .nvmrc if available
  nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
  nvm run [--silent] <version> [<args>]     Run `node` on <version> with <args> as arguments. Uses .nvmrc if available
  nvm current                               Display currently activated version
  nvm ls                                    List installed versions
  nvm ls <version>                          List versions matching a given description
  nvm ls-remote                             List remote versions available for install
  nvm version <version>                     Resolve the given description to a single local version
  nvm version-remote <version>              Resolve the given description to a single remote version
  nvm deactivate                            Undo effects of `nvm` on current shell
  nvm alias [<pattern>]                     Show all aliases beginning with <pattern>
  nvm alias <name> <version>                Set an alias named <name> pointing to <version>
  nvm unalias <name>                        Deletes the alias named <name>
  nvm reinstall-packages <version>          Reinstall global `npm` packages contained in <version> to current version
  nvm unload                                Unload `nvm` from shell
  nvm which [<version>]                     Display path to installed node version. Uses .nvmrc if available

Example:
  nvm install v0.10.32                  Install a specific version number
  nvm use 0.10                          Use the latest available 0.10.x release
  nvm run 0.10.32 app.js                Run app.js using node v0.10.32
  nvm exec 0.10.32 node app.js          Run `node app.js` with the PATH pointing to node v0.10.32
  nvm alias default 0.10.32             Set default node version on a shell

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

node.jsの導入

nvm ls-remoteでインストール可能なnode.jsのバージョン一覧が表示されるので、自分でインストールしたいバージョンを指定してインストールする。
2015/11月現在の最新は5.1.0なのでnvm install 5.1.0とすればOK。


hoge@fuga:~$ node -v
v5.1.0
hoge@fuga:~$ npm -v
3.3.12

node.jsとnpmのバージョンを確認する。インストールが正常にできていれば上記のように表示されるはず。

スクリプトの修正

ログイン時にnvmとnode.jsを有効にするために公式通りにexportを記載して、最後にnvmコマンドでデフォルトで使用するnode.jsのバージョンを指定しておく。

.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
nvm use 5.1.0 > /dev/null

上の二行は公式通り。
最終行を追加する。

さいごに

nvm(Node Version Manager)を使用するとnode.jsの複数バージョン管理が楽で、かつユーザ単位での切り替えも容易になる。
使わない理由が特に見当たらないため、活用することをおすすめする。

追記:2015/12/06

nvm use 5.1.0と.bashrcに記載すると、WinSCPなどから入れなくなる。これはnvm useがechoで表示を行っているためで、SCPではこれが邪魔になる。
正しい対処方法か少し微妙だが、/dev/nullで非表示にすることでWinSCPも利用できるようになる。

1
2
0

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
1
2