LoginSignup
45
46

More than 5 years have passed since last update.

Debian/UbuntuでNode.jsをインストールする(nvm)

Last updated at Posted at 2014-12-02

Motivation

Debian/Ubuntuにapt-getでNode.jsをインストールするとコマンド名がnodejsとなります。そうなるとnpmやその他のコマンドが動かなくなります。(他のOSではnodeというコマンド名でインストールされています)

結構Ubuntuを使っている人がこれにハマっていて、lnでシンボリックリンクを張ったりmvしたりして解決しているのを見ます。

そこでnvmでNode.jsをインストールします。

nvmでインストールすると、各ユーザのホームディレクトリ下にNode.jsがインストールされるのでrootになる必要はないですし、sudoも不要です。

手順

公式にあるとおりです。

  1. nvm のインストール
  2. .profile への設定
  3. nvm を使った Node.js のダウンロード

コマンド

tamura@ubu $ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.18.0/install.sh | bash
=> Downloading nvm from git to '/home/tamura/.nvm'
=> Cloning into '/home/tamura/.nvm'...
remote: Counting objects: 2638, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 2638 (delta 6), reused 1 (delta 0)
Receiving objects: 100% (2638/2638), 505.22 KiB | 254.00 KiB/s, done.
Resolving deltas: 100% (1425/1425), done.
Checking connectivity... done.
Note: checking out 'v0.18.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 5f5eb47... v0.18.0

=> Appending source string to /home/tamura/.bashrc
=> Close and reopen your terminal to start using nvm

tamura@ubu $ echo "source ~/.nvm/nvm.sh" >> ~/.profile

ここで一回ログアウトしてログインしなおします。もしくは source で nvm.sh を読み込みます。
nvm コマンドを打ち、反応があればOKです。

tamura@ubu $ nvm
Node Version Manager

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
    nvm uninstall <version>     Uninstall a version
    nvm use <version>           Modify PATH to use <version>. Uses .nvmrc if available
    nvm run <version> [<args>]  Run <version> with <args> as arguments. Uses .nvmrc if available for <version>
    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 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 copy-packages <version> Install global NPM packages contained in <version> to current version
    nvm unload                  Unload NVM from shell

Example:
    nvm install v0.10.24        Install a specific version number
    nvm use 0.10                Use the latest available 0.10.x release
    nvm run 0.10.24 myApp.js    Run myApp.js using node v0.10.24
    nvm alias default 0.10.24   Set default node version on a shell

Note:
    to remove, delete, or uninstall nvm - just remove ~/.nvm, ~/.npm, and ~/.bower folders

nvmでNode.jsを入れてみます。

tamura@ubu $ nvm ls-remote
...
(省略)
...
    v0.11.11
    v0.11.12
    v0.11.13
    v0.11.14

v0.11.14を入れてみます。

tamura@ubu $ nvm install 0.11.14
--2014-11-09 18:29:30--  https://nodejs.org/dist/v0.11.14/node-v0.11.14-linux-x64.tar.gz
nodejs.org (nodejs.org) をDNSに問いあわせています... 165.225.133.150
nodejs.org (nodejs.org)|165.225.133.150|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 6675959 (6.4M) [application/octet-stream]
`/home/tamura/.nvm/bin/node-v0.11.14-linux-x64/node-v0.11.14-linux-x64.tar.gz' に保存中

100%[========================================>] 6,675,959    156KB/s   時間 12s  

2014-11-09 18:29:42 (566 KB/s) - `/home/tamura/.nvm/bin/node-v0.11.14-linux-x64/node-v0.11.14-linux-x64.tar.gz' へ保存完了 [6675959/6675959]

Now using node v0.11.14
 
tamura@ubu $ node --version
v0.11.14

できました。

まとめ

  • Node.jsはnvmで入れましょう

nvmの使い方はその他のqiita等を参照してください。

参考

https://github.com/creationix/nvm

45
46
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
45
46