【PHP】phpenvからasdfへの乗り換え

PHP

複数バージョンのPHPを管理するツールとしてphpenvを使ってきましたが、このところアップデートの反映頻度が落ちてきて、現在PHP8.4の反映PRがマージされずに2カ月止まっている状態です。今後のアップデートも期待できそうにないため、asdfへ乗り換えることにしました。

※2025/06/09追記: phpenv の更新がスピーディに実施されるようになったため、筆者は引き続き phpenv を使用しています。

asdf
Manage multiple runtime versions with a single CLI tool

asdfとは

asdf はツールのバージョン マネージャーです。すべてのツール バージョン定義は 1 つのファイル (.tool-versions) 内に含まれており、プロジェクトの Git リポジトリにチェックインしてチームと共有できるため、全員がまったく同じバージョンのツールを使用できるようになります。

以前の作業方法では、複数の CLI バージョン マネージャーが必要で、それぞれに個別の API、構成ファイル、実装 ($PATH 操作、shim、環境変数など) が必要でした。 asdf は、開発ワークフローを簡素化する単一のインターフェイスと構成ファイルを提供し、シンプルなプラグイン インターフェイスを介してすべてのツールとランタイムに拡張できます。

▼asdfプラグインリスト

GitHub - asdf-vm/asdf-plugins: Convenience shortname repository for asdf community plugins
Convenience shortname repository for asdf community plugins - asdf-vm/asdf-plugins

前提条件

  • Ubuntu24.04(WSL2)を使っています
  • phpenvインストール済
  • phpenvのインストール先は「~/.phpenv」
  • 利用シェルはbash
  • gitインストール済
  • curlインストール済

phpenvの無効化

無効化の前に、現状の確認をしておきます。

「~/.profile」内の次の行を削除またはコメントアウトします。

export PHPENV_ROOT="${HOME}/.phpenv"
if [ -d "${PHPENV_ROOT}" ]; then
    export PATH="${PHPENV_ROOT}/bin:${PATH}"
    eval "$(phpenv init -)"
fi

VSCodeやターミナルを一度全て閉じてからWSL2のUbuntuのターミナルを再起動します。

aptでインストールしたPHPが適用されており、

phpenvはコマンドが見つからない状態となっています。

phpenvが全く不要な場合は、

「~/.phpenv」をフォルダごと削除して構いません。

rm -rf ~/.phpenv

※筆者はしばらく様子を見たいので、削除せずに残しておきます。

asdfインストール

git コマンドでリポジトリを ~/.asdf にクローンします。

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.15.0

「~/.bashrc」に次の2行を追記します。

. "$HOME/.asdf/asdf.sh"
. "$HOME/.asdf/completions/asdf.bash"

「~/.bashrc」への変更を現在のシェルに反映します。

source ~/.bashrc

asdfコアのインストールはできました。

一応、ヘルプを確認しておきます。

$ asdf --help
version: v0.15.0-31e8c93

MANAGE PLUGINS
asdf plugin add <name> [<git-url>]      Add a plugin from the plugin repo OR,
                                        add a Git repo as a plugin by
                                        specifying the name and repo url
asdf plugin list [--urls] [--refs]      List installed plugins. Optionally show
                                        git urls and git-ref
asdf plugin list all                    List plugins registered on asdf-plugins
                                        repository with URLs
asdf plugin remove <name>               Remove plugin and package versions
asdf plugin update <name> [<git-ref>]   Update a plugin to latest commit on
                                        default branch or a particular git-ref
asdf plugin update --all                Update all plugins to latest commit on
                                        default branch


MANAGE PACKAGES
asdf current                            Display current version set or being
                                        used for all packages
asdf current <name>                     Display current version set or being
                                        used for package
asdf global <name> <version>            Set the package global version
asdf global <name> latest[:<version>]   Set the package global version to the
                                        latest provided version
asdf help <name> [<version>]            Output documentation for plugin and tool
asdf install                            Install all the package versions listed
                                        in the .tool-versions file
asdf install <name>                     Install one tool at the version
                                        specified in the .tool-versions file
asdf install <name> <version>           Install a specific version of a package
asdf install <name> latest[:<version>]  Install the latest stable version of a
                                        package, or with optional version,
                                        install the latest stable version that
                                        begins with the given string
asdf latest <name> [<version>]          Show latest stable version of a package
asdf latest --all                       Show latest stable version of all the
                                        packages and if they are installed
asdf list <name> [version]              List installed versions of a package and
                                        optionally filter the versions
asdf list all <name> [<version>]        List all versions of a package and
                                        optionally filter the returned versions
