はじめに
WSLには初めからpythonがインストールされていますが、デフォルトとしてPython3.10が入っています。
これを変更しようとしてpyenvを使ってみたのですが、なぜかうまくいきませんでした。
自分の知識がまだ不十分なせいだと思うのですが・・・
そこで別の方法で変更したのでその手順についてメモとして書き残しておきます。
自分の環境について
- Ubuntu 22.04.4
- Python3.10.12, Python3.11.9, Python3.12.4 が入っています
Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)
xxx@DESKTOP:~$ python3.11 --version
Python 3.11.9
xxx@DESKTOP:~$ python3 --version
Python 3.10.12
xxx@DESKTOP:~$ python3.12 --version
Python 3.12.4
update-alternativesを使用する
- alternativesがまだ適用されていない状態
xxx@DESKTOP:~$ sudo update-alternatives --config python3
[sudo] password for xxx:
update-alternatives: error: no alternatives for python3
- update-alternativesで登録していきます
xxx@DESKTOP:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 12
xxx@DESKTOP:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 9
xxx@DESKTOP:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 4
- update-alternativesでどのpythonを使用するか決めます
xxx@DESKTOP:~$ sudo update-alternatives --config python3
There are 3 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.10 12 auto mode
1 /usr/bin/python3.10 12 manual mode
2 /usr/bin/python3.11 9 manual mode
3 /usr/bin/python3.12 4 manual mode
Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/python3.12 to provide /usr/bin/python3 (python3) in manual mode
xxx@DESKTOP:~$ python3 -V
Python 3.12.4
これでPythonのバージョンの変更ができるようになりました。