Ubuntu に、パッケージマネージャを使って、
Node.js の比較的新しいバージョンをインストールしようとしたら、
ちょっとだけ手順が面倒だったので、そのメモ。
なお、インストールされるバージョンは、2016-06-03 現在のものなので、
これを実施するタイミングに応じて、バージョンに差異があるかもしれません。
インストール作業前に、どのバージョンが導入されるのか、
確認を行うようにしてください。
環境
- Ubuntu 14.04 LTS
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
セットアップ
セットアップするモジュールごとに、書いていきます。
Node.js (nodejs)
まずはパッケージマネージャで入る Node.js のバージョンを確認。
$ apt search nodejs | grep nodejs
WARNING: apt does not have a stable CLI interface yet. Use with caution in scripts.
unobtrusive notification system for nodejs
nodejs/trusty 0.10.25~dfsg2-2ubuntu1 amd64
nodejs-dbg/trusty 0.10.25~dfsg2-2ubuntu1 amd64
nodejs-dev/trusty 0.10.25~dfsg2-2ubuntu1 amd64
nodejs-legacy/trusty 0.10.25~dfsg2-2ubuntu1 all
どうやら、0.10.25
が入ってきそう。
インストールを進めます。
$ sudo apt install nodejs
[sudo] password for yo1000:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libc-ares2 libv8-3.14.5
The following NEW packages will be installed:
libc-ares2 libv8-3.14.5 nodejs
0 upgraded, 3 newly installed, 0 to remove and 67 not upgraded.
Need to get 1,912 kB of archives.
After this operation, 7,538 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
$ nodejs -v
v0.10.25
想定通り、0.10.25
が入りました。
npm
続いて、Node.js のパッケージマネージャである npm
をインストールします。
$ apt search npm | grep npm
WARNING: apt does not have a stable CLI interface yet. Use with caution in scripts.
node-npmlog/trusty 0.0.4-1 all
Read package.json for npm module for Node.js
npm/trusty 1.3.10~dfsg-1 all
NPM は、1.3.10
が入ってきそうです。
そのままインストールを進めます。
$ sudo apt install npm
Reading package lists... Done
Building dependency tree
...
The following packages will be upgraded:
gcc-4.8-base libc6 libssl1.0.0 libstdc++6
4 upgraded, 88 newly installed, 0 to remove and 63 not upgraded.
Need to get 49.1 MB of archives.
After this operation, 130 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
$ npm -v
1.3.10
こちらも想定通り、1.3.10
が入ってきました。
n
パッケージマネージャが入ったので、
引き続き、n
をインストールします。
n は、Node.js のバージョンアップに使用します。
$ sudo npm -g install n
npm http GET https://registry.npmjs.org/n
npm http 200 https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n/-/n-2.1.0.tgz
npm http 200 https://registry.npmjs.org/n/-/n-2.1.0.tgz
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
n@2.1.0 /usr/local/lib/node_modules/n
$ n --version
2.1.0
$ n -h
Usage: n [options/env] [COMMAND] [args]
Environments:
n [COMMAND] [args] Uses default env (node)
n io [COMMAND] Sets env as io
n project [COMMAND] Uses custom env-variables to use non-official sources
Commands:
n Output versions installed
n latest Install or activate the latest node release
n -a x86 latest As above but force 32 bit architecture
n stable Install or activate the latest stable node release
n lts Install or activate the latest LTS node release
n <version> Install node <version>
n use <version> [args ...] Execute node <version> with [args ...]
n bin <version> Output bin path for <version>
n rm <version ...> Remove the given version(s)
n --latest Output the latest node version available
n --stable Output the latest stable node version available
n --lts Output the latest LTS node version available
n ls Output the versions of node available
(iojs):
n io latest Install or activate the latest iojs release
n io -a x86 latest As above but force 32 bit architecture
n io <version> Install iojs <version>
n io use <version> [args ...] Execute iojs <version> with [args ...]
n io bin <version> Output bin path for <version>
n io rm <version ...> Remove the given version(s)
n io --latest Output the latest iojs version available
n io ls Output the versions of iojs available
Options:
-V, --version Output current version of n
-h, --help Display help information
-q, --quiet Disable curl output (if available)
-d, --download Download only
-a, --arch Override system architecture
Aliases:
which bin
use as
list ls
- rm
n がインストールされたため、
Node.js のバージョンアップができるようになりました。
主に使用することになるオプションが、以下あたりかと思うので、
頭の隅に留めておくと良いかもしれません。
n stable
n lts
n <version>
ここから、Node.js と NPM のバージョンアップを行うのですが、
ここで注意が必要です。
必ず、Node.js のバージョンアップから行うようにしてください。
NPM のバージョンアップを先行すると面倒なことになります。
というのも、古い NPM では、nodejs
コマンドを使用するのに対し、
比較的新しいバージョンの NPM では、node
コマンドを使用するため、
NPM を先行してバージョンアップしてしまうと、node
が見つからず、
依存関係の解決で失敗するようになってしまうのです。
一方、n
で、新しい Node.js をバージョンアップ (インストール) しても、
古い nodejs
は削除されないため、
NPM のバージョンアップは、その後、問題なく実行できるというわけです。
Node.js のバージョンアップ (node)
以上を踏まえ、まずは Node.js のバージョンを上げます。
Node.js 4 にバージョンアップする場合と、6 にバージョンアップする場合で、
それぞれ、いずれかを行なってください。
$ # Node.js 4 にバージョンアップする場合 (2016-06-03 現在)
$ sudo n lts
install : node-v4.4.5
mkdir : /usr/local/n/versions/node/4.4.5
fetch : https://nodejs.org/dist/v4.4.5/node-v4.4.5-linux-x64.tar.gz
######################################################################## 100.0%
installed : v4.4.5
$ nodejs -v
v0.10.25
$ node -v
v4.4.5
$ # Node.js 6 にバージョンアップする場合 (2016-06-03 現在)
$ sudo n stable
install : node-v6.2.1
mkdir : /usr/local/n/versions/node/6.2.1
fetch : https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-x64.tar.gz
...
installed : v6.2.1
$ nodejs -v
v0.10.25
$ node -v
v6.2.1
NPM のバージョンアップ
Node.js のバージョンアップができたら、NPM をバージョンアップします。
$ sudo npm update npm -g
loadRequestedDeps → gunzT ▄ ╢█████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╟
...
└── npm@3.9.6
$ npm -v
1.3.10
1.3.10
のままでした。
これ実は、内部的には反映されて 3.9.6
となっているのですが、
パスの読み込み直しなどするまでは、こうなってしまうようです。
再起動したところ、正しく更新されていることが確認できました。
$ sudo reboot
...
$ npm -v
3.9.6
おさらい
Node.js、NPM バージョンアップの際には、順序に気をつけよう。
以下の順序に従うのが良いかと思います。
-
sudo apt install nodejs
Node.js-0.10.25 が入る
nodejs コマンドが使えるようになる -
sudo apt install npm
npm-1.3.10 が入る
このバージョンの npm は、nodejs コマンドを使う -
sudo npm install n -g
n-2.1.0 が入る -
sudo n stable
stable を指定すると、Node.js-6.2.1 が入る (2016-06-03 現在)
lts を指定すると、Node.js-4.4.5 が入る (2016-06-03 現在)
node コマンドが使えるようになる -
sudo npm update npm -g
npm 3.9.5 が入る (2016-06-03 現在)
このバージョンの npm は、node コマンドを使う