内容目录
Ubuntu 24.04配置安装 Jupyter Notebook教程
# 更新系统包
sudo apt update && sudo apt upgrade -y
# 安装 Python3、pip 和虚拟环境工具
sudo apt install python3 python3-pip python3-venv -y
# 1. 创建 Jupyter 虚拟环境目录(可自定义路径,这里以 /opt/hep-jupyter 为例)
sudo mkdir -p /opt/hep-jupyter
sudo chown $USER:$USER /opt/hep-jupyter # 将目录所有权赋予当前用户
cd /opt/hep-jupyter
# 2. 创建 Python 虚拟环境
python3 -m venv venv
# 3. 激活虚拟环境
source venv/bin/activate
sudo apt install nodejs npm -y
# 4. 安装 Jupyter Notebook(或 JupyterLab,根据需求选择)
# pip install jupyter notebook # 经典 Jupyter Notebook
pip install jupyterlab # 如果你更习惯 JupyterLab
# 生成 Jupyter Server 配置文件(JupyterLab 通用)
jupyter server --generate-config
# 为 JupyterLab 设置登录密码
jupyter server password
# 创建新的工作目录 /usr/software/jupyter/
sudo mkdir -p /usr/software/jupyter
# 将目录所有权赋予当前用户(避免权限问题)
sudo chown $USER:$USER /usr/software/jupyter
vim /root/.jupyter/jupyter_server_config.py
# JupyterLab 核心配置(root 用户专用)
c.ServerApp.ip = '0.0.0.0'
c.ServerApp.port = 1050
c.ServerApp.open_browser = False
c.ServerApp.root_dir = '/usr/software/jupyter'
c.ServerApp.token = ''
c.ServerApp.allow_root = True
# 可选:关闭安全检查(避免 root 运行的权限警告)
c.ServerApp.allow_remote_access = True
c.ServerApp.disable_check_xsrf = True
vim /etc/systemd/system/hep-jupyter.service
[Unit]
Description=HEP JupyterLab Service
After=network.target
[Service]
Type=simple
User=root
Group=root
# 加载全局环境变量(解决 JDK/Python 环境变量问题)
EnvironmentFile=-/etc/profile
WorkingDirectory=/opt/hep-jupyter
Environment="PATH=/opt/hep-jupyter/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# 启动命令(简化,无需手动指定 config,Lab 会自动读取 /root/.jupyter/jupyter_server_config.py)
ExecStart=/opt/hep-jupyter/venv/bin/jupyter lab --no-browser --ip=0.0.0.0 --port=1050 --notebook-dir=/usr/software/jupyter
# 重启策略(增加超时,避免频繁重启)
Restart=on-failure
RestartSec=5
TimeoutSec=30
[Install]
WantedBy=multi-user.target
# 1. 重新加载 systemd 配置
sudo systemctl daemon-reload
# 2. 启动 hep-jupyter 服务
sudo systemctl start hep-jupyter.service
# 3. 设置开机自启
sudo systemctl enable hep-jupyter.service
# 4. 查看服务状态(确认是否运行成功)
sudo systemctl status hep-jupyter.service
sudo ufw allow 1050/tcp
http://<你的服务器IP>:1050
# 停止服务
sudo systemctl stop hep-jupyter.service
# 重启服务
sudo systemctl restart hep-jupyter.service
# 查看服务日志
sudo journalctl -u hep-jupyter.service -f
# 安装目录导航插件(toc = Table of Contents)
pip install jupyterlab-toc
# 安装 LaTeX 扩展支持
pip install jupyterlab-latex
# 重启服务生效
sudo systemctl restart hep-jupyter.service