LoginSignup
123
118

More than 5 years have passed since last update.

phpenvで複数のPHPのバージョンを管理する

Last updated at Posted at 2014-09-04

はじめに

phpenvを利用して複数のバージョンのPHPを管理する方法を記述します。
PHPのインストールには、php-buildを使用しますが、phpenvのプラグインとして導入します。

環境

  • CentOS 6.4
  • git 1.7.1

phpenvのインストール

$ sudo yum install git
$ mkdir -p repos/git
$ cd repos/git
$ git clone https://github.com/CHH/phpenv.git
$ cd phpenv/bin
$ ./phpenv-install.sh
Installing phpenv in /path/to/.phpenv
remote: Counting objects: 1889, done.
remote: Total 1889 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1889/1889), 297.15 KiB | 155 KiB/s, done.
Resolving deltas: 100% (1182/1182), done.
Success.

export PATH="/path/to/.phpenv/bin:$PATH"
eval "$(phpenv init -)"

Add above line at the end of your ~/.bashrc and restart your shell to use phpenv.
~/.bashrc
...(省略)...
export PATH="/path/to/.phpenv/bin:$PATH"
eval "$(phpenv init -)"
$ source ~/.bashrc

php-buildのインストール

php-buildを.phpenv/pluginsディレクトリへインストールします。
phpenvインストール時は、pluginsディレクトリが存在しないようなので、
gitからcloneする際にpluginsディレクトリも作成します。

$ git clone https://github.com/CHH/php-build.git ~/.phpenv/plugins/php-build
  • インストール可能なPHPのバージョンの確認
$ phpenv install --list

5.2.17
5.3.10
5.3.11
...(省略)...
5.6.0RC4
5.6snapshot
master

PHPのインストール

  • epelリポジトリの追加

libmcryptが必要となるため、epelリポジトリを追加しておきます。

$ sudo yum install wget
$ wget http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
$ sudo yum localinstall epel-release-6-8.noarch.rpm
  • 必要なパッケージのインストール
$ sudo yum install gcc bison libxml2 libxml2-devel openssl-devel   \
    libcurl-devel libjpeg-turbo-devel libpng-devel libmcrypt-devel \
    readline-devel libtidy-devel libxslt-devel
  • PHP5.3.29をインストール
$ phpenv install 5.3.29
  • PHP5.4.32をインストール
$ phpenv install 5.4.32
  • PHP5.5.16をインストール
$ phpenv install 5.5.16
  • インストール済みのバージョンの確認
$ phpenv versions
  5.3.29
  5.4.32
  5.5.16

利用するPHPのバージョン切替

  • PHP5.4.32を指定
$ phpenv local 5.4.32
$ phpenv version
5.4.32 (set by /path/to/.php-version)
$ phpenv versions
  5.3.29
* 5.4.32 (set by /path/to/.php-version)
  5.5.16
$ php --version
PHP 5.4.32 (cli) (built: Sep  5 2014 01:04:26)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

  • PHP5.5.16へ変更
$ phpenv 5.5.16
$ phpenv version
5.5.16 (set by /path/to/.php-version)
$ phpenv versions
  5.3.29
  5.4.32
* 5.5.16 (set by /path/to/.php-version)
$ php --version
PHP 5.5.16 (cli) (built: Sep  5 2014 01:24:23)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

参考

123
118
0

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
123
118