How to Install gRPC for PHP

gRPC is a modern, open-source, and high-performance remote procedure call framework. If you want to use PHP client libraries for APIs that support gRPC, you must install gRPC for PHP. This tutorial explains how to install and enable gRPC.

Destination:

  • Install the gRPC extension for PHP.

  • Enable the gRPC extension for PHP.

Requirements

  1. PHP 7.0 or later

  2. PECL (unless you are building from source)


Installing PECL

  1. Ubuntu/Debian

sudo apt-get install autoconf zlib1g-dev php-dev php-pear
If using PHP 7.4+, PHP must have been installed with the --with-pear flag.


  1. Centos/RHEL7

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 php-devel php-pear gcc zlib-devel


  1. MacOS

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


Install extensive gRPC


  1. Using PECL

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


Note: For users on CentOS/RHEL 6, unfortunately this step will not work. Follow the instructions under the Build from source tab to compile the extension from source.




  1. Build from Source

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


  1. Clone the gRPC repository from GitHub.


git clone --depth=1 -b $(curl -L https://grpc.io/release) \
  https://github.com/grpc/grpc


  1. Build and install the gRPC C core library.

cd grpc
git submodule update --init
make
sudo make install

It can take a few minutes to download and execute the library.
If you have git version 1.8.4 or greater, you can speed up
the `git submodule update --init` command by adding the `--depth=1`
flag.


  1. Compile the gRPC PHP extension.

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


Enable gRPC extension in php.ini

  1. Linux/MacOS

Add this line anywhere in your php.ini file, for example, /etc/php7/cli/php.ini. You can find this file by running php --ini.


    extension=grpc.so


Installing protobuf runtime library


You can choose from two protobuf runtime libraries. The APIs they offer are identical. The C implementation performs better than the (native) PHP implementation, while the native implementation installs easier than the C implementation.


  1. C Implementation 

For better performance with gRPC, enable the C-extension protobuf.


Linux/MacOS

Install the protobuf.so extension by using PECL.

sudo pecl install protobuf
Now add this line to your php.ini file, for example, /etc/php5/cli/php.ini.

extension=protobuf.so


  1. PHP Implementation

For easier installation, it requires the google/protobuf package using Composer.

    

    composer require "google/protobuf:^3.17"


Now that you have installed gRPC and the gRPC PHP extension


Good luck and thank you.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.