一、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