このブログの更新は Twitterアカウント @m_hiyama で通知されます。
Follow @m_hiyama

メールでのご連絡は hiyama{at}chimaira{dot}org まで。

はじめてのメールはスパムと判定されることがあります。最初は、信頼されているドメインから差し障りのない文面を送っていただけると、スパムと判定されにくいと思います。

参照用 記事

LinuxとWindowsにnode.jsをインストールしてみた

Google Chromeに搭載のJavaScript処理系であるV8エンジンを使ってみたいな、と思いました。ここんところ巷で評判のnode.jsにもREPL(Read-Eval-Print Loop; 対話的シェル)があるようなので(http://nodejs.org/docs/v0.3.1/api/repl.html)、node.jsを入れてみることにします。

古いLinux

まずは無難にLinux環境で。僕が借りているサーバーは古いので、そのせいで何か問題があるかもしれませんけど。


$ cat /proc/version
Linux version 2.6.9-78.0.5.plus.c4 (mockbuild@builder16.centos.org) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)) #1 Wed Oct 8 13:39:28 EDT 2008

$ cat /etc/redhat-release
CentOS release 4.7 (Final)

$ gcc --version
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$

node.jsのビルドに使うツールSConsでPythonを使いますが、Pythonが古すぎるとマズイようです。僕はたまたまPython 2.6.4を入れていたので大丈夫でした。


$ /usr/bin/python -V
Python 2.3.4

$ /usr/local/python/bin/python -V
Python 2.6.4

$ which python
/usr/local/python/bin/python

$

http://nodejs.org/ から次のアーカイブをダウンロードしました。

  • 2011.02.19更新版 node-v0.4.1.tar.gz

SSL付きでビルドしたいなら、前もってOpenSLLをインストールしておきます。


$ sudo yum install openssl-devel

後はお決まりの手順:


$ ./configure --prefix=$HOME/local/node

$ make

$ make install

しかし、リンクの段階で、PR_SET_NAMEというシンボルが未定義でエラーとなりました。


