# TensorFlow on WSL 安裝指南 由於新版的 TensorFlow (Version >= 2.11) 即不支援 Windows 平台。所以如果有需要在 Windows 下使用新版的 TensorFlow-gpu 有 DirectML,抑或是通過 [WSL](https://learn.microsoft.com/zh-tw/windows/wsl/about) (Windows Subsystem for Linux)使用。本教學將使用 TensorFlow 運行在 WSL,實現新版的 TensorFlow 模型訓練。 ## WSL 安裝 WSL 安裝方式有兩種,第一種是通過 Powershell 安裝,而第二種是通過 Windows Store 安裝。 ### Powershell 安裝 WSL 使用管理員權限在 Powershell 中使用 ``wsl --list --online`` 可以查看所有 WSL 可以安裝的 Linux 發行版。 接著使用 ``wsl --install -d <Distribution Name>``。將 ``<Distribution Name>`` 取代為想要安裝的散發套件名稱。 安裝後,並重新啟動電腦,即可完成 WSL 的安裝。 ### Windows Store 安裝 WSL 在 Windows Store 搜尋想要安裝的 WSL 發行版後,點選安裝。若沒有電腦從未安裝過 WSL,將會跳出一些腳本,請依照腳本指示,進行操作。等待安裝完成後,重新啟動電腦,即可完成 WSL 的安裝。 相關連結:[Debian 商店連結](https://apps.microsoft.com/detail/9MSVKQC78PK6?hl=neutral&gl=TW&ocid=pdpshare)、[Ubuntu 24.04 LTS 商店連結](https://apps.microsoft.com/detail/9NZ3KLHXDJP5?hl=neutral&gl=TW&ocid=pdpshare)、[Kali Linux 商店連結](https://apps.microsoft.com/detail/9PKR34TNCV07?hl=neutral&gl=TW&ocid=pdpshare) ## 安裝 TensorFlow 相關軟體 這邊的環境將以 TensorFlow 2.19.0 為基準進行安裝教學。 開啟 WSL 安裝完成相關的設定後,請先更新系統。 ```terminal sudo apt-get upgrade sudo apt-get update ``` ### 安裝 CUDA 12.5 及 cuDNN 9.3 由於 [TensorFlow 2.19 with GPU](https://www.tensorflow.org/install/source#gpu) 在 Linux 推薦的環境如下圖 (由於中文文檔更新較慢,若自己需要查看相關文檔,語言請選用 "English",不要使用 "繁體中文"), ![image](https://hackmd.io/_uploads/B1xTfPaT1l.png) 因此請依照頁面中的指示,安裝 [CUDA 12.5](https://developer.nvidia.com/cuda-12-5-0-download-archive?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local) 及 [cuDNN 9.3](https://developer.nvidia.com/cudnn-9-3-0-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=24.04&target_type=deb_local) (連結中的版本已經先選好 CUDA 12.5 on WSL-Ubuntu 及 cuDNN 9.3 on Ubuntu 24.04 版本)。 ![image](https://hackmd.io/_uploads/SkzzNva6ke.png) CUDA 12.5 for Linux WSL-Ubuntu 2.0 x86_64 版本安裝指令 ![image](https://hackmd.io/_uploads/HyLI4DpTkx.png) cuDNN 9.3 for Linux Ubuntu 24.04 x86_64 版本安裝指令 (先使用前 3 個指令即可) 接者使用下列指令將相關的函式庫安裝 (下列函式庫僅適用於 cuDNN 9.3 for CUDA 12,若需要使用其他的 cuDNN for CUDA 版本,請嘗試使用 ``sudo apt list -a 函式庫名稱`` 將對的版本號碼填到 ``=`` 後面)。 參考網址:[Installing cuDNN Debian Packages](https://developer.nvidia.com/docs/drive/drive-os/archives/6.0.4/linux/sdk/common/topics/install_sdk_manager/Installing_cuDNN_Debians_Linux.html) ```terminal sudo apt install libcudnn9-cuda-12=9.3.0.75-1 sudo apt install libcudnn9-dev-cuda-12=9.3.0.75-1 sudo apt install libcudnn9-samples=9.3.0.75-1 ``` 接著使用 ``apt-mark`` 將上面的三個函式庫鎖定,避免被更新。 ```terminal # 鎖定套件 sudo apt-mark hold libcudnn9-cuda-12 libcudnn9-dev-cuda-12 libcudnn9-samples # 解鎖套件 sudo apt-mark unhold libcudnn9-cuda-12 libcudnn9-dev-cuda-12 libcudnn9-samples ``` ### 確認 CUDA 及 cuDNN 安裝成功 安裝完成後,使用 ``nvcc -V`` 確認是否有以下輸出,若有則代表 CUDA 安裝成功。 ```Terminal nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2024 NVIDIA Corporation Built on Thu_Jun__6_02:18:23_PDT_2024 Cuda compilation tools, release 12.5, V12.5.82 Build cuda_12.5.r12.5/compiler.34385749_0 ``` > [!Note] ``nvcc -V`` 沒有輸出 > > 由於 nvcc 可能沒有被加入 PATH 中,請使用以下指令將 nvcc 加入 PATH 後再進行測試。 > > ```Terminal > # 修改 ~/.bashrc,vim 可以自行替換成習慣的文字編輯器 > vim ~/.bashrc > > # 加入以下兩行將 cuda 相關連結加入 PATH 中 > export PATH=/usr/local/cuda/bin${PATH:+:${PATH}} > export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} > > # 存檔後使用以下指令將 ~/.bashrc 配置套用 > source ~/.bashrc > ``` 使用 ``grep -E '#define CUDNN_(MAJOR|MINOR|PATCHLEVEL)' /usr/include/cudnn_version.h`` 查詢 cuDNN 的版本。若輸出如下,則代表 cuDNN 9.3 安裝成功。 ```terminal #define CUDNN_MAJOR 9 #define CUDNN_MINOR 3 #define CUDNN_PATCHLEVEL 0 ``` ## 安裝 Tensorflow 及 Jupyter Notebook 以下安裝方式將使用 pip 安裝。此步驟也可以透過 conda 安裝,可以自行查閱相關的教學。 ### 安裝 Jupyter Notebook 1. 使用 ``sudo apt install Jupyter`` 安裝 Jupyter 相關套件。 2. 接著使用 ``jupyter notebook`` 即可在瀏覽器使用 ``localhost:8888`` 使用 Jupyter Notebook。 ### 建立虛擬環境 1. 使用 ``python3 -m venv .venv`` 建立虛擬環境 2. 使用 ``source ./.venv/bin/activate`` 啟動虛擬環境 3. 使用 ``pip install numpy tensorflow pandas matplotlib scikit-learn ipykernel`` 安裝 TensorFlow、NumPy、pandas、Matplotlib、scikit-learn、IPython Kernel for Jupyter。 4. 使用 ``python -m ipykernel install --user --name .venv --display-name "Python 3 (TensorFlow)"`` 建立可用於 Jupyter Notebook 的 Kernel。 5. 重新啟動 Jupyter Notebook 後,即可在 Kernel > Change Kernel > Python 3 (TensorFlow) 選擇使用該虛擬環境的套件。 ![image](https://hackmd.io/_uploads/SyjOm4ATkg.png) ### 確認 TensorFlow 是否可以正常使用 在 Jupyter Notebook 中使用以下程式,確認是否在 TensorFlow 中是否可以調用 GPU。 ```Python= import tensorflow as tf from tensorflow.python.client import device_lib gpus = tf.config.list_physical_devices('GPU') if gpus: print(f"找到 {len(gpus)} 張 GPU:") else: print("未找到 GPU") devices = device_lib.list_local_devices() for device in devices: if device.device_type == 'GPU': print(f"名稱:{device.name}") print(f"描述:{device.physical_device_desc}") ``` 亦或是在已經啟用虛擬環境的 Terminal 中使用 ``python -c "import tensorflow as tf; print(tf.sysconfig.get_build_info())"`` ### 補充 若出現以下錯誤,你有解法的話,也請救救我。我目前仍然無法解決。 ![image](https://hackmd.io/_uploads/ryvcZ4ApJl.png) 疑似可以在 ``import tensorflow`` 前先使用 ``import os;os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'`` 解決此問題。 若在執行結束後發現 VRAM 占用沒有被釋放掉,可以參考 [此篇](https://corettainformation.blogspot.com/2020/07/jupytergpu.html) 釋放 VRAM  # 參考資料 - {%preview https://learn.microsoft.com/zh-tw/windows/wsl/install%} - {%preview https://magicjackting.pixnet.net/blog/post/225214893 %} - {%preview https://zhuanlan.zhihu.com/p/112876097 %} - {%preview https://blog.csdn.net/qq_32033383/article/details/135015041 %}