|
| 1 | +# Install gRPC for PHP |
| 2 | + |
| 3 | +gRPC is a modern, open-source, high-performance remote procedure call framework. |
| 4 | +If you want to use PHP client libraries for gRPC-enabled APIs, you must install |
| 5 | +gRPC for PHP. This tutorial explains how to install and enable gRPC. |
| 6 | + |
| 7 | +## Objectives |
| 8 | + |
| 9 | +* Install the gRPC extension for PHP. |
| 10 | +* Enable the gRPC extension for PHP. |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +* PHP 7.0 or later |
| 15 | +* [PECL](https://pecl.php.net/) (unless you build from source) |
| 16 | +* [Composer](https://getcomposer.org/) |
| 17 | + |
| 18 | +Note: Windows users can |
| 19 | +[download and enable DLLs][pecl_grpc] |
| 20 | +from PECL. |
| 21 | + |
| 22 | +[pecl_grpc]: https://pecl.php.net/package/gRPC |
| 23 | + |
| 24 | +## Installing PECL |
| 25 | + |
| 26 | +### Ubuntu / Debian |
| 27 | + |
| 28 | +``` |
| 29 | +sudo apt-get install autoconf zlib1g-dev php-dev php-pear |
| 30 | +``` |
| 31 | +If using PHP 7.4+, PHP must have been installed with the `--with-pear` flag. |
| 32 | + |
| 33 | +### CentOS / RHEL 7 |
| 34 | + |
| 35 | +``` |
| 36 | +sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm |
| 37 | +sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm |
| 38 | +sudo yum install php-devel php-pear gcc zlib-devel |
| 39 | +``` |
| 40 | + |
| 41 | +### macOS |
| 42 | + |
| 43 | +``` |
| 44 | +curl -O https://pear.php.net/go-pear.phar |
| 45 | +sudo php -d detect_unicode=0 go-pear.phar |
| 46 | +``` |
| 47 | + |
| 48 | +### Windows |
| 49 | + |
| 50 | +Windows does not require PECL. |
| 51 | + |
| 52 | +[pecl-grpc]: https://pecl.php.net/package/grpc |
| 53 | + |
| 54 | +## Installing the gRPC extension |
| 55 | + |
| 56 | +### Using PECL |
| 57 | + |
| 58 | +``` |
| 59 | +sudo pecl install grpc |
| 60 | +``` |
| 61 | + |
| 62 | +This compiles and installs the gRPC PHP extension into the standard PHP |
| 63 | +extension directory. |
| 64 | + |
| 65 | +**Note**: For users on CentOS/RHEL 6, unfortunately this step won't work. Follow |
| 66 | +the instructions under the **Build from source** tab to compile the |
| 67 | +extension from source. |
| 68 | + |
| 69 | + |
| 70 | +### Build from source |
| 71 | + |
| 72 | +Follow these instructions to compile the gRPC core library and PHP extension |
| 73 | +from source. |
| 74 | + |
| 75 | +1. Clone the gRPC repository from GitHub. |
| 76 | + |
| 77 | + git clone https://github.com/grpc/grpc |
| 78 | + |
| 79 | +2. Build and install the gRPC C core library. |
| 80 | + |
| 81 | + ``` |
| 82 | + cd grpc |
| 83 | + git submodule update --init |
| 84 | + make |
| 85 | + sudo make install |
| 86 | + ``` |
| 87 | +
|
| 88 | + It can take a few minutes to download and execute the library. |
| 89 | + If you have git version 1.8.4 or greater, you can speed up |
| 90 | + the `git submodule update --init` command by adding the `--depth=1` |
| 91 | + flag. |
| 92 | +
|
| 93 | +3. Compile the gRPC PHP extension. |
| 94 | + ``` |
| 95 | + cd src/php/ext/grpc |
| 96 | + phpize |
| 97 | + ./configure |
| 98 | + make |
| 99 | + sudo make install |
| 100 | + ``` |
| 101 | +
|
| 102 | +#### Windows |
| 103 | +
|
| 104 | +Windows users can download the pre-compiled gRPC directly from the |
| 105 | +[PECL website][pecl-grpc]. |
| 106 | +
|
| 107 | +Read the [PHP documentation for installing extensions](https://www.php.net/manual/en/install.pecl.windows.php) on Windows. |
| 108 | +
|
| 109 | +
|
| 110 | +[pecl-grpc]: https://pecl.php.net/package/grpc |
| 111 | +
|
| 112 | +### Enable the gRPC extension in php.ini |
| 113 | +
|
| 114 | +#### Linux / macOS |
| 115 | +
|
| 116 | +Add this line anywhere in your `php.ini` file, for example, `/etc/php7/cli/php.ini`. |
| 117 | +You can find this file by running `php --ini`. |
| 118 | +
|
| 119 | +```sh |
| 120 | +extension=grpc.so |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | +#### Windows |
| 125 | + |
| 126 | +Add this line anywhere in your `php.ini` file, for example, `C:\Program Files\PHP\7.3\php.ini`. |
| 127 | + |
| 128 | +```sh |
| 129 | +extension=php_grpc.dll |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +### Add gRPC as a Composer dependency |
| 134 | + |
| 135 | +Use Composer to add the `grpc/grpc` package to your PHP project: |
| 136 | + |
| 137 | +```sh |
| 138 | +composer require "grpc/grpc:^1.38" |
| 139 | +``` |
| 140 | + |
| 141 | +## Installing the protobuf runtime library |
| 142 | + |
| 143 | +You can choose from two protobuf runtime libraries. The APIs they offer are |
| 144 | +identical. The C implementation performs better than the PHP (native) |
| 145 | +implementation, while the native implementation installs easier than the |
| 146 | +C implementation. |
| 147 | + |
| 148 | +### C implementation |
| 149 | + |
| 150 | +For better performance with gRPC, enable the protobuf C-extension. |
| 151 | + |
| 152 | +**Linux / macOS** |
| 153 | + |
| 154 | +Install the `protobuf.so` extension by using PECL. |
| 155 | + |
| 156 | +``` |
| 157 | +sudo pecl install protobuf |
| 158 | +``` |
| 159 | + |
| 160 | +Now add this line to your `php.ini` file, for example, |
| 161 | +`/etc/php5/cli/php.ini`. |
| 162 | + |
| 163 | +``` |
| 164 | +extension=protobuf.so |
| 165 | +``` |
| 166 | + |
| 167 | +**Windows** |
| 168 | + |
| 169 | +Download the pre-compiled protobuf extension directly from the |
| 170 | +[PECL website][pecl-protobuf]. |
| 171 | + |
| 172 | +Now add this line to your `php.ini` file, for example, |
| 173 | +`C:\Program Files\PHP\7.3\php.ini`. |
| 174 | + |
| 175 | +``` |
| 176 | +extension=php_protobuf.dll |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | +[pecl-protobuf]: https://pecl.php.net/package/protobuf |
| 181 | + |
| 182 | +### PHP implementation |
| 183 | + |
| 184 | +For easier installation, require the `google/protobuf` package by using |
| 185 | +Composer. |
| 186 | + |
| 187 | +``` |
| 188 | +composer require "google/protobuf:^3.17" |
| 189 | +``` |
| 190 | + |
| 191 | + |
| 192 | +## What's next |
| 193 | + |
| 194 | +Now that you've installed gRPC and the gRPC PHP extension, try out gRPC-enabled |
| 195 | +APIs such as |
| 196 | +[Google Cloud Spanner](https://cloud.google.com/php/docs/reference/cloud-spanner/latest). |
0 commit comments