WSL2+PHP+VSCodeで開発環境構築(その1)

PHPのアプリを作ることにしたので、開発環境を構築してみました。

Windows 11 の VSCode で開発します。本番環境は Linux なので、コードは WSL2 上の Ubuntu 22.04 に置き、動作確認も WLS2 上で起動して行います。

DBは Windows上 の MariaDB を使います。

まず、phpenv のインストールでだいぶつまずいたので、その1は phpenv のインストールまでです。

結局、私の環境では以下の Ubuntu のパッケージを apt でインストールする必要がありました。

  • pkg-config
  • libxml2-dev
  • sqlite3
  • libsqlite3-dev
  • libbz2-dev
  • libcurl4-openssl-dev
  • libpng-dev
  • libjpeg-dev
  • libonig-dev
  • libreadline-dev
  • libtidy-dev
  • libxslt1-dev
  • libzip-dev
  • autoconf

以下、詳細です。

phpenv のインストール

Ubuntu 22.04 のデフォルトのPHP(php-cli)は 8.1 ですが、本番環境は PHP 8.2 なので、デフォルトのPHPはインストールせず、phpenv で 8.2 をインストールして使うことにしました。

phpenv のインストール方法は以下2つのページを参考にしました。

まずは phpenv 本体のインストールと、.profile への設定追加し、ログインしなおします。

$ git clone https://github.com/phpenv/phpenv.git ~/.phpenv
$ cat >> ~/.profile <<_EOF
# phpenv
export PATH="$HOME/.phpenv/bin:$PATH"
eval "$(phpenv init - --no-rehash)"
_EOF
$ exec $SHELL -l
$

phpenv 本体のインストールができたので、php-build プラグインをインストールし、8.2の詳細なバージョンを調べます。

$ git clone https://github.com/php-build/php-build $(phpenv root)/plugins/php-build
$ phpenv install -l | grep 8.2
  8.2.0
  8.2.1

    :
  8.2.13
  8.2snapshot
$ 

ということで、最新の 8.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/mtystg/.phpenv/versions/8.2.13
[Downloading]: https://www.php.net/distributions/php-8.2.13.tar.bz2
[Preparing]: /tmp/php-build/source/8.2.13

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118150414.log'.
[Warn]: Aborting build.
$ 

pkg-config が必要とのことで、インストールします。

$ sudo apt install pkg-config

再度 8.2.13 をインストールすると、またしてもエラー。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (libxml-2.0 >= 2.9.0) were not met:

No package 'libxml-2.0' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118151936.log'.
[Warn]: Aborting build.

libxml2 が必要らしいので調べてみました。

$ apt search libxml2
    :
libxml2/jammy-updates,jammy-security,now 2.9.13+dfsg-1ubuntu0.3 amd64 [installed,automatic]
  GNOME XML library

libxml2-dev/jammy-updates,jammy-security 2.9.13+dfsg-1ubuntu0.3 amd64
  GNOME XML library - development files
    :
$

LIBXML_CFLAGS や LIBXML_LIBS は libxml2-dev が入ってないと指定できないと思うので、入れます。

$ sudo apt install libxml2-dev

再度 8.2.13 をインストールすると、またしてもエラー。。。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (sqlite3 >= 3.7.7) were not met:

No package 'sqlite3' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables SQLITE_CFLAGS
and SQLITE_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118153106.log'.
[Warn]: Aborting build.

はいはい、sqlite3 ね。ライブラリもね。

$ sudo apt install sqlite3 libsqlite3-dev

再度 8.2.13 をインストールすると、またしてもエラー。。。。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118153708.log'.
[Warn]: Aborting build.

はいはい、bzip2 ですね、わかりました。

$ sudo apt install libbz2-dev

再度 8.2.13 をインストールすると、またしてもエラー。。。。。。。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (libcurl >= 7.29.0) were not met:

No package 'libcurl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables CURL_CFLAGS
and CURL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118153946.log'.
[Warn]: Aborting build.

こんどは libcurl ね、はいはい、これかな。

