LoginSignup
3
0

More than 3 years have passed since last update.

Amazon Linux2にJupyter Notebookをインストール

Last updated at Posted at 2019-10-12

JupyterNotebookとは

ごく簡単に言うと、コーディング & 試行 & 実行結果の記録がローカル環境で簡単に繰り返し行えるアプリです。

言語はPythonで使われることが多いようですが、perlとかのスクリプト言語、それからOSコマンドやSQLなんかもちゃんと使えます。

インフラ系の構築作業やリリース時の手順書としてJupyterNotebookが使えないか検討してみようと思ったので、まずはEC2へのインストール手順をメモ。

インストール手順

まずはpython3をサーバにインストールして、パッケージを最新化します。

$ sudo amazon-linux-extras install python3
$ sudo yum update -y

そしたら、jupyterをインストール

$ sudo pip3 install jupyter

次に、jupyter上でpostgreSQLからデータが取れるようにpsycopg2と、ipython-sqlをインストール

$ sudo pip3 install ipython-sql
$ sudo pip3 install psycopg2

すんなりいくかと思ったら、psycopg2のインストールでエラー…

ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-8ef5s1hl/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-8ef5s1hl/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-info
         cwd: /tmp/pip-install-8ef5s1hl/psycopg2/
    Complete output (25 lines):
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    /usr/lib64/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'project_urls'
      warnings.warn(msg)

    Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.

    For further information please check the 'doc/src/install.rst' file (also at
    <http://initd.org/psycopg/docs/install.html>).

    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

pg_configを先に入れてやる必要があるとのことで、postgresql-develにそれが含まれているらしいのでこちらを先にyumる

sudo yum install postgresql-devel

で、今度こそいけるかと思ったらまたエラー…?

ERROR: Command errored out with exit status 1:
   command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-79589jn5/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-79589jn5/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-e0x6fj5k --python-tag cp36
       cwd: /tmp/pip-install-79589jn5/psycopg2/
  Complete output (38 lines):
  /usr/lib64/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'project_urls'
    warnings.warn(msg)
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.6
  creating build/lib.linux-x86_64-3.6/psycopg2
  copying lib/pool.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/errorcodes.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/sql.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/_lru_cache.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/extras.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/_range.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/__init__.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/extensions.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/tz.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/_json.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/errors.py -> build/lib.linux-x86_64-3.6/psycopg2
  copying lib/compat.py -> build/lib.linux-x86_64-3.6/psycopg2
  running build_ext
  building 'psycopg2._psycopg' extension
  creating build/temp.linux-x86_64-3.6
  creating build/temp.linux-x86_64-3.6/psycopg
  gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSYCOPG_VERSION=2.8.3 (dt dec pq3 ext) -DPG_VERSION_NUM=90224 -I/usr/include/python3.6m -I. -I/usr/include -I/usr/include/pgsql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.6/psycopg/psycopgmodule.o -Wdeclaration-after-statement
  unable to execute 'gcc': No such file or directory

  It appears you are missing some prerequisite to build the package from source.

  You may install a binary package by installing 'psycopg2-binary' from PyPI.
  If you want to install psycopg2 from source, please install the packages
  required for the build and try again.

  For further information please check the 'doc/src/install.rst' file (also at
  <http://initd.org/psycopg/docs/install.html>).

  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for psycopg2

gccがないよと言われているので、じゃあgcc入れりゃええんか?とまたyumる

sudo yum install gcc

…で、再度psycopg2をyumろうとするもまだ解決せず…

./psycopg/psycopg.h:34:10: fatal error: Python.h: No such file or directory
     #include <Python.h>

で、ネット上をぐるぐる巡った結果、下記のteratailの質問に行き当たり、
https://teratail.com/questions/209721
どうやらpython3-devをインストールすれば治ったとのことなので、一回アンインストールしてからダメ元でyumる

sudo yum remove python-devel
sudo yum install python3-devel  # 必要なのはpython3用っぽいのでpython3-develを入れる

これでようやくpycopg2が入った!
(前にやった時はもうちょい素直にインストールできた気がするんだけども…)

Collecting psycopg2
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/5c/1c/6997288da181277a0c29bc39a5f9143ff20b8c99f2a7d059cfb55163e165/psycopg2-2.8.3.tar.gz (377kB)
    100% |????????????????????????????????| 378kB 3.0MB/s
Building wheels for collected packages: psycopg2
  Running setup.py bdist_wheel for psycopg2 ... done
  Stored in directory: /root/.cache/pip/wheels/48/06/67/475967017d99b988421b87bf7ee5fad0dad789dc349561786b
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.8.3

で、python-develを一度アンインストールしたことにより必要なものがなくなってしまった気配なので、再度jupyter系をpip。

sudo pip3 install jupyter
sudo pip3 install ipython-sql

これで、下記のコマンドを実行してやれば、とりあえず接続できるようになりました。
※クライアントPCから接続できるようにEC2のセキュリティグループにも穴開けてます

jupyter notebook --ip=0.0.0.0

コマンド実行後に表示されるトークン付きのURLのIP部分だけEC2のグローバルIPに変更し、クライアントPCのブラウザに打ち込んでjupyternotebookにアクセス。

http://127.0.0.1:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

あらかじめ作っておいたRDSのテーブルへselectを打ってみると、
image.png
無事、取得することができました!

ローカルのwindows環境にjupyternotebook入れるだけなら5分ぐらいあればできるのに、えらく時間がかかってしまった…。

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