CentOS6.7 への Python3 インストール手順メモ

#はじめに

諸事情により、古い環境 (CentOS6.7) で Python を動かす必要がありました。
Python2系は標準入っているけど、Pythonは 3系で書かれていたので Python3をインストールすることにしました。
色んな所でつまずいてしまったので、メモとして手順を残しておきます。


#インストール手順
標準では 2系がインストールされています。

```
$ python -V
Python 2.6.6
```

こちらを参考に yum でインストールしてみようとしたところ・・・

https://qiita.com/f-akazawa/items/5ec741f8a5cca999d7b3

```
# yum install -y https://centos6.iuscommunity.org/ius-release.rpm

Loaded plugins: fastestmirror
Setting up Install Process
Cannot open: https://centos6.iuscommunity.org/ius-release.rpm. Skipping.
Error: Nothing to do
```

IUS のリンクがなくなっているみたいでエラーに。（泣）


こちらを参考に、別のリンクから rpm を参照することに。

https://qiita.com/seal_qiita/items/981a9284ecd44e54af96

```
# yum install https://repo.ius.io/ius-release-el6.rpm

Loaded plugins: fastestmirror
Setting up Install Process
ius-release-el6.rpm                                                                                                                                                            | 8.2 kB     00:00
Examining /var/tmp/yum-root-aiX6j7/ius-release-el6.rpm: ius-release-2-1.el6.ius.noarch
Marking /var/tmp/yum-root-aiX6j7/ius-release-el6.rpm to be installed
Determining fastest mirrors
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
 Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/cache/yum/x86_64/6/base/mirrorlist.txt
Error: Cannot find a valid baseurl for repo: base
```

CentOS6のサポートが切れているせいかリポジトリが参照できなくなっている。（泣）
下記を参考にリポジトリを変更してみる。

https://qiita.com/imunew/items/3810a41960f40db85c94

```
# sed -i -e "s/^mirrorlist=http:\/\/mirrorlist.centos.org/#mirrorlist=http:\/\/mirrorlist.centos.org/g" /etc/yum.repos.d/CentOS-Base.repo
# sed -i -e "s/^#baseurl=http:\/\/mirror.centos.org/baseurl=http:\/\/vault.centos.org/g" /etc/yum.repos.d/CentOS-Base.repo
```

再度インストール

```
# yum install https://repo.ius.io/ius-release-el6.rpm
Loaded plugins: fastestmirror
Setting up Install Process
ius-release-el6.rpm                                                                                                                                                            | 8.2 kB     00:00
Examining /var/tmp/yum-root-aiX6j7/ius-release-el6.rpm: ius-release-2-1.el6.ius.noarch
Marking /var/tmp/yum-root-aiX6j7/ius-release-el6.rpm to be installed
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ius-release.noarch 0:2-1.el6.ius will be installed
--> Processing Dependency: epel-release = 6 for package: ius-release-2-1.el6.ius.noarch
--> Running transaction check
---> Package epel-release.noarch 0:6-8 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================================================================
 Package                                         Arch                                      Version                                          Repository                                           Size
======================================================================================================================================================================================================
Installing:
 ius-release                                     noarch                                    2-1.el6.ius                                      /ius-release-el6                                    4.5 k
Installing for dependencies:
 epel-release                                    noarch                                    6-8                                              extras                                               14 k

Transaction Summary
======================================================================================================================================================================================================
Install       2 Package(s)

Total size: 19 k
Total download size: 14 k
Installed size: 26 k
Is this ok [y/N]: y
Downloading Packages:
epel-release-6-8.noarch.rpm                                                                                                                                                    |  14 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : epel-release-6-8.noarch                                                                                                                                                            1/2
  Installing : ius-release-2-1.el6.ius.noarch                                                                                                                                                     2/2
  Verifying  : ius-release-2-1.el6.ius.noarch                                                                                                                                                     1/2
  Verifying  : epel-release-6-8.noarch                                                                                                                                                            2/2

Installed:
  ius-release.noarch 0:2-1.el6.ius

Dependency Installed:
  epel-release.noarch 0:6-8

Complete!
```

インストールできました！
yum で python3 を検索してみる。

```
# yum search python34

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
```

