Install gRPC for PHP

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

Objectives

  • Install the gRPC extension for PHP.
  • Enable the gRPC extension for PHP.

Requirements

Note: Windows users candownload and enable DLLsfrom PECL.

Installing PECL

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.

CentOS / RHEL 7

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

macOS

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

Windows

Windows does not require PECL.

Installing the gRPC extension

Using PECL

sudo pecl install grpc

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

Note: For users on CentOS/RHEL 6, unfortunately this step won't work. Followthe instructions under theBuild from source tab to compile theextension from source.

Build from source

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

  1. Clone the gRPC repository from GitHub.

     git clone https://github.com/grpc/grpc
  2. Build and install the gRPC C core library.

    cd grpcgit submodule update --initmakesudo 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 thegit submodule update --init command by adding the--depth=1 flag.

  3. Compile the gRPC PHP extension.

    cd src/php/ext/grpcphpize./configuremakesudo make install

Windows

Windows users can download the pre-compiled gRPC directly from thePECL website.

Read thePHP documentation for installing extensions on Windows.

Enable the gRPC extension in php.ini

Linux / macOS

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

extension=grpc.so

Windows

Add this line anywhere in yourphp.ini file, for example,C:\Program Files\PHP\7.3\php.ini.

extension=php_grpc.dll

Add gRPC as a Composer dependency

Use Composer to add thegrpc/grpc package to your PHP project:

composer require "grpc/grpc:^1.38"

Installing the protobuf runtime library

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

C implementation

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

Linux / macOS

Install theprotobuf.so extension by using PECL.

sudo pecl install protobuf

Now add this line to yourphp.ini file, for example,/etc/php5/cli/php.ini.

extension=protobuf.so

Windows

Download the pre-compiled protobuf extension directly from thePECL website.

Now add this line to yourphp.ini file, for example,C:\Program Files\PHP\7.3\php.ini.

extension=php_protobuf.dll

PHP implementation

For easier installation, require thegoogle/protobuf package by usingComposer.

composer require "google/protobuf:^3.17"

What's next

Now that you've installed gRPC and the gRPC PHP extension, try out gRPC-enabledAPIs such asGoogle Cloud Spanner.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.