LoginSignup
6
4

More than 3 years have passed since last update.

【第二回】CentOSにPythonの機械学習開発環境を構築する(Jupyter Notebook編)

Last updated at Posted at 2019-10-19

前書き

LinuxディストリビューションのCenOSJupyter Notebookを使用して、Python、Pythonのライブラリであるscikit-learnTensorflowkeras等を使って機械学習のプログラムを作成できる機械学習の開発環境を作成する手順を紹介したいと思います。

「【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)」ではAnacondaをインストールし、機械学習用環境(ml_env)を作成するところまで紹介しました。
今回はJupyter Notebookを起動し、他のPC等からブラウザでJupyter Notebookを使用できるよう設定します。

環境

  • OS:CentOS Linux release 7.7.1908

前提条件

  • CentOSAnaconda3Anaconda3-2019.07-Linux-x86_64.sh)がインストールされていること
  • 機械学習用の環境(ml_env)が作成されていること

「【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)」で紹介)

手順

1. anacondaユーザーでログイン

CentOSにanacondaユーザーでログインまたはスイッチします。
su - anaconda

コマンド実行結果
[root@CENTOS7 ~]# su - anaconda
最終ログイン: 2019/10/19 (土) 22:05:17 JST日時 pts/0
[anaconda@CENTOS7 ~]$

2. 環境変数の読み込み

「【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)」で作成した環境変数.anaconda.envを読み込みます。

source ./.anaconda.env

コマンド実行結果
[anaconda@CENTOS7 ~]$ source ./.anaconda.env
[anaconda@CENTOS7 ~]$

3. 機械学習用環境(ml_env)をアクティブにする

「【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)」で作成した機械学習用環境(ml_env)をアクティブにします。

conda activate ml_env

コマンド実行結果
[anaconda@CENTOS7 ~]$ conda activate ml_env
(ml_env) [anaconda@CENTOS7 ~]$

4. jupyterの確認

jupyterがインストールされていることを確認します。
conda list | grep jupyter

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ conda list | grep jupyter
jupyter                   1.0.0                    py37_7
jupyter_client            5.3.3                    py37_1
jupyter_console           6.0.0                    py37_0
jupyter_core              4.5.0                      py_0
jupyterlab                1.1.4              pyhf63ae98_0
jupyterlab_server         1.0.6                      py_0
(ml_env) [anaconda@CENTOS7 ~]$

5. jupyter notebookの起動確認

jupyter notebookが起動できることを確認します。
jupyter notebook

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ jupyter notebook
[W 02:32:49.684 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 02:32:49.710 NotebookApp] JupyterLab extension loaded from /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/jupyterlab
[I 02:32:49.710 NotebookApp] JupyterLab application directory is /home/anaconda/anaconda3/envs/ml_env/share/jupyter/lab
[I 02:32:49.712 NotebookApp] Serving notebooks from local directory: /home/anaconda
[I 02:32:49.712 NotebookApp] The Jupyter Notebook is running at:
[I 02:32:49.712 NotebookApp] http://CENTOS7:8888/?token=524a1b3ec4c1a46637c595b73d7945d5bce5f0ceef917e3a
[I 02:32:49.712 NotebookApp]  or http://127.0.0.1:8888/?token=524a1b3ec4c1a46637c595b73d7945d5bce5f0ceef917e3a
[I 02:32:49.712 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 02:32:49.716 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/anaconda/.local/share/jupyter/runtime/nbserver-13921-open.html
    Or copy and paste one of these URLs:
        http://CENTOS7:8888/?token=524a1b3ec4c1a46637c595b73d7945d5bce5f0ceef917e3a
     or http://127.0.0.1:8888/?token=524a1b3ec4c1a46637c595b73d7945d5bce5f0ceef917e3a

この状態ではまだ他のPCのブラウザから接続することはできません。

6. jupyter notebookの停止

Ctrl + cでjupyter notebookを停止します。

コマンド実行結果
Shutdown this notebook server (y/[n])? y
[C 02:38:39.455 NotebookApp] Shutdown confirmed
[I 02:38:39.457 NotebookApp] Shutting down 0 kernels
(ml_env) [anaconda@CENTOS7 ~]$

7. jupyterのパスを確認

jupyterのパスを確認します。
jupyter --path

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ jupyter --path
config:
    /home/anaconda/.jupyter
    /home/anaconda/anaconda3/envs/ml_env/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /home/anaconda/.local/share/jupyter
    /home/anaconda/anaconda3/envs/ml_env/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /home/anaconda/.local/share/jupyter/runtime
(ml_env) [anaconda@CENTOS7 ~]$

8. Jupyterの設定ファイルの雛形作成

Jupyterの設定ファイルの雛形を作成します。

jupyter notebook --generate-config

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ jupyter notebook --generate-config
Overwrite /home/anaconda/.jupyter/jupyter_notebook_config.py with default config? [y/N]y
Writing default config to: /home/anaconda/.jupyter/jupyter_notebook_config.py
(ml_env) [anaconda@CENTOS7 ~]$

9. パスワードの作成

IPytonを使用してパスワードを作成します。
ipython

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ ipython
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:
  • パスワードのハッシュ値を生成するためにnotebook.authモジュールのpasswd関数を読み込みます。
    from notebook.auth import passwd

  • passwd関数を実行し、パスワードを入力します。
    passwd()

  • パスワードを入力します。

  • 確認用にもう一度パスワードを入力します。

  • パスワードのハッシュ値が出力されます。

  • IPythonを終了します。
    exit

Python実行結果
In [1]: from notebook.auth import passwd

In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:4fb2f50440ea:f52805b46a917a77814c538f8708c54bde4e87ea'

In [3]: exit

パスワードのハッシュ値が出力されます。

10. デフォルトの作業ディレクトリの作成

jupyter notebookのデフォルトの作業ディレクトリを作成する。
mkdir /home/anaconda/work

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ mkdir /home/anaconda/work
(ml_env) [anaconda@CENTOS7 ~]$

11. Jupyterの設定ファイル(jupyter_notebook_config.py)の編集

/home/anaconda/.jupyter/jupyter_notebook_config.pyに以下の5行を追加します。

  • c = get_config()
  • c.NotebookApp.ip = '*' (すべてのIPアドレスから接続を許可)
  • c.NotebookApp.open_browser = False (JupyterNotebookの起動時にブラウザを起動しない)
  • c.NotebookApp.port = 8888 (8888番ポートで起動)
  • c.NotebookApp.password = u'sha1:4fb2f50440ea:f52805b46a917a77814c538f8708c54bde4e87ea' (パスワードのハッシュ値の設定(9で設定したパスワードのハッシュ値))
  • c.NotebookApp.notebook_dir = u'/home/anaconda/work' (デフォルトのディレクトリパス(10で作成したディレクトリのパス))
  • c.IPKernelApp.pylab = 'inline'matplotlibで描画したものがnotebook上で表示できるようにする)matplotlibで描画したものを表示する場合、Notebook上で%matplotlib inlineまたは%matplotlib notebookを記載する)
