FreeBSD と Python3 で Jupyter notebook を動かした話
By takagiwa on Thursday, February 2 2017, 22:04 - Python - Permalink
pkg や ports で Python3 をインストールしようとしても、周りのライブラリなどが複数のバージョンの Python2 をインストールしようとするので、観念して pkg や ports を使わずにセットアップした話。
ターゲットはもちろん、「ゼロから作るDeep Learning」
参考にしたサイトはこちら
- Running My Own Jupyter Notebook Server in a FreeBSD Jail
- Jupyter notebookをApache2によるプロキシ経由のリモートサーバで実行する
方針は、
- Jail 環境にインストール
- Python はソースから
- Python のライブラリは pip で。
- Python の外のライブラリなどは pkg で。
- pkg install -n で、あらかじめ Python2 を使っていないかチェック
- Apache 2.4 でプロキシ
あらかじめホームディレクトリに install ディレクトリを作り、そこに Python のソースコードのアーカイブをコピー済み。
>pkg install curl sqlite3 blas atlas lapack png freetype2 pkgconf fontconfig >cd install >tar zvxf Python-3.5.3.tgz >cd Python-3.5.3 >./configure >make >make install >rehash >python3 --version Python 3.5.3 >make clean >cd ~ >pip3 install pyzmq >rehash >pip3 install jupyter >rehash
実際は後からやったけど、あらかじめパスワードを生成しておく。
>python3 >>> from notebook.auth import passwd; passwd() Enter password: Verify password: 'sha1:.........................' >>> exit(); >mkdir notebook >jupyter notebook --generate-config
これで jupyter のコンフィグレーションファイルが /root/.jupyter/jupyter_notebook_config.py に出力される。
これを編集。
c.NotebookApp.ip ='*' c.NotebookApp.open_browser = False c.NotebookApp.notebook_dir = '/notebook' #default start up directory c.NotebookApp.port = 8888
起動してみる。
>jupyter notebook
他の PC のブラウザから、この jail の IP アドレスとポート番号 8888 にアクセス。
さっき指定したパスワードでログイン&ログアウト。
jail で Ctrl+C で終了。
>pip3 install numpy
pkg でインストールしたパッケージの依存関係で gcc がインストールされていて、numpy を呼び出すプログラムがみんなライブラリを呼ばないといけないらしい。
>setenv LD_LIBRARY_PATH /usr/local/lib/gcc49
を実行しておく。
>pip3 install scipy >rehash >pip3 install pandas >rehash
matplotlib は git でとってきたいけれど、pkg では Python2 がインストールされてしまう。
jail の親 OS に git をインストールして、そこから jail の中のディレクトリに入って、そこでとる。
/usr/jail/***/root/install で git を実行。
>cd ~/install/matplotlib >python3 setup.py install >rehash >jupyter notebook
これで jupyter で、import numpy as np など実行して問題なければOK。
jupyter は Ctrl+C で終了させる。
次は Apache 。
>pkg install -n apache24
Python Image Library (PIL) が欲しくなったので、これもインストールしてみる。
>pkg install jpeg tiff lcms webp openjpeg pngquant >pip3 install pillow
ただ、PIL で画像を表示してくれなかったのはまだ分からない。
以下は設定ファイルの変更箇所。
>diff jupyter_notebook_config.py jupyter_notebook_config.py.org 68d67 < c.NotebookApp.base_url = '/notebook' 160d158 < c.NotebookApp.ip = '*' 198d195 < c.NotebookApp.notebook_dir = '/root/notebook' 205d201 < c.NotebookApp.open_browser = False 215d210 < c.NotebookApp.password = ''
jupyter
- base_url は、サブディレクトリでの運用の実験のために、/notebook に変更
- ip は本当は localhost のままで行きたかったけれど、エラーになったので保留。多分 Jail 関係の気がする。
>diff httpd.conf httpd.conf.sample 126c126 < LoadModule proxy_module libexec/apache24/mod_proxy.so --- > #LoadModule proxy_module libexec/apache24/mod_proxy.so 129c129 < LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so --- > #LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so 133c133 < LoadModule proxy_wstunnel_module libexec/apache24/mod_proxy_wstunnel.so --- > #LoadModule proxy_wstunnel_module libexec/apache24/mod_proxy_wstunnel.so 505c505 < Include etc/apache24/extra/httpd-vhosts.conf --- > #Include etc/apache24/extra/httpd-vhosts.conf
httpd.conf
- proxy 系を有効にする。
- vhosts 運用を見越して、あとの設定は extra/httpd-vhosts.conf に。
>cat extra/httpd-vhosts.conf
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerName jupyter.example.jp
ProxyRequests Off
ProxyPreserveHost On
DocumentRoot /notebook/
<Location /notebook/>
RequestHeader unset Accept-Encoding
ProxyPass http://localhost:8888/notebook/
ProxyPassReverse http://localhost:8888/notebook/
ProxyPreserveHost on
Require all granted
</Location>
<Location /notebook/api/kernels/>
ProxyPass ws://localhost:8888/notebook/api/kernels/
ProxyPassReverse ws://localhost:8888/notebook/api/kernels/
</Location>
ErrorLog /var/log/httpd/jupyternb.example.com-error_log
CustomLog /var/log/httpd/jupyternb.example.com-access_log common
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
</VirtualHost>
httpd-vhosts.conf
- あらかじめログ用ディレクトリ /var/log/httpd/ を作っておくか、ログファイルのパスを変える。
- DocumentRoot 、Location 、ProxyPass 、ProxyPassReverse は、jupyter_notebook_config.py の base_url にあわせる。
- /api/kernels/ はよく分かってないのでそのまま。
自動起動がしたくてファイルを作ってみたけど、自動では動いてくれなかった。
/usr/local/etc/rc.d >cat jupyter #!/bin/sh LD_LIBRARY_PATH=/usr/local/lib/gcc49 nohup /usr/local/bin/jupyter notebook --config=/root/.jupyter/jupyter_notebook_config.py > /dev/null 2>&1 &
これでは異常もログにのこらないから酷いものだけれど。
Numpy のインストールは悩まされた。
building 'matplotlib.ft2font' extension
Traceback (most recent call last):
File "setup.py", line 305, in <module>
**extra_args
File "/usr/local/lib/python3.5/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/local/lib/python3.5/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.5/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/usr/local/lib/python3.5/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "setup.py", line 147, in run
return BuildExtCommand.run(self)
File "/usr/local/lib/python3.5/site-packages/setuptools/command/build_ext.py", line 75, in run
_build_ext.run(self)
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 339, in run
self.build_extensions()
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 448, in build_extensions
self._build_extensions_serial()
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 473, in _build_extensions_serial
self.build_extension(ext)
File "/usr/local/lib/python3.5/site-packages/setuptools/command/build_ext.py", line 196, in build_extension
_build_ext.build_extension(self, ext)
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 530, in build_extension
include_dirs=ext.include_dirs,
File "/root/install/matplotlib/setupext.py", line 924, in __get__
result = obj._hooks[self._name]() + result
File "/root/install/matplotlib/setupext.py", line 944, in include_dirs_hook
import numpy
File "/usr/local/lib/python3.5/site-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/usr/local/lib/python3.5/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python3.5/site-packages/numpy/lib/__init__.py", line 18, in <module>
from .polynomial import *
File "/usr/local/lib/python3.5/site-packages/numpy/lib/polynomial.py", line 13, in <module>
import numpy.core.numeric as NX
AttributeError: module 'numpy' has no attribute 'core'
これは
>setenv LD_LIBRARY_PATH /usr/local/lib/gcc49
を実行していないだけだった。