LoginSignup
1
4

More than 5 years have passed since last update.

(macOS) Vagrant + CentOS + pyenv + anaconda でホストOSから jupyter notebookを使う

Last updated at Posted at 2019-01-26

macOSをクリーンインストールする度に再設定するのでメモ

環境

以下の環境で動作確認済み

  • ホスト

    • macOS 10.14.3
    • VirtualBox 6.0.2
    • Vagrant 2.2.3
  • ゲスト

    • CentOS 7.6 (1810)
    • pyenv 1.2.9
    • anaconda 3-5.3.1
    • Python 3.7.2

VirtualBoxのインストール

$ VBoxManage -v
6.0.2r128162

Vagrantのインストール

$ vagrant --version
Vagrant 2.2.3

Vagrantの設定

Vagrant用ディレクトリを作成し、ボックスの作成・初期化 (box addは不要)

 # ホームディレクトリから開始
$ cd ~
 # Vagrantディレクトリを作成
$ mkdir Vagrant
 # Vagrantに移動
$ cd Vagrant
 # Vagrantの下にcentOSディレクトリを作成
$ mkdir centOS
 # centOSに移動
$ cd centOS
 # vagrant init 実行
$ vagrant init centos/7

Vagrantfileの編集

vagrant initを実行すると、カレントディレクトリ(ここではcentOSディレクトリ)にVagrantfileができているのでテキストエディタで開き、以下の2行をアンコメント('#'を削除してコメントアウトを取り消し)

# config.vm.network "forwarded_port", guest: 80, host: 8080
 <中略>
# config.vm.network "private_network", ip: "192.168.33.10"

 ↓

config.vm.network "forwarded_port", guest: 80, host: 8080
 <中略>
config.vm.network "private_network", ip: "192.168.33.10"

ゲストOSの立ち上げ

以下のコマンドでCentOS7を起動

$ vagrant up

CentOSにssh接続

起動に成功したらsshでログイン

$ vagrant ssh

pyenvとAnacondaのインストール

ログインしたら最初にyumの更新とgitインストール

$ sudo yum -y update
$ sudo yum -y install git

pyenvをgitから取得

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv

pyenvの環境変数を設定

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "(pyenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

memo
環境変数を.bashrcに書き込むと、bashが正常に動かなく(コマンドが全てcommand not found に)なったので
.bash_profileに書き込んだところ正常に動作した

anacondaのインストール

 # anacondaのバージョン一覧確認
$ pyenv install -l | grep ana
 # 最新版インストール(少し時間がかかる)
$ pyenv install anaconda3-5.3.1
$ pyenv rehash
 # anacondaのPythonをデフォルトに設定
$ pyenv global anaconda3-5.3.1
 # anacondaの環境変数を設定
$ echo 'export PATH="$PYENV_ROOT/versions/anaconda3-5.3.1/bin/:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
 # Pythonのバージョン確認
$ pyenv versions
  system
* anaconda3-5.3.1 (set by /home/vagrant/.pyenv/version)
 # condaのアップデート
$ conda update conda
# 下記が表示されたら'y'を入力してEnter
Proceed ([y]/n)?

jupyter notebookの起動

jupyter notebookはanacondaに同梱されているので、ここではそのまま使う
(anacondaで仮想環境を作る場合は別途設定)

 # jupyter notebookを起動
$ jupyter notebook --no-browser --ip=0.0.0.0
 # 下記が表示されたらtoken(xxxxxxxxxxxxxxxxxxxxxxxxxの部分)をコピーしておく
    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://(localhost.localdomain or 127.0.0.1):8080/?token=xxxxxxxxxxxxxxxxxxxxxxxxx

jupyter notebookへアクセス

ホストOSに戻り、ブラウザで以下のURLを開く

初回アクセス時にtokenの入力とパスワード設定を聞かれるので、コピーしておいたtokenを入力し、
パスワードを設定する

500 : Internal Server Errorが表示された場合は、ゲストOSのterminalに戻り
[⌃C]でjupyter notebookを一旦終了し、再度起動する


以下の記事、サイトを参考にさせて頂きました
https://qiita.com/sk427/items/9f215931c8249ada75cd
https://qiita.com/1000ch/items/93841f76ea52551b6a97
https://qiita.com/N-K-Kota/items/a36902d43c48d054bb6f
https://qiita.com/supersaiakujin/items/50def6f33b79f9a61b18
https://mashi-prog.hatenablog.com/entry/2018/06/09/105911

1
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
1
4