Linux 服务器普通用户配置 JupyterLab 完整教程
在多人共用的 Linux 服务器上,每个普通用户都可以在自己的 Conda 环境中独立安装和运行 JupyterLab,而不需要管理员长期维护 Jupyter 服务。
本文介绍一种比较简单的配置方式:
登录服务器 ↓ 激活个人 Conda 环境 ↓ 输入 jupyter lab ↓ 服务器启动个人 JupyterLab ↓ 本地浏览器通过服务器 IP + 端口访问 ↓ 首次使用 Token 登录并设置密码配置完成后,以后每次使用只需要:
conda activate 用户环境名 jupyter lab然后在自己的浏览器中打开对应地址即可。
一、进入自己的 Conda 环境
首先 SSH 登录服务器,激活自己的 Conda 环境:
conda activate sibh可以通过下面的命令确认当前 Python 和 Jupyter 安装位置:
which python which jupyter正常情况下应该指向自己的 Conda 环境,例如:
/home/sibh/.conda/envs/sibh/bin/python /home/sibh/.conda/envs/sibh/bin/jupyter二、安装 JupyterLab
如果服务器已经配置好 Conda,可以直接:
conda install jupyterlab ipykernel -y也可以使用 pip 安装。
例如这里使用 JupyterLab 4.2.3:
python -m pip install \ jupyterlab==4.2.3 \ jupyter_server==2.14.1 \ jupyterlab_server==2.27.2 \ notebook==7.2.1 \ ipykernel==6.29.5安装完成后检查版本:
jupyter lab --version jupyter server --version例如:
4.2.3 2.14.1如果 pip 下载速度比较慢,也可以设置 PyPI 镜像。例如使用清华源:
python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple查看是否配置成功:
python -m pip config list之后再执行:
pip install 软件包名就会默认使用配置好的镜像源。
三、生成 Jupyter Server 配置文件
执行:
jupyter server --generate-config一般会生成:
~/.jupyter/jupyter_server_config.py例如用户sibh:
/home/sibh/.jupyter/jupyter_server_config.py编辑配置文件:
nano ~/.jupyter/jupyter_server_config.py四、配置服务器 IP、端口和个人目录
移动到配置文件最后,在末尾添加:
c.ServerApp.ip = '服务器固定IP' c.ServerApp.open_browser = False c.ServerApp.port = 8811 c.ServerApp.root_dir = '/home/用户名'例如服务器固定 IP 为:
10.103.44.135用户名为:
sibh则写成:
c.ServerApp.ip = '10.103.44.135' c.ServerApp.open_browser = False c.ServerApp.port = 8888 c.ServerApp.root_dir = '/home/sibh'这里几个参数分别表示:
ServerApp.ip JupyterLab 监听的服务器 IP ServerApp.open_browser 服务器启动后不尝试自动打开浏览器 ServerApp.port JupyterLab 使用的端口 ServerApp.root_dir JupyterLab 文件浏览器允许访问的根目录如果是多人服务器,建议每个用户设定不同端口,例如:
用户A:8888 用户B:8889 用户C:8890 用户D:8891避免不同用户之间出现端口占用冲突。
编辑完成后:
Ctrl + O Enter Ctrl + X保存退出。
五、启动 JupyterLab
以后不需要输入复杂的启动参数。
直接:
conda activate sibh然后:
jupyter lab正常情况下终端里会看到类似:
Jupyter Server 2.14.1 is running at: http://10.103.44.135:8811/lab?token=xxxxxxxxxxxxxxxxxxxxxxxx这说明 JupyterLab 已经启动成功。
注意:
这个终端不能关闭。
因为这里没有使用 tmux、nohup 或 systemd。只要关闭当前 SSH 会话或者按Ctrl+C,JupyterLab 就会停止。
这种模式比较适合:
需要时启动 不用时关闭六、在自己电脑的浏览器中访问
假设服务器 IP 是:
10.103.44.135JupyterLab 端口是:
8888那么在自己电脑的浏览器中输入:
http://10.103.44.135:8888/lab即可打开 JupyterLab 登录页面。
七、第一次登录:使用 Token
JupyterLab 第一次启动时会生成一个随机 Token。
终端中可以看到:
token=xxxxxxxxxxxxxxxxxxxxxxxx在浏览器登录页面输入这个 Token 即可。
如果没有找到 Token,也可以在另一个 SSH 终端执行:
jupyter server list通常可以看到当前运行中的地址以及 Token。
八、设置 JupyterLab 登录密码
第一次通过 Token 登录时,可以设置一个固定密码,通常就设定为自己的用户名即可,便于登录,当然设定其他的也可以。
登录页面通常会提供设置密码的入口,可以填写:
Token:终端显示的 Token New Password:自己准备设置的密码设置完成以后,下次启动 JupyterLab 后,可以直接使用密码登录,而不需要每次复制 Token。
例如实验室内部服务器中,有时为了方便,会将密码设置成和用户名相同,例如:
用户名:sibh 密码:sibh但这种密码强度很低。
如果服务器只能通过受信任的实验室内网访问,风险相对有限;如果端口能够被更大范围网络访问,则不建议用户名和密码相同,应使用独立且更强的密码。
也可以直接在服务器终端中设置密码:
jupyter server password然后根据提示输入两次密码。
九、以后如何使用
配置只需要做一次。
以后每次登录服务器,进入终端后,直接输入:
jupyter lab浏览器访问:
http://服务器IP:端口/lab例如:
http://10.103.44.135:8888/lab输入之前设置的密码即可。
十、关闭 JupyterLab
如果使用完成,可以回到运行 JupyterLab 的终端:
Ctrl + C通常会出现:
Shutdown this Jupyter server (y/[n])?输入:
y即可关闭。
再次执行:
jupyter lab就可以重新启动。