コンテンツにスキップ

Linux や macOS へ google-cloud-sdk をインストールする

Linux や macOS へ google-cloud-sdk をインストールするには以下のような選択肢があります。

  1. curl https://sdk.cloud.google.com | bash でインストールする
  2. Cloud SDK のインストーラ を使う
  3. Homebrew などのパッケージマネージャでインストールする

今回は各々のインストール方法についてメモしておきます。

1. curl https://sdk.cloud.google.com | bash でインストールする

Linux 環境へ https://sdk.cloud.google.com からインストールするには以下を実行します。

curl https://sdk.cloud.google.com | bash

実行中に幾つか、確認事項があります。

Installation directory (this will create a google-cloud-sdk subdirectory) (/root):
Do you want to help improve the Google Cloud SDK (y/N)?
Do you want to continue (Y/n)?
Enter a path to an rc file to update, or leave blank to use
[/root/.bashrc]:

インストールが完了したら ~/.bashrc に下記が追加されていました。

# The next line updates PATH for the Google Cloud SDK.
if [ -f '/root/google-cloud-sdk/path.bash.inc' ]; then . '/root/google-cloud-sdk/path.bash.inc'; fi

# The next line enables shell command completion for gcloud.
if [ -f '/root/google-cloud-sdk/completion.bash.inc' ]; then . '/root/google-cloud-sdk/completion.bash.inc'; fi

シェルをログアウト/ログインし直すなどで .bashrc を再読み込みすると gcloud などへのパスが有効化されます。今回はバージョン 359.0.0 がインストールされました。

# gcloud --version
Google Cloud SDK 359.0.0
bq 2.0.71
core 2021.09.24
gsutil 5.2

2. Cloud SDK のインストーラを使う

Cloud SDK のインストール からインストーラをダウンロードしてインストールします。 公式ページからは google-cloud-sdk-358.0.0-linux-x86_64.tar.gz にリンクされていますが、実際の最新バージョンは 359.0.0 です。 そこで今回は (公式ページ上の URL は利用せずに) 以下のように、359.0.0 をインストールしました。

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-359.0.0-linux-x86_64.tar.gz
tar zxvf google-cloud-sdk-359.0.0-linux-x86_64.tar.gz

ヘルプによると「インストーラに --quiet を指定すると対話的オプションに対して全て「デフォルト値」を指定したのと同じ効果がある」ということですが、実際に --quiet を指定してインストールすると ~/.bashrc がアップデートされませんでした。

./google-cloud-sdk/install.sh --quiet

そこでインストール時に確認されるオプションを予め指定してインストーラを実行します。

./google-cloud-sdk/install.sh \
  --usage-reporting false \
  --rc-path ~/.bashrc \
  --command-completion true \
  --path-update true

シェルをログアウト/ログインし直すなどで .bashrc を再読み込みすると gcloud などへのパスが有効化されます。今回はバージョン 359.0.0 がインストールされました。

# gcloud --version
Google Cloud SDK 359.0.0
bq 2.0.71
core 2021.09.24
gsutil 5.2

実際のインストール作業ログは以下の通りです。

# ./google-cloud-sdk/install.sh \
  --usage-reporting false \
  --rc-path ~/.bashrc \
  --command-completion true \
  --path-update true
Welcome to the Google Cloud SDK!
WARNING: You appear to be running this script as root. This may cause
the installation to be inaccessible to users other than the root user.

Your current Cloud SDK version is: 359.0.0
The latest available version is: 359.0.0

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
                                                 Components                                                 ├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬──────────┤
     Status                             Name                                     ID               Size   ├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼──────────┤
 Not Installed  App Engine Go Extensions                              app-engine-go              4.9 MiB  Not Installed  Appctl                                                appctl                    21.0 MiB  Not Installed  Cloud Bigtable Command Line Tool                      cbt                        7.8 MiB  Not Installed  Cloud Bigtable Emulator                               bigtable                   6.6 MiB  Not Installed  Cloud Datalab Command Line Tool                       datalab                    < 1 MiB  Not Installed  Cloud Datastore Emulator                              cloud-datastore-emulator  18.4 MiB  Not Installed  Cloud Firestore Emulator                              cloud-firestore-emulator  40.4 MiB  Not Installed  Cloud Pub/Sub Emulator                                pubsub-emulator           60.4 MiB  Not Installed  Cloud SQL Proxy                                       cloud_sql_proxy            7.6 MiB  Not Installed  Cloud Spanner Emulator                                cloud-spanner-emulator    22.7 MiB  Not Installed  Emulator Reverse Proxy                                emulator-reverse-proxy    14.5 MiB  Not Installed  Google Cloud Build Local Builder                      cloud-build-local          6.3 MiB  Not Installed  Google Container Registry's Docker credential helper  docker-credential-gcr      1.8 MiB  Not Installed  Kustomize                                             kustomize                  4.3 MiB  Not Installed  Minikube                                              minikube                  26.9 MiB  Not Installed  Nomos CLI                                             nomos                     25.6 MiB  Not Installed  On-Demand Scanning API extraction helper              local-extract             14.7 MiB  Not Installed  Skaffold                                              skaffold                  18.8 MiB  Not Installed  anthos-auth                                           anthos-auth               18.2 MiB  Not Installed  config-connector                                      config-connector          48.7 MiB  Not Installed  gcloud Alpha Commands                                 alpha                      < 1 MiB  Not Installed  gcloud Beta Commands                                  beta                       < 1 MiB  Not Installed  gcloud app Java Extensions                            app-engine-java           52.7 MiB  Not Installed  gcloud app Python Extensions                          app-engine-python          7.8 MiB  Not Installed  gcloud app Python Extensions (Extra Libraries)        app-engine-python-extras  26.4 MiB  Not Installed  kpt                                                   kpt                       12.2 MiB  Not Installed  kubectl                                               kubectl                    < 1 MiB  Not Installed  kubectl-oidc                                          kubectl-oidc              18.2 MiB  Not Installed  pkg                                                   pkg                                 Installed      BigQuery Command Line Tool                            bq                         < 1 MiB  Installed      Cloud SDK Core Libraries                              core                      20.3 MiB  Installed      Cloud Storage Command Line Tool                       gsutil                     8.5 MiB └───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴──────────┘
