0
0

More than 1 year has passed since last update.

備忘:macにてAWS CLIをインストール

Last updated at Posted at 2022-08-19

超個人メモ
macにてAWS CLIをインストールしたので、備忘録。

■実行環境

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H512

■導入手順
手持ちのAWSのテキストに書かれている手順

# pyhonのpipパッケージを入れる
$ curl "https://bootstrap.pypa.io/pip/get-pip.py" -o "get-pip.py"
$ sudo python get-pip.py
$ pip --version

# AWS CLIをインストール
$ sudo pip install awscli
# 正常にインストールされたか確認
$ aws --version

しかし、下記のcurlコマンドでエラーになった。。少し調べてみる。

$ curl "https://bootstrap.pypa.io/pip/get-pip.py" -o "get-pip.py"

■調査結果
get-pip.pyは現在python3系の書き方になっているようです。

get-pip.py 行23〜34
import sys

this_python = sys.version_info[:2]
min_version = (3, 7)
if this_python < min_version:
    message_parts = [
        "This script does not work on Python {}.{}".format(*this_python),
        "The minimum supported Python version is {}.{}.".format(*min_version),
        "Please use https://bootstrap.pypa.io/pip/{}.{}/get-pip.py instead.".format(*this_python),
    ]
    print("ERROR: " + " ".join(message_parts))
    sys.exit(1)

私はpythonは未勉強ですが、、、おそらく
①import sys でシステムの環境情報を取得
②this_python にpythonの実行環境の情報を入れる
③if文で②で代入した変数とmin_versionを比較。pythonのバージョンが3.7.xより古い場合、エラーメッセージを標準出力に表示

・・・という流れでしょうか。pythonのインタプリタで試してみました。

pythonのバージョンの確認

$ python --version
Python 2.7.16

インタプリタでget-pip.pyの一部を実行

>>> import sys
>>> 
>>> this_python = sys.version_info[:2]
>>> min_version = (3, 7)
>>> if this_python < min_version:
...     message_parts = [
...         "This script does not work on Python {}.{}".format(*this_python),
...         "The minimum supported Python version is {}.{}.".format(*min_version),
...         "Please use https://bootstrap.pypa.io/pip/{}.{}/get-pip.py instead.".format(*this_python),
...     ]
...     print("ERROR: " + " ".join(message_parts))
... 
ERROR: This script does not work on Python 2.7 The minimum supported Python version is 3.7. Please use https://bootstrap.pypa.io/pip/2.7/get-pip.py instead.
>>> 

どうやら、そうみたいです。

なので、
  ・現在使用しているpythonのバージョンに対応したpipをインストールする
  ・現在使用しているpythonをget-pip.pyに書かれたバージョン以上にして、最新のpipをインストールする

のどちらかの対応を取る必要があります。今回は、pythonのバージョンアップも兼ねて、後者の方法を試してみます。
前者の方法については下記の記事をご参照ください。

導入手順

1.pythonのバージョン確認
2.事前準備(pyenvをインストール)
3.事前準備(シェルにpyenvの環境変数を通す)
4.事前準備(3系のpythonのインストール)
5.pipのインストール
6.AWS CLIのインストール


1.pythonのバージョン確認
$ python -V
Python 2.7.16

Macで標準搭載されているものは2系なので、3系のインストールが必要
以下手順2〜4で3系のインストールを行う

2.事前準備(pyenvをインストール)

Homebrewを用いてpyenvをインストール

$ brew install pyenv
3.事前準備(シェルにpyenvの環境変数を通す)

現在利用しているシェルを確認

$ echo $SHELL
/bin/bash

・利用しているシェルがbashなら「~/.bash_profile」に環境変数の設定を記載
・利用しているシェルがzshなら「~/.zshrc」に環境変数の設定を記載
⇛今回利用しているシェルはbashなので.bash_profileに環境変数を記載

.bash_profile
# 2022/08/20 Pythonインストールのためpyenvをインストール。それに伴い環境変数設定
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
4.事前準備(3系のpythonのインストール)

以下コマンドを実行

pythonインストール.sh
# インストール可能なpythonのバージョン確認
pyenv install --list

# pyenvのインストール
pyenv install 3.10.4

# pyenvのバージョン確認
pyenv versions

# pyenvコマンドで今使用しているpythonのバージョンを変更する
pyenv global 3.10.4

# 今使用しているpythonのバージョン確認
python --version
5.pipのインストール

AWS CLIインストールに必要なpythonパッケージをインストール

# pipをインストール
sudo python get-pip.py

# versionを確認
pip --version
6.AWS CLIのインストール
# AWS CLI をインストール
sudo pip install awscli

正常にインストールされたことを確認

$ aws --version
aws-cli/1.25.56 Python/3.10.4 Darwin/19.6.0 botocore/1.27.55

以上

参考にしたサイト
■.bashrcと.bash_profileの違い
https://techracho.bpsinc.jp/hachi8833/2021_07_08/66396
■3系のpythonのインストール
https://prog-8.com/docs/python-env
■get-pipを使ったpipインストールでエラーが発生
https://qiita.com/hsawaji/items/5ec5533e95a42bbfa4b5
■pyenv,pipの違い
https://oversleptabit.com/archives/2195

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0