在 Windows & Mac 環境上安裝 Jupyter Lab

前言

最近開始在做碩專論文第二階段的實驗,過去因為第一階段的實驗範圍是機器學習相關的演算法,所以 Google colab 就足以應付了,但第二階段開始要進行深度學習的演算法,發現 Colab 已經沒辦法滿足我的需求了,因此不得已只能在本地端的環境上架設 Jupyter Lab 來當作實驗環境了。

一開始我先在 Windows 環境上安裝,但因為該環境上沒有顯示卡,所以跑起來頗為吃力,幸好我手邊有一台閒置的 iMac(M1晶片),所以改用 Mac 來進行安裝,目前看來,搭配 iMac 的效能確實比僅擁有 CPU 的 Windows 環境好很多,因此,本文將記錄我在這兩個平台上的設定過程和相關步驟。

對了,在囉嗦一句,因為未來可能會有不同套件版本的需求,所以實務上建議將 Jupyter Lab 安裝在虛擬環境上面,至於如何安裝在虛擬環境?這個後續我會在另外一篇在分享了。


使用環境及軟體

  • Windows Server 2022,Jupyter Lab 3.6.3 
  • macOS 12.4,Jupyter Lab 4.0.9


Windows 安裝 Python & JupyterLab

若不想要手動安裝一大堆套件,可以直接安裝 Anaconda,但就我個人實際的使用經驗,Anaconda 一開始會覺得開箱即用很方便,但後續反而延伸出更多不太好解決的問題,後來我整個環境重裝後,就再也沒用這種方式了。另外透過 Anaconda3 來安裝,會自行安裝對應的 Python 版本和其它基礎套件,若果有指定版本安裝的需求,也是建議就手動安裝。

  • 安裝 Python 3.11.4 Windows installer (64-bit)
  • 安裝 JupyterLab pip3 install jupyterlab==3.6.3
  • 安裝完成,若執行有問題,建議重新建置環境 jupyter lab build


MacOS 安裝 Python & JupyterLab

  • 安裝 Homebrew。
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  • 安裝 Python 3.11。
    brew install python@3.11
    
  • 安裝 JupyterLab pip3 install jupyterlab==4.0.9


常用套件

因為選擇手動安裝,所以一些基礎的套件還是得自己安裝,底下列出來的是我的環境有用到的套件版本。
python -m pip install --upgrade pip
 
pip3 install numpy==1.23.5
pip3 install pandas==1.5.3
pip3 install matplotlib==3.7.1
pip3 install scikit-learn==1.2.2 
pip3 install scipy==1.11.4
pip3 install pydot==1.4.2
pip3 install scikit-learn==1.2.2
pip3 install seaborn==0.13.1

# 底下非必要
pip3 install gensim==4.3.2
pip3 install transformers==4.35.2
pip3 install torch==2.2.0
pip3 install statsmodels==0.14.1


Jupyter Lab 環境設定

若使用 Anaconda,請小心不要啟動錯誤了,Jupyter Lab 正確的介面如下圖,與 Jupyter Notebook 略有不同,Anaconda 控制台內自帶的啟動工具是 Jupyter Notebook 的,底下紀錄幾個初始化的步驟。


遠端存取設定

因為我是安裝在另外一台實體機上面,但會透過筆電來操作,所以一開始必須先進行允許遠端存取的設定,首先,必須先產生設定檔案 jupyter lab --generate-config

  • Windows 環境:找到設定檔案調整底下兩個參數設定,通常位置在 C:\Users\你的登入帳號\.jupyter\jupyter_lab_config.py
  • Mac 環境:找到設定檔案調整底下兩個參數設定,通常位置在 /Users/你的登入帳號/.jupyter/jupyter_lab_config.py
# Set the Access-Control-Allow-Origin header
c.ServerApp.allow_origin = '*'

# (非必要),The base URL for the Jupyter server.
c.ServerApp.base_url = 'jupyter'

# The IP address the Jupyter server will listen on.
c.ServerApp.ip = '0.0.0.0' 


設定密碼

