Protocol Buffer 編譯器安裝

如何安裝 Protocol Buffer 編譯器。

Protocol Buffer 編譯器 protoc 用於編譯包含服務和訊息定義的 .proto 檔案。選擇以下方法之一來安裝 protoc

安裝預編譯二進位制檔案(任何作業系統)

要從預編譯的二進位制檔案安裝最新版本的 protocol 編譯器,請按照以下說明操作

  1. https://github.com/google/protobuf/releases 手動下載與您的作業系統和計算機架構對應的 zip 檔案 (protoc-<version>-<os>-<arch>.zip),或者使用以下命令獲取檔案

    PB_REL="https://github.com/protocolbuffers/protobuf/releases"
    curl -LO $PB_REL/download/v30.2/protoc-30.2-linux-x86_64.zip
    
  2. 將檔案解壓縮到 $HOME/.local 或您選擇的目錄中。例如

    unzip protoc-30.2-linux-x86_64.zip -d $HOME/.local
    
  3. 更新您的環境路徑變數以包含 protoc 可執行檔案的路徑。例如

    export PATH="$PATH:$HOME/.local/bin"
    

使用包管理器安裝

您可以使用包管理器在 Linux、macOS 或 Windows 上透過以下命令安裝 Protocol 編譯器 protoc

  • Linux,例如使用 aptapt-get

    apt install -y protobuf-compiler
    protoc --version  # Ensure compiler version is 3+
    
  • macOS,使用 Homebrew

    brew install protobuf
    protoc --version  # Ensure compiler version is 3+
    
  • Windows,使用 Winget

    > winget install protobuf
    > protoc --version # Ensure compiler version is 3+
    

其他安裝選項

如果您想從原始碼構建 protocol 編譯器,或訪問較舊版本的預編譯二進位制檔案,請參閱下載 Protocol Buffers