安装 Miniconda
在 Linux 服务器中安装 Miniconda 的步骤如下。本次使用的是 Ubuntu 20.04 系统。
cd /home/ubuntu
# Anaconda
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.07-2-Linux-x86_64.sh &&
bash Anaconda3-2023.07-2-Linux-x86_64.sh &&
rm -rf Anaconda3-2023.07-2-Linux-x86_64.sh
# Miniconda
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh &&
bash Miniconda3-latest-Linux-x86_64.sh &&
rm -rf Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc
conda create -n work python=3.10 && conda activate work
python -m pip install --upgrade pip &&
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install torch torchvision matplotlib jupyterlab
flopy smt gstools seaborn scikit-opt
screen -S work && conda activate work
mkdir -p /home/ubuntu/work
jupyter lab
--notebook-dir=/home/ubuntu/mount0/Python
--ip=0.0.0.0
--port=8888
--NotebookApp.token=123456
--no-browser --allow-root
cd /home/ubuntu/work
wget https://github.com/MODFLOW-USGS/executables/releases/latest/download/linux.zip
unzip linux.zip -d ./modflow
设置开机自启
如果服务器常年不断电,则使用 Screen 持久化运行体验良好。
但本人物理机设置了一周一次重启,每次重启后都要重新启动 Jupyter lab 实在太过麻烦。
因此将 Jupyter lab 设为开机自动启动。具体步骤如下:
jupyter.service
:sudo vim /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter Lab
[Service]
Type=simple
ExecStart=/bin/bash -c "/home/ubuntu/miniconda3/envs/work/bin/jupyter lab --notebook-dir=/home/ubuntu/mount0/Python --ip=0.0.0.0 --port=8888 --NotebookApp.token=123456 --no-browser --allow-root"
User=ubuntu
WorkingDirectory=/home/ubuntu/mount0/Python
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
❝
需要注意,如果直接用 jupyter lab 启动服务会返回报错,这是因为一方面 conda 未必有系统环境变量,另一方面安装的 jupyter lab 本身也未必在 conda 的 base 环境里。
因此,应使用完整路径来指定 conda 环境中的 Jupyter Lab 可执行文件,如本次为:
/home/ubuntu/miniconda3/envs/work/bin/jupyter lab
。❞
sudo systemctl daemon-reload
sudo systemctl start jupyter.service
sudo systemctl status jupyter.service
此时输出以下内容则说明配置成功:
● jupyter.service - Jupyter Lab
Loaded: loaded (/etc/systemd/system/jupyter.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-09-19 11:11:20 CST; 1h 12min ago
Main PID: 665 (jupyter-lab)
Tasks: 1 (limit: 28657)
Memory: 108.7M
CGroup: /system.slice/jupyter.service
└─665 /home/ubuntu/miniconda3/envs/work/bin/python /home/ubuntu/miniconda3/envs/work/bin/jupyt>
......