終端機輸入 jupyter-lab password,會跳出輸入視窗,連續輸入兩次即可。


查看版本

jupyter --version,目前 Jupbyter Lab 使用版本。


啟動服務

Mac 環境第一次需要先建置 jupyter lab build

#!/bin/bash
jupyter lab


Jupyter Lab 擴充套件

安裝執行時間套件

jupyterlab-execute-time,方便查看該區段執行花了多少時間,使用情境如下圖。

  • Windows(Anaconda版):pip install jupyterlab_execute_time==2.3.1 --user
    • --user 參數非必要,是僅安裝給當前使用者。
    • 請不要安裝 3.x 版本,因為會把 JupyterLab 的版本一併升級,導致後面 Git 套件無法安裝
  • MacOS:pip3 install jupyterlab_execute_time==3.1.1


Git 套件 

jupyterlab-git,方便在 Jupyter Lab 上面管理、查看異動的檔案以及簽入Git Repo 用。

  • Windows:pip install --upgrade jupyterlab-git==0.43.0,也可以透過 JupyterLab 的 Console 直接安裝,但有個小訣竅,輸入完命令之後要 Shift+Enter(預設設定,可以調整) 才能送出,但很可惜截至 2023/09/28 止暫時無法支援 v4 以上的版本
  • MacOS:pip3 install jupyterlab-git==0.50.0

若安裝完套件看到底下的錯誤訊息,代表尚未安裝 Git,請先安裝 Git > 2 版本後,記得關閉 Console 視窗後,重新啟動 JupyterLab 即可排除此問題。


資源監控套件

jupyter-resojurce-usage,用來查看執行 Jupyter Lab 的 OS 的資源狀況,如下圖底部紅框處。

  • Windows:pip install jupyter-resource-usage==1.0.0,JupyterLab 3.x 版最高只能安裝到 1.0.0 版,該套件預設安裝好後只會顯示記憶體狀態,若需進階的設定需調整 jupyter_lab_config.py,我的環境設定如下參考。 
  • MacOS:pip install jupyter-resource-usage==1.0.1
    # 使用者接近記憶體限制 20% 時,資源顯示的背景可以變更為紅色 (預設不指定)
    c.ResourceUseDisplay.mem_warning_threshold = 0.2
    # 限制使用記憶體 23 G
    c.ResourceUseDisplay.mem_limit = 23 * 1024 * 1024 * 1024
    
    # 啟用 CPU 使用情況追蹤(預設為停用)
    c.ResourceUseDisplay.track_cpu_percent = True
    # 限制使用幾個 CPU
    c.ResourceUseDisplay.cpu_limit = 16
    


疑難雜症

Jupyter K-Means 記憶體問題

KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2.

UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.


讀取外部資料集出現 SSL 下載問題

# 請加上底下兩行
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

cifar10.load_data()
# 錯誤訊息 URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None 
# -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)


使用 ARR (Reverse Proxy) 轉入後擴充功能異常

因為有共用同一個 domain 的需求,因此使用了 Reverse Proxy 來進行轉址,但轉過去後發現有部分功能異常,會出現 400 的錯誤 (如下圖),這個原因是因為 Jupyter Lab 的擴充套件使用的請求方式,被 Net Framework 認為是 XSS (Cross Site Script) 攻擊。


發生這個問題網路上有很多解法,例如 web.config 關閉頁面驗證等等,但.... 我沒有要用 NET 呀,所以更快的方法是直接把應用程式集區改成不使用 NET 即可。

一併附上 web.config 的轉址設定。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
	<!-- Jupyter Lab -->
        <rule name="Redirect to Jupyter" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="my.domain.com" />
            <add input="{REQUEST_URI}" pattern="^/jupyter" />
          </conditions>
          <action type="Rewrite" url="http://localhost:8888/{R:1}" />
        </rule>

	<!-- 原本的站台 -->
        <rule name="Other" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="my.domain.com" /> 
          </conditions>
          <action type="Rewrite" url="http://localhost:8006/{R:0}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>



參考網站

留言