Jupyter Notebookは、ライブコードと方程式を作成して共有できるオープンソースのWebベースの開発ツールです。柔軟性と拡張性があり、Python、Julia、Haskell、Rubyなどの多くのプログラミング言語をサポートしています。通常、データと機械学習に使用されます。 Pythonでプログラムを作成できるIPythonカーネルが付属しています。
この投稿では、Ubuntu20.04にJupyterNotebookをインストールする方法を紹介します。
- Ubuntu 20.04 VPS(SSD 2 VPSプランを使用します)
- rootユーザーアカウントへのアクセス(またはroot権限を持つ管理者アカウントへのアクセス)
サーバーにログインし、サーバーOSパッケージを更新します
まず、rootユーザーとしてSSH経由でUbuntu20.04サーバーにログインします。
ssh root@IP_Address -p Port_number
ログインしたら、次のコマンドを使用してシステムパッケージを更新します。
apt-get update -y
JupyterNotebookはPythonに基づいています。そのため、Pythonやその他のライブラリをシステムにインストールする必要があります。次のコマンドですべてをインストールできます:
apt-get install python3 python3-pip python3-dev -y
次に、次のコマンドを使用して、インストールされているPythonのバージョンを確認します。
python3 --version
出力:
Python 3.8.5
次に、次のコマンドを使用してPIPを最新バージョンにアップグレードします。
pip3 install --upgrade pip
次に、次のコマンドを使用してPIPのバージョンを確認します。
pip3 --version
出力:
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
次に、次のコマンドを使用して仮想環境パッケージをインストールします。
pip3 install virtualenv
JupyterNotebookをインストールする
まず、JupyterNotebook用に別のユーザーを作成します。次のコマンドで作成できます:
adduser jupyter
次に、次のコマンドを使用してJupyterユーザーでログインします。
su - jupyter
次に、Jupyterディレクトリを作成し、Jupyterノートブックの仮想環境を作成します。
mkdir ~/Jupyter cd ~/Jupyter virtualenv notebook
次に、次のコマンドを使用して仮想環境をアクティブ化します。
source notebook/bin/activate
次に、次のコマンドを使用してJupyterをインストールします。
(notebook) jupyter@jupyter:~/Jupyter$ pip install jupyter
インストールしたら、次のコマンドでJupyterノートブックを実行します。
(notebook) jupyter@jupyter:~/Jupyter$ jupyter notebook
次の出力が得られるはずです:
[I 06:12:57.527 NotebookApp] Writing notebook server cookie secret to /home/jupyter/.local/share/jupyter/runtime/notebook_cookie_secret [I 06:12:57.786 NotebookApp] Serving notebooks from local directory: /home/jupyter/Jupyter [I 06:12:57.786 NotebookApp] Jupyter Notebook 6.3.0 is running at: [I 06:12:57.787 NotebookApp] http://localhost:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed [I 06:12:57.787 NotebookApp] or http://127.0.0.1:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed [I 06:12:57.787 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 06:12:57.791 NotebookApp] No web browser found: could not locate runnable browser. [C 06:12:57.791 NotebookApp] To access the notebook, open this file in a browser: file:///home/jupyter/.local/share/jupyter/runtime/nbserver-11919-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed or http://127.0.0.1:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed
CTRL+Cを押してJupyterNotebookを停止します。
次に、JupyterNotebookの構成ファイルを生成する必要があります。次のコマンドで生成できます:
(notebook) jupyter@jupyter:~/Jupyter$ jupyter notebook --generate-config
次の出力が得られるはずです:
Writing default config to: /home/jupyter/.jupyter/jupyter_notebook_config.pyに書き込みます
次に、次のコマンドを使用してJupyterのパスワードを設定します。
(notebook) jupyter@jupyter:~/Jupyter$ jupyter notebook password
次の出力が表示されます。
Enter password: Verify password: [NotebookPasswordApp] Wrote hashed password to /home/jupyter/.jupyter/jupyter_notebook_config.json
次に、次のコマンドを使用してPython仮想環境から非アクティブ化します。
(notebook) jupyter@jupyter:~/Jupyter$ deactivate
次に、次のコマンドを使用してJupyterユーザーからログアウトします。
exit
JupyterのSystemdサービスファイルを作成する
次に、JupyterNotebookを管理するためのsystemdサービスファイルを作成する必要があります。次のコマンドで作成できます:
nano /etc/systemd/system/jupyter.service
次の行を追加します:
[Unit] Description=Jupyter Notebook [Service] Type=simple PIDFile=/run/jupyter.pid ExecStart=/home/jupyter/Jupyter/notebook/bin/jupyter-notebook --config=/home/jupyter/.jupyter/jupyter_notebook_config.py User=jupyter Group=jupyter WorkingDirectory=/home/jupyter/Jupyter/notebook Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
ファイルを保存して閉じてから、次のコマンドを使用してsystemdデーモンをリロードします。
systemctl daemon-reload
次に、Jupyterサービスを開始し、次のコマンドを使用してシステムの再起動時に開始できるようにします。
systemctl start jupyter systemctl enable jupyter
次のコマンドを使用して、Jupyterのステータスを確認することもできます。
systemctl status jupyter
出力:
● jupyter.service - Jupyter Notebook Loaded: loaded (/etc/systemd/system/jupyter.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2021-04-06 06:32:47 UTC; 2min 33s ago Main PID: 14630 (jupyter-noteboo) Tasks: 1 (limit: 2353) Memory: 40.6M CGroup: /system.slice/jupyter.service └─14630 /home/jupyter/Jupyter/notebook/bin/python /home/jupyter/Jupyter/notebook/bin/jupyter-notebook --config=/home/jupyter/.jup> Apr 06 06:32:47 jupyter systemd[1]: Started Jupyter Notebook. Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.023 NotebookApp] Serving notebooks from local directory: /home/jupyter/Jupyter/no> Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.024 NotebookApp] Jupyter Notebook 6.3.0 is running at: Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.024 NotebookApp] http://localhost:8888/ Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.024 NotebookApp] Use Control-C to stop this server and shut down all kernels (twi> Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [W 06:32:48.030 NotebookApp] No web browser found: could not locate runnable browser.
NginxをJupyterNotebookのリバースプロキシとして構成する
次に、NginxをJupyterNotebookのリバースプロキシとして構成する必要があります。
まず、次のコマンドを使用してNginxをインストールします。
apt-get install nginx -y
インストールしたら、次のコマンドを使用してNginx仮想ホスト構成ファイルを作成します。
nano /etc/nginx/conf.d/jupyter.conf
次の行を追加します:
upstream jupyter { server 127.0.0.1:8888; } server { listen 80; server_name jupyter.example.com; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; location / { proxy_pass http://localhost:8888; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_http_version 1.1; proxy_redirect off; proxy_buffering off; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; } }
ファイルを保存して閉じてから、次のコマンドでNginxを確認します。
nginx -t
出力:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
次に、Nginxを再起動して変更を適用します:
systemctl restart nginx
次に、次のコマンドを使用して、Jupyterユーザーをwww-dataグループに追加します。
usermode -g www-data jupyter
次に、Jupyter Notebook構成ファイルを編集して、リモートアクセスを許可します。
nano /home/jupyter/Jupyter/.jupyter/jupyter_notebook_config.py
次の行を変更します:
c.NotebookApp.allow_remote_access = True
ファイルを保存して閉じてから、Jupyterサービスを再起動して変更を適用します。
systemctl restart jupyter
次に、Webブラウザーを開き、URL http://jupyter.example.comを使用してJupyterNotebookにアクセスします。 。以下に示すように、パスワードの入力を求められます。
パスワードを入力して、ログインをクリックします ボタン。次の画面にJupyterNotebookダッシュボードが表示されます。
おめでとう! Ubuntu20.04VPSにJupyterNotebookが正常にインストールされました。
Ubuntu20.04にJupyterNotebookをインストールする方法がわかったので、マネージドLinuxVPSホスティングがいかに簡単で手頃な価格であるかを確認できます。自分でUbuntuにJupyterNotebookをインストールする必要はありません。システム管理者に、完全に無料でインストールするよう依頼してください。これらは24時間年中無休で利用可能であり、サーバー管理のあらゆる側面を支援する準備ができています。