`PR_SET_NAME' was not declared in this scope

PR_SET_NAMEをgrepで探してみると、次の2つのファイルで使っています。

  • node-v0.4.1/deps/v8/src/platform-linux.cc
  • node-v0.4.1/src/platform_linux.cc

プロセスタイトルをセットするだけで、動作に影響はないようなのでコメントアウト


static void* ThreadEntry(void* arg) {
Thread* thread = reinterpret_cast<Thread*>(arg);
// This is also initialized by the first argument to pthread_create() but we
// don't know which thread will run first (the original thread or the new
// one) so we initialize it here too.
// prctl(PR_SET_NAME, thread->name(), 0, 0, 0); // コメントアウト
thread->thread_handle_data()->thread_ = pthread_self();
ASSERT(thread->IsValid());
thread->Run();
return NULL;
}


void Platform::SetProcessTitle(char *title) {
if (process_title) free(process_title);
process_title = strdup(title);
// prctl(PR_SET_NAME, process_title); // コメントアウト
}

この手直しでどうやら出来たようです。


$ which node
~/bin/node

$ node --version
v0.4.1

$

この後、curl http://npmjs.org/install.sh | sudo sh とすると、npm(node package manager)がインストールできます。ただ、ここで sudo でインストールすると、その後も sudo npm install express のようにsudoが必要みたいです。僕の場合は特に問題になりませんが、自分のホーム内にインストールしたときは、sudoなしのほうが便利でしょう。

Windows7

Windowsでは無理かな、と思いましたが意外と簡単でした。

node.jsの新しいバージョンのほうがWindowsサポートが進捗していそうなので、今度はgithubから取ってくることにします。まず、Windowsにgitをインストールします。MSysGitにしました。

ダウンロードエリア http://code.google.com/p/msysgit/downloads/list からネットインストーラーmsysGit-netinstall-1.7.4-preview20110204.exeをダウンロードして実行しました。かなり時間はかかりますが、インストールは万事やってくれます。

MSysGitディレクトリーの下にMinGW/MSYS一式が入ります。既にMSYSを使っている僕にはちょっと迷惑ですが、とりあえずはMSysGitで作業します。

適当な場所にnode.js一式をクローンします。


$ git clone https://github.com/joyent/node.git

僕はこの機会にgithubアカウントを取ってみたのですが、cloneするのにアカウントが必要なわけではないようです。

あとは https://github.com/ry/node/wiki/Building-node.js-on-mingw に従えばいいのですが、なぜかこのページが消えていました。Googleのキャッシュからサルベージしたものを最後に付けておきます。
[追記]ページは復活しました(2011-03-02T16:35)。事情はkoichikさんのコメントを参照してください。 [/追記]

さて、手順書きにある「必要なもの」(Prerequisites)で、MinGW/MSYSとgitは、msysgitを入れれば揃います。Python 2.7が必要と書いてありますが、2.6でも大丈夫です。OpenSSLはナシにして、./configure --without-ssl; makeしてみました。

次のような警告メッセージが出ます。


In file included from ..\deps\libev\ev.c:523:
..\deps\libev\ev_win32.c: In function 'ev_time':
..\deps\libev\ev_win32.c:149: warning: integer constant is too large for 'long' type

対処法がわからないのでとりあえず放置。

リンクのときに次のエラーで失敗。


c:/installed/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -lpthread.dll

pthreadライブラリがないのだろうと、ftp://sources.redhat.com/pub/pthreads-win32/ からpthreads-w32-2-8-0-release.tar.gzをダウンロードして作ったのですが、これは不要だったかも知れません(後述)。


$ tar zxvf pthreads-w32-2-8-0-release.tar.gz
$ cd pthreads-w32-2-8-0-release/
$ make clean GC

必要なファイルは次の2つです。

  • pthreadGC2.dll
  • libpthreadGC2.a

pthread-w32を作らなくても、この名前のファイルは /mingw/bin/, /mingw/lib/ にあるようです。適切な名前でコピーします(MSYSでは、シンボリックリンクしてもコピーと同じです)。


$ cp libpthreadGC2.a /mingw/lib/libpthread.dll.a

$ cp pthreadGC2.dll /usr/bin/ # たぶん不要

これでmakeし直して、なんとかWindows版node.jsが出来ました。


$ which node
/c/Installed/node/./node.exe

$ node --version
v0.5.0-pre

$ node
>

もともと入れてあった少し古いMSYSだとnode.exeが動かなかったのですが、PATHのどこかにpthreadGC2.dllをコピーすればOKです。

「動いただけ」という状況ですが、JavaScript処理系としては問題なく使えます。

Building node.js on mingw

Prerequisites

Build steps

This will assume you're checking out and compiling node in c:\node.

Known issues

These are known issues with the build process. A list of known issues with mingw-built node can be found found in TODO.win32.

Git doesn't work from the mingw bash shell

Some people have reported problems getting this to work. If you are affected by this, use git from the windows command prompt.

Build fails with msvc / Microsoft Visual Studio installed

Having Microsoft Visual Studio installed confuses the V8 build system. This is a know issue, there is currently no good solution for it.

Then you need to fix toolician for scons: 'tools/scons/scons-local-1.2.0/SCons/Tool/init.py'.


if str(platform) == 'win32':
"prefer Microsoft tools on Windows"
linkers = ['mslink', 'gnulink', 'ilink', 'linkloc', 'ilink32' ]
c_compilers = ['msvc', 'mingw', 'gcc', 'intelc', 'icl', 'icc', 'cc', 'bcc32' ]

Fix above to following(changing order of c_compilers).


if str(platform) == 'win32':
"prefer Microsoft tools on Windows"
linkers = ['mslink', 'gnulink', 'ilink', 'linkloc', 'ilink32' ]
c_compilers = ['mingw', 'msvc', 'gcc', 'intelc', 'icl', 'icc', 'cc', 'bcc32' ]

./configure can't find openssl

Linking with openssl is currently unsupported. Use ./configure --without-ssl.

There is few openssl packages working on win32. But it is difference about linker options. Below is a workaround that I tried.

  1. Open editor to modify 'wscript'.
  2. Comment out 262-264. and add following.
    conf.env["USE_OPENSSL"] = Options.options.use_openssl = True
    conf.env.append_value("CPPFLAGS", "-DHAVE_OPENSSL=1")
  3. configure & mingw32-make. You'll get fail to link.
  4. Open editor to modify 'build/c4che/default.cache.py'. And Change LIB and LIB_OPENSSL.

I succeeded to compile nodejs-SSL on mingw32 with following.


LIB = ['ws2_32', 'winmm', 'pthread.dll', 'ssl32']
LIBPATH_ST = '-L%s'
LIB_CEIL = ['m']
LIB_DL = ['dl']
LIB_OPENSSL = ['crypto', 'gdi32']

Last edited by mattn, Sun Feb 06 22:28:13 -0800 2011