在 Linux 上安装和配置 gRPC 需要以下几个步骤:
安装依赖项:
gRPC 依赖于 Protocol Buffers(protobuf)和 cmake。首先,确保你的系统已经安装了这些依赖项。对于基于 Debian 的系统(如 Ubuntu),可以使用以下命令安装:
sudo apt-get updatesudo apt-get install build-essential autoconf libtool pkg-config cmake对于基于 RHEL/CentOS 的系统,可以使用以下命令安装:
sudo yum install automake autoconf libtool make gcc-c++ unzip cmake安装 Protocol Buffers(protobuf):
从 GitHub 上克隆 protobuf 源代码:
git clone https://github.com/protocolbuffers/protobuf.gitcd protobuf切换到最新的稳定版本(例如 v3.17.3):
git checkout v3.17.3编译并安装 protobuf:
./autogen.sh./configuremakesudo make install将 protobuf 库添加到 LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib安装 gRPC:
从 GitHub 上克隆 gRPC 源代码:
git clone https://github.com/grpc/grpc.gitcd grpc切换到最新的稳定版本(例如 v1.40.0):
git checkout v1.40.0编译并安装 gRPC:
mkdir build && cd buildcmake ..makesudo make install将 gRPC 库添加到 LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib安装 gRPC C++ 插件:
cd grpc/src/compilermake grpc_cpp_pluginsudo cp grpc_cpp_plugin /usr/local/bin/现在,你已经在 Linux 上成功安装并配置了 gRPC。接下来,你可以开始创建和运行 gRPC 应用程序了。


