Install PHP with phpenv on macOS

This is a note on how to install phpenv on mac and how to use it.

1. Install phpenv

Install phpenv

1
$ git clone https://github.com/madumlao/phpenv.git ~/.phpenv

Install php-build to build php

1
$ git clone https://github.com/php-build/php-build.git ~/.phpenv/plugins/php-build

Install libraries to build php

1
$ brew install re2c openssl bison libxml2 autoconf automake icu4c libjpeg libpng libmcrypt libzip

If Mojave

1
$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Homebrew is needed

Add the following to .bash_profile

1
2
3
4
5
# phpenv
export PATH="/usr/local/opt/bison/bin:$PATH"
export PHPENV_ROOT="$HOME/.phpenv"
export PATH="$PHPENV_ROOT/bin:$PATH"
eval "$(phpenv init -)"

Load .bash_profile in shell

1
$ source .bash_profile

2. How to Use phpenv ( Install and manage PHP )

How to check which version you can use.

1
$ phpenv install -l

How to check which version you insatlled.

1
$ phpenv versions

How to install the specified version of PHP.

1
$ phpenv install 7.3.7

Run this command after you install a new version of PHP

1
$ phpenv rehash

How to use the specified version of PHP at global.

1
$ phpenv global 7.3.7

How to use the specified version of PHP at local ( directory ).

1
$ phpenv local 7.3.7

This command will create .php-version file at current directory.

3. How to use composer

Install composer to phpenv

1
2
$ git clone https://github.com/ngyuki/phpenv-composer.git ~/.phpenv/plugins/phpenv-composer
$ phpenv rehash

4. How to Update phpenv

1
2
$ cd $(phpenv root)
$ git pull

5. How to Update php-build

When a new version of PHP is released, you need to update php-build. Otherwise you can not install the new version of PHP.

1
2
$ cd $(phpenv root)/plugins/php-build
$ git pull