Ubuntu24.04へのPythonの複数バージョンのインストールと切り替え方法についてのメモです。
前提条件
- Ubuntu24.04(WSL2)
インストールポリシーの確認
- pythonのインストールにaptは使わない
- 一般ユーザー権限で管理
- インストールと切り替えにはpenvを使う
- 「~/.pyenv/versions/」内に各バージョンをインストールする
ビルドに必要なライブラリのインストール
python本体のインストールにはaptを使いませんが、
pythonのビルドに必要なライブラリをaptでインストールしていきます。
OS毎のインストールの仕方はpyenvのリポジトリに英語の説明があります。
Ubuntuの場合は次のコマンドを実行します。
▼apt update
sudo apt update
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_01.png)
▼ライブラリをインストール
sudo apt install build-essential libbz2-dev libdb-dev \
libreadline-dev libffi-dev libgdbm-dev liblzma-dev \
libncursesw5-dev libsqlite3-dev libssl-dev \
zlib1g-dev uuid-dev tk-dev
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_02.png)
pyenvインストール
Pythonのバージョン管理ツールである「pyenv」をインストールしていきます。
複数のインストール方法がありますが、
今回はGithubリポジトリを「~/.pyenv」にcloneします。
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_07.png)
PATHを通すために「~/.bashrc」に追記します。
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
現在のシェルに反映します。
source ~/.bashrc
pyenvが実行できるかを確認するためにバージョンを表示しておきましょう。
pyenv --version
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_08.png)
インストール可能バージョンの一覧表示
次のコマンドで、インストール可能なバージョンがすべて表示されます。
pyenv install -l
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_09.png)
バージョン指定でPythonインストール
執筆時点での最新バージョン「3.12.4」をインストールしていきます。
pyenv install 3.12.4
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_10.png)
「3.11.9」と「3.10.14」もインストールしてみます。
pyenv install 3.11.9
pyenv install 3.10.14
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_11.png)
インストール済みのバージョン一覧を確認してみます。
pyenv versions
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_12.png)
バージョン切り替え
使用するバージョンの指定にはレベルが3段階あります。
- global ・・・ユーザーの環境全体で使用するバージョンの指定
- shell ・・・現在のシェルで使用するバージョンの指定
- local ・・・現在のディレクトリ配下で使用するバージョンの指定
▼ global のバージョン指定
pyenv global 3.12.4
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_13.png)
▼shell のバージョン指定
pyenv shell 3.11.9
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_14.png)
▼local のバージョン指定
※「pyenv shell」を実行したシェルでは、「pyenv shell」の設定が優先されるので、「pyenv local」の指定は有効になりません。
※「pyenv local」は新しいシェルのターミナルを開いて実行しましょう。
pyenv local 3.10.14
![](http://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_15.png)
local 指定の時は、そのディレクトリに「.python-version」というファイルが作成され、
その中にバージョン番号が記録されます。
「.python-version」を削除すると、local の指定を解除できます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_16.png)
バージョン指定でPythonアンインストール
「3.10.14」をアンインストールしてみます。
pyenv uninstall 3.10.14
![](https://macocci7.net/blog/wp-content/uploads/2024/06/python_ubuntu_python_install_17.png)
pyenvのアップデート
Githubリポジトリをcloneしたので、gitコマンドでpullすればOKです。
cd ~/.pyenv/
git pull
pyenvインストーラーでインストールした場合は
pyenv update
でいけます。
参考サイト
![](https://www.python.jp/static/pythonjp/images/logo_red_120.png)
コメント