LoginSignup
7
3

More than 5 years have passed since last update.

JupyterhubにPython3xとPython2xを同居させる

Last updated at Posted at 2017-12-02

JupyterhubにPython3xとPython2xを同居させる

12月3日のJupyter Advent Calendarです。
タイトルのとおり、JupyterhubにPython3.xとPython2.xを同居させます。
Jupyterhub自体の構築は以下で紹介しているのですが、その発展版です。
https://qiita.com/cvusk/items/afa46c35d8d5f0d930ed

環境

AWS EC2のCentOS7.xインスタンスを使います。

事前準備

まずはyumで必要なpkgをインストールします。

sudo su -
yum check-update
yum -y update
yum -y install wget bzip2 gcc gtk2 libgomp
yum -y clean all

Jupyterhubに外部からアクセスできるよう、selinuxとfirewalldをオフにします。
このあたりのセキュリティ設定は自己責任でお願いします。

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
getenforce
cat /etc/selinux/config
systemctl disable iptables-services firewalld
systemctl stop iptables-services firewalld

AWS EC2インスタンスのホスト名とIPアドレスをOS上で固定に設定します。

ipad=$(ip addr show eth0 | sed -nEe 's/^[ \t]*inet[ \t]*([0-9.]+)\/.*$/\1/p')
hostnamectl set-hostname $(cat /etc/hostname)
echo "HOSTNAME=$(cat /etc/hostname)" >> /etc/sysconfig/network
echo "${ipad}  $(hostname)"  >> /etc/hosts
echo "preserve_hostname: true" >> /etc/cloud/cloud.cfg
hostname && hostname -i && hostname -f

ここまでで事前準備は終了です。

Anacondaの導入

Pythonの各種ツールはAnacondaでインストールします。
Anacondaの最新版および過去リリース版はこちらから入手してください。
今回はAnaconda3-5.0.1を使います。
OS上のPythonバージョンはPython3.xがベースになります。

wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
bash Anaconda3-5.0.1-Linux-x86_64.sh -b -p /opt/anaconda3
echo 'export PATH="/opt/anaconda3/bin:$PATH"' >> /etc/profile
source /etc/profile

これでPython3.xとAnacondaをインストールできました。

Python3.xとPython2.xの同居

つづいて、condaコマンドでPython2.xを追加します。

conda install -y -c conda-forge jupyterhub
conda install -y notebook ipykernel
ipython kernel install

conda create -y -n py27 python=2.7 ipykernel
conda install -y -c http://conda.binstar.org/menpo opencv
conda info -e
python --version
source activate py27
python --version
conda install -y notebook ipykernel
ipython kernel install
conda install -y -c http://conda.binstar.org/menpo opencv
pip install matplotlib
source deactivate

Python2.xをインストールしましたが、Python2.x用のライブラリは未インストールです。
Python3.xでインストール済みのライブラリをもとに、Python2.xでも同様にインストールします。
なお、Python3.x側はcondaをインストールしていますが、Python2.xにもcondaをインストールするとバグるので、sedで省いています。

pip freeze > requirements.txt
sed -i 's/==.*//g' ./requirements.txt
sed -i 's/conda.*//g' ./requirements.txt

source activate py27
cat requirements.txt | xargs -n 1 pip install
source deactivate

Jupyterhubの設定

ここまででJupyterhub、Python3.x、Python2.xおよび各種ライブラリをインストールしました。

まずはJupyterhub用にjupyというユーザを作っておきます。

groupadd -g 2000 jupy
useradd -g 2000 -u 2000 jupy
echo p@ssw0rd | passwd --stdin jupy
su - jupy
mkdir ~/note
exit

JupyterhubのConfigファイルを作り、設定値を記入します。

cd /opt/
jupyterhub --generate-config

cat << EOF >> jupyterhub_config.py
c.Spawner.notebook_dir = '~/note/'
c.Authenticator.admin_users = {"jupy"}
c.Authenticator.whitelist = {"jupy"}
EOF

最後に、Jupyterhubをsystemdに登録します。

cat << EOF > /etc/systemd/system/jupyterhub.service
[Unit]
Description=Jupyterhub

[Service]
User=root
Environment="PATH=/opt/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
ExecStart=/opt/anaconda3/bin/jupyterhub --no-ssl -f /opt/jupyterhub_config.py
WorkingDirectory=/opt/

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable jupyterhub
systemctl start jupyterhub
systemctl status jupyterhub

これでJupyterhubのインストールが完了し、Python3.xとPython2.x両方をJupyterhub上で使えるようになります。

7
3
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
7
3