LoginSignup
0

More than 1 year has passed since last update.

posted at

updated at

Python: Windows10 WSL Ubuntu20.04に、apt-getで入ってくれないPython3.10.xをインストール

なにはともあれ↓をしましょう

$ sudo apt-get upgrade
$ sudo apt-get update

ではPython3の最新バージョン3.10.3をインストールしましょう。
わたしの場合はすでにPython3.Xは入っていたので、upgrade で実行してみます。

$ sudo apt-get upgrade python3

Reading package lists... Done
Building dependency tree
Reading state information... Done
python3 is already the newest version (3.8.2-0ubuntu2).
Calculating upgrade... Done
The following packages have been kept back:
  fwupd libfwupd2 libfwupdplugin1 libgl1-mesa-dri libglapi-mesa libglx-mesa0 mesa-vulkan-drivers
0 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.

apt-get じゃ3.8.2までしか入らないのでソースコードから手動で入れます。

まず必要なものをインストールします。

$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

Python公式サイト https://www.python.org/downloads/source/ でソースコード本体のURLを調べてwgetでダウンロードします。

$ wget https://www.python.org/ftp/python/3.10.3/Python-3.10.3.tgz

tgz圧縮されているので解凍します。

$ tar -xzvf Python-3.10.3.tgz

インストールの準備をします。

$ cd Python-3.10.3/
$ ./configure --enable-optimizations

$ make -j 12

満を持してインストールを実行します。

$ sudo make altinstall

インストール結果を確認してみます。

$ python3.10 --version
Python 3.10.3

$ pip3.10 --version
pip 22.0.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)

ええやん。

だがしかし・・・

$ python --version
Python 3.8.10

python コマンドだと古いバージョンが呼ばれる。
直したい。

$ sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.10 1
update-alternatives: using /usr/local/bin/python3.10 to provide /usr/bin/python (python) in auto mode

$ python --version
Python 3.10.3

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 1
update-alternatives: using /usr/local/bin/python3.10 to provide /usr/bin/python3 (python3) in auto mode

$ python3 --version
Python 3.10.3

pip も直します。

$ echo "alias pip=pip3.10" >> ~/.bashrc

# ターミナルを再起動

$ pip --version
pip 22.0.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)

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
What you can do with signing up
0