エラーが出てる（泣）
SSL3.0を使っているのが原因みたいなので、下記記事を参考に http へ変更

https://qiita.com/maruware/items/eb659266a45021cf486c

```
# sudo sed -i "s/mirrorlist=https/mirrorlist=http/" /etc/yum.repos.d/epel.repo
```

そして、いよいよ Python3 をインストール
（インストールできるVersionは 3.4 だけみたい・・）

```
# yum install python34u python34u-libs python34u-devel python34u-pip
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * epel: d2lzkl7pfhq30w.cloudfront.net
No package python34u-pip available.
Resolving Dependencies
--> Running transaction check
---> Package python34.x86_64 0:3.4.10-4.el6 will be installed
---> Package python34-devel.x86_64 0:3.4.10-4.el6 will be installed
--> Processing Dependency: python-rpm-macros for package: python34-devel-3.4.10-4.el6.x86_64
--> Processing Dependency: python3-rpm-macros for package: python34-devel-3.4.10-4.el6.x86_64
---> Package python34-libs.x86_64 0:3.4.10-4.el6 will be installed
--> Running transaction check
---> Package python-rpm-macros.noarch 0:3-15.el6 will be installed
--> Processing Dependency: python-srpm-macros for package: python-rpm-macros-3-15.el6.noarch
---> Package python3-rpm-macros.noarch 0:3-15.el6 will be installed
--> Running transaction check
---> Package python-srpm-macros.noarch 0:3-15.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================================================================
 Package                                                Arch                                       Version                                             Repository                                Size
======================================================================================================================================================================================================
Installing:
 python34                                               x86_64                                     3.4.10-4.el6                                        epel                                      51 k
 python34-devel                                         x86_64                                     3.4.10-4.el6                                        epel                                     187 k
 python34-libs                                          x86_64                                     3.4.10-4.el6                                        epel                                     8.3 M
Installing for dependencies:
 python-rpm-macros                                      noarch                                     3-15.el6                                            epel                                     6.7 k
 python-srpm-macros                                     noarch                                     3-15.el6                                            epel                                     5.9 k
 python3-rpm-macros                                     noarch                                     3-15.el6                                            epel                                     5.6 k

Transaction Summary
======================================================================================================================================================================================================
Install       6 Package(s)

Total download size: 8.6 M
Installed size: 30 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): python-rpm-macros-3-15.el6.noarch.rpm                                                                                                                                   | 6.7 kB     00:00
(2/6): python-srpm-macros-3-15.el6.noarch.rpm                                                                                                                                  | 5.9 kB     00:00
(3/6): python3-rpm-macros-3-15.el6.noarch.rpm                                                                                                                                  | 5.6 kB     00:00
(4/6): python34-3.4.10-4.el6.x86_64.rpm                                                                                                                                        |  51 kB     00:00
(5/6): python34-devel-3.4.10-4.el6.x86_64.rpm                                                                                                                                  | 187 kB     00:00
(6/6): python34-libs-3.4.10-4.el6.x86_64.rpm                                                                                                                                   | 8.3 MB     00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                 1.4 MB/s | 8.6 MB     00:06
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Importing GPG key 0x0608B895:
 Userid : EPEL (6) <epel@fedoraproject.org>
 Package: epel-release-6-8.noarch (@extras)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : python34-3.4.10-4.el6.x86_64                                                                                                                                                       1/6
  Installing : python34-libs-3.4.10-4.el6.x86_64                                                                                                                                                  2/6
  Installing : python3-rpm-macros-3-15.el6.noarch                                                                                                                                                 3/6
  Installing : python-srpm-macros-3-15.el6.noarch                                                                                                                                                 4/6
  Installing : python-rpm-macros-3-15.el6.noarch                                                                                                                                                  5/6
  Installing : python34-devel-3.4.10-4.el6.x86_64                                                                                                                                                 6/6
  Verifying  : python-srpm-macros-3-15.el6.noarch                                                                                                                                                 1/6
  Verifying  : python34-devel-3.4.10-4.el6.x86_64                                                                                                                                                 2/6
  Verifying  : python34-libs-3.4.10-4.el6.x86_64                                                                                                                                                  3/6
  Verifying  : python34-3.4.10-4.el6.x86_64                                                                                                                                                       4/6
  Verifying  : python3-rpm-macros-3-15.el6.noarch                                                                                                                                                 5/6
  Verifying  : python-rpm-macros-3-15.el6.noarch                                                                                                                                                  6/6

Installed:
  python34.x86_64 0:3.4.10-4.el6                                python34-devel.x86_64 0:3.4.10-4.el6                                python34-libs.x86_64 0:3.4.10-4.el6

Dependency Installed:
  python-rpm-macros.noarch 0:3-15.el6                              python-srpm-macros.noarch 0:3-15.el6                              python3-rpm-macros.noarch 0:3-15.el6

Complete!
```