asdf local <name> <version>             Set the package local version
asdf local <name> latest[:<version>]    Set the package local version to the
                                        latest provided version
asdf shell <name> <version>             Set the package version to
                                        `ASDF_${LANG}_VERSION` in the current shell
asdf uninstall <name> <version>         Remove a specific version of a package
asdf where <name> [<version>]           Display install path for an installed
                                        or current version
asdf which <command>                    Display the path to an executable


UTILS
asdf exec <command> [args...]           Executes the command shim for current version
asdf env <command> [util]               Runs util (default: `env`) inside the
                                        environment used for command shim execution.
asdf info                               Print OS, Shell and ASDF debug information.
asdf version                            Print the currently installed version of ASDF
asdf reshim <name> <version>            Recreate shims for version of a package
asdf shim-versions <command>            List the plugins and versions that
                                        provide a command
asdf update                             Update asdf to the latest stable release
asdf update --head                      Update asdf to the latest on the master branch

RESOURCES
GitHub: https://github.com/asdf-vm/asdf
Docs:   https://asdf-vm.com

"Late but latest"
-- Rajinikanth

プラグインインストール

まずはインストール可能なプラグインの一覧を確認してみます。

asdf plugin list all

膨大な数のプラグインが利用可能です。

執筆時点で821個のプラグインがあります。

では、PHP用のプラグインをインストールします。

asdf plugin add php

PHPインストール

利用可能なPHPのバージョン一覧を確認してみます。

asdf list all php

全体像が見えないので、行番号を付けて less で閲覧してみます。

asdf list all php | nl | less

4.0.0から利用可能なようです。

「>」(大小記号)キーで最終行に飛んでみます。

執筆時点(2024/12/30)で8.4.2RC1までの1227個のバージョンが利用可能です。

では、PHP8.4.2をインストールしてみます。

asdf install php 8.4.2

インストール済のバージョンを確認してみます。

asdf list
または
asdf list php

他のバージョンをインストールした状態では次のようになります。

バージョン切替

asdfでのバージョン切替のスコープは、

「グローバル(全体)」と「ローカル(現在のディレクトリ)」の2つです。

▼グローバルスコープでのバージョン指定

asdf global php 8.4.2

~/.tool-versions

にバージョン情報が記録されます。

▼ローカルスコープでのバージョン指定

asdf local php 8.3.15

※適用フォルダ外ではグローバルスコープで指定したバージョンになっています。

ImagickをPHP拡張としてインストール

各PHPバージョンをインストール後に、対象のバージョンに切替をしてから

peclコマンドでPHP拡張をインストールします。

では、PHP8.4.2にimagickをインストールしてみます。

cd
asdf global php 8.4.2
pecl install imagick

「Please provide the prefix of ImageMagick installation [autodetect] :」と訊かれるので、何も入力しないで [Enter] を押します。

インストール完了すると、

~/.asdf/installs/php/8.4.2/lib/php/extensions/no-debug-non-zts-20240924

に imagick.so が作成されます。

が、実行権限が無いので imagick.so に実行権限を追加します。

chmod 755 ~/.asdf/installs/php/8.4.2/lib/php/extensions/no-debug-non-zts-20240924/imagick.so

~/.asdf/installs/php/8.4.2/conf.d/php.ini

にimagickを有効化する設定を追記します。

extension="imagick.so"

imagickが有効になっているか確認します。

php -i | grep imagick

所感

miseと同様に、前回インストールしたvfoxとは違って、バージョンの適用が安定しているようです。

やはり、asdf-phpを使っているmiseと同様、Configureオプションは全バージョン共通でした。

どうにかならないんですかね?

PHP拡張は後からバージョン毎に追加というのはmiseやvfoxと同様ですね。

インストール可能なバージョン一覧については、ページング表示から選択してインストールできる点でvfoxの方が優れているように思います。

ただし、対応PHPバージョンはvfoxより断然多いです。

というわけで、これで「asdf」「mise」「vfox」の3ツールを試してみたわけですが、

コンパイルの信頼性についての感覚は

phpenv > asdf == mise == vfox

といった感じです。

対応バージョンの豊富さについては

asdf == mise > vfox > phpenv

使いやすさについては

vfox > phpenv > mise > asdf

バージョン適用の信頼性は

phpenv == asdf == mise >> vfox

総合評価

mise > asdf > phpenv > vfox

といったところでしょうか。

まだインストールしたばかりの段階での評価となりますが、

今後使い続けてみたら評価が変わるかもしれません。

コメント

タイトルとURLをコピーしました