Labels

Sunday, April 26, 2015

Setting Up a PHP7 Development Environment

Update 10/2016: This is no longer the correct way to install PHP7 now that stable releases are out and published on your distribution's repositories; however, this information is still useful if you want to install the latest bleeding edge versions.


As a PHP developer I am excited about the anticipated stable release of PHP7 coming near the end of this year (2015), but in order to test out its new features it is necessary to set up a development environment using one of the nightly releases of PHP7.  Unfortunately this was not quite as straight forward as I had hoped and hopefully this will help some of you avoid the same frustrations.

This information is accurate as of 04/26/2015 for installation on a *ubuntu (virtual) machine:

The first gotcha that I ran into is that one of the dependencies (libt1-dev) only seems to work on the latest LTS release, 14.04, so if you are running anything newer, even 14.10, it will not install.  You will need to set up your machine for *ubuntu 14.04.

  1. Download a current nightly release from http://php7.zend.com/
  2. As shown in the installation instructions, you will need to install PHP7's dependencies:
    apt-get update && apt-get install -y \
    libcurl4-openssl-dev \
    libmcrypt-dev \
    libxml2-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libmysqlclient-dev \
    libt1-dev \
    libgmp-dev \
    libpspell-dev \
    libicu-dev \
    librecode-dev
    This also proved to present some problems with permissions, even when using sudo.  I'm not exactly sure why, maybe because I'm still a linux newbie, but this is what I did get it done:
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install libcurl4-openssl-dev
    sudo apt-get install libmcrypt-dev
    sudo apt-get install libxml2-dev
    sudo apt-get install libjpeg-dev
    sudo apt-get install libfreetype6-dev
    sudo apt-get install libmysqlclient-dev
    sudo apt-get install libt1-dev
    sudo apt-get install libgmp-dev
    sudo apt-get install libpspell-dev
    sudo apt-get install libicu-dev
    sudo apt-get install librecode-dev
  3. Move into the directory that you downloaded the PHP7 nightly into, probably Downloads and then run the next command to unpack the file and place it in the root.
    Note: If the second command doesn't find the file, just start typing php7 and hit tab and it should auto-complete.
    cd Downloads
    sudo tar xzPf php7*.tar.gz
    PHP7 should now be installed in your /usr/local/php7/bin/ directory and PHP cli should be operational.  You can test it by typing:
    /usr/local/php7/bin/php --version
  4. If all you need is the cli, you should be good to go.  To use with Apache keep going.
    Important: If you have another version of PHP installed (like PHP5), you will need to uninstall it first.
    sudo apt-get install apache2-bin
    sudo cp /usr/local/php7/libphp7.so /usr/lib/apache2/modules/
    sudo cp /usr/local/php7/php7.load /etc/apache2/mods-available/
  5. You will then need to open up /etc/apache2/apache2.conf as root (replace "leafpad" with your editor of choice),
    sudo leafpad /ect/apache2/apache2.conf
    add the following lines to the end of the file and save.
    <FilesMatch \.php$>
    SetHandler application/x-httpd-php
    </FilesMatch>
  6. Run these last commands from your terminal:
    sudo a2dismod mpm_event
    sudo a2enmod mpm_prefork
    sudo a2enmod php7
    and you should be done!
I hope this helps someone.

On a related note, I will be speaking on May 13th at the Fox Cities PHP Meetup about PHP7 scalar type hints, scalar wrapper classes and PHP7 return type hints.  If you can't make it, I will be posting my talk and the slides here on my blog.

Happy PHPing and Go PHP7!