centos8 python3.9をインストールして実行する

centos8 python3.9をインストールして実行する

centos7でpython3.9をインストールするまでの手順を記述してます。CentOSのバージョンはStream release 8となります。

環境

  • OS CentOS Stream release 8
  • python 3.9.2

事前準備

必要なツールをインストールしておきます。

アップデートしておきます。

sudo dnf -y update

ツールをインストールします。

sudo dnf groupinstall "Development Tools" -y
sudo dnf install openssl-devel libffi-devel bzip2-devel -y

Python 3.9ダウンロード

こちらのサイトで最新バージョンを確認してダウンロードします。

現時点では3.9.2が最新だったので、3.9.2をダウンロードします。

sudo wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz

※wgetがなければインストールしておきます。

sudo dnf install wget -y

python3.9インストール

ダウンロードが完了したのでインストールを開始します。

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

tar xvf Python-3.9.2.tgz

解凍したディレクトリに移動します。

cd Python-3.9*/

設定を行います。

./configure --enable-optimizations

checking for GCC >= 4.7 __atomic builtins... yes
checking for ensurepip... upgrade
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... yes
checking for library containing shm_open... -lrt
checking for sys/mman.h... (cached) yes
checking for shm_open... yes
checking for shm_unlink... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... yes
checking for --with-ssl-default-suites... python
checking for --with-builtin-hashlib-hashes... md5,sha1,sha256,sha512,sha3,blake2
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile

ビルドを行います。

sudo make altinstall

バージョンを確認します。

python3.9 --version

<出力結果>
Python 3.9.2

パッケージ管理ツールであるpipのバージョンも確認します。

pip3.9 --version

<出力結果>
pip 20.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Hello Worldしてみる

「hello.py」という名前で、以下のコードを記述します。

vi hello.py

<編集>
print("Hello World")

実行します。

python3.9 hello.py

<出力結果>
Hello World