Linux服务器上搭建Jupyter Lab远程访问

创建jupyter环境,在 jupyter 环境中安装nb_conda_kernels,其他包为常用插件

conda create -c conda-forge -n jupyter 'python >=3.7,<3.9.0a0' 'jupyterlab' 'jupyterlab-lsp' 'jupyter-lsp-python' 'xeus-python' 'perspective' 'jupyterlab-nvdashboard' 'jupyterlab-variableinspector' 'jupyterlab-git' 'nb_conda_kernels' 'pip install jupyter-lsp-python'
conda activate jupyter

已经建立的环境,可以手动安装ipykernel,这样jupyter lab就能自动识别到python虚拟环境

conda create -n openai python==3.8.19 ipykernel 

运行 jupyter lab --generate-config 生成配置文件(如果之前生成过Config配置文件则不需要这一步),然后输入 jupyter lab password,生成密码:

jupyter lab config文件需要改的参数:

c.ServerApp.allow_remote_access = True
c.ServerApp.ip = '*'

# 启动时不自动打开浏览器 
c.ServerApp.open_browser = False
c.LabServerApp.open_browser = False
c.ExtensionApp.open_browser = False
c.LabApp.open_browser = False

# 添加刚刚生成的密钥
c.ServerApp.password = 'XXXXXXX'

# 根据个人需要修改端口号(默认 8888)
c.ServerApp.port = 18888

将jupyter lab设置为开机自启动

vim start_jupyter.sh
#!/bin/bash
# 延迟30秒再启动
sleep 30
cd /home/jensen
# 激活 Conda 环境
#eval "$(conda shell.bash hook)"
su - jensen -c '
source /home/jensen/miniconda3/etc/profile.d/conda.sh
conda activate jupyter
nohup jupyter lab > /dev/null 2>&1 &
'
echo "JupyterLab已在后台启动"

加入开机自启动

sudo vim /etc/systemd/system/jupyter.service

在jupyter.service文件中添加以下内容:

[Unit]
Description=jupyter
After=network.target NetworkManager.service systemd-networkd.service iwd.service

[Service]
Type=simple
LimitNPROC=500
LimitNOFILE=1000000
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
Restart=always
ExecStartPre=/usr/bin/sleep 30s
ExecStart=/usr/local/bin/start_jupyter.sh
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

运行以下命令激活并启用该服务:

sudo systemctl daemon-reload
sudo systemctl enable jupyter.service
sudo systemctl start jupyter.service

jupyter lab实用插件推荐

https://github.com/ml-tooling/best-of-jupyter?tab=readme-ov-file#jupyterlab-extensions

Leave a Comment

您的邮箱地址不会被公开。 必填项已用 * 标注