Red Hat Enterprise Linux 8にPythonをインストールする

Red Hat Enterprise Linux 8ではPythonがインストールされていません。$ pythonがcommand not foundとなって頭を抱えてしまった人向けに、RHEL 8でのPythonのインストール方法を紹介します。
2019.05.09

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

Red Hat Enterprise Linux 8(以下 RHEL 8) ではデフォルトで Python がインストールされていないことがあります。 AWS EC2 の Quick Start で提供されている AMI 「Red Hat Enterprise Linux 8 (HVM), SSD Volume Type」がまさにそのような例です。

$ pythoncommand not foundとなって頭を抱えてしまった人向けに、RHEL 8 での Python のインストール方法を紹介します。

3行まとめ

  • RHEL 8 では /usr/bin/{python,python2,python3} が存在しないことがある
  • Pythonをインストールするには $ sudo yum install @python36 または $ sudo yum install @python27 を実行
  • $ /usr/bin/python を特定の Python に向けるには $ sudo alternatives --config python を実行

検証環境

AWS EC2 の RHEL Quick Start で提供されている、次の AMI を利用して検証しました。

  • RHEL-8.0.0_HVM-20190426-x86_64-1-Hourly2-GP2 (ami-02fc41eea185ef7b2)

システム向けとユーザー向けで Python を分離

RHEL 8 では システムが利用する Python とユーザーが利用する Python が分離されました。

これにより

  • システムとユーザーが異なる Python バージョンを利用できる
  • システムが利用するライブラリとユーザーが利用するライブラリが競合しない

といったメリットを享受できるようになります。

システム向け Python は /usr/libexec/platform-python にインストールされており、 実際 YUM の shebang はこの Python を指しています。

$ head -n 1 /usr/bin/yum
#!/usr/libexec/platform-python

この Python のバージョンは 3.6.8 です。

ユーザー向け Python はインストールされていない

ユーザー向けにはどの Python が インストールされているのか気になるところですが、ベースイメージにより、どのバージョンもインストールされていないことがあります。

このようなケースでは、 python/python2/python2.7/python3 といったコマンドはことごとく command not found になるため、ユーザーが Python をバージョン指定してインストールする必要があります。

$ python
-bash: python: command not found
$ python2
-bash: python2: command not found
$ python2.7
-bash: python2.7: command not found
$ python3
-bash: python3: command not found

RHEL 8 の GA と前後して公開された、公式ブログから引用します。

TL;DR Of course we have Python! You just need to specify whether you want Python 3 or 2 as we didn’t want to set a default. Give yum install python3 and/or yum install python2 a try. Or, if you want to see what packages we recommend, use yum install @python36 or yum install @python27. Read on for why.

What, no Python in Red Hat Enterprise Linux 8? - Red Hat Developer Blog

YUM で Python をインストール

サクッとインストール

なにか Python がインストールされていれば OK な場合、2系/3系を指定して yum インストールしてください。

$ sudo yum install python2
$ sudo yum install python3

Application Streams でパッケージをインストール

RHEL 8 のお作法に則って Python をインストールしたい場合、今後主流となる Application Streams を利用しましょう。アプリケーション単発のインストールではなく、周辺ツールを含めたインストールやバージョン指定でのアプリケーション群のインストールが可能になります。

Introducing Application Streams in RHEL 8 - Red Hat Developer Blog

Amazon Linux 2 で言うところの Amazon Linux Extras ですね。

AppStream で提供されているストリーム一覧は $ sudo yum module list で確認可能です。

Python 系に限定して確認してみます。

$ sudo yum module list | grep ^python
Name             Stream      Profiles                   Summary
...
python27         2.7 [d]     common [d]                 Python programming language, version 2.7
python36         3.6 [d][e]  common [d] [i], build      Python programming language, version 3.6
...
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

$ sudo yum module info python36 --profile
Last metadata expiration check: 0:51:27 ago on Wed 08 May 2019 03:00:35 PM UTC.
Name   : python36:3.6:8000020190410133122:593c47b3:x86_64
common : python36
build  : python36
       : python36-devel
       : python36-rpm-macros

ストリーム python36 には、その下位カテゴリーとしてデフォルトの common と build の 2 つのプロフィール が提供されていることがわかります。

Application Streams を利用してインストールするには $ sudo yum install @AppStreamName または $ sudo yum install @AppStreamName/ProfileName とします。

$ sudo yum install @python36       # => デフォルトのプロフィールを利用
$ sudo yum install @python36/build # => build プロフィールを利用

実際に build プロフィールをインストールし、インストールされていることを確認します。

$ sudo yum install @python36/build
...
$ sudo yum module list python36
Last metadata expiration check: 0:56:46 ago on Wed 08 May 2019 08:23:22 PM UTC.
Red Hat Enterprise Linux 8 for x86_64 - AppStream from RHUI (RPMs)
Name                   Stream                   Profiles                            Summary
python36               3.6 [d][e]               common [d], build [i]               Python programming language, version 3.6

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

build [i] の [i] からインストールされていることがわかります。

ようやく $ pytho3python3.6 で Python3 を呼び出せるようになりました。

一方で、バージョンを指定しない $ pythoncommand not found のままです。次のステップでこの問題を解決します。

インストールした Python を /usr/bin/python に向ける

互換性などのために、#!/usr/bin/python のようにバージョンを指定せずに Python を呼び出した場合、先程インストールした Python 3.6 が呼び出されるようにしてみます。

設定は alternatives コマンドで行います。 詳細は $ man unversioned-python を確認ください。

$ sudo alternatives --config python # インタラクティヴに設定

There are 3 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/libexec/no-python
   2           /usr/bin/python2
   3           /usr/bin/python3

Enter to keep the current selection[+], or type selection number: 3    # 入力

$ ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 36 May  8 21:11 /usr/bin/python -> /etc/alternatives/unversioned-python
$ python -V
Python 3.6.8

PEP 394 -- The "python" Command on Unix-Like Systems によると、バージョン指定のない python は python2 と同じダーゲットとするよう記載されていますが、この PEP が時代にそぐわなくなってきていることは Python コアディベロッパーも重々承知しています。

一般ユーザーは現実に合わせて柔軟に対応すればよいかと思います。

Python 2.7 の EOL

Python 2系最後の 2.7 は、機能追加はとうの昔に終了しており、現在はほそぼそとセキュリティ・バグ系のメンテナンスリリースが行われているだけです。そのメンテナンスリリースも、2020年1月を最後にリリースされなくなる予定です。

2019/05/09 時点 の Red Hat Software Collections Product Life Cycle では、Python 2.7 Life Extension の Retirement Date は 2020年4月に設定されています。

速やかに Python 3 系に移行しましょう。

きっかけ

RHEL 8 が 2019/05/08 にリリースされ、AWS EC2 コンソールでは最新の 8 がデフォルトになりました。 バージョンをよく確認せずに RHEL を起動し、結果として RHEL 8 には Python がインストールされていないことを知りました。

Python 行方不明問題含め、RHEL 8 の大きな変更点は公式のリリースブログにまとめられています。ぜひ、ご一読ください。

Red Hat Enterprise Linux 8 now generally available - Red Hat Developer Blog

Python に限定しても 3 本もあります。

それでは

参考