いつもはpythonで使っているjupyterだけど、他の言語もサポートしているというので入れてみました。
- A) ruby
- B) R
- C) nodes
- D) go
結果、・・・、同じディレクトリにpython, ruby, R, go, nodejsの.ipynbファイルがあると混乱しますね(笑)。
jupyterインストール
まずはjupyter本体(&python3)インストールから(インストール済みの人は飛ばしてください)
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install python3
pip3 install jupyter
インストール後、
jupyter notebook
でサーバが動作、少し待つとブラウザで
http://localhost:8888
が開く。なお、&をつけてバックグラウンドで立ち上げると後でkillすることになり面倒(kill `ps -ef| grep jupyter | awk {'print $2'}`
で2列目を取って消す。いい方法はないか)。
(ひとこと)Macにpythonというと、pyenvからpython・・、とか、anacondaは色々入ってて・・とかの記事が多い。が、普通に使うだけならbrewでpython3が楽だと思う。python2ならpython/pip、python3ならpython3/pip3と切り替えなしに併用できる。anacondaのバージョンが上がっても置いてきぼり、もなし。
A) jupyterでruby (iruby)
brew install zeromq czmq
gem install rbczmq -- --with-system-libs
gem install iruby
gem install nyaplot # これはjupyterとか関係ないが欲しくて入れた。
これで話題の?nyaplotも動いて満足(が、期待の3d plotは、
Javascript error adding output!
TypeError: undefined is not an object (evaluating 'ext.pane')
See your browser Javascript console for more details.
とエラー、なぜ・・・)
rbczmqの入れ方は下記記事を参考にさせていただきました。
(備考)なお、Macならrbczmqの代わりにffi-rzmq、とも言われているが、例)
# 自分の環境ではffi-rzmqでは動作せず、、
gem install ffi-rzmq
gem install iruby
、立ち上がりはするのですが、すぐにDead kernelとなって正常に動作しませんでした。結局、gem uninstall ffi-rzmqして上のやり方でrbczmqを入れました。
以下のどれを打っても同じものが立ち上がります。
- iruby notebook
- python notebook
- jupyter notebook
なお、rubyはrbenvから入れたと思う(詳細は忘れた)。
brew install rbenv
...
B) jupyterで R
上記公式の通り、Rを起動、
install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools'))
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec() # to register the kernel in the current R installation
でインストール完。jupyter notebookで立ち上げNewでRを選んだら、plot(iris)してみよう。
別サイトでは他のやり方が紹介されていたが動かず。公式を見てやった。なお、Rはパッケージをダウンロードして入れたと思う。
brewからRを入れる場合、tap homebrew/scienceとしないと、ない、と言われる。
brew tap homebrew/science
brew install R
なお、Rの環境としてexploratoryが出てきた。注目。
https://exploratory.io
http://qiita.com/21-Hidetaka-Ko/items/0c746d84d568113536bf
C) jupyterでnodejs
git clone git@github.com:notablemind/jupyter-nodejs.git
では認証で怒られた。ので、
git clone https://github.com/notablemind/jupyter-nodejs.git
あとはそのまま
git clone https://github.com/notablemind/jupyter-nodejs.git
cd jupyter-nodejs
mkdir -p ~/.ipython/kernels/nodejs/
npm install && node install.js
make
でインストール完。途中、なんか言われたような気がしたが、普通に動いている。console.log('hello')してみよう。
なお、nodejsインストールは、
brew install node
D-1) jupyterでgo (docker編)
を見るとローカルに入れる手順が煩雑(実はそうでもない)と思い、まずはdockerでやる。Macでは以下、
docker pull dwhitena/gophernotes:latest
docker run --net host -d dwhitena/gophernotes jupyter notebook --no-browser --ip=0.0.0.0
とあるが動かず(http://localhost:8888を見てもhttp://0.0.0.0:8888を見てもない)。探すと、
に回答が。ポートマッピングが必要らしい(当たり前か・・)。8888はローカルのjupyterが使っているので、ここでは9000番を使う。
docker run -p 9000:8888 -d dwhitena/gophernotes jupyter notebook --no-browser --ip=0.0.0.0
なお、issues/43では、--rmをつけている。--rmをつけるとCTRL-Cでコンテナ停止、削除できて便利。が、もう一度立ち上げると前のファイル(Untitled.ipynb)は消えているのはマズイ。ので、上記が良いと思う。
動いたら9000番で接続。
http://localhost:9000
NewでGoを選んだら、fmt.Println("Hello, 世界")
としてみる
goのインストールは、以下、
brew install golang
dockerは公式ページからダウンロード(Docker.dmg)してインストール。
D-2) jupyterでgo (ローカル編)
dockerは楽で起動も一瞬。だが、巨大なイメージ、コンテナを置いておくのは面白くない。ので、やはりローカルに入れる。再度、公式、
を見る。面倒に見えるが実は、設定の置き方が人により違うというだけ。まず、jupyter --data-dirとして、どこに自分のjupyterカーネル設定があるのかを確認する。自分は、~Library/Jupyterらしい。
jupyter --data-dir
/Users/xxx/Library/Jupyter
あとは、下記ですんなり入る。
go get golang.org/x/tools/cmd/goimports
go get -tags zmq_4_x github.com/gopherds/gophernotes
mkdir -p ~/Library/Jupyter/kernels/gophernotes
cp -r $GOPATH/src/github.com/gopherds/gophernotes/kernel/* ~/Library/Jupyter/kernels/gophernotes
なお、設定(jupyter --data-dirの出力)が~Library/Jupyterではなく、~/.ipythonの人は、以下になる(go getの所は同じ)。
go get golang.org/x/tools/cmd/goimports
go get -tags zmq_4_x github.com/gopherds/gophernotes
mkdir ~/.ipython/kernels/gophernotes
cp -r $GOPATH/src/github.com/gopherds/gophernotes/kernel/* ~/.ipython/kernels/gophernotes/
(ZeroMQ 2.2.xとZeroMQ 4.xがあるようだが、ZeroMQ 2.2.xは入らなかったのでZeroMQ 4.xとした。)
D-3) goの実行
試しに、公式https://golang.org
にあるhello worldを試してみるとDead kernel!!!
// mainを使おうとするとjupyterではDead kernelになって止まる!!
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
どうもpackage mainとか、するのはダメらしい。以下のように、mainでなくtestならOK。
import "fmt"
func test() {
fmt.Println("Hello, 世界")
}
test()
なお、fmtは最初からimportされているらしくimport "fmt"なしで、fmt.Printlnできる。
(ご参考)Macでのjupyterカーネル設定の場所
私の場合、
- ruby、nodesは、~/.ipython/kernels/
- ir(=R)、gophernotes(=go)は、~Library/Jupyter/kernels/
にインストールされた。現状、~Library/Jupyterでも、~/.ipython/kernels/でも、どちらでも動くらしい。実際、goも最初は~/.ipython/kernels/に入れたが普通に動いた。後から、置き場所を間違えた!とmvで~Library/Jupyter/kernels/に移した。どちらにおいても、一方から他方に場所を移動しても動くような。