Version を確認してみると

```
# python -V
Python 2.6.6
# python3 -V
Python 3.4.10
```

現状はそのままに、Python3.4 がインストールできました

Python モジュール で requests を import していたので、pip でインストールしようとすると。

```
# python3 -m pip install requests
/usr/bin/python3: No module named pip
```

pip 入ってなかった・・？
pip インストール

```
# yum install --enablerepo=epel python34-pip
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * epel: d2lzkl7pfhq30w.cloudfront.net
Resolving Dependencies
--> Running transaction check
---> Package python34-pip.noarch 0:7.1.0-2.el6 will be installed
--> Processing Dependency: python34-setuptools for package: python34-pip-7.1.0-2.el6.noarch
--> Running transaction check
---> Package python34-setuptools.noarch 0:39.2.0-2.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================================================================
 Package                                                 Arch                                       Version                                            Repository                                Size
======================================================================================================================================================================================================
Installing:
 python34-pip                                            noarch                                     7.1.0-2.el6                                        epel                                     1.6 M
Installing for dependencies:
 python34-setuptools                                     noarch                                     39.2.0-2.el6                                       epel                                     664 k

Transaction Summary
======================================================================================================================================================================================================
Install       2 Package(s)

Total download size: 2.2 M
Installed size: 9.1 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): python34-pip-7.1.0-2.el6.noarch.rpm                                                                                                                                     | 1.6 MB     00:00
(2/2): python34-setuptools-39.2.0-2.el6.noarch.rpm                                                                                                                             | 664 kB     00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                 408 kB/s | 2.2 MB     00:05
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : python34-setuptools-39.2.0-2.el6.noarch                                                                                                                                            1/2
  Installing : python34-pip-7.1.0-2.el6.noarch                                                                                                                                                    2/2
  Verifying  : python34-pip-7.1.0-2.el6.noarch                                                                                                                                                    1/2
  Verifying  : python34-setuptools-39.2.0-2.el6.noarch                                                                                                                                            2/2

Installed:
  python34-pip.noarch 0:7.1.0-2.el6

Dependency Installed:
  python34-setuptools.noarch 0:39.2.0-2.el6

Complete!
```

pip の version 確認

```
# pip3 --version
pip 7.1.0 from /usr/lib/python3.4/site-packages (python 3.4)
```

pip3 インストールできました。続いて pip3 でインストール。

```
# pip3 install requests
You are using pip version 7.1.0, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting requests
  Using cached https://files.pythonhosted.org/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl
Collecting idna<3,>=2.5 (from requests)
  Using cached https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
  Using cached https://files.pythonhosted.org/packages/5e/a0/5f06e1e1d463903cf0c0eebeb751791119ed7a4b3737fdc9a77f1cdfb51f/certifi-2020.12.5-py2.py3-none-any.whl
Collecting chardet<5,>=3.0.2 (from requests)
  Using cached https://files.pythonhosted.org/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl
Collecting urllib3<1.27,>=1.21.1 (from requests)
  Using cached https://files.pythonhosted.org/packages/09/c6/d3e3abe5b4f4f16cf0dfc9240ab7ce10c2baa0e268989a4e3ec19e90c84e/urllib3-1.26.4-py2.py3-none-any.whl
Installing collected packages: idna, certifi, chardet, urllib3, requests
Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.4
```

pip3 で requests もインストールできました！

#おわりに
思ったより長い道のりでした。。
一時的かつ限定的とはいえ、古いOSはあまり触りたくないですね。。