Pythonの導入

CentOSではインストール直後からPyhtonが利用できるのでバージョンを確認。


$ python -V
Python 2.7.5

2系なので、yumコマンドで3系の導入を試みます。


$ yum info python3
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
利用可能なパッケージ
名前                : python3
アーキテクチャー    : i686
バージョン          : 3.6.8
リリース            : 18.el7
容量                : 70 k
リポジトリー        : updates/7/x86_64
要約                : Interpreter of the Python programming language
URL                 : https://www.python.org/
ライセンス          : Python
説明                : Python is an accessible, high-level, dynamically typed,
                    : interpreted programming language, designed with an
                    : emphasis on code readability. It includes an extensive
                    : standard library, and has a vast ecosystem of third-party
                    : libraries.
                    :
                    : The python3 package provides the "python3" executable: the
                    : reference interpreter for the Python language, version 3.
                    : The majority of its standard library is provided in the
                    : python3-libs package, which should be installed
                    : automatically along with python3. The remaining parts of
                    : the Python standard library are broken out into the
                    : python3-tkinter and python3-test packages, which may need
                    : to be installed separately.
                    :
                    : Documentation for Python is provided in the python3-docs
                    : package.
                    :
                    : Packages containing additional libraries for Python are
                    : generally named with the "python3-" prefix.

名前                : python3
アーキテクチャー    : x86_64
バージョン          : 3.6.8
リリース            : 18.el7
容量                : 70 k
リポジトリー        : updates/7/x86_64
要約                : Interpreter of the Python programming language
URL                 : https://www.python.org/
ライセンス          : Python
説明                : Python is an accessible, high-level, dynamically typed,
                    : interpreted programming language, designed with an
                    : emphasis on code readability. It includes an extensive
                    : standard library, and has a vast ecosystem of third-party
                    : libraries.
                    :
                    : The python3 package provides the "python3" executable: the
                    : reference interpreter for the Python language, version 3.
                    : The majority of its standard library is provided in the
                    : python3-libs package, which should be installed
                    : automatically along with python3. The remaining parts of
                    : the Python standard library are broken out into the
                    : python3-tkinter and python3-test packages, which may need
                    : to be installed separately.
                    :
                    : Documentation for Python is provided in the python3-docs
                    : package.
                    :
                    : Packages containing additional libraries for Python are
                    : generally named with the "python3-" prefix.

ということで、yumコマンドで導入すると3.6.8ということで要求を満たせません。 そこで、Software Collection(SCL)を利用してCentOS7にPython 3.8をインストールすることにします。

まずは、SCLが利用できるようにします。


$ sudo yum install -y centos-release-scl

Python 3.8をインストールします。


$ sudo yum install -y rh-python38 which

インストールパッケージはパスが通っていないので、環境変数を読み込んで利用します。


$ sudo scl enable rh-python38 bash

バージョンの確認をします。 無事にPython 3.8が導入できました。


# python -V
Python 3.8.11

インストール場所の確認をします。


# which python
/opt/rh/rh-python38/root/usr/bin/python

以下のように設定することで、ログイン時に自動的に有効にできます。


$ sudo vi /etc/profile.d/python38.sh
# 以下の内容で新規作成
source /opt/rh/rh-python38/enable
export X_SCLS="`scl enable rh-python38 'echo $X_SCLS'`"

再ログイン後、バージョンを再度確認してみます。


$ python -V
Python 3.8.11