3
1

More than 3 years have passed since last update.

EC2 python3 install & alias

Last updated at Posted at 2019-10-11
1 / 15
前回の話

AWSで基盤お試し構築~RDS~

やりたいこと

  • python3.6をEC2にインストールする。
  • pythonコマンドでpython3.6が動くようにする
    • デフォルトは2系のpythonが動く。

前提

OS:AmazonLinux2
→python36が見つけられないので、リポジトリを追加する


epelリポジトリの追加

「ネタを探しに行く先が増やせる」くらいに思っていてくれればいい。

  • コマンド
    amazon-linux-extras install epel

[root@ip-10-0-1-99 yum.repos.d]# amazon-linux-extras install epel
Installing epel-release
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-epel
10 metadata files removed
4 sqlite files removed
0 metadata files removed
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                                                    | 2.4 kB  00:00:00
amzn2extra-docker                                                                                                                             | 1.3 kB  00:00:00
amzn2extra-epel                                                                                                                               | 1.3 kB  00:00:00
(1/5): amzn2-core/2/x86_64/group_gz                                                                                                           | 2.4 kB  00:00:00
(2/5): amzn2-core/2/x86_64/updateinfo                                                                                                         | 122 kB  00:00:00
(3/5): amzn2extra-epel/2/x86_64/primary_db                                                                                                    | 1.8 kB  00:00:00
(4/5): amzn2extra-docker/2/x86_64/primary_db                                                                                                  |  56 kB  00:00:00
(5/5): amzn2-core/2/x86_64/primary_db                                                                                                         |  31 MB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================
 Package                                   Arch                                Version                            Repository                                    Size
=====================================================================================================================================================================
Installing:
 epel-release                              noarch                              7-11                               amzn2extra-epel                               15 k

Transaction Summary
=====================================================================================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y

Is this ok [y/d/N]: yでインストールされる。


python3.6をインストール

  • コマンド
    yum install python36

[root@ip-10-0-1-99 ~]# yum install python36
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                                                                                     | 2.1 kB  00:00:00
amzn-updates                                                                                                                                  | 2.5 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package python36.x86_64 0:3.6.8-1.13.amzn1 will be installed
--> Processing Dependency: python36-libs(x86-64) = 3.6.8-1.13.amzn1 for package: python36-3.6.8-1.13.amzn1.x86_64
--> Processing Dependency: python36-setuptools for package: python36-3.6.8-1.13.amzn1.x86_64
--> Processing Dependency: python36-pip for package: python36-3.6.8-1.13.amzn1.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python36-3.6.8-1.13.amzn1.x86_64
--> Running transaction check
---> Package python36-libs.x86_64 0:3.6.8-1.13.amzn1 will be installed
---> Package python36-pip.noarch 0:9.0.3-1.26.amzn1 will be installed
---> Package python36-setuptools.noarch 0:36.2.7-1.33.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================
 Package                                      Arch                            Version                                    Repository                             Size
=====================================================================================================================================================================
Installing:
 python36                                     x86_64                          3.6.8-1.13.amzn1                           amzn-updates                           65 k
Installing for dependencies:
 python36-libs                                x86_64                          3.6.8-1.13.amzn1                           amzn-updates                           12 M
 python36-pip                                 noarch                          9.0.3-1.26.amzn1                           amzn-updates                          2.7 M
 python36-setuptools                          noarch                          36.2.7-1.33.amzn1                          amzn-main                             831 k

Transaction Summary
=====================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 15 M
Installed size: 48 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): python36-3.6.8-1.13.amzn1.x86_64.rpm                                                                                                   |  65 kB  00:00:00
(2/4): python36-setuptools-36.2.7-1.33.amzn1.noarch.rpm                                                                                       | 831 kB  00:00:00
(3/4): python36-pip-9.0.3-1.26.amzn1.noarch.rpm                                                                                               | 2.7 MB  00:00:00
(4/4): python36-libs-3.6.8-1.13.amzn1.x86_64.rpm                                                                                              |  12 MB  00:00:01
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                8.2 MB/s |  15 MB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python36-libs-3.6.8-1.13.amzn1.x86_64                                                                                                             1/4
  Installing : python36-3.6.8-1.13.amzn1.x86_64                                                                                                                  2/4
  Installing : python36-setuptools-36.2.7-1.33.amzn1.noarch                                                                                                      3/4
  Installing : python36-pip-9.0.3-1.26.amzn1.noarch                                                                                                              4/4
  Verifying  : python36-setuptools-36.2.7-1.33.amzn1.noarch                                                                                                      1/4
  Verifying  : python36-libs-3.6.8-1.13.amzn1.x86_64                                                                                                             2/4
  Verifying  : python36-3.6.8-1.13.amzn1.x86_64                                                                                                                  3/4
  Verifying  : python36-pip-9.0.3-1.26.amzn1.noarch                                                                                                              4/4

Installed:
  python36.x86_64 0:3.6.8-1.13.amzn1

Dependency Installed:
  python36-libs.x86_64 0:3.6.8-1.13.amzn1             python36-pip.noarch 0:9.0.3-1.26.amzn1             python36-setuptools.noarch 0:36.2.7-1.33.amzn1

Complete!

普通にコンプリートするはず。


pythonコマンドのpythonが3系で動くようにする

まずは単純にpython --versionを叩いてみる

[root@ip-10-0-1-99 ~]# python --version
Python 2.7.16

→2系のpythonと言われた


続けて、何も考えずpython3 --versionを叩いてみる

[root@ip-10-0-1-99 ~]# python3 --version
Python 3.6.8

→3系のpythonと言われた


3系のpythonはpython3で動いてくれる。
→ということで、pythonpython3を紐づけてあげればいい。


aliasで紐づける

何も考えずこのコマンドを叩いてみる
alias = python="python3"


もう一度python --versionを叩いてみる

[root@ip-10-0-1-99 ~]# alias python="python3"
[root@ip-10-0-1-99 ~]#
[root@ip-10-0-1-99 ~]# python --version
Python 3.6.8

→3系のpythonとして反応してくれた
→つまり、aliasで紐づけると何も考えずpythonコマンドで3系pythonが使える。


aliasを.bashrcに書き込む

aliasはいいけど、欠点はログアウトしたら設定が消えてしまう。
ということで、.bashrcに書き込んで、ログインするたびにaliasの設定を自動でやってもらう。


サクッと下記コマンドを叩く
vim ~/.bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias python="python3" 

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

alias python="python3"を追記して保存する。

コンソールで下記を叩く
. .bashrc

もう1回python --versionを実行する

[root@ip-10-0-1-99 ~]# . .bashrc
[root@ip-10-0-1-99 ~]#
[root@ip-10-0-1-99 ~]# python --version
Python 3.6.8

無事に3系pythonで動いてくれた。

以上。

3
1
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
3
1