Install gRPC for PHP

Objectives

  • Install and enable the gRPC extension for PHP.

Requirements

  • PHP 5.5 and above or PHP 7.0 and above
  • PECL (Unless you build from source)
  • Composer

Install PECL

Ubuntu / Debian

For PHP5

sudo apt-get install php5-dev php-pear phpunit

For PHP7

sudo apt-get install php7.0-dev php-pear phpunit

CentOS / RHEL 7

 sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
 sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
 sudo yum install php56w-devel php-pear phpunit gcc zlib-devel

Mac

 curl -O http://pear.php.net/go-pear.phar
 sudo php -d detect_unicode=0 go-pear.phar

Install Composer

Linux or Mac

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Install the gRPC extension

Using PECL

sudo pecl install grpc

This compiles and installs the gRPC PHP extension into the standard PHP extension directory.

Build from Source

Follow these instructions to compile the gRPC core library and PHP extension from source.

  1. Clone the gRPC repository from GitHub.

    git clone -b $(curl -L http://grpc.io/release) https://github.com/grpc/grpc
    
  2. Build and install the gRPC C core library.

    cd grpc
    git pull --recurse-submodules && git submodule update --init --recursive
    make
    sudo make install
    
  3. Compile the gRPC PHP extension

    cd src/php/ext/grpc
    phpize
    ./configure
    make
    sudo make install
    

Enable the gRPC extension in php.ini

Add this line to your php.ini file, e.g. /etc/php5/cli/php.ini

extension=grpc.so

Add gRPC as a Composer dependency

Require the grpc/grpc package using Composer.

composer require "grpc/grpc:^v1.1.0"

Install the Protobuf Runtime library

There are two protobuf runtime libraries to choose from. They are identical in terms of APIs offered. The C implementation provides better performance, while the native implementation is easier to install.

C Implementation

For better performance with gRPC, install the protobuf.so extension using PECL.

sudo pecl install protobuf

Now add this line to your php.ini file, e.g. /etc/php5/cli/php.ini

extension=protobuf.so

PHP implementation

For easier installation, simply require the google/protobuf package using Composer.

composer require "google/protobuf:^v3.3.0"

Next Steps

Now that gRPC and the gRPC PHP extension are installed, try out gRPC-enabled APIs such as Cloud Spanner.

Send feedback about...