LoginSignup
5
5

More than 5 years have passed since last update.

phpbrew @ Mac で複数バージョンのphpの環境構築するAnsible Playbook

Last updated at Posted at 2016-05-01

phpenvはrbenvとコンフリクトしてぐちゃぐちゃになるしで悩んでいたら、phpbrewというphpのバージョンを気軽に変えられる似たツールがあったので、こちらでMacの環境構築作った。

例によってAnsibleにして管理してるので、playbookの形で紹介。

ikuwow/mac-provision: Provisioner of My Mac.
https://github.com/ikuwow/mac-provision

これがplaybookだ

(taskです)

php.yml
---

- name: Install taps of php
  homebrew_tap: tap=homebrew/php
  # phpbrewがはいってる

- name: Remove current php packages
  homebrew: name={{item}} state=absent
  with_items:
    - php56
    - composer
    - php56-mcrypt
    - php56-intl
    - php56-pdo-pgsql
  # いままでのを削除

- name: Install required packages
  homebrew: name={{item}}
  with_items:
    - phpbrew
    - openssl
    - libxml2
  # この辺りのパッケージを入れておく必要がある

- name: Initlalize phpbrew
  shell: "phpbrew init"
  args:
    creates: ~/.phpbrew
  # After initialization you have to append .bashrc `source ~/.phpbrew/bashrc`

- name: Is openssl of homebrew enabled?
  shell: "[[ `which openssl` = /usr/local/bin/openssl ]]; echo $?"
  register: brew_openssl_enabled
  changed_when: brew_openssl_enabled.stdout != '0'

- name: Force link openssl
  shell: "brew link --force openssl"
  when: brew_openssl_enabled.stdout != '0'

- name: Force link libxml2
  shell: "brew link --force libxml2"
  args:
    creates: /usr/local/include/libxml2

- name: Install php {{base_php_version}}
  shell: "phpbrew install {{base_php_version}} +default"
  args:
    creates: ~/.phpbrew/php/php-{{base_php_version}}

- name: Is php in phpbrew?
  shell: "which php | grep phpbrew > /dev/null; echo $?"
  register: php_is_in_phpbrew
  changed_when: php_is_in_phpbrew.stdout != '0'

- name: Use php in phpbrew
  shell: "source ~/.phpbrew/bashrc && phpbrew switch {{base_php_version}}"
  when: php_is_in_phpbrew.stdout != '0'

- name: Install composer
  shell: "phpbrew app get composer"
  args:
    creates: "~/.phpbrew/bin/composer"

気持ち悪いところ

やってみたはいいが、まだ問題はいろいろある。

brew install phpbrew 時にbrew install phpされる

これはphpbrewがphpに依存しているから。MacにはデフォルトでPHPがあるからわざわざ入れてもらわなくてもいいのに・・・。

brew doctorがパスの指定を怒る

phpenv入れてる時brew doctorしたら出るWarning消した - Qiita
http://qiita.com/takc923/items/45386905f70fde9af0e7#comment-857c092cc39dbec09c8c

brewdoctorの出力
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.

Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
    /Users/degawaikuo/.phpbrew/php/php-5.6.21/bin/php-config

以下の記事のように、aliasで$PATHを変えてしまえばいいんだけど、二重に置換かけるのと、$PATHにバージョン番号が入っちゃってるのと、高いシェルスクリプト力が要求されそうでまだ手を付けてない。

いまのパス
$ ehco $PATH
/Users/username/.pyenv/shims:/Users/username/.rbenv/shims:/Users/username/.phpbrew/php/php-5.6.21/bin:/Users/username/.phpbrew/bin:/Users/username/bin:/usr/local/heroku/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

phpenv入れてる時brew doctorしたら出るWarning消した - Qiita
http://qiita.com/takc923/items/45386905f70fde9af0e7#comment-857c092cc39dbec09c8c

brew doctorがkeg-onlyなformulaがlinkされていると怒る

最近のリリースでlinkする必要はなくなったみたいなので、この問題は起こらなくなりました。もしまだ出る場合はbrew unlink {libxml2,openssl}してしまって大丈夫そうでSす。

上の続き
Warning: Some keg-only formula are linked into the Cellar.
Linking a keg-only formula, such as gettext, into the cellar with
`brew link <formula>` will cause other formulae to detect them during
the `./configure` step. This may cause problems when compiling those
other formulae.

Binaries provided by keg-only formulae may override system binaries
with other strange results.

You may wish to `brew unlink` these brews:
    libxml2
    openssl

phpのコンパイル時にヘッダファイルが見つからないというエラーが出て止まるため、brewで入れたlibxml2とopensslをbrew link --forceしたために出てくるWarning。いつかどこかで問題起こしそうで怖い。

今後

phpのビルドって手間が非常にかかるなあと痛感。もっと楽にならないかなあ。

5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5