R/Conda 环境备份与 JupyterHub 服务配置,方便系统重装后恢复工作环境。

R 包的备份与恢复

  • 运行以下 R 代码,备份已安装包的列表,后续重新安装。
1
2
3
4
5
6
# 保存 CRAN 包列表
installed_pkgs <- installed.packages()[, "Package"]
writeLines(installed_pkgs, "installed_packages.txt")

# 使用 sessionInfo() 可以查看当前环境的信息,建议也保存这个信息到一个单独的文本文件
sessionInfo()
  • 备份 R 配置文件:

    • 全局配置文件:~/.Rprofile
    • 环境变量文件:.Renviron
    • RStudio 配置:~/.config/rstudio
  • 恢复全局配置文件:将备份的 .Rprofile.Renviron 和 RStudio 配置复制到新系统的对应路径。

  • 安装 R 包:

1
2
3
4
5
6
installed_pkgs <- readLines("installed_packages.txt")
install.packages(installed_pkgs)

# 有些包要通过 Bioconductor/github 安装
remotes::install_github(github_pkgs)
BiocManager::install(bioc_pkgs)
  • 安装时可能存在的问题:
    • 依赖的系统库缺失:某些 R 包需要系统级依赖,需要在新系统中重新安装。
    • 包版本冲突:指定版本安装,可查看先前保存的 R session 信息进行排查。

Conda 环境的备份与恢复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看环境
conda env list

# 导出当前活动环境的配置到 YAML 文件
conda env export --name 环境名 > environment.yml

# 根据 YAML 文件创建环境
conda env create -f environment.yml --name 新环境名

# 强制覆盖
conda create -n myenv -f env.yml --force

# 删除环境
conda remove -n myenv --all

JupyterHub 配置记录

NOTE: This tutorial copied from https://jupyterhub.readthedocs.io/en/stable/tutorial/quickstart.html 2025.06.06 with modification. Refer to the latest official document if you need.

由于 Linux 需要重做系统解决历史遗留问题,这里备份一下 JupyterHub 配置记录,方便后续复用。

JupyterHub is the best way to serve Jupyter notebook for multiple users. Because JupyterHub manages a separate Jupyter environment for each user, it can be used in a class of students, a corporate data science group, or a scientific research group. It is a multi-user Hub that spawns, manages, and proxies multiple instances of the single-user Jupyter notebook server.

Prerequisites

Before installing JupyterHub, we need:

  • a Linux/Unix-based system: our Ubuntu 22.04
  • Python version > 3.8 (we use 3.13.2)
  • Node.js 18.0 (12.0 comes with errors), use NodeSource:
1
2
3
4
5
6
7
8
# 移除旧版 nodejs(可选)
sudo apt remove --purge nodejs npm

# 添加 NodeSource 仓库(以 v20.x LTS 为例)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

# 安装新版本 Node.js 和 npm
sudo apt install -y nodejs
  • Don't install with conda for the global usage in our system
  • PAM is available by default

Installation

JupyterHub can be installed with pip (and the proxy with npm) or conda:

1
2
3
sudo python3 -m pip install jupyterhub
sudo npm install -g configurable-http-proxy
sudo python3 -m pip install jupyterlab notebook

Test your installation. If installed, these commands should return the packages' help contents:

1
2
jupyterhub -h
configurable-http-proxy -h

Start the Hub server

To allow multiple users to sign in to the Hub server, you must start jupyterhub as a privileged user, such as root:

1
sudo jupyterhub

Modify configuration to allow multiple user login with their system account.

1
2
sudo mkdir -p /etc/jupyterhub
sudo nano /etc/jupyterhub/jupyterhub_config.py

Add:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 允许所有系统用户登录
c.Authenticator.allow_all = True

# 确保使用 PAM 认证
c.PAMAuthenticator.open_sessions = False # 解决 Ubuntu 上的 PAM 问题

# 添加以下可选配置
# c.JupyterHub.trust_user_provided_cookie = True
c.JupyterHub.shutdown_on_logout = False
c.JupyterHub.authenticate_prometheus = False

c.Authenticator.admin_users = {'zyf'} # 添加您的管理员用户名
c.JupyterHub.port = 8888

# 设置工作目录
c.Spawner.default_url = '/lab' # 默认使用 JupyterLab
c.Spawner.notebook_dir = '~' # 设置用户工作目录

# 解决路径问题
import os
c.Spawner.environment = {
"PATH": os.environ["PATH"],
"VIRTUAL_ENV": os.environ.get("VIRTUAL_ENV", "")
}

Use systemd to auto run JupyterHub when system start.

1
sudo nano /etc/systemd/system/jupyterhub.service

Add:

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=JupyterHub
After=syslog.target network.target

[Service]
Environment="PATH=/opt/conda/envs/jupyterhub/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/opt/conda/envs/jupyterhub/bin/jupyterhub -f /etc/jupyterhub/jupyterhub_config.py

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
# 重新加载 systemd
sudo systemctl daemon-reload

# 启动服务
sudo systemctl start jupyterhub

# 启用开机启动
sudo systemctl enable jupyterhub

# 检查状态
sudo systemctl status jupyterhub

Enable frp

1
sudo vi /home/network/frp/frp_0.62.1_linux_amd64/frpc.toml

Add:

1
2
3
4
5
6
[[proxies]]
name = "jupyter"
type = "tcp"
localIP = "127.0.0.1"
localPort = 8888
remotePort = 8786

If not, using ssh tunnel.

JupyterHub Usage

JupyterHub running at server port 8888, Use ssh on your computer in terminal:

1
ssh -L 8887:localhost:8888 user@39.102.66.182 -p 6005

Visit at your personal computer: localhost:8887 to use JupyterHub, login with your account.

For win, recommend use mobaxterm tunnel to get stable ssh tunnel.

user default work directory is /home/user.

Recommend adding kernel for yourself, for example (using conda):

1
2
3
4
5
6
conda create -n your_env_name python=3.12
conda activate your_env_name
conda install ipykernel

# Add kernel
python -m ipykernel install --user --name your_env_name --display-name "your_env_name_display"

NOTE: Default system CUDA version: 11.5 Use module to load cuda version 10.1 / 11.2 / 12.2

1
2
3
4
module avail
module purge
module load cuda/12.2
nvcc --version