【PHP】複数バージョン切替ツール phpenv のインストール

PHP

PHPの複数のバージョンを簡単にインストール&切替できるツール phpenv をUbuntuにインストールした際の記録と解説です。

この記事のゴール

  • phpenvをUbuntuにインストールする
  • PHP8.2.13とPHP8.3.0をphpenvでインストールする
  • phpenvでバージョンを切替する

phpenv + php_build のインストール

phpenv/phpenvphp_build/php_build

phpenv/phpenv-installer を使って同時にインストールします。

bashのターミナル上で次のコマンドを実行します。

cd ~
curl -L https://raw.githubusercontent.com/phpenv/phpenv-installer/master/bin/phpenv-installer | bash

実行結果:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--   100  3685  100  3685    0     0   7788      0 --:--:-- --:--:-- --:--:--  7100  3685  100  3685    0     0   7786      0 --:--:-- --:--:-- --:--:--  7774
INFO
|  $PHPENV_ROOT is defined as /home/macocci7/.phpenv

Installing phpenv
Cloning into '/home/macocci7/.phpenv'...
remote: Enumerating objects: 1213, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 1213 (delta 3), reused 0 (delta 0), pack-reused 1205
Receiving objects: 100% (1213/1213), 235.52 KiB | 1000.00 KiB/s, done.
Resolving deltas: 100% (748/748), done.
|  Done.

Installing php-build/php-build
|  Done.

Installing madumlao/phpenv-aliases
|  Done.

Installing madumlao/phpenv-man
|  Done.

Installing ngyuki/phpenv-composer
|  Done.

WARNING
|  # Seems you still have not added 'phpenv' to the load path.
|  
|  # Load phpenv automatically by adding
|  # the following to ~/.bash_profile:

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

~/.bash_profile の最後に次の5行を追記します。

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

追記した設定をシェルに反映するために次のコマンドを実行します。

exec $SHELL -l

依存関係をインストール

PHPのビルドに必要な依存関係をインストールします。

次のコマンドを実行します。

~/.phpenv/plugins/php-build/install-dependencies.sh

少し時間が掛かります。

実行結果:

/usr/bin/sudo
[sudo] password for macocci7: 
Hit:1 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease

・・・(中略)・・・

Setting up libxslt1-dev:amd64 (1.1.34-4ubuntu0.22.04.1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for install-info (6.8-4build1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

PHP8.2インストール

PHP8.2 と PHP8.3 をインストールしていきます。

それぞれのコンパイルに数十分かかります。

この記事を書いている2023年12月30日時点では、

最新バージョンの 8.2.14 と 8.3.1 の定義ファイルが無いため

一つ前のバージョンをインストールしていきます。

※ PHPバージョンのインストールオプション定義ファイルは

php_build/php_build に含まれていますが、

※ なる早で最新版が適用されるようになっています。

※定義ファイルの更新の仕方は▼の記事をご覧ください。

imagick等の拡張機能をインストールする場合は

こちらの記事を参考にしてください。

まずは、次のコマンドで PHP8.2.13 をインストールします。

phpenv install 8.2.13

実行結果:

[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 8.2.13 into /home/macocci7/.phpenv/versions/8.2.13
[Skipping]: Already downloaded and extracted https://www.php.net/distributions/php-8.2.13.tar.bz2
[Preparing]: /tmp/php-build/source/8.2.13
[Compiling]: /tmp/php-build/source/8.2.13
[xdebug]: Installing version 3.3.1
[Downloading]: http://xdebug.org/files/xdebug-3.3.1.tgz
[xdebug]: Compiling xdebug in /tmp/php-build/source/xdebug-3.3.1
[xdebug]: Installing xdebug configuration in /home/macocci7/.phpenv/versions/8.2.13/etc/conf.d/xdebug.ini
[xdebug]: Cleaning up.
Makefile:245: warning: overriding recipe for target 'test'
Makefile:136: warning: ignoring old recipe for target 'test'
[Info]: Enabling Opcache...
[Info]: Done
[Info]: The Log File is not empty, but the Build did not fail. Maybe just warnings got logged. You can review the log in /tmp/php-build.8.2.13.20231230121721.log or rebuild with '--verbose' option
[Success]: Built 8.2.13 successfully.
8.2 => 8.2.13

PHPインストール直後には、phpenvのshimをリビルドする必要があります。

次のコマンドを実行します。

phpenv rehash

一瞬で終わります。特に出力はされません。

次にバージョンの確認をしてみましょう。

まずは現在のバージョンから

php -v

実行結果:

PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies
    with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans

次に、phpenvで管理バージョンの確認をしてみます。

phpenv versions

実行結果:

* system (set by /home/macocci7/.phpenv/version)
  8.2
  8.2.13

バージョンの切替をしてみます。

phpenv shell 8.2.13

実行結果:

8.2.13

phpenvの管理バージョンを確認してみます。

phpenv versions

実行結果:

  system
  8.2
* 8.2.13 (set by PHPENV_VERSION environment variable)

PHPのバージョンを確認してみます。

php -v

実行結果:

PHP 8.2.13 (cli) (built: Dec 30 2023 12:23:12) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.13, Copyright (c), by Zend Technologies
    with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans

切替できていますね。

PHP8.3インストール

次に、PHP8.3.0 をインストールしていきます。

phpenv install 8.3.0

実行結果:

[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 8.3.0 into /home/macocci7/.phpenv/versions/8.3.0
[Downloading]: https://www.php.net/distributions/php-8.3.0.tar.bz2
[Preparing]: /tmp/php-build/source/8.3.0
[Compiling]: /tmp/php-build/source/8.3.0
[xdebug]: Installing version 3.3.1
[Skipping]: Already downloaded http://xdebug.org/files/xdebug-3.3.1.tgz
[xdebug]: Compiling xdebug in /tmp/php-build/source/xdebug-3.3.1
[xdebug]: Installing xdebug configuration in /home/macocci7/.phpenv/versions/8.3.0/etc/conf.d/xdebug.ini
[xdebug]: Cleaning up.
Makefile:245: warning: overriding recipe for target 'test'
Makefile:136: warning: ignoring old recipe for target 'test'
[Info]: Enabling Opcache...
[Info]: Done
[Info]: The Log File is not empty, but the Build did not fail. Maybe just warnings got logged. You can review the log in /tmp/php-build.8.3.0.20231230134529.log or rebuild with '--verbose' option
[Success]: Built 8.3.0 successfully.
8.3 => 8.3.0
Download composer.phar ...
Move composer.phar to /home/macocci7/.phpenv/versions/8.2.13/composer

なんか、最後の1行が気になりますが、

とりあえずインストール完了はしたようです。

shimのリビルドをします。

phpenv rehash

実行結果:

Download composer.phar ...
Move composer.phar to /home/macocci7/.phpenv/versions/8.3.0/composer

やはり、composerのコピー先が。。

では、phpenvの管理バージョンを確認してみます。

phpenv versions

実行結果:

  system
  8.2
* 8.2.13 (set by PHPENV_VERSION environment variable)
  8.3
  8.3.0

8.3.0が追加されていますね。

バージョンを切替します。

phpenv shell 8.3.0

実行結果:

8.3.0

PHPのバージョンを確認します。

php -v

実行結果:

PHP 8.3.0 (cli) (built: Dec 30 2023 13:55:57) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.0, Copyright (c), by Zend Technologies
    with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans

うまくいったようです。

コメント

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