1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CentOS8にオフラインで最新のpython3.X系をインストールする

Last updated at Posted at 2021-03-07

0.はじめに

RHEL8では /usr/bin/{python,python2,python3} が存在しないことがある
今回はインターネットに接続しない環境でpython3.x系をインストールする。
pythonとpipをインストールすることでAnsibleの勉強も進めていきたい。(←こちらはまた別の記事で)

$ python --version
-bash: python: command not found

1. 依存関係

6.必要なパッケージのインストール

コンパイルに必要

# yum -y install make gcc

Python、pipインストールに必要。

# yum -y install zlib-devel openssl-devel tk-devel libffi-devel

2.ソースコードのダウンロード

以下のサイトからpythonのソースコードをインストールする。
https://www.python.org/downloads/source/
※今回はPython-3.9.0.tar.xzをインストール

3.サーバに配置

ダウンロードしたファイルをサーバに配置

# ls -ltr
-rw-r--r--. 1 root root 18866140 11月 23 19:32 Python-3.9.0.tar.xz

4.解凍

ダウンロードしたファイルを解凍。

# tar Jxvf Python-3.9.0.tar.xz

5.解凍したフォルダに移動

以下コマンドで回答したフォルダに移動。

cd Python-3.9.0

6.makefileの作成

./configure --prefix=/usr/local/python390 --with-ensurepip

注意点1
実行前に、gccが入っていることを確認すること。

# gcc --version
gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gcc等のCコンパイラが含まれていない場合は以下のエラーが発生するので注意。

# ./configure --prefix=/usr/local/python390 --with-ensurepip
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.9... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/work/python/Python-3.9.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

注意点2
OpenSSLのバージョンについて、pip3.9を利用する際には、OpenSSL1.0.2以上のインストールが必要

7.make

# make

注意点
事前にmakeコマンドが利用できることを確認しておくこと

# make -v
GNU Make 4.2.1
このプログラムは x86_64-redhat-linux-gnu 用にビルドされました
Copyright (C) 1988-2016 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 以降 <http://gnu.org/licenses/gpl.html>
これはフリーソフトウェアです: 自由に変更および配布できます.
法律の許す限り、 無保証 です.

8.インストール

# make install
Successfully installed pip-20.2.3 setuptools-49.2.1

※make install の代わりに、make altinstallを利用することが推奨されている。
既存のpython、python3w置き換えるシンボリックリンクを作成しないため。
altinstallを利用することで、既存の環境を壊さず異なるバージョンのpythonをインストールすることができる。

9.シンボリックリンクの作成

python3.9で実行できるようにシンボリックリンクを作成

ln -s /usr/local/python390/bin/python3.9 /usr/bin/python3.9

pipも同様にpip3.9にシンボリックを作成しておく

# ln -s /usr/local/python390/bin/pip3.9 /usr/bin/pip3.9

10.確認

Python 3.9.0と表示されれば完了です。

# python3.9 --version
Python 3.9.0

pipについても確認します。

# pip3.9 --version
pip 20.2.3 from /usr/local/python390/lib/python3.9/site-packages/pip (python 3.9)
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?