node.js を使ってみよう(環境構築編)
node.js を使ってみよう、環境構築編です。
node.js ですが、まだ全然分かってません。
ざっくり言えば、サーバサイドで実行するjsだと認識しています…が、
それはあってるんでしょうか…というレベルです。
概要などは上記のリンクで見ていただくとして、
ここでは環境構築を行います。
node.js の導入
node.js はバイナリでは配布されていないので、
自分でソースをダウンロード、コンパイルする必要があります。
node.js のソースコードをダウンロード
下記のようにダウンロードします。
wget でないとダメという事はありません。
$ wget http://nodejs.org/dist/node-v0.2.4.tar.gz --2010-10-30 21:33:25-- http://nodejs.org/dist/node-v0.2.4.tar.gz nodejs.org をDNSに問いあわせています... 8.12.44.238 nodejs.org|8.12.44.238|:80 に接続しています... 接続しました。 HTTP による接続要求を送信しました、応答を待っています... 200 OK 長さ: 4002347 (3.8M) [application/octet-stream] `node-v0.2.4.tar.gz' に保存中 100%[=========================================================>] 4,002,347 1.92M/s 時間 2.0s 2010-10-30 21:33:27 (1.92 MB/s) - `node-v0.2.4.tar.gz' へ保存完了 [4002347/4002347]
node.js のコンパイル → インストール
まず、tarを解凍します。
$ tar xfz node-v0.2.4.tar.gz # 解凍後は下記ディレクトリができます $ ls node-v0.2.4/ node-v0.2.4.tar.gz
コンパイル準備をします。
今回は特にインストール場所を指定しません。
デフォルトの /usr/local にインストールします。
$ cd node-v0.2.4/ $ ./configure Checking for program g++ or c++ : /usr/bin/g++ Checking for program cpp : /usr/bin/cpp Checking for program ar : /usr/bin/ar Checking for program ranlib : /usr/bin/ranlib Checking for g++ : ok Checking for program gcc or cc : /usr/bin/gcc Checking for gcc : ok Checking for library dl : yes Checking for openssl : not found Checking for function SSL_library_init : not found Checking for header openssl/crypto.h : not found /tmp/node.js/node-v0.2.4/wscript:186: error: Could not autodetect OpenSSL support. Make sure OpenSSL development packages are installed. Use configure --without-ssl to disable this message.
はい、エラーがでました。
エラーメッセージに従い、openssl-devel パッケージをインストールします。
$ sudo yum install openssl-devel
再度コンパイル準備を行います。
$ ./configure Checking for program g++ or c++ : /usr/bin/g++ Checking for program cpp : /usr/bin/cpp Checking for program ar : /usr/bin/ar Checking for program ranlib : /usr/bin/ranlib Checking for g++ : ok Checking for program gcc or cc : /usr/bin/gcc Checking for gcc : ok Checking for library dl : yes Checking for openssl : yes Checking for library rt : yes --- libeio --- Checking for library pthread : yes Checking for function pthread_create : yes Checking for function pthread_atfork : yes Checking for futimes(2) : yes Checking for readahead(2) : yes Checking for fdatasync(2) : yes Checking for pread(2) and pwrite(2) : yes Checking for sendfile(2) : yes Checking for sync_file_range(2) : no --- libev --- Checking for header sys/inotify.h : yes Checking for function inotify_init : yes Checking for header sys/epoll.h : yes Checking for function epoll_ctl : yes Checking for header port.h : not found Checking for header poll.h : yes Checking for function poll : yes Checking for header sys/event.h : not found Checking for header sys/queue.h : yes Checking for function kqueue : not found Checking for header sys/select.h : yes Checking for function select : yes Checking for header sys/eventfd.h : not found Checking for SYS_clock_gettime : yes Checking for library rt : yes Checking for function clock_gettime : yes Checking for function nanosleep : yes Checking for function ceil : yes Checking for fdatasync(2) with c++ : yes 'configure' finished successfully (2.216s)
正常に終了しました。
では、コンパイルします。
$ make ...省略... Waf: Leaving directory `/tmp/node.js/node-v0.2.4/build' 'build' finished successfully (3m14.938s)
最後に、インストールします。
コンパイル準備をする時にも書きましたが、
/usr/local にインストールするのでroot権限が必要になります(通常の場合)
$ sudo make install Waf: Entering directory `/tmp/node.js/node-v0.2.4/build' DEST_OS: linux DEST_CPU: x86_64 Parallel Jobs: 1 * installing deps/libeio/eio.h as /usr/local/include/node/eio.h * installing deps/v8/include/v8-debug.h as /usr/local/include/node/v8-debug.h ...省略... Waf: Leaving directory `/tmp/node.js/node-v0.2.4/build' 'install' finished successfully (0.195s)
node.js インストール確認
インストールがちゃんと出来たか確認します。
nodeコマンドを実行すると対話モードになるので、jsを書きます。
$ node > console.log("Hello node.js") Hello node.js
npm の導入
npm は Nodeのパッケージマネージャです(Node Package Manager)
npm | build amazing things
インストール方法には推奨3種類、非推奨1種類の計4種類あります。
npm/README.md at master · isaacs/npm · GitHub
/usr/local の権限を変えたくないので、
ここでは、推奨されない方法でインストールします。
準備
セキュリティ的に問題があるのかもしれないですが、
sudoで環境変数を引き継ぐようにします。
$ vim .bashrc # 下記を追記 alias sudo="sudo env PATH=$PATH" # 設定反映 $ source .bashrc
インストール
npm をインストールします。
「root権限で使うな」と警告が出ると思いますが、無視します。
$ curl http://npmjs.org/install.sh | sudo sh
npmの動作確認
npmコマンドでhelpのhelpを見てみます。
表示出来れば、動作問題ないと思います。
$ sudo npm help help
次回予告
環境構築はとりあえず、終わりです。
次回は、サンプルを動かしてみます。