To install or remove components at your current SDK version [359.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [359.0.0], run:
  $ gcloud components update

Backing up [/root/.bashrc] to [/root/.bashrc.backup].
[/root/.bashrc] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

3. Homebrew などのパッケージマネージャでインストールする

M1 Chip macOS 11.6 + Homebrew 3.2.14 環境で Homebrew から google-cloud-sdk をインストールするには以下を実行します。

brew install google-cloud-sdk

以下はインストールログからの抜粋です。 こちらもバージョン 359.0.0 がインストールされました。

(snip)
==> Caveats
google-cloud-sdk is installed at /opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile:

  for bash users
    source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc"
    source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc"

  for zsh users
    source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc"
    source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc"

  for fish users
    source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.fish.inc"
(snip)
Your current Cloud SDK version is: 359.0.0
Installing components from version: 359.0.0

┌─────────────────────────────────────────────────────────────────────────────┐
                     These components will be installed.                     ├─────────────────────────────────────────────────────┬────────────┬──────────┤
                         Name                          Version      Size   ├─────────────────────────────────────────────────────┼────────────┼──────────┤
 BigQuery Command Line Tool                               2.0.71   < 1 MiB  BigQuery Command Line Tool (Platform Specific)           2.0.71   < 1 MiB  Cloud SDK Core Libraries (Platform Specific)         2021.09.17   < 1 MiB  Cloud Storage Command Line Tool                             5.2   8.5 MiB  Cloud Storage Command Line Tool (Platform Specific)        4.68   < 1 MiB  anthoscli                                                0.2.20  47.4 MiB  gcloud cli dependencies                              2021.04.16   < 1 MiB └─────────────────────────────────────────────────────┴────────────┴──────────┘

私の環境では bash を使っている為、~/.bashrc へ下記を追記しました。

source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc"
source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc"

これでバージョン 359.0.0 がインストールされました。

$ gcloud version
Google Cloud SDK 359.0.0
bq 2.0.71
core 2021.09.24
gsutil 5.2

初期設定する

gcloud init を実行して初期設定を行います。

gcloud init

途中で下記のように表示されますので、指定の URL へアクセスします。

Go to the following link in your browser:

    https://accounts.google.com/…

下記のような画面が表示されますので 許可 をクリックして続行します。

file

verification code が表示されるのでコピーし、CLI で実行している gcloud init へ貼り付けます。

file

参考

インストーラのヘルプ表示

# ./google-cloud-sdk/install.sh --help
Welcome to the Google Cloud SDK!
WARNING: You appear to be running this script as root. This may cause
the installation to be inaccessible to users other than the root user.
usage: install.py [-h] [--usage-reporting USAGE_REPORTING]
                  [--screen-reader SCREEN_READER] [--rc-path RC_PATH]
                  [--command-completion COMMAND_COMPLETION]
                  [--path-update PATH_UPDATE] [--disable-installation-options]
                  [--override-components [OVERRIDE_COMPONENTS [OVERRIDE_COMPONENTS ...]]]
                  [--additional-components ADDITIONAL_COMPONENTS [ADDITIONAL_COMPONENTS ...]]
                  [--quiet] [--install-python INSTALL_PYTHON]

optional arguments:
  -h, --help            show this help message and exit
  --usage-reporting USAGE_REPORTING
                        (true/false) Enable anonymous usage reporting.
  --screen-reader SCREEN_READER
                        (true/false) Enable screen reader mode.
  --rc-path RC_PATH     Profile to update with PATH and completion. If given
                        without --command-completion or --path-update in
                        "quiet" mode, a line will be added to this profile for
                        both command completion and path updating.
  --command-completion COMMAND_COMPLETION, --bash-completion COMMAND_COMPLETION
                        (true/false) Add a line for command completion in the
                        profile. In "quiet" mode, if True and you do not
                        provide--rc-path, the default profile will be updated.
  --path-update PATH_UPDATE
                        (true/false) Add a line for path updating in the
                        profile. In "quiet" mode, if True and you do not
                        provide --rc-path, the default profile will be
                        updated.
  --disable-installation-options
                        DEPRECATED. This flag is no longer used.
  --override-components [OVERRIDE_COMPONENTS [OVERRIDE_COMPONENTS ...]]
                        Override the components that would be installed by
                        default and install these instead.
  --additional-components ADDITIONAL_COMPONENTS [ADDITIONAL_COMPONENTS ...]
                        Additional components to install by default. These
                        components will either be added to the default install
                        list, or to the override-components (if provided).
  --quiet, -q           Disable all interactive prompts. If input is required,
                        defaults will be used or an error will be raised
  --install-python INSTALL_PYTHON
                        (true/false) Attempt to install Python. MacOs only.

https://sdk.cloud.google.com の内容

#!/bin/bash

URL=https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash

function download {
  scratch="$(mktemp -d -t tmp.XXXXXXXXXX)" || exit
  script_file="$scratch/install_google_cloud_sdk.bash"

  echo "Downloading Google Cloud SDK install script: $URL"
  curl -# "$URL" > "$script_file" || exit
  chmod 775 "$script_file"

  echo "Running install script from: $script_file"
  "$script_file" "$@"
}

download "$@"