🗂

pyenv のアップデート方法

2022/04/22に公開1

pyenv とは、pythonのバージョン管理システムです。
プロジェクトごとに、3.6を使ったり、3.10を使ってみたいなんてことがある時に、このpyenvをインストールしておくことで、すぐに使いたいpythonのバージョンに切り替えることができます。

しかし、先日私がこのpyenvを使った時に最新のpythonをインストールできない問題にぶち当たり、数日悩むことになりました。

結果として、pyenv が最新に更新できていなかったことが原因でした。
そこで、ここでは備忘録的にpyenvを最新にアップデートする方法を書き残します。

pyenvの更新方法

pyenvを最新に更新するには以下の2つの方法があるようです。

  • githubの該当リポジトリからfetchしてくる
  • pyenv-updateを使う

githubの該当リポジトリからfetchしてくる

こちらは単純にpyenvの該当ディレクトに移動して、git pullしてくる方法です。

cd ~/.pyenv/plugins/python-build/../.. && git pull && cd -

私の場合、猛烈な量のdiffがあったようで、そりゃ動かなくなりますよね。。。

**.github/FUNDING.yml                                       |  12 +
 .github/ISSUE_TEMPLATE.md                                 |   4 +-
 .github/workflows/macos_tests.yml                         |  41 ++
 .github/workflows/pyenv_tests.yml                         |  35 ++
 .github/workflows/ubuntu_tests.yml                        |   4 +-
 CHANGELOG.md                                              |  96 +++++
 COMMANDS.md                                               |  92 ++++
 CONTRIBUTING.md                                           | 104 +++++
 README.md                                                 | 358 ++++++++++------
 libexec/pyenv                                             |  10 +-
 libexec/pyenv---version                                   |   2 +-
 libexec/pyenv-exec                                        |   4 +-
 libexec/pyenv-help                                        |   5 +-
 libexec/pyenv-init                                        |  63 +--
 libexec/pyenv-prefix                                      |  14 +-
 plugins/python-build/bin/pyenv-uninstall                  |   2 +-
 plugins/python-build/bin/python-build                     | 138 +++++-
 plugins/python-build/scripts/add_miniconda.py             |  54 ++-
 plugins/python-build/share/python-build/2.6.0             |   4 +
 plugins/python-build/share/python-build/2.6.1             |   4 +
 plugins/python-build/share/python-build/2.6.2             |   4 +
 plugins/python-build/share/python-build/2.6.3             |   4 +
 plugins/python-build/share/python-build/2.6.4             |   4 +
 plugins/python-build/share/python-build/2.6.5             |   4 +
 plugins/python-build/share/python-build/2.7.18            |   2 +-
 plugins/python-build/share/python-build/3.10-dev          |   2 +-
 plugins/python-build/share/python-build/3.10.0            |  10 +
 plugins/python-build/share/python-build/3.10.0rc1         |  10 -
 plugins/python-build/share/python-build/3.10.1            |  10 +
 plugins/python-build/share/python-build/3.10.2            |  10 +
 plugins/python-build/share/python-build/3.10.3            |  10 +
 plugins/python-build/share/python-build/3.10.4            |  10 +
 plugins/python-build/share/python-build/3.11.0a7          |  10 +
 plugins/python-build/share/python-build/3.6.15            |   9 +
 plugins/python-build/share/python-build/3.7.12            |  10 +
 plugins/python-build/share/python-build/3.7.13            |  10 +
 plugins/python-build/share/python-build/3.8.13            |  10 +
 plugins/python-build/share/python-build/3.9.10            |  10 +
 plugins/python-build/share/python-build/3.9.11            |  10 +
 plugins/python-build/share/python-build/3.9.12            |  10 +
 plugins/python-build/share/python-build/3.9.8             |  10 +
 plugins/python-build/share/python-build/3.9.9             |  10 +
 plugins/python-build/share/python-build/anaconda3-2021.11 |  19 +
 .../python-build/share/python-build/graalpython-21.3.0    |  48 +++
 .../python-build/share/python-build/graalpython-22.0.0    |  48 +++
 .../python-build/share/python-build/mambaforge-4.10.3-10  |  22 +
 plugins/python-build/share/python-build/mambaforge-pypy3  |   2 +-
 plugins/python-build/share/python-build/micropython-1.16  |   4 +
 plugins/python-build/share/python-build/micropython-1.17  |   4 +
 .../python-build/share/python-build/miniconda2-2.7-4.8.3  |  19 +
 .../python-build/share/python-build/miniconda3-3.7-4.10.3 |  19 +
 .../python-build/share/python-build/miniconda3-3.7-4.11.0 |  19 +
 .../python-build/share/python-build/miniconda3-3.8-4.10.3 |  19 +
 .../python-build/share/python-build/miniconda3-3.8-4.11.0 |  19 +
 .../python-build/share/python-build/miniconda3-3.9-4.10.3 |  19 +
 .../python-build/share/python-build/miniconda3-3.9-4.11.0 |  19 +
 plugins/python-build/share/python-build/miniconda3-latest |   6 +
 .../python-build/share/python-build/miniforge3-4.10.3-10  |  25 ++
 .../patches/2.6.0/Python-2.6/000_patch-setup.py.diff      |  71 ++++
 .../patches/2.6.0/Python-2.6/002_readline63.patch         |  61 +++
 .../python-build/patches/2.6.0/Python-2.6/003_tk86.patch  |  12 +
 .../2.6.0/Python-2.6/010_ssl_no_ssl2_no_ssl3.patch        |  95 +++++
 .../patches/2.6.1/Python-2.6.1/000_patch-setup.py.diff    |  71 ++++
 .../patches/2.6.1/Python-2.6.1/002_readline63.patch       |  61 +++
 .../patches/2.6.1/Python-2.6.1/003_tk86.patch             |  12 +
 .../2.6.1/Python-2.6.1/010_ssl_no_ssl2_no_ssl3.patch      |  95 +++++
 .../patches/2.6.2/Python-2.6.2/000_patch-setup.py.diff    |  71 ++++
 .../patches/2.6.2/Python-2.6.2/002_readline63.patch       |  61 +++
 .../patches/2.6.2/Python-2.6.2/003_tk86.patch             |  12 +
 .../2.6.2/Python-2.6.2/010_ssl_no_ssl2_no_ssl3.patch      |  95 +++++
 .../patches/2.6.3/Python-2.6.3/000_patch-setup.py.diff    |  71 ++++
 .../patches/2.6.3/Python-2.6.3/002_readline63.patch       |  61 +++
 .../patches/2.6.3/Python-2.6.3/003_tk86.patch             |  12 +
 .../2.6.3/Python-2.6.3/010_ssl_no_ssl2_no_ssl3.patch      |  95 +++++
 .../patches/2.6.4/Python-2.6.4/000_patch-setup.py.diff    |  71 ++++
 .../patches/2.6.4/Python-2.6.4/002_readline63.patch       |  61 +++
 .../patches/2.6.4/Python-2.6.4/003_tk86.patch             |  12 +
 .../2.6.4/Python-2.6.4/010_ssl_no_ssl2_no_ssl3.patch      |  95 +++++
 .../patches/2.6.5/Python-2.6.5/000_patch-setup.py.diff    |  71 ++++
 .../patches/2.6.5/Python-2.6.5/002_readline63.patch       |  61 +++
 .../patches/2.6.5/Python-2.6.5/003_tk86.patch             |  12 +
 .../2.6.5/Python-2.6.5/010_ssl_no_ssl2_no_ssl3.patch      |  95 +++++
 .../patches/2.6.6/Python-2.6.6/000_patch-setup.py.diff    |  18 +-
 .../patches/2.6.7/Python-2.6.7/000_patch-setup.py.diff    |  18 +-
 .../patches/2.6.8/Python-2.6.8/000_patch-setup.py.diff    |  18 +-
 .../patches/2.6.9/Python-2.6.9/000_patch-setup.py.diff    |  18 +-
 .../Python-2.7.18/0001-Detect-arm64-in-configure.patch    |  41 ++
 ...macOS-_tkinter-use-of-Tck-Tk-in-Library-Framewor.patch | 237 +++++++++++
 .../0003-Support-arm64-in-Mac-Tools-pythonw.patch         |  34 ++
 .../0004-Use-system-libffi-for-Mac-OS-10.15-and-up.patch  |  85 ++++
 ...types-use-the-correct-ABI-for-variadic-functions.patch | 221 ++++++++++
 ...es-probe-libffi-for-ffi_closure_alloc-and-ffi_pr.patch | 108 +++++
 .../0007-Remove-QuickTime-from-link-args.patch            |  40 ++
 ...45350-Rerun-autoreconf-with-the-pkg-config-macro.patch | 535 ++++++++++++++++++++++++
 .../Python-3.6.15/0001-Detect-arm64-in-configure.patch    |  41 ++
 ...36231-Support-building-on-macOS-without-usr-incl.patch |  93 ++++
 ...macOS-_tkinter-use-of-Tck-Tk-in-Library-Framewor.patch | 240 +++++++++++
 ...-ctypes-and-system-libffi-patches-for-arm64-macO.patch | 314 ++++++++++++++
 ...PO-41100-Support-macOS-11-when-building-GH-21113.patch |  48 +++
 ...bpo-41100-fix-_decimal-for-arm64-Mac-OS-GH-21228.patch |  37 ++
 ...42351-Avoid-error-when-opening-header-with-non-U.patch |  30 ++
 ...45405-Prevent-internal-configure-error-when-runn.patch |  86 ++++
 ...-ctypes-and-system-libffi-patches-for-arm64-macO.patch | 310 ++++++++++++++
 ...bpo-41100-fix-_decimal-for-arm64-Mac-OS-GH-21228.patch |  36 ++
 ...42351-Avoid-error-when-opening-header-with-non-U.patch |  30 ++
 ...45405-Prevent-internal-configure-error-when-runn.patch |  43 ++
 ...-ctypes-and-system-libffi-patches-for-arm64-macO.patch | 310 ++++++++++++++
 ...bpo-41100-fix-_decimal-for-arm64-Mac-OS-GH-21228.patch |  36 ++
 ...42351-Avoid-error-when-opening-header-with-non-U.patch |  30 ++
 plugins/python-build/share/python-build/pypy2.7-7.3.2     |  39 ++
 plugins/python-build/share/python-build/pypy2.7-7.3.2-src |   4 +
 plugins/python-build/share/python-build/pypy2.7-7.3.3     |  39 ++
 plugins/python-build/share/python-build/pypy2.7-7.3.3-src |   4 +
 plugins/python-build/share/python-build/pypy2.7-7.3.4     |  39 ++
 plugins/python-build/share/python-build/pypy2.7-7.3.4-src |   4 +
 plugins/python-build/share/python-build/pypy2.7-7.3.5     |  39 ++
 plugins/python-build/share/python-build/pypy2.7-7.3.5-src |   4 +
 plugins/python-build/share/python-build/pypy2.7-7.3.6     |  39 ++
 plugins/python-build/share/python-build/pypy2.7-7.3.6-src |   4 +
 plugins/python-build/share/python-build/pypy2.7-7.3.8     |  81 ++++
 plugins/python-build/share/python-build/pypy2.7-7.3.8-src |  14 +
 plugins/python-build/share/python-build/pypy2.7-7.3.9     |  81 ++++
 plugins/python-build/share/python-build/pypy2.7-7.3.9-src |  14 +
 plugins/python-build/share/python-build/pypy3.7-7.3.6     |  43 ++
 plugins/python-build/share/python-build/pypy3.7-7.3.6-src |   7 +
 plugins/python-build/share/python-build/pypy3.7-7.3.7     |  43 ++
 plugins/python-build/share/python-build/pypy3.7-7.3.7-src |   7 +
 plugins/python-build/share/python-build/pypy3.7-7.3.8     |  81 ++++
 plugins/python-build/share/python-build/pypy3.7-7.3.8-src |  14 +
 plugins/python-build/share/python-build/pypy3.7-7.3.9     |  81 ++++
 plugins/python-build/share/python-build/pypy3.7-7.3.9-src |  14 +
 plugins/python-build/share/python-build/pypy3.8-7.3.6     |  43 ++
 plugins/python-build/share/python-build/pypy3.8-7.3.6-src |   8 +
 plugins/python-build/share/python-build/pypy3.8-7.3.7     |  43 ++
 plugins/python-build/share/python-build/pypy3.8-7.3.7-src |   8 +
 plugins/python-build/share/python-build/pypy3.8-7.3.8     |  81 ++++
 plugins/python-build/share/python-build/pypy3.8-7.3.8-src |  14 +
 plugins/python-build/share/python-build/pypy3.8-7.3.9     |  81 ++++
 plugins/python-build/share/python-build/pypy3.8-7.3.9-src |  14 +
 plugins/python-build/share/python-build/pypy3.9-7.3.8     |  81 ++++
 plugins/python-build/share/python-build/pypy3.9-7.3.8-src |  14 +
 plugins/python-build/share/python-build/pypy3.9-7.3.9     |  81 ++++
 plugins/python-build/share/python-build/pypy3.9-7.3.9-src |  14 +
 plugins/python-build/share/python-build/pyston-2.2        |   2 +-
 plugins/python-build/share/python-build/pyston-2.3        |   2 +-
 plugins/python-build/share/python-build/pyston-2.3.1      |  19 +
 plugins/python-build/share/python-build/pyston-2.3.2      |  19 +
 plugins/python-build/share/python-build/pyston-2.3.3      |  16 +
 plugins/python-build/test/build.bats                      | 117 +++++-
 plugins/python-build/test/compiler.bats                   |   8 +-
 plugins/python-build/test/pyenv.bats                      |   4 +-
 plugins/python-build/test/pyenv_ext.bats                  |  54 ++-
 plugins/python-build/test/stubs/stub                      |  40 +-
 plugins/python-build/test/test_helper.bash                |   1 +
 plugins/python-build/test/version.bats                    |   3 +-
 test/exec.bats                                            |   2 +-
 test/init.bats                                            |   2 +-
 test/prefix.bats                                          |  11 +-
 test/test_helper.bash                                     |  38 +-
 test/versions.bats                                        |   2 +-
 160 files changed, 7204 insertions(+), 340 deletions(-)**

pyenv-update を使う

続いては、pyenv-update を使う方法です。pyenv には自身をアップデートするコマンドがありません。

pyenv 2.2.5
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   --version   Display the version of pyenv
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version(s)
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefixes for Python versions
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall a specific Python version
   version     Show the current Python version(s) and its origin
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

そこで、他の多くのブログで紹介されているのが、このpyenv-updateです。

詳しくは、上の公式のREADMEをみていただくのがよいかと思いますが、インストールすることで pyenv update というコマンドが追加できます。

インストールは以下。

> git clone https://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update

実行は以下。

pyenv update

上のgithubから直接更新を取ってくるよりも、直感的でいいですね!

余談。。。(発生したエラーと解決への道筋)

完全に余談なのですが、私の場合、pyenvでpythonをインストールした時に以下のエラーが発言しました。

> pyenv install 3.8.12

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.12.tar.xz...
-> https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tar.xz
Installing Python-3.8.12...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.3.1 using python-build 2.0.6-1-g22e1aa6e)

Inspect or clean up the working tree at /var/folders/cj/zpmqfxgn5sd07ff_r28mpvlc0000gn/T/python-build.20220422080243.84090
Results logged to /var/folders/cj/zpmqfxgn5sd07ff_r28mpvlc0000gn/T/python-build.20220422080243.84090.log

Last 10 log lines:
checking for python3.8... python3.8
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "darwin"
checking for gcc... /opt/homebrew/bin/gcc-11
checking whether the C compiler works... no
configure: error: in `/var/folders/cj/zpmqfxgn5sd07ff_r28mpvlc0000gn/T/python-build.20220422080243.84090/Python-3.8.12':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop.

このエラーでググると、以下のpyenvのissueに行きつきました。
https://github.com/pyenv/pyenv/issues/2143

そのため、こちらで記載のある

CC=/opt/homebrew/bin/gcc-11 pyenv install 3.8.12

とかを試したのですが、動かず途方に暮れていました。。。

しかし、ふとした時に気づいたのが、本issueは既にcloseステータスになっていました。(2022/4月にて)

そこで、考えたのがもしかしてこのissue修正など関わるアップデートが入っていない?という考えでした。

てっきり、pyenv は brew でインストールしたものというふうに勘違いしており、普段の更新でpyenvも最新になっていると思っていました。
しかし、実際はpyenvはgithubから直接取得していたたため、色々と更新されずだったのでした。

開発環境周りは初心者エンジニアにはまだまだ難しいなと学んだ今日一日でした。

Discussion

j0hntaj0hnta

わかりやすく助かりました。ありがとうございます。