在Ubuntu 24.04部署JupyterLab实现远程访问
首先同步软件包列表。
sudo apt update
通过官方仓库安装必要的运行时环境,包括Python和Node.js:
sudo apt install python3-dev python3-pip python3-venv nodejs npm
确认提示后输入"Y"继续安装。
部署JupyterLab
安装完系统依赖后,将在Python虚拟环境中通过pip安装Jupyter。出于安全考虑,建议使用非root用户运行。
切换到目标用户(这里以devuser为例):
su - devuser
创建并激活新的虚拟环境:
mkdir workspace
python3 -m venv workspace
source workspace/bin/activate
通过pip安装Jupyter核心组件:
pip3 install jupyter
配置Jupyter服务
接下来需要配置jupyter_server和jupyterlab两个核心组件。首先生成配置文件并设置访问密码:
jupyter server --generate-config
jupyter server password
生成JupyterLab配置文件并查看当前配置:
jupyter lab --generate-config
jupyter lab --show-config
编辑JupyterLab配置文件:
sudo nano /home/devuser/.jupyter/jupyter_lab_config.py
在文件中添加以下参数,允许所有网络接口访问,并设置工作目录:
c.ServerApp.ip = '*'
c.ServerApp.notebook_dir = '/home/devuser/projects/'
完成配置后,通过以下命令启动JupyterLab:
jupyter lab
然后通过浏览器访问 http://服务器IP:8888/lab
配置为系统服务
为了实现后台持续运行,需要将JupyterLab配置为systemd服务。
创建服务配置文件:
sudo nano /etc/systemd/system/jupyter.service
填入以下内容,请将用户名devuser替换为实际使用的用户:
[Unit]
Description=JupyterLab Daemon
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/devuser/workspace/bin/jupyter lab --config=/home/devuser/.jupyter/jupyter_lab_config.py
User=devuser
Group=devuser
WorkingDirectory=/home/devuser/workspace
Restart=on-failure
RestartSec=15
[Install]
WantedBy=multi-user.target
重新加载systemd并启动服务:
sudo systemctl daemon-reload
sudo systemctl start jupyter
sudo systemctl enable jupyter
查看服务运行状态:
sudo systemctl status jupyter
配置防火墙放行
如果系统启用了防火墙,需要开放8888端口:
sudo ufw allow 8888/tcp
sudo ufw reload
完成上述步骤后,即可在远程浏览器通过 http://服务器IP:8888/lab 访问JupyterLab环境。