pyenvを使って最新のPythonをインストールする
pyenvとは
複数のバージョンの Python を切り替えて使えるようにするもの。Ruby の rvm とか rbenv みたいなもの。
pyenvのインストール
インストールというか、GitHub からクローンしてくる。ここではホームディレクトリ下の .pyenv にクローン。
takatoh@nightschool $ git clone git://github.com/yyuu/pyenv.git .pyenv
Cloning into '.pyenv'...
remote: Counting objects: 11571, done.
remote: Compressing objects: 100% (158/158), done.
remote: Total 11571 (delta 107), reused 0 (delta 0), pack-reused 11391
Receiving objects: 100% (11571/11571), 2.05 MiB | 399.00 KiB/s, done.
Resolving deltas: 100% (8114/8114), done.
Checking connectivity... done.
続いて .bashrc に以下を追記。
#for pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
シェルを起動し直すと pyenv コマンドが使えるようになる。
takatoh@nightschool $ pyenv
pyenv 20151210-1-ge66dcf2
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help ' for information on a specific command.
For full documentation, see: https://github.com/yyuu/pyenv#readme
Python 2.7.11をインストール
特定のバージョンをインストールするには pyenv install コマンド。
takatoh@nightschool $ pyenv install 2.7.11
Downloading Python-2.7.11.tgz...
-> https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
Installing Python-2.7.11...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Installed Python-2.7.11 to /home/takatoh/.pyenv/versions/2.7.11
あれ、なんか WARNING が出た。インストール自体はできてるようだけど……bzip2 が足りないようだ。このページを参考に bzip2 をインストール。
takatoh@nightschool $ sudo apt-get install -y bzip2
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
bzip2 はすでに最新版です。
以下のパッケージが自動でインストールされましたが、もう必要とされていません:
ax25-node firefox-locale-en libax25 linux-headers-3.13.0-34
linux-headers-3.13.0-34-generic linux-image-3.13.0-34-generic
linux-image-extra-3.13.0-34-generic openbsd-inetd
これを削除するには 'apt-get autoremove' を利用してください。
アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 21 個。
インストールされてるじゃん。なんで WARNING が出たんだ?
まあいい、このまま進めよう。
Python 3.5.1をインストール
Python3 系の最新バージョンをインストール。
takatoh@nightschool $ pyenv install 3.5.1
Downloading Python-3.5.1.tgz...
-> https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
Installing Python-3.5.1...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Installed Python-3.5.1 to /home/takatoh/.pyenv/versions/3.5.1
shimのリフレッシュ
shim がなんだかわからないけど、参考にしたページに書いてあったのでやっておく。
takatoh@nightschool $ pyenv rehash
Pythonのバージョンの切り替え
ここまでの作業で、複数のバージョンがインストールできた。利用可能なバージョンは pyenv versions コマンドで確認できる。
takatoh@nightschool $ pyenv versions
* system (set by /home/takatoh/.pyenv/version)
2.7.11
3.5.1
takatoh@nightschool $ python -V
Python 2.7.6
今はシステム標準の 2.7.6。これを 3.5.1 に切り替えるには次のようにする。
takatoh@nightschool $ pyenv global 3.5.1
takatoh@nightschool $ pyenv versions
system
2.7.11
* 3.5.1 (set by /home/takatoh/.pyenv/version)
バージョンの切り替えには、もうひとつ pyenv local コマンドもある。どう違うのかよくわからない。
ともかく、これでバージョンを切り替えて使えるようになった。
[追記]
参考ページ
cf. Macでpyenv+virtualenv - Qiita
