LoginSignup
18
14

More than 5 years have passed since last update.

macOS Mojaveで pyenvを使ってpython3系を使えるようにする

Posted at

はじめに

非エンジニアがTensorFlowを使うための技術を学んでいるところで、そもそもPythonのインストールでハマったので忘備録のための記事。

書いた人

Macで最低限のコマンドを知っている (cd cat lsなど)
Linuxの最低限のコマンドを知っている(cd cat lsなど)
プログラミングはした事がない

環境

  • macOS 10.14.1 Mojave
  • Xcode 10.1

参考にした記事

やること

  • Xcodeのインストール
  • Xcode Command Line Tools のインストール
  • Homebrewのインストール
  • pyenvのインストール
  • pyenv-virtualenvのインストール
  • 環境変数の設定
  • pyenvでpython3のインストール
  • Mojave用のmacOS SDK headerのインストール

Xcodeのインストール

Appストアから最新版を入手してインストールします。

Xcode Command Line Toolsのインストール

Homebrewをインストールするために必要なものらしいです。そもそもHomebrewが何か知りたい人はこちらへ。

ターミナルで下記のコマンドを打ち込みます。コピペでOK。

terminal
$ xcode-select --install

下記が出力されたらOK

terminal
xcode-select: note: install requested for command line developer tools

Homebrewをインストール

公式サイトにあるスクリプトを貼り付けるだけ。

terminal
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

なんやか色々表示されますが、すぐに続行を促されますので、returnキーを押しましょう。

terminal
Press RETURN to continue or any other key to abort

さらになんやら表示されて、最後がこんな表示で終わるはず。

terminal
==> Next steps:
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh

一応、確認をします。

terminal
$ brew doctor

下記の表示が出ればOKぽい。

terminal
Your system is ready to brew.

pyenvのインストール

とりあえずコマンドを入力していくだけ。

terminal
$ brew install pyenv

なんやか色々出て進んでいきます。とりあえず終わると下記の表示が出るはず。理解したい人は途中もちゃんと読むといいと思います。

terminal
==> pyenv
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh functions have been installed to:
  /usr/local/share/zsh/site-functions

pyenv-virtualenvをインストール

これも下記を入力

terminal
$ brew install pyenv-virtualenv

下記が出たら完了

terminal
🍺  /usr/local/Cellar/pyenv-virtualenv/1.1.3: 20 files, 62.2KB, built in 6 seconds

ようやくPythonインストール

まずはpyenvでインストールできるpythonのバージョンのリストを確認

なんやかんやいっぱい出てきますけど、TensorFlow使うために3.6.0をインストールします。3.7.xだと現時点でTensorFlowが追いついていないとかなんとか。

terminal
$ pyenv install 3.6.0

エラーが出た! -> 環境変数の設定

ここまで順調に進んできたところで下記のエラーが出てインストールができませんでした。

terminal
BUILD FAILED (OS X 10.14.1 using python-build 20180424)

なんやかんや調べて環境変数を設定します。

terminal
$ 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

この辺りは書き方色々ありましたけども、要はpyenvにpathを通すために必要な記述ということです。

シェルを再起動します。

terminal
$ exec $SHELL -l

それでも解決しない! -> Mojave用のmacOS SDK headerを追加

どうやら色々調べると、こんな記述が。

code-selectの最新バージョン(2354)にMojave用のmacOS SDK headerがデフォルトで入っていないのが原因のようです。

ということで、下記をインストールします。

terminal
$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

今回はすんなりいきました!

一応、確認します。

terminal
$ pyenv versions
* system (set by /Users/Takeshi/.pyenv/version)
  3.6.0

OKぽいですね。
出来る人は5分くらいの作業かと思うのですけど、自分は半日調べたりしてました。
以上!これから始める人は頑張ってください。

18
14
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
18
14