一、Jupyter Notebook的一般安裝
① 安裝pip
sudo apt install python3-pip
② 解決pip下載速度慢問題
這裏使用清華大學的,參考https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
升級 pip 到最新的版本 (>=10.0.0) 後進行配置:
pip3 install pip -U
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
完成時顯示:Writing to /home/xxxxx/.config/pip/pip.conf
如果您到 pip 默認源的網絡連接較差,臨時使用本鏡像站來升級 pip:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
③ 安裝jupyter
pip3 install --upgrade pip
pip3 install jupyter
完成時顯示:Successfully installed……
sudo apt install jupyter-core
④ 配置jupyter的環境變量
sudo nano ~/.bashrc
在最後添加以下兩行:
PATH=~/.local/bin:$PATH
export PATH
保存退出(Ctrl+X,Y,回車)
source ~/.bashrc
⑤ 啓動Jupyter
目標文件夾下,使用命令行
jupyter notebook
更細緻的設置:
jupyter notebook --ip=0.0.0.0 --port=8000
--ip這個參數設置jupyter監控的IP地址,如果不帶這個參數,默認監聽127.0.0.1這個地址
--port用來設置監聽的端口,如果不設置的話,默認是8888端口
jupyter自動打開瀏覽器運行。
二、設置 jupyter notebook 可遠程訪問
jupyter notebook --generate-config
生成一個 notebook 配置文件,創建 ~/.jupyter/jupyter_notebook_config.py,
成功後顯示:Writing default config to: /home/XXXXX/.jupyter/jupyter_notebook_config.py
jupyter notebook password
設置密碼,成功後顯示:
[NotebookPasswordApp] Wrote hashed password to /home/XXXXX/.jupyter/jupyter_notebook_config.json
查看密碼轉換成sha1的密文:
cat ~/.jupyter/jupyter_notebook_config.json
複製"sha1:和後面的一串密文"
修改~/.jupyter/jupyter_notebook_config.py:
sudo nano ~/.jupyter/jupyter_notebook_config.py
直接在第一行插入添加以下內容:
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha1:和後面的一串密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
#可自行指定一個端口, 訪問時使用該端口
然後目標文件夾下,使用命令行
jupyter notebook
顯示:本程序運行在: http://XXXXXX:8889/,則說明8888端口被佔用,jupyter自動改爲下一個端口
在其他電腦的瀏覽器輸入:
比如http://192.168.1.123:8888/
三、瀏覽器開終端運行 jupyter notebook
目的:爲了在網絡內的終端(計算機、手機、平板等),只要有網頁瀏覽器就能ssh遠程操作主機打開jupyter notebook。
前提:主機安裝有ssh功能,請參考我的文章《Ubuntu遠程登錄服務器——SSH的安裝和配置》。
解決辦法:用GateOne實現Web終端SSH功能,不過GateOne的安裝設置特別麻煩,容易出錯。所以用別人製作好的Docker容器來解決,安裝和刪除都很方便。
① 安裝Docker
正確安裝Docker有點複雜,請參考我的文章《Ubuntu安裝Docker的方法》。
② 安裝GateOne容器
抓取GateOne容器:
docker pull liftoff/gateone
安裝GateOne容器:
docker run -t -p 8800:8000 -h Rats --name gateone liftoff/gateone gateone
說明:
-p 8800:8000,把主機的8800端口映射給容器的8000端口。由於GateOne默認要用8000端口,但8000端口可能會被主機使用到,所以自定義一個沒有用到的8800端口給GateOne。
--name gateone,給這個容器起個名字叫gateone,方便以後使用。
完成後在瀏覽器輸入https://IP:主機端口/
比如:https://192.168.1.123:8800/
注意:必須是https://,不能是http://
點擊打開頁面的圖標,然後ssh進入主機終端,比如:
Host/IP or ssh:// URL [localhost]: 192.168.1.123
Port [22]: 22
User: abcd
Connecting to ssh://[email protected]:22
[email protected]'s password:
第一次在服務器按Ctrl+C就退出gateone,下次要開啓使用以下命令
docker start gateone
③ 設置爲開機自動啓動容器
docker update --restart=always gateone
(如果第一次運行容器時就想開機啓動,則在run後加入--restart=always,
比如:docker run --restart=always -t -p 8800:8000 -h Rats --name gateone liftoff/gateone gateone)
四、可能遇到的問題
問題一:Jupyter notebook 裏面不見python3
重裝一遍jupyter:
pip3 install jupyter
然後,再次啓動Jupyter Notebook:
jupyter notebook
問題二:Jupyter notebook 界面爲英文,如何改中文?
Jupyter notebook會按照主機的語言設置來配置界面的語言,因此要給Ubuntu設置終端中文環境:
$ locale
顯示:LANG=en_US.UTF-8
安裝中文:
sudo apt install language-pack-zh-hant language-pack-zh-hant-base
安裝中文支持:
sudo apt install `check-language-support -l zh`
將默認語言設置爲中文:
sudo localectl set-locale LANG=zh_CN.UTF-8
然後,重啓:
sudo reboot
$ locale
顯示:LANG=zh_CN.UTF-8