2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Windows11にWSL2+Python+GPU環境を構築

Last updated at Posted at 2021-12-23

前提条件

詳細
OS Windows 11 Home
CPU AMD Ryzen 7 4800HS with Radeon Graphics 2.90 GHz
GPU NVIDIA GeForce RTX 2060

手順

CUDA on WSL インストール

CUDA on Windows Subsystem for Linux (WSL)からWSL用のCUDAドライバーをダウンロード

image-20211127234900696.png

インストーラーを起動し、ドライバーのみインストールを実施。

image.png

WSL2セットアップ

Windows Terminalを管理者権限で起動

image.png

下記のコマンドを実行し、ubuntu 20.04をインストール

wsl --install -d Ubuntu-20.04

ユーザー名とパスワードを設定

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: (Ubuntu側で使用するユーザー名を入力して[Enter])
New password:(パスワードを入力して[Enter]。※表示されません)
Retype new password:(同じパスワードを再入力し[Enter]。※表示されません)

aptのアップデート

sudo apt update && sudo apt -y upgrade

CUDAインストール

下記のコマンドを順に実行し、CUDAをインストールする。
※ 本環境はPytorch 1.10用にCUDA 11.3をセットアップする。

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-3-local/7fa2af80.pub
sudo apt update && sudo apt -y upgrade
sudo apt install -y cuda

環境変数を設定

cat << 'EOS' >> ~/.profile

# Added by install-cuda-on-wsl.sh
# Ref: https://astherier.com/blog/2021/07/windows11-cuda-on-wsl2-setup/
export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
# Added: end

EOS

Git セットアップ

最新版をインストール

sudo apt install git

ユーザ名とメールアドレスを設定

git config --global user.name "your name"
git config --global user.email "your email"

SSHを設定

ssh-keygen -t rsa
chmod 600 ~/.ssh/id_rsa

pythonセットアップ

下記のコマンドを実行し、Python3がインストール済みであることを確認

python3 --version

下記のコマンドを実行し、pipとpipenvをインストール

sudo apt install python3-pip
pip install pipenv

動作確認

下記のコマンドで動作確認用の環境を作成

cd ~
mkdir project && cd project
mkdir pytorch_sample && cd pytorch_sample
   
# Pytorchサンプルプログラム ダウンロード
git clone https://github.com/pytorch/examples.git
 
# 実行環境作成
pipenv --python 3
pipenv shell
   
# pytorchインストール
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
   
# 実行
cd examples/mnist
python3 main.py --epochs 2
   
# GPUが使えるかどうかのみ確認
python
import torch
torch.cuda.is_available()
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?