Python3.9.1をWSL2に構築したubuntu20.04にインストールします。
WSL2のインストールからubuntu20.04を使用できるまでの手順は以下を参照して下さい。
本投稿は以下を参考に構築しております。
sudo apt-get install python3では、、、
デフォルトではPython3.8.2がインストール済みです。
sudo apt-get install python3ではPython3.8.5がインストールされます。
(sudo apt upgradeでも同様。)
最新Pythonがリポジトリに登録されていないようですので
Pythonのダウンロードページから3.9.1のソースをダウンロードしてUbuntuで
インストールしていこうと思います。
![python3 -V](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-1024x392.png)
/etc/apt/sources.listの変更
設定ファイルを編集するのでバックアップを取得しておきましょう。
cp /etc/apt/sources.list /etc/apt/sources.list.bak
![sources.listのバックアップ取得](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-2-1024x392.png)
sources.listを編集します。
sudo vi /etc/apt/sources.list
![sources.listの編集](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-3-1024x392.png)
vi画面にて以下を入力し、[Enter]を押下します。
:%s/# deb-src/deb-src/g
![sources.list内の置換](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-4-1024x392.png)
「● substitutions on ● lines」と下部にメッセージが表示されたら[:wq]で上書き保存します。
![sources.listの更新](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-5-1024x392.png)
Python3.9の依存関係を解決する
パッケージリストの更新を行います。
sudo apt update
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-6-1024x392.png)
Python3.9の依存関係を解決します。
sudo apt-get build-dep python3.9
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-8-1024x392.png)
sources.listでdeb-srcはデフォルトでコメントアウトされているので
「sudo apt-get build-dep python3.9」を実行しても以下エラーで失敗します。
E: You must put some ‘deb-src’ URIs in your sources.list
もし、セキュリティの関係でsources.listを編集できない場合は以下のパッケージをsudo apt install後にインストールしてください。
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://phoenixnap.com/kb/wp-content/uploads/2021/04/install-python-3-on-ubuntu.png)
必ずsources.listの編集または上のapt installで依存関係を解決してください。
例えばlibssl-devをインストールしていないと
WARNIG: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
となり、pipコマンドが失敗します。
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-15-1024x405.png)
Python3.9.1のインストール
下記URLの下部にある「Gzip圧縮されたソースtarball」のリンクから
「Python-3.9.1.tgz」をダウンロードします。
![](https://www.python.org/static/opengraph-icon-200x200.png)
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/01/image-61-1024x529.png)
任意のフォルダに格納しておきます。
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/01/image-62-1024x455.png)
「Python-3.9.1.tgz」を解凍します。
sudo tar -xzvf Python-3.9.1.tgz
![Python-3.9.1.tgzの解凍](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-9-1024x392.png)
解凍が完了すると「Python-3.9.1」というフォルダが作成されるので
「Python-3.9.1」フォルダに移動します。
cd ./Python-3.9.1
![Python-3.9.1フォルダへの移動](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-10-1024x392.png)
「configure」というファイルがあるので実行します。
sudo ./configure
![./configureの実行](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-13-1024x392.png)
./configureを実行するとMakefileが作成されるので以下を実行します。
![./configureの実行前と後の比較](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-12-1024x429.png)
sudo make
![makeの実行](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-14-1024x392.png)
Python3.9.1をインストールします。
sudo make install
![python3.9.1のインストール
make install](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-16-1024x392.png)
![python3.9.1 install 完了](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-17-1024x392.png)
Python3.9.1がインストールされているか確認します。
![Python3.9.1の確認](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-18-1024x392.png)
バージョンが変わっていないときはubuntuを再起動してみてください。
pipもアップグレードしておきます。
pip3 install –upgrade pip
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-21-1024x392.png)
以下メッセージの通りpipがインストールされたPATHが通っていないとのことで
WARNING: The scripts pip, pip3 and pip3.9 are installed in ‘/home/test/.local/bin’ which is not on PATH.
環境変数を設定します。
export PATH=”$PATH:/home/test/.local/bin”
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-23-1024x392.png)
適当なパッケージをインストールしてエラーが解消されたか試します。
![pip install
Defaulting to user installation because normal site-packages is not writeable](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-24-1024x392.png)
Defaulting to user installation because normal site-packages is not writeable
pipコマンドを実行したユーザーが
ユーザーインストールしかできないことを表すメッセージです。
インストールのベースディレクトリは以下で確認することができ、
エラーがないようであれば無視しても問題なさそうです。
python3 -m site –user-base
![インストールのベースディレクトリの表示](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-25-1024x392.png)
![](https://docs.python.org/3/_static/og-image.png)
Defaulting to user installation because normal site-packages is not writeable
が気になるようでしたら–userを明記してインストールしてあげると
上記メッセージは出力されません。
![pip install --user](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-27-1024x514.png)
下記の通り、importも無事に動作しました。
![](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/02/image-28-1024x514.png)
コメント