HomebrewでインストールしたAWS CLIの「v1」と「v2」を切り替えて使用する

brew link/unlink コマンドでできたよ
2023.02.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

どうも、ちゃだいん(@chazuke4649)です。

AWS CLIの「v1」と「v2」をどちらも使いたいという場合は、以下の記事が参考になります。

上記では、直接Zipファイルをダウンロード/インストールする方法で紹介されています。

これの、「自分はMacのHomebrewでAWS CLIをインストールしているんだが...?」という方向け版です。

やってみた

Homebrew では AWS CLI v2 も v1 も提供されています。

awscli — Homebrew Formulae

awscli@1 — Homebrew Formulae

Homebrewでのv1は、v2ほどすぐに更新されていないようで、利用可能な最新バージョンより、Homebrewでのstableバージョンが少し古い可能性があります。最新バージョンを使いたい場合は直接ダウンロードが良さそうです。

ちなみに、Homebrewでのv1は keg-only であり、シンボリックリンクがなく、そのままインストールするだけだと使えない仕様になってます。

keg-onlyとは

手順

今回は、brew link/unlink コマンドを使用して、v1とv2を切り替えてみます。

  1. brewでAWS CLI v2をインストールする
  2. brewでAWS CLI v1をインストールする
  3. brewで切り替える(v2->v1)
  4. brewで元に戻す(v1->v2)

1. brewでv2をインストールする

※AWS CLI v2はすでにインストール済みのため、作業は割愛します。

## インストールされているv2の情報を表示する
% brew info awscli
==> awscli: stable 2.9.23 (bottled), HEAD
Official Amazon AWS command-line interface
https://aws.amazon.com/cli/
/opt/homebrew/Cellar/awscli/2.9.23 (13,780 files, 122MB) *
  Poured from bottle on 2023-02-15 at 12:41:07
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/awscli.rb
License: Apache-2.0
==> Dependencies
Build: cmake ✘, rust ✘
Required: docutils ✔, python@3.11 ✔, six ✔
==> Options
--HEAD
	Install HEAD version
==> Caveats
The "examples" directory has been installed to:
  /opt/homebrew/share/awscli/examples

zsh completions and functions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Analytics
install: 294,578 (30 days), 780,706 (90 days), 2,910,851 (365 days)
install-on-request: 288,170 (30 days), 766,715 (90 days), 2,869,960 (365 days)
build-error: 40 (30 days)

2. brewでv1をインストールする

v1をインストールします。

% brew install awscli@1
Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).

You have 12 outdated formulae and 1 outdated cask installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

==> Fetching awscli@1
==> Downloading https://ghcr.io/v2/homebrew/core/awscli/1/manifests/1.27.60
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/awscli/1/blobs/sha256:41b06cbef2d8b75d13c205c8ea144a3473b4e200f73dbeda33b6a66ade4fee02
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:41b06cbef2d8b75d13c205c8ea144a3473b4e200f73dbeda33b
######################################################################## 100.0%
==> Pouring awscli@1--1.27.60.arm64_monterey.bottle.tar.gz
==> Caveats
The "examples" directory has been installed to:
  /opt/homebrew/share/awscli/examples

awscli@1 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have awscli@1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/awscli@1/bin:$PATH"' >> ~/.zshrc


zsh completions and functions have been installed to:
  /opt/homebrew/opt/awscli@1/share/zsh/site-functions
==> Summary
?  /opt/homebrew/Cellar/awscli@1/1.27.60: 13,056 files, 103.9MB
==> Running `brew cleanup awscli@1`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

## インストールされているv1の情報を表示する
% brew info awscli@1
==> awscli@1: stable 1.27.60 (bottled) [keg-only]
Official Amazon AWS command-line interface
https://aws.amazon.com/cli/
/opt/homebrew/Cellar/awscli@1/1.27.60 (13,056 files, 103.9MB)
  Poured from bottle on 2023-02-15 at 12:50:31
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/awscli@1.rb
License: Apache-2.0
==> Dependencies
Required: docutils ✔, python@3.11 ✔, pyyaml ✔, six ✔
==> Caveats
The "examples" directory has been installed to:
  /opt/homebrew/share/awscli/examples

awscli@1 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have awscli@1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/awscli@1/bin:$PATH"' >> ~/.zshrc


zsh completions and functions have been installed to:
  /opt/homebrew/opt/awscli@1/share/zsh/site-functions
==> Analytics
install: 2,718 (30 days), 7,399 (90 days), 32,573 (365 days)
install-on-request: 2,717 (30 days), 7,399 (90 days), 32,569 (365 days)
build-error: 1 (30 days)

