タイトル通りですが、Windows11上で動くWSL (Ubuntu22.04)にgcloudコマンドをインストールする方法を紹介します。
はまったポイントとしては、gcloudコマンドはPython 3.10では動きません。
ほぼ自分のための備忘録です。
gcloudコマンドのインストール
基本的には公式ドキュメント(Cloud SDKのインストール)を参考にインストールしましょう。ディストリビューションに合わせて複数の方法が紹介されていますが、WSLではネットワークの設定周りがめんどくさいので、パッケージを利用しない”Linux”のインストールに従うのがおすすめです。
注意するポイントとして、Ubuntu 22.04にデフォルトでインストールされているPython 3.10.X
にgcloudコマンドが対応していません。なので、追加でPython 3.8
系をインストールする必要があります。
Python3.8のインストール
PPAを追加してPython3.8をインストールします
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
gcloudコマンドのインストール
下記に自分が利用したコマンドを残しておきますが、パッケージのバージョンは適宜変更してください。
必ずCLOUDSDK_PYTHON
環境変数にpython3.8へのパスを設定してからgcloudコマンドを実行します。
# ホームディレクトリへ移動
$ cd
# gcloudコマンドでpython3.8を利用する
$ export CLOUDSDK_PYTHON=/usr/bin/python3.8
# CloudSDKのダウンロードと解凍
$ curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-370.0.0-linux-x86_64.tar.gz
$ tar xvzf google-cloud-sdk-370.0.0-linux-x86_64.tar.gz
# インストール
$ ./google-cloud-sdk/install.sh
# 初期化
./google-cloud-sdk/bin/gcloud init --console-only
Python3.10を利用した時のエラー
上記でCLOUDSDK_PYTHONを指定していなかった場合、下記のエラーが出力されます。
ERROR: (gcloud) Command name argument expected.
ERROR: gcloud failed to load (gcloud.interactive): Problem loading gcloud.interactive: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py).
This usually indicates corruption in your gcloud installation or problems with your Python interpreter.
Please verify that the following is the path to a working Python 2.7 or 3.5+ executable: /usr/bin/python3
If it is not, please set the CLOUDSDK_PYTHON environment variable to point to a working Python 2.7 or 3.5+ executable.
If you are still experiencing problems, please run the following command to reinstall: $ gcloud components reinstall
If that command fails, please reinstall the Cloud SDK using the instructions here: https://cloud.google.com/sdk/
collectionsパッケージの構成がPython3.10で変更されているので、importエラーが出てしまうようです。
必ずpython3.8以下を利用しましょう。
Googleアカウントの設定
gcloudコマンドがインストール出来たら、gcloudコマンドで利用するGoogleアカウントを登録します。
登録されているユーザの確認(gcloud auth list)
$ gcloud auth list
Credentialed Accounts
ACTIVE: *
ACCOUNT: hoge@gmail.com
To set the active account, run: $ gcloud config set account `ACCOUNT`
アカウントの追加(gcloud auth login)
gcloud auth login
コマンドを実行すると、ブラウザでGoogleアカウントにログインする画面が表示されるので、追加したいユーザでログインします。
$ gcloud auth login
# 登録後、gcloud auth listで再確認
$ gcloud auth list
Credentialed Accounts
ACTIVE: *
ACCOUNT: fuga@gmail.com
ACTIVE:
ACCOUNT: hoge@gmail.com
To set the active account, run: $ gcloud config set account `ACCOUNT`
アクティブアカウントの変更(gcloud config set account)
glocud auth listコマンドでもコマンドのガイドラインが表示されていますが、下記コマンドでアクティブアカウントを変更可能です。
$ gcloud config set account hoge@gmail.com
Configurationsの作成
複数のプロジェクトを利用しているときには、named configurations
(名前付けした設定)を設定するのが便利です。
configurationsとは、プロジェクト固有の設定(プロジェクトID、アクティブユーザー、ComputeEngineゾーンなど)を一つにまとめものです。<key, value>形式で設定を保存します。
例えば、プロジェクトA用にはproject-Aというconfigurationsを作成し、プロジェクトB用にはproject-Bというconfigurationsを作成します。
このproject-Aとproject-Bを切り変えることで、それぞれのプロジェクトに応じた設定を利用することができます。
詳細は、公式のドキュメントを参考にしてください。(gcloud topic configurations: https://cloud.google.com/sdk/gcloud/reference/topic/configurations?hl=ja)

configurationsは、複数プロジェクトを扱う上ではほぼ必須です!ただし、プロジェクトを一つしか利用しない場合は必要ないです。
configurationsの確認(gcloud config configurations list)
設定済みのconfigurationsを表示します。
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default True hoge@gmail.com sample us-west2-a us-west2
configurationsの追加(gcloud config configurations create)
デフォルトでは、configurationsを作成すると、アクティブなconfigurationsも変更されます。
$ gcloud config configurations create sample
Created [sample].
Activated [sample].
# 改めて確認
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default False hoge@gmail.com sample us-west2-a us-west2
sample True
configurationsに設定を追加(gcloud config set)
# アカウントの追加
$ gcloud config set account fuga@gmail.com
Updated property [core/account].
# プロジェクトを追加
$ gcloud config set project sample-project
Updated property [core/project].
# Compute Engineのゾーンを追加
$ gcloud config set compute/zone us-central1-b
Updated property [compute/zone].
$ gcloud config set compute/region us-central1
Updated property [compute/region].
# 設定を確認
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default False hoge@gmail.com sample us-west2-a us-west2
sample True fuga@gmail.com sample-project us-central1-b us-central1
configurationsの切り替え(gcloud config configurations activate)
現在ACTIVEなconfigurationsを切り替えます。
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default False hoge@gmail.com sample us-west2-a us-west2
sample True fuga@gmail.com sample-project us-central1-b us-central1
$ gcloud config configurations activate default
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default True hoge@gmail.com sample us-west2-a us-west2
sample False fuga@gmail.com sample-project us-central1-b us-central1
まとめ
gcloudの初期設定の方法をまとめました。
GCPを利用していると、configurationsはほぼ必須となるので、ぜひ使い方を覚えてください。