Windows Server 2019でWSLのUbuntu18.04にJupyterHub環境を構築

タイトル通り，Windows Server 2019上のWSL (Windows Subsystem for Linux)にJupyterHub環境を構築した際の備忘録です．
[Windows Server 2019でWSLやHyper-V上のUbuntu18.04にCUDA環境を構築しようとして断念](https://qiita.com/fararrow/items/5f9eb1e4386707c11b3c)
の，WSL部分(Python環境構築)です．

勘違いをしていましたが，WSLはVT-x使ってないのですね．(おかげでVirtualBoxと共存可)
そして直接GPU等のデバイスをサポートしてくれるわけでもなく，DDAも使えないので，後述のaliasでもない限りCUDAは使えません．
[完全なLinuxがWindows 10上で稼働する？　「WSL 2」とは](https://www.atmarkit.co.jp/ait/articles/1906/14/news019.html)
とかも出てきたので，Hyper-V上に乗っかるこちらならいけるのかも知れません．
(現時点では[Hyper-V上のUbuntu18.04でもうまくGPU passthrough出来なかった](https://qiita.com/fararrow/items/8df089dd17d1699b299c)のでわかりませんが．)

その上で，とりあえずCPU OnlyなPython環境のJupyterHub構築メモ．

WSLでUbuntu18.04を動かすまでは
[Windows Server 2019のWSLにUbuntu18.04をインストール](https://qiita.com/fararrow/items/1007c850d81022799420)
↑こちら


# はじめに

冒頭で書いた通り，JupyterHub環境を構築します．

- [[Ubuntu18.04] JupyterHubによる機械学習環境の構築手順一式（複数人利用想定）](https://qiita.com/kei556/items/5005bea8d0676f913041)

辺りを参考にしました．

<!--なお，JupyterHubの辺りでコマンドが``$ (cd /usr/local/lib/anaconda3 && sudo xxx)``みたいになっていますが，これはパスを通すのに横着したからです．-->
パスを通すのに横着していますが，実際運用するなら(これで良いかはさておき)
[Windows Server 2019でHyper-V上のUbuntu18.04にJupyterHub環境を構築](https://qiita.com/fararrow/items/e2bba195f08096471d31)
のように，もしくは他の方法で，ちゃんと通した方が良いと思います．


# Anaconda3

せっかくChromeを入れたので，GUIで
https://www.anaconda.com/
のDownloadから「for Linux Installer」を落としてインストール．
2019年6月半ば時点では「2019.03」な「Anaconda3-2019.03-Linux-x86_64.sh」が最新．
以下，rootさんで入れる前提だと

```shell-session
$ cd ~/Downloads/
$ sudo su
# bash Anaconda3-2019.03-Linux-x86_64.sh

Welcome to Anaconda3 2019.03

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 
```

Enterを押してLicense確認して

```shell-session
Please answer 'yes' or 'no':'
>>> yes
```

インストール先を聞かれるので``/usr/local/lib/anaconda3``へ入れるため

```shell-session
Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> /usr/local/lib/anaconda3
```

として，

```shell-session
PREFIX=/usr/local/lib/anaconda3
installing: python-3.7.3-h0371630_0 ...
Python 3.7.3
installing: xxx ...
```

そこそこ待つと

```shell-session
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>>
```

と聞かれる．
公式の[Frequently asked questions](https://docs.anaconda.com/anaconda/user-guide/faq/)に「We do not recommend adding Anaconda to the PATH manually.」とあるのだが，defaultはnoという(笑)
環境混在等々色々あるならnoで自分で設定すべきだろうし，そうでないならnoで自分でやるべきだろうし．
自動だと.bashrcに通してくれるけれど，とりあえず今回はrootだし参考記事同様に


```shell-session
[no] >>> no

You have chosen to not have conda modify your shell scripts at all.
To activate conda's base environment in your current shell session:

eval "$(/usr/local/lib/anaconda3/bin/conda shell.YOUR_SHELL_NAME hook)"

To install conda's shell functions for easier access, first activate, then:

conda init

If you'd prefer that conda's base environment not be activated on startup,
   set the auto_activate_base parameter to false:

conda config --set auto_activate_base false

Thank you for installing Anaconda3!

===========================================================================

Anaconda and JetBrains are working together to bring you Anaconda-powered
environments tightly integrated in the PyCharm IDE.

PyCharm for Anaconda is available at:
https://www.anaconda.com/pycharm
```

で，

```shell-session
# echo 'export PATH="/usr/local/lib/anaconda3/bin:$PATH"' >> /etc/profile.d/anaconda.sh
```

として

```shell-session
# exit
exit
$ exit
logout
> wsl
$ conda --version
conda 4.6.11
```

こんなノリでcondaが使える事を確認．
このままだとrootはprofile.dを読みに行かないので，rootでcondaするなら``sudo -i``と打つ．
ただしファイル入出力が絡む場合はカレントディレクトリが面倒な事になるので，

```shell-session
$ sudo visudo
```

で(``sudo vi /etc/sudoers``は怖いので避ける)

```shell
#Defaults       env_reset
Defaults        env_keep += "PATH"
Defaults        mail_badpass
#Defaults       secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
```

こうしておく．
なお冒頭に書いた通りこれは横着で，これだと/usr/sbinが実行出来ないなどがあるので[Hyper-Vではこうした](https://qiita.com/fararrow/items/e2bba195f08096471d31)．

# JupyterHub環境の構築

WindowsでJupyterHubを使いたくて始めたことなのでここが本番．

## JupyterHubのインストール

```shell-session
$ sudo conda install jupyterhub
```
インストール完了後

```shell-session
$ (cd /usr/local/lib/anaconda3 && sudo jupyterhub --no-ssl)
```

と叩き， http://localhost:8000/ にアクセスして

![jupyterhub_login](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/438132/c4fef7e3-e676-9c26-511f-c0b8ebafa356.png)

こんなログイン画面が出ればとりあえずOK．
一応ログインもしてみると，homeが見えるはず．

## JupyterHubの設定

homeが見えると書いた通り，そんな所からスタートするのもアレなので，

```shell-session
$ (cd /usr/local/lib/anaconda3 && sudo jupyterhub --generate-config)
Writing default config to: jupyterhub_config.py
```

でdefaultのjupyterhub_config.pyを作り，

```shell-session
$ (cd /usr/local/lib/anaconda3 && sudo vi jupyterhub_config.py)
```

で編集．

```shell:jupyterhub_config.py
## Path to the notebook directory for the single-user server.
#
#  The user sees a file listing of this directory when the notebook interface is
#  started. The current interface does not easily allow browsing beyond the
#  subdirectories in this directory's tree.
#
#  `~` will be expanded to the home directory of the user, and {username} will be
#  replaced with the name of the user.
#
#  Note that this does *not* prevent users from accessing files outside of this
#  path! They can do so with many other means.
#c.Spawner.notebook_dir = ''
```

辺りを探してこのコメントアウトを外し，

```shell:jupyterhub_config.py
c.Spawner.notebook_dir = '~/notebooks'
```

とする．
当然，普通に``adduser``しただけではnotebooksなんてディレクトリは作られないので，

```shell-session
$ sudo mkdir /etc/skel/notebooks
```

で/etc/skel/にnotebooksディレクトリを用意しておく．
既存ユーザにも無いので，

```shell-session
$ mkdir ~/notebooks
```

既にいっぱいユーザが居るなら…頑張って作って回るか，スクリプト書く．

あと，ポートが8000なのは嫌なので，

```shell:jupyterhub_config.py
c.JupyterHub.port = [お好きなポート番号]
```

さらに，テストでログイン画面出した際にWarningが出ていた通りhttpsで通信したいので，参考記事同様にSSLの設定を

```shell-session
$ cd /usr/local/lib/anaconda3
$ sudo su
# mkdir cert
# chmod 600 cert
# cd cert
# openssl req -x509 -nodes -newkey rsa:2048 -keyout secret.key > jupyter.pem
```

とりあえずCommon NameにIPかドメイン(orマシン)名，後は適当に埋めるか空欄で

```shell-session
Country Name (2 letter code) [AU]:JP
Common Name (e.g. server FQDN or YOUR name) []:xxx.xxx.xxx.xxx
```

jupyter.pemとsecret.keyを作成．

再度編集して

```shell:jupyterhub_config.py
c.JupyterHub.ssl_cert = '/usr/local/lib/anaconda3/cert/jupyter.pem'
c.JupyterHub.ssl_key = '/usr/local/lib/anaconda3/cert/secret.key'
```

と設定．
今度はssl有り起動で

```shell-session
$ (cd /usr/local/lib/anaconda3 && sudo jupyterhub)
```

https://localhost:[お好きなポート番号]/ に繋がればOK．
うまく出来ていれば
![jupyterhub_ssl.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/438132/2a9ba489-01f3-d5d2-1c31-65d7eeb104bd.png)
のようにWarning無しのログイン画面になる．


# CUDA関連

結局動かなかったわけですが，一応CuPyを入れてみた際のメモ．


## nvidiaドライバ

これで入ったけど，結局使えないので無駄作業です．
WSLはKernelが無いので，頑張って落としてきてみたり，build-essentialに入ってたgccではバージョンが合わなかったりと色々試行錯誤しましたが，

- [UbuntuにNvidia GPUのDriver + CUDAをInstallする(GTX1080対応版)](https://qiita.com/conta_/items/d639ef0068c9b7a0cd12)

の手順で入りました．

まずは既存のものが無いか確認＆パージ

```shell-session
$ dpkg -l | grep nvidia
$ dpkg -l | grep cuda
$ sudo apt-get --purge remove nvidia-*
$ sudo apt-get --purge remove cuda-*
```

WSLなら普通は入ってない，意味ないわけですし．
続いてリポジトリを追加

```shell-session
$ sudo add-apt-repository ppa:graphics-drivers/ppa
```

色々表示されるけどとりあえずEnter

```shell-session
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Get:1 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease [21.3 kB]
Hit:2 http://security.ubuntu.com/ubuntu bionic-security InRelease
Ign:3 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:4 http://dl.google.com/linux/chrome/deb stable Release
Hit:5 http://jp.archive.ubuntu.com/ubuntu bionic InRelease
Get:6 http://jp.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:7 http://jp.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:8 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic/main amd64 Packages [24.0 kB]
Get:10 http://jp.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [644 kB]
Get:11 http://jp.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [954 kB]
Get:12 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic/main Translation-en [6252 B]
Get:13 http://jp.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3728 B]
Fetched 1816 kB in 9s (207 kB/s)
Reading package lists... Done
```

で，

```shell-session
$ sudo apt-get update
$ apt-cache search 'nvidia-[0-9]+$'
```


<details><summary>入れられそうなバージョンを確認</summary><div>

```shell-session
$ apt-cache search 'nvidia-[0-9]+$'
nvidia-331 - Transitional package for nvidia-331
nvidia-346 - Transitional package for nvidia-346
nvidia-352 - Transitional package for nvidia-361
nvidia-361 - Transitional package for nvidia-367
nvidia-367 - Transitional package for nvidia-375
nvidia-375 - Transitional package for nvidia-384
nvidia-384 - Transitional package for nvidia-driver-390
nvidia-headless-no-dkms-390 - NVIDIA headless metapackage - no DKMS
xserver-xorg-video-nvidia-390 - NVIDIA binary Xorg driver
nvidia-340 - NVIDIA binary driver - version 340.107
nvidia-304 - NVIDIA legacy binary driver - version 304.137
nvidia-387 - Transitional package for nvidia-driver-390
nvidia-390 - Transitional package for nvidia-driver-390
nvidia-headless-no-dkms-396 - NVIDIA headless metapackage - no DKMS
nvidia-headless-no-dkms-410 - NVIDIA headless metapackage - no DKMS
nvidia-headless-no-dkms-415 - NVIDIA headless metapackage - no DKMS
nvidia-headless-no-dkms-418 - NVIDIA headless metapackage - no DKMS
nvidia-headless-no-dkms-430 - NVIDIA headless metapackage - no DKMS
xserver-xorg-video-nvidia-396 - NVIDIA binary Xorg driver
xserver-xorg-video-nvidia-410 - NVIDIA binary Xorg driver
xserver-xorg-video-nvidia-415 - NVIDIA binary Xorg driver
xserver-xorg-video-nvidia-418 - NVIDIA binary Xorg driver
xserver-xorg-video-nvidia-430 - NVIDIA binary Xorg driver
```
</div></details>
nvidia-390が新しそうなので

```shell-session
$ sudo apt install nvidia-390
```


## CUDA10.1, cuDNN

こちらも，結局使えないので無駄作業です．

### CUDA10.1

https://developer.nvidia.com/cuda-downloads の「Linux→x86_64→Ubuntu→18.04→deb(network)」に従い，

```shell-session
$ cd ~/Downloads/
$ wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.168-1_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu1804_10.1.168-1_amd64.deb
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
$ sudo apt update
```

そして入れられそうなバージョンの確認

```shell-session
$ apt show cuda -a
```

wgetしてる段階で当然ですが，6月半ば時点では
<details><summary>10.1.168-1</summary><div>

```shell-session
$ apt show cuda -a
Package: cuda
Version: 10.1.168-1
Priority: optional
Section: multiverse/devel
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 25.6 kB
Depends: cuda-10-1 (>= 10.1.168)
Download-Size: 2512 B
APT-Sources: http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  Packages
Description: CUDA meta-package
 Meta-package containing all the available packages required for native CUDA
 development. Contains the toolkit, samples, driver and documentation.

Package: cuda
Version: 10.1.105-1
Priority: optional
Section: multiverse/devel
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 25.6 kB
Depends: cuda-10-1 (>= 10.1.105)
Download-Size: 2514 B
APT-Sources: http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  Packages
Description: CUDA meta-package
 Meta-package containing all the available packages required for native CUDA
 development. Contains the toolkit, samples, driver and documentation.

Package: cuda
Version: 10.0.130-1
Priority: optional
Section: multiverse/devel
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 25.6 kB
Depends: cuda-10-0 (>= 10.0.130)
Download-Size: 2514 B
APT-Sources: http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  Packages
Description: CUDA meta-package
 Meta-package containing all the available packages required for native CUDA
 development. Contains the toolkit, samples, driver and documentation.
```
</div></details>
が最新，まぁそのまま最新で良いので

```shell-session
$ sudo apt install cuda
```

…とすると，えらく依存関係で怒られる．仕方ないので辿っていって1つずつ入れる．

```shell-session
$ sudo apt install libnvidia-encode-418
$ sudo apt install cuda-drivers
$ sudo apt install cuda
```

### cuDNN

とりあえず，リポジトリを追加してキー追加してupdate，そして入れられそうなバージョンの確認

```shell-session
$ echo "deb https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" | sudo tee /etc/apt/sources.list.d/nvidia-ml.list
$ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys --recv-keys F60F4B3D7FA2AF80
$ sudo apt update
$ apt show libcudnn7-dev -a
```

6月半ば時点では
<details><summary>libcudnn7-dev=7.6.0.64-1+cuda10.1</summary><div>

```shell-session
$ apt show libcudnn7-dev -a
Package: libcudnn7-dev
Version: 7.6.0.64-1+cuda10.1
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 388 MB
Depends: libcudnn7 (= 7.6.0.64-1+cuda10.1)
Download-Size: 154 MB
APT-Manual-Installed: yes
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.6.0.64-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 375 MB
Depends: libcudnn7 (= 7.6.0.64-1+cuda10.0)
Download-Size: 145 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.5.1.10-1+cuda10.1
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 367 MB
Depends: libcudnn7 (= 7.5.1.10-1+cuda10.1)
Download-Size: 148 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.5.1.10-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 354 MB
Depends: libcudnn7 (= 7.5.1.10-1+cuda10.0)
Download-Size: 140 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.5.0.56-1+cuda10.1
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 367 MB
Depends: libcudnn7 (= 7.5.0.56-1+cuda10.1)
Download-Size: 148 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.5.0.56-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 354 MB
Depends: libcudnn7 (= 7.5.0.56-1+cuda10.0)
Download-Size: 140 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.4.2.24-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 346 MB
Depends: libcudnn7 (= 7.4.2.24-1+cuda10.0)
Download-Size: 137 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.4.1.5-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 348 MB
Depends: libcudnn7 (= 7.4.1.5-1+cuda10.0)
Download-Size: 137 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.3.1.20-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 346 MB
Depends: libcudnn7 (= 7.3.1.20-1+cuda10.0)
Download-Size: 138 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.

Package: libcudnn7-dev
Version: 7.3.0.29-1+cuda10.0
Priority: optional
Section: multiverse/devel
Source: cudnn
Maintainer: cudatools <cudatools@nvidia.com>
Installed-Size: 330 MB
Depends: libcudnn7 (= 7.3.0.29-1+cuda10.0)
Download-Size: 136 MB
APT-Sources: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Packages
Description: cuDNN development libraries and headers
 cuDNN development libraries and headers. cuDNN provides primitives for deep
 neural networks.
```
</div></details>
が最新なようなので

```shell-session
$ sudo apt install libcudnn7-dev=7.6.0.64-1+cuda10.1
```

で入ります．

### 1803でのバグ

ちなみにWin10でテストしていた際，1803だとこれではそれぞれキー追加時に

```shell-session
Executing: /tmp/apt-key-gpghome.dAsJ4VKeIP/gpg.1.sh --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
gpg: requesting key from 'https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub'
gpg: connecting dirmngr at '/tmp/apt-key-gpghome.dAsJ4VKeIP/S.dirmngr' failed: IPC connect call failed
gpg: WARNING: unable to fetch URI https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub: No dirmngr
```

とか

```shell-session
Executing: /tmp/apt-key-gpghome.VgdiwVpfMG/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys --recv-keys F60F4B3D7FA2AF80
gpg: connecting dirmngr at '/tmp/apt-key-gpghome.VgdiwVpfMG/S.dirmngr' failed: IPC connect call failed
gpg: keyserver receive failed: No dirmngr
```

のようなエラーでキー追加できませんでした．

[【期間限定】WSLのUbuntu 18.04でapt-keyコマンドを実行した時に「IPC connect call failed」のエラーになった時のワークアラウンド](https://blog.fieldnotes.jp/entry/workaround-wsl-1804-apt-key-bug)

を見ると，1809以降で修正されているそうですが，とりあえず記事のような感じで

```shell-session
curl -sL http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub | sudo apt-key add
curl -sL "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF60F4B3D7FA2AF80" | sudo apt-key add
```

としてあげると「OK」と言ってくれるので，引き続き``sudo apt update``して``sudo apt show``でどれ入れよう？って進んでいけばOK．



## CuPy, Chainer

こちらも，CuPyは結局使えないので無駄作業です．Chainerは動きますけど．

```shell-session
$ sudo pip install cupy-cuda101
$ sudo pip install chainer
```

あとは

```shell-session
$ python -c 'import chainer; chainer.print_runtime_info()'
```

して

```shell-session
Platform: Linux-4.4.0-17763-Microsoft-x86_64-with-debian-buster-sid
Chainer: 6.0.0
NumPy: 1.16.2
CuPy:
  CuPy Version          : 6.0.0
  CUDA Root             : /usr/local/cuda
  CUDA Build Version    : 10010
  CUDA Driver Version   : 10010
  CUDA Runtime Version  : CUDARuntimeError('cudaErrorNoDevice: no CUDA-capable device is detected')
  cuDNN Build Version   : 7500
  cuDNN Version         : 7500
  NCCL Build Version    : 2402
  NCCL Runtime Version  : 2402
iDeep: Not Available
```

という結果を見て(´・ω・｀)となれば成功です．


# WSLで頑張ってGPU演算を使う

WSLの良い所はWindows側のバイナリを呼び出せる点．
というわけで，

[GPU Support for Deep Learning on Windows Subsystem Linux(WSL) with Conda](https://earnfs.wordpress.com/2019/02/24/gpu-support-for-deep-learning-on-windows-subsystem-linuxwsl-with-conda/)

にあるように，

```shell-session
$ vi ~/.bashrc
```

なり何なりで各ユーザの.bashrcに

```shell:.bashrc
alias conda="/mnt/c/Users/(user_name)/Anaconda3/Scripts/conda.exe"
alias python="/mnt/c/Users/(user_name)/Anaconda3/python.exe"
alias ipython="/mnt/c/Users/(user_name)/Anaconda3/Scripts/ipython.exe"
alias nosetests="/mnt/c/Users/(user_name)/Anaconda3/Scripts/nosetests.exe"
alias pip="/mnt/c/Users/(user_name)/Anaconda3/Scripts/pip.exe"

cd /mnt/c/Users/(user_name)
```

とかを書いておき，

```shell-session
$ source ~/.bashrc
```

もしくはログインし直して反映させ，

```shell-session
$ python -c 'import chainer; chainer.print_runtime_info()'
```

してあげれば，

```shell-session
Platform: Windows-10-10.0.17763-SP0
Chainer: 6.0.0
NumPy: 1.16.2
CuPy:
  CuPy Version          : 6.0.0
  CUDA Root             : C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
  CUDA Build Version    : 10010
  CUDA Driver Version   : 10020
  CUDA Runtime Version  : 10010
  cuDNN Build Version   : 7500
  cuDNN Version         : 7500
  NCCL Build Version    : None
  NCCL Runtime Version  : None
iDeep: Not Available
```

当然だけど[Windows側で構築した環境](https://qiita.com/fararrow/items/875c93cc3a2545bf90a7)での結果が返ってきます．
パスがUsers以下になっているのは↑の記事に書いた通り「それぞれJust Meで入れる」という形にしたからです．

ただし，JupyterHub経由で呼び出すとUbuntu側が呼ばれるので無理です．
(New→TerminalならTerminalなのでエイリアスが効いてます．)

jupyterhub_config.pyとかjupyter_notebook_config.pyとか弄れば使えるのかも知れませんが，
そもそもこうなってくるとJupyterHubのメリットを見失い始めるのでここで終了．