本教程基于Ubuntu Server 20.04 LTS,其他Linux系统大同小异,默认已安装Python3。
1. 更新pip版本(旧版本的pip可能会导致意料之外的问题)
sudo pip install --upgrade pip
2. 安装jupyter
pip install jupyter
3. 配置环境变量
jupyter安装完成后直接在终端输入jupyter notebook可能会报jupyter: command not found
的错误,原因是没有配置环境变量,在终端输入~/.local/bin/jupyter-notebook
可以使用,但是关闭此终端再打开时还是会报错。因此需要简单配置一下环境变量:
在~/.bashrc文件中添加 export PATH=~/.local/bin:${PATH} ,保存文件后在终端运行source ~/.bashrc 即可直接在终端中使用 jupyter notebook。
4. 生成notebook配置文件
jupyter notebook --generate-config
这行代码会在当前路径下生成一个.jupyter
文件夹,并且文件夹中含有配置文件jupyter_notebook_config.py
。
5. 配置密码
$ jupyter notebook password
Enter password: ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to xxx/.jupyter/jupyter_notebook_config.json
填写密码并确认密码,会在配置文件夹下生成含有密码hash的jupyter_notebook_config.json
。
6. 配置jupyter_notebook_config.py
c.NotebookApp.ip = '*'
c.NotebookApp.password = "填入jupyter_notebook_config.json中的密码hash"
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888 #可自定义端口
c.NotebookApp.allow_remote_access = True
默认配置文件全被注释了,所以直接复制粘贴进去就可以。
7. nohup启动jupyter notebook (后台挂载)
nohup jupyter notebook &
登录http://x.x.x.x:8888,输入密码即可登录jupyter notebook。
8. 结束进程
ps -aux|grep jupyter #查找进程
kill -9 进程号 #结束进程