/home/anaconda/.jupyter/jupyter_notebook_config.py
c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
c.NotebookApp.password = u'sha1:4fb2f50440ea:f52805b46a917a77814c538f8708c54bde4e87ea'
c.NotebookApp.notebook_dir = u'/home/anaconda/work'

12. CentOSのファイアウォールの設定

rootユーザーにスイッチします。
su -

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ su -
パスワード:
最終ログイン: 2019/10/19 (土) 22:05:11 JST 192.168.0.2から開始日時 pts/0
[root@CENTOS7 ~]#

CentOSに外部から8888番ポートへのアクセスを許可するよう設定します。
firewall-cmd --add-port=8888/tcp --zone=public --permanent

コマンド実行結果
[root@CENTOS7 ~]# firewall-cmd --add-port=8888/tcp --zone=public --permanent
success
[root@CENTOS7 ~]#

firewallの設定を反映させます。
firewall-cmd --reload

コマンド実行結果
[root@CENTOS7 ~]# firewall-cmd --reload
success
[root@CENTOS7 ~]#

firewallの設定を確認します。
firewall-cmd --list-all --zone=public

コマンド実行結果
[root@CENTOS7 ~]# firewall-cmd --list-all --zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: dhcpv6-client ssh
  ports: 8888/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

[root@CENTOS7 ~]#

exitでrootユーザーからログアウトする。
exit

コマンド実行結果
[root@CENTOS7 ~]# exit
ログアウト
(ml_env) [anaconda@CENTOS7 ~]$

13. jupyter notebookを起動

jupyter notebookを起動します。

jupyter notebook

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ jupyter notebook
[W 04:18:22.028 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 04:18:22.054 NotebookApp] JupyterLab extension loaded from /home/anaconda/anaconda3/envs/ml_env/lib/python3.7/site-packages/jupyterlab
[I 04:18:22.054 NotebookApp] JupyterLab application directory is /home/anaconda/anaconda3/envs/ml_env/share/jupyter/lab
[I 04:18:22.055 NotebookApp] Serving notebooks from local directory: /home/anaconda
[I 04:18:22.055 NotebookApp] The Jupyter Notebook is running at:
[I 04:18:22.055 NotebookApp] http://CENTOS7:8888/
[I 04:18:22.055 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

14. ブラウザから接続確認

他のPCのブラウザから接続できることを確認します。

http://[サーバのIPアドレス]:8888/
01.png

パスワードを入力し、ログインします。
02.png

jupyter notebookに接続できることが確認できました。

後書き

第二回ではブラウザからJupyter Notebookを使用できるようにするための設定を行いました。
次回は機械学習用環境(ml_env)にscikit-learn、Tensorflowやkeras等の機械学習ライブラリのインストールを行い、Jupyter Notebookでサンプルソースを実行したいと思います。

リンク

【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)
【第三回(最終回)】CentOSにPythonの機械学習開発環境を構築する(Tensorflow + Kerasインストール編)
【番外編】CentOSにPythonの機械学習開発環境を構築する(systemdでのJupyter Notebookの自動起動編)

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4