Ubuntu20.04LTS部署JupyterLab

1.在服务器上安装jupyterlab、jupyter_server

pip install jupyterlab
pip install jupyter_server -i https://pypi.tuna.tsinghua.edu.cn/simple

安装完成后,输入下列命令检查是否安装完成:

pip list | grep jupyter

如果列出的pip包中有 jupyterlabjupyter_server ,说明安装成功了。

在这里插入图片描述

2.服务器端配置jupyterlab

2.1.生成jupyterlab的配置文件

输入下列命令进行配置:

jupyter notebook --generate-config 

注:如果该命令没有执行成功,且提示:ImportError: cannot import name 'soft_unicode' from 'markupsafe' ,尝试输入下列命令(更换markupsafe版本):

python -m pip install markupsafe==2.0.1

若输入完 jupyter notebook --generate-config 命令,显示:Writing default config to: /root/.jupyter/jupyter_notebook_config.py 说明配置成功了。

2.2.设置密码

输入下列命令,为jupyterlab设置密码:

jupyter notebook password

命令输入完,系统会显示 Enter password: ,此时你需要输入要设置的密码。输入完密码之后,系统会再次提示 Verify password ,需要你进行二次确认,你需要再次输入密码进行确认。

完成后系统会提示:Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json,说明设置的密码生效了。

3.服务器端启动jupyterlab

3.1安装ipykernel

pip install ipykernel

3.2.将环境写入jupyterlab的kernel核中

python -m ipykernel install --user --name 环境名称 --display-name "显示的名称"

例:python -m ipykernel install --user --name base --display-name "base_jupyterlab"

4.启动jupyter

jupyter-lab --ip 0.0.0.0 --port 8888 --no-browser --allow-root

启动完成后,访问 IP:8888 即可看到jupyterlab的界面。

5.持久化

nohup jupyter-lab --ip 0.0.0.0 --port 8888 --no-browser --allow-root > /home/xxx/jupyter.log 2>&1 &

6.更新Jupyter-Lab的默认的根目录

编辑Jupyter-Lab的配置文件

sudo vim /home/xxx/.jupyter/jupyter_lab_config.py

大约432行,找到c.ServerApp.notebook_dir,修改为自定义的目录,如下所示:

c.ServerApp.notebook_dir = '/home/xxx/'

7.Jupyter-lab添加到系统服务并设为开机自启动

7.1.新建jupyter.service文件

sudo vim /etc/systemd/system/jupyter.service  # 以.service为后缀,名字任意起

修改文件内容:

[Unit]
Description=Jupyterlab
After=network.target
[Service]
Type=simple
ExecStart=/home/ubuntu/anaconda3/bin/jupyter-lab --config=/home/ubuntu/.jupyter/jupyter_lab_config.py --no-browser  # jupyter-lab启动的命令
User=ubuntu  # 设置用户
Group=ubuntu  # 设置用户所在的组
WorkingDirectory=/home/ubuntu/  # 设置用户访问的根目录
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

7.2.启动服务

注意,这里用的是jupyter,也可以用其他的名字,但要跟jupyter.service.service前面的名字保持一致。

sudo systemctl start jupyter  # 启动
sudo systemctl stop jupyter  # 停止
sudo systemctl restart jupyter  # 重启

7.3.设置为开机自启动

sudo systemctl enable jupyter

猜你喜欢

转载自blog.csdn.net/m0_38068876/article/details/129596234