3. brewで切り替える(v2->v1)

brewのlink/unlinkコマンドにて、awsコマンドのシンボリックリンクをv2からv1へ切り替えることができます。

brew link — Homebrew Documentation brew unlink — Homebrew Documentation

## インストールされている aws cliのバージョンを確認する
% aws --version
aws-cli/2.9.23 Python/3.11.2 Darwin/21.5.0 source/arm64 prompt/off

% which aws
/opt/homebrew/bin/aws

## シンボリックリンクがv2に向いていることを確認する
% ls  -la /opt/homebrew/bin/aws
lrwxr-xr-x  1 chadain  admin  31  2 15 16:44 /opt/homebrew/bin/aws -> ../Cellar/awscli/2.9.23/bin/aws

それでは実際に切り替えていきます。

## dry-runで実行前に確認する
% brew unlink --dry-run awscli
Would remove:
/opt/homebrew/bin/aws
/opt/homebrew/bin/aws_completer
/opt/homebrew/etc/bash_completion.d/aws_bash_completer
/opt/homebrew/share/awscli
/opt/homebrew/share/zsh/site-functions/_aws
/opt/homebrew/share/zsh/site-functions/aws_zsh_completer.sh

## dry-runで実行前に確認する
% brew link --dry-run awscli@1
Would link:
/opt/homebrew/etc/bash_completion.d/aws_bash_completer
/opt/homebrew/bin/aws
/opt/homebrew/bin/aws_completer
/opt/homebrew/share/awscli@1
/opt/homebrew/share/zsh/site-functions/_aws
/opt/homebrew/share/zsh/site-functions/aws_zsh_completer.sh

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/opt/homebrew/opt/awscli@1/bin:$PATH"' >> ~/.zshrc


## v2をunlinkする
% brew unlink awscli
Unlinking /opt/homebrew/Cellar/awscli/2.9.23... 6 symlinks removed.

## リンクが切れたことを確認する
% aws --version
zsh: command not found: aws

% which aws
aws not found

## v1をlinkする
% brew link awscli@1
Linking /opt/homebrew/Cellar/awscli@1/1.27.60... 6 symlinks created.

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/opt/homebrew/opt/awscli@1/bin:$PATH"' >> ~/.zshrc

## v1に切り替わったことを確認する
% aws --version
aws-cli/1.27.60 Python/3.11.2 Darwin/21.5.0 botocore/1.29.60

% which aws
/opt/homebrew/bin/aws

% ls -la /opt/homebrew/bin/aws
lrwxr-xr-x  1 chadain  admin  34  2 15 14:51 /opt/homebrew/bin/aws -> ../Cellar/awscli@1/1.27.60/bin/aws

AWS CLI v1に切り替えることができました。

4. brewで元に戻す(v1->v2)

戻す場合は、逆の手順を踏むだけです。

## dry-runで確認する
% brew unlink --dry-run awscli@1
Would remove:
/opt/homebrew/bin/aws
/opt/homebrew/bin/aws_completer
/opt/homebrew/etc/bash_completion.d/aws_bash_completer
/opt/homebrew/share/awscli@1
/opt/homebrew/share/zsh/site-functions/_aws
/opt/homebrew/share/zsh/site-functions/aws_zsh_completer.sh

## dry-runで確認する
% brew link --dry-run awscli
Would link:
/opt/homebrew/etc/bash_completion.d/aws_bash_completer
/opt/homebrew/bin/aws
/opt/homebrew/bin/aws_completer
/opt/homebrew/share/awscli
/opt/homebrew/share/zsh/site-functions/_aws
/opt/homebrew/share/zsh/site-functions/aws_zsh_completer.sh

## v1をunlinkする
% brew unlink awscli@1
Unlinking /opt/homebrew/Cellar/awscli@1/1.27.60... 6 symlinks removed.

## v2をlinkする
% brew link awscli
Linking /opt/homebrew/Cellar/awscli/2.9.23... 6 symlinks created.

## 元に戻ったか確認する
% aws --version
aws-cli/2.9.23 Python/3.11.2 Darwin/21.5.0 source/arm64 prompt/off

% which aws
/opt/homebrew/bin/aws

% ls  -la /opt/homebrew/bin/aws
lrwxr-xr-x  1 chadain  admin  31  2 15 16:44 /opt/homebrew/bin/aws -> ../Cellar/awscli/2.9.23/bin/aws

確認は以上です。

参考情報

aws/aws-cli: Universal Command Line Interface for Amazon Web Services

MacのHomebrewとは?仕組み・使い方と用語整理 - もちゅろぐ

Homebrew で複数パッケージのバージョンを切り替える方法 - Qiita