0
2
はじめての記事投稿
Qiita Engineer Festa20242024年7月17日まで開催中!

WSL2を使用してLinux(Ubuntu)上にPython環境を作成してみる

Last updated at Posted at 2024-07-14

目的:Python 実行環境の構築

元々はWindows10環境下にてPythonを使っていましたが、新規PCへの移行に伴い、環境構築をすることになりました。
せっかくなのでPandasの高速化(?)ライブラリであるfireducksなどを使用してみたく、今回の内容を実施しました。

使うもの

  • Windows10
    • WSL2
  • Linux
    • Ubuntu-24.04
  • Python
    • 3.7~3.13
  • VisualStudioCode
    • 1.91.1

1. WSL2のインストール

  • 管理者権限でPowerShellを開く
  • 以下のコマンドを実行してWSLをインストールする
wsl --install
  • コンピューターを再起動する。
  • 再起動後、Ubuntuが自動的に起動する
  • ユーザー名とパスワードを設定する
    • パスワード入力時、画面には表示されない

WSLが起動できない場合

  • WSLで規定のディストリビューションを確認する
    • 規定のディストリビューションがUbuntuなどでは無い可能性がある([ docker-desktop-data ]などはWSLから起動できない)
wsl --status
  • 表示されたディストリビューションを控える
  • 規定のディストリビューションを[ Ubuntu-24.04 ]に変更する
  • ディストリビューションのインストール
wsl --install Ubuntu-24.04
  • ユーザー名とパスワードを設定する
    • パスワード入力時、画面には表示されない
  • 規定のディストリビューションを終了する
wsl --terminal [ 規定されていたディストリビューション ]
  • 規定のディストリビューションを指定する
wsl --set-default Ubuntu-24.04

起動できなかった時のログ↓


PS C:\Users\'ユーザー名'> wsl
<3>WSL (10) ERROR: CreateProcessParseCommon:711: Failed to translate C:\Users\'ユーザー名'
<3>WSL (10) ERROR: CreateProcessParseCommon:757: getpwuid(0) failed 2
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\system32
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\Wbem
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\WindowsPowerShell\v1.0\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\OpenSSH\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Common Files\Autodesk Shared\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Microsoft SQL Server\150\Tools\Binn\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\dotnet\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Common Files\Autodesk Shared\Advance\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Common Files\Autodesk Shared\Advance\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Git\cmd
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Docker\Docker\resources\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\'ユーザー名'\AppData\Local\Microsoft\WindowsApps
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\'ユーザー名'\AppData\Local\Programs\Microsoft VS Code\bin
Processing fstab with mount -a failed.
Failed to mount C:\, see dmesg for more details.
Failed to mount D:\, see dmesg for more details.

<3>WSL (10) ERROR: CreateProcessEntryCommon:334: getpwuid(0) failed 2
<3>WSL (10) ERROR: CreateProcessEntryCommon:505: execvpe /bin/sh failed 2
<3>WSL (10) ERROR: CreateProcessEntryCommon:508: Create process not expected to return

2. UbuntuでPython環境の設定

2.1. Ubuntuターミナルで以下のコマンドを順次実行し、必要なパッケージをインストールする

    sudo apt update
    sudo apt install software-properties-common
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt upgrade
    sudo apt update
    sudo apt upgrade
    sudo apt update

2.2. Pythonの各Versionのインストール

  • 必要なバージョンのみ順次実行

sudo apt install python3.7 python3.7-venv python3.7-dev
sudo apt install python3.8 python3.8-venv python3.8-dev
sudo apt install python3.9 python3.9-venv python3.9-dev
sudo apt install python3.10 python3.10-venv python3.10-dev
sudo apt install python3.11 python3.11-venv python3.11-dev
sudo apt install python3.12 python3.12-venv python3.12-dev
sudo apt install python3.13 python3.13-venv python3.13-dev

2.3. 利用可能なPython Versionの確認

    $ ls /usr/bin/python3*

実行結果

    /usr/bin/python3            /usr/bin/python3.12         /usr/bin/python3.7-config   /usr/bin/python3.9
    /usr/bin/python3.10         /usr/bin/python3.12-config  /usr/bin/python3.7m         /usr/bin/python3.9-config
    /usr/bin/python3.10-config  /usr/bin/python3.13         /usr/bin/python3.7m-config
    /usr/bin/python3.11         /usr/bin/python3.13-config  /usr/bin/python3.8
    /usr/bin/python3.11-config  /usr/bin/python3.7          /usr/bin/python3.8-config

2.4. Pipのインストール


python3.13 get-pip.py

2.5. 仮想環境の作成

  • python[ 適用したいバージョン ] -m venv ~/[ 環境名 ]

python3.7 -m venv ~/python3.7-env
python3.8 -m venv ~/python3.8-env
python3.9 -m venv ~/python3.9-env
python3.10 -m venv ~/python3.10-env
python3.11 -m venv ~/python3.11-env
python3.12 -m venv ~/python3.12-env
python3.13 -m venv ~/python3.13-env

2.6. 仮想環境のアクティベート


$ source ~/python3.12-env/bin/activate
(python3.12-env) $

2.7. 仮想環境のディアクティベート

  • [ deactivate ]

(python3.12-env) $ deactivate

3. Visual Studio Codeのインストールと設定

3.1. VisualStudioCodeのインストール

  • URLにアクセスし、OSを選択

VScode


  • ダウンロード完了後インストール

3.2. VisualStudioCode拡張機能のインストール

  • Linux上で VisualStudioCode 及び Pythonを動作させるため拡張機能をインストールする
    • 画面左のアイコンの拡張機能をクリック
    • 検索窓に下記を入力し、それぞれインストールする

  • Python
  • WSL
  • Remote Development

4. VisualStudioCodeにおけるLinux仮想環境のパス設定

4.1. 仮想環境のパス取得

  • アクティベートされた仮想環境にて[ which python ]でパスを取得

$ which python
/home/'ユーザー名'/python3.12-env/bin/python

4.2.仮想環境のパス設定

  • VisualStudioCodeでコマンドパレット[ Ctrl+Shift+P ]を起動
  • [ Remote-WSL: New Window ]を選択し、WSL環境に接続する
  • 再びVisualStudioCodeでコマンドパレット[ Ctrl+Shift+P ]を起動
  • [ Python: Select Interpreter ]を選択する
    • 4.1.で取得した仮想環境のパス(/home/ユーザー名/python[ 任意のバージョン ]-env/bin/python)を選択

5. Pythonコードの実行テスト

  • 構築した仮想環境が動作しているかテストする
    • WSLに接続したVSCode内で新しいPythonファイル(例:test.py)を作成する
    • 以下のようなサンプルコードを入力

print('WSL2 Python')

  • ファイル名[ test.py ]で保存する
  • [ python '実行するpythonファイル.py']コマンドで作成したサンプルコードを実行する

python test.py
  • ターミナルに実行結果が表示されれば成功
WSL2 Python

おわりに

PC1台当たり2時間程度で作業完了できました。
今後はQiitaの投稿もしていけたらと思っています。

0
2
0

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
  3. You can use dark theme
What you can do with signing up
0
2