自分用。
Redhat9にpyenv環境を構築します。
環境確認
rpm -qa |grep release
アップデート後ですが、以下のバージョンでpyenvをインストールします。
[root@localhost ~]# rpm -qa | grep release
redhat-release-eula-9.8-1.0.el9.x86_64
redhat-release-9.8-1.0.el9.x86_64
インストール(1)
OS側で最低限必要なツールをインストールする
dnf update -y update
dnf install git
pyenvのインストール
pyenvはシステムワイドでインストールはされません。pyenvを使う予定のあるユーザでインストールを実行する必要があります。
su - saito
curl -fsSL https://pyenv.run | bash
.bashrcの編集
前途のコマンドを実行すると、以下の出力がされます。指示通りユーザプロファイルに追記します。
私は.bash_profileへ記載しました。
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
# Restart your shell for the changes to take effect.
# Load pyenv-virtualenv automatically by adding
# the following to ~/.bashrc:
eval "$(pyenv virtualenv-init -)"
ユーザプロファイルの読み込み
人それぞれだと思いますが、私はシェルを開きなおします。
bash -l
pyenvコマンドのパスが通っていればOKです。
[saitou@localhost ~]$ pyenv --version
pyenv 2.7.3
インストール(2)
ここからの手順は(2)として分けました。
開発ツールのインストール
pyenvはtarボールをダウンロードしてビルドしてくれるので、コンパイラなどが必要です。
dnf -y groupinstall "Development Tools"
dnf -y install readline-devel zlib-devel bzip2-devel sqlite-devel openssl-devel libffi-devel
pyenv のテスト
前途したように、一般ユーザで実行します。
pyenv update
まず、pyenvの更新をしてみます。以前はupdateできるようにプラグインの追加が必要でしたが、便利になってます。
pyenv update
Pythonディストリビューションのインストール
細かい使い方はいったんおいておいて、適当なディストリビューションがエラーなくインストールできるか確認します。
[saitou@localhost ~]$ which python
/usr/bin/python
[saitou@localhost ~]$ python -V # OSデフォルトのPythonを確認
Python 3.9.25
[saitou@localhost ~]$ pyenv install 3.13.14
Downloading Python-3.13.14.tar.xz...
-> https://www.python.org/ftp/python/3.13.14/Python-3.13.14.tar.xz
Installing Python-3.13.14...
Installed Python-3.13.14 to /home/saitou/.pyenv/versions/3.13.14
[saitou@localhost ~]$
pyenv installで失敗するのは、たいていの場合XXX-develが不足しているのが原因かなと思います。
dnf search や dnf providesを駆使して必要なパッケージを探して追加してください。Debian系、Redhat系ともに、OS標準レポジトリのパッケージだけで解決できます。OS以外が必要な場合があったらごめん。
外部モジュールのインストール(pandas、matplot)
ねんのため、外部モジュールも追加できるか確認します。人それぞれかと思いますが、pandas、matpotがインストールできるか確認します。pandasやmatplotのような依存関係が多い外部モジュールが、エラーなくインストールできれば問題ないのかなと思います。
[saitou@localhost ~]$ python -V
Python 3.9.25
[saitou@localhost ~]$ mkdir -p work/test && cd work/test
[saitou@localhost test]$ pyenv virtualenv 3.13.14 test-3.13.14
[saitou@localhost test]$ pyenv local test-3.13.14
(test-3.13.14) [saitou@localhost test]$
(test-3.13.14) [saitou@localhost test]$ python -V
Python 3.13.14
(test-3.13.14) [saitou@localhost test]$ pip list
\Package Version
------- -------
pip 26.1.2
(test-3.13.14) [saitou@localhost test]$ pip install pandas
・・・(略)
(test-3.13.14) [saitou@localhost test]$ pip install matplot
・・・(略)
(test-3.13.14) [saitou@localhost test]$ pip list
Package Version
--------------------- -----------
・・・(略)
matplotlib 3.11.0
・・・(略)
pandas 3.0.3
・・・(略)
(test-3.13.14) [saitou@localhost test]$
pyenv install や pip install でエラーになる原因は、たいていの場合パッケージが足りない時です。エラーをよく見て、OS側で足りないライブラリを追加インストールします。
今回はRHELなので、Ubutuでpyenvを導入したい人はちょっと異なりますが、Ubuntuでもアプローチは同じです。
使い方
pyenvの古いバージョンと変わらないはずです。
インストール可能なディストリビューション、バージョンのリスト
pyenv install -l
いまどきPython2系が使えるのがイカしてます。
Pythonディストリビューションのインストール
上記で確認したディストリビューションをインストールします。
pyenv install 3.13.14
インストールしたディストリビューションのリスト
「* 」がついているところがいま使用できるPythonで、systemはOSデフォルトのPythonになります。
基本的に、OSデフォルトの実行環境はいじりたくないので、次の手順でデフォルトで使用するディストリビューションの変更を行います。
[saitou@localhost ~]$ pyenv versions
* system (set by /home/saitou/.pyenv/version)
3.13.14
デフォルトで使用するディストリビューションの変更
システム側のPythonに変更を加えないようにします。
pyenv global 3.13.14
[saitou@localhost ~]$ pyenv global 3.13.14
[saitou@localhost ~]$ python -V
Python 3.13.14
[saitou@localhost ~]$ pyenv versions
system
* 3.13.14 (set by /home/saitou/.pyenv/version)
pyenv-virtualenv
外部モジュールを変えたい場合が多々あります。
pyenv virtualenv <複製元> <任意の複製名>
参考