$ sudo apt install libcurl4-openssl-dev

再度エラー。。。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (libpng) were not met:

No package 'libpng' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables PNG_CFLAGS
and PNG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118154543.log'.
[Warn]: Aborting build.

libpng でがすな。

$ sudo apt install libpng-dev

再エラー。。。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (libjpeg) were not met:

No package 'libjpeg' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables JPEG_CFLAGS
and JPEG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118154824.log'.
[Warn]: Aborting build.

こんどは libjpeg ですな。

$ sudo apt install libjpeg-dev 

はい、エラー。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (oniguruma) were not met:

No package 'oniguruma' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ONIG_CFLAGS
and ONIG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118155027.log'.
[Warn]: Aborting build.

oniguruma ? しらなーい。こかれな?

$ sudo apt install libonig-dev

まだまだ出るぞー、エラー。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Please reinstall readline - I cannot find readline.h
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118155243.log'.
[Warn]: Aborting build.

readline.h ね。

$ sudo apt install libreadline-dev

はーい、エラーですねー。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Cannot find libtidy
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118155450.log'.
[Warn]: Aborting build.

libtidy 。。。しらん。入れる。

$ sudo apt install libtidy-dev

エラー。泣き

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (libxslt >= 1.1.0) were not met:

No package 'libxslt' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables XSL_CFLAGS
and XSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118160007.log'.
[Warn]: Aborting build.

libxslt でよろしいですね。。。

$ sudo apt install libxslt1-dev

今度のエラーは。。。

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
No package 'libzip' found
No package 'libzip' found
No package 'libzip' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBZIP_CFLAGS
and LIBZIP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118160244.log'.
[Warn]: Aborting build.

libzip ですね。3回も言うほど大事だよってか。

$ sudo apt install libzip-dev

続きましてのエラーは。。。あれ? エラーがでない? と思ったら出た。なんだこりゃ?

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
In file included from /tmp/php-build/source/8.2.13/ext/opcache/jit/zend_jit.c:714:
/tmp/php-build/source/8.2.13/ext/opcache/jit/zend_jit.c: In function ‘zend_jit_startup’:
/tmp/php-build/source/8.2.13/ext/opcache/jit/dynasm/dasm_x86.h:127:19: warning: array subscript -10 is outside array bounds of ‘void *[38]’ [-Warray-bounds]
  127 |   D->globals = gl - 10;  /* Negative bias to compensate for locals. */
      |                ~~~^~~~
In file included from /tmp/php-build/source/8.2.13/ext/opcache/jit/zend_jit.c:772:
/tmp/php-build/source/8.2.13/ext/opcache/jit/zend_jit_x86.dasc:143:14: note: while referencing ‘dasm_labels’
  143 | static void* dasm_labels[zend_lb_MAX];
      |              ^~~~~~~~~~~
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
-----------------------------------------

The full Log is available at '/tmp/php-build.8.2.13.20240118160700.log'.
[Warn]: Aborting build.

上のほうは警告なんで無視していいのかな。最後の「PEAR package …」は何かと調べたら、autoconf が必要なんだとか。元ネタはこちら

$ sudo apt install autoconf

で、再度挑戦したら、以下のメッセージ。

$ phpenv install 8.2.13
phpenv: /home/mtystg/.phpenv/versions/8.2.13 already exists
continue with installation? (y/N)

変なのがインストールされたままってのも気持ちが悪いので「Y」。

[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/mit/.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
[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/mit/.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.20240118163409.log or rebuild with '--verbose' option
[Success]: Built 8.2.13 successfully.

無事インストールできました。

チェックしてみます。

$ phpenv versions
  8.2.13
$ phpenv version
system (set by /home/mtystg/.phpenv/version)
$ phpenv global 8.2.13
8.2.13
$ phpenv version
8.2.13 (set by /home/mtystg/.phpenv/version)
$ php -r 'echo "Hello world!",PHP_EOL;'
Hello world!

長い道のりでした。(泣)

その2へ続く予定。。。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です