NLP线上环境部署

Anconda环境部署

1、创建文件夹,下载Anaconda

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.05-Linux-x86_64.sh

2、安装

chmod +x Anaconda3-2021.05-Linux-x86_64.sh

./Anaconda3-2021.05-Linux-x86_64.sh

1
2
3
4
5
6
7
8
9
10
Please, press ENTER to continue
# 按enter 出现用户协议 一直按enter
Please answer 'yes' or 'no':'
# 输入 yes 按 enter
[/root/anaconda3] >>> /root/Anaconda
# 设置安装目录(必须未存在的文件夹),直接enter就是默认的目录,显示在左边中括号那个
Unpacking payload ...
#出现就是就是在安装了
by running conda init? [yes|no]
# 是否初始化conda,这里一定要输入yes 不要直接按enter,因为默认是no 然后就安装完成了

打开~/.bashrc会看到这段配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/Anaconda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/root/Anaconda/etc/profile.d/conda.sh" ]; then
. "/root/Anaconda/etc/profile.d/conda.sh"
else
export PATH="/root/Anaconda/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

3、验证安装

1
2
3
4
source ~/.bashrc
# 检查是否安装成功
conda -V
conda 4.10.1 # 出来这个就说明安装成功了

4、取消默认自动进入base环境

1
2
conda config --set auto_activate_base false
conda deactivate

5、配置清华镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 开启下载时通道显示
conda config --set show_channel_urls yes
# ./condarc 中追加镜像信息
channels:
- defaults
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

conda config --show验证

开发环境配置

1、新建nlp虚拟环境

1
conda create -n nlp python=3.6.8

Jupyter Lab环境搭建

1、安装jupyter lab

1
conda install jupyterlab

2、编辑配置信息

1
2
3
4
5
6
7
8
9
10
# 生成配置文件
jupyter notebook --generate-config
# 在配置文件中追加
c.ServerApp.open_browser = False # 禁止自动打开浏览器
c.ServerApp.ip='*' # 就是设置所有ip皆可访问
c.ServerApp.allow_remote_access = True # 允许远程访问
c.ServerApp.allow_root = True # 以root身份运行
c.ServerApp.port = 8888 # 指定端口,默认8888
c.ServerApp.root_dir = '/jupyter/xuxingchen' # 工作目录
c.ServerApp.password = ''

3、生成远程密码

1
2
3
# 进入python命令行
from jupyter_server.auth import passwd; passwd()
# 输入密码、验证密码后会输出'sha1:*****'结果,复制粘贴到配置文件中的c.ServerApp.password值中

4、使用conda的Python环境

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装依赖
conda install nb_conda_kernels
# 定位到anaconda/etc/jupyter/jupyter_config.json,修正配置为以下:
{
"CondaKernelSpecManager": {
"kernelspec_path": "--user",
"name_format": "{kernel} ({environment})"
}
}
# base下直接指定环境安装ipykernel
conda install -n 环境名称 ipykernel
# 切换到对应环境中 写入jupyter notebook 的kernel
python -m ipykernel install --user --name 环境名称

5、多用户配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 复制多份.jupyter/jupyter_lab_config.py,修改其中端口号与密码,以脚本形式后台执行
# 启动脚本
#!/bin/bash

source /root/Anaconda/etc/profile.d/conda.sh
# 切换环境
conda activate base

# 执行jupyter
root_dir="/jupyter/"
log_dir="/root/.jupyter/logs/"
declare -A dic
dic=([zl]=8888 [xj]=8889 [zgp]=8890 [xxc]=8891)
if [ $1 == "all" ]
then
for dirname in ${!dic[*]}
do
(jupyter-lab --port=${dic[$dirname]} --notebook-dir=$root_dir$dirname) > $log_dir$dirname.log 2>&1 &
sleep 2
cat $log_dir$dirname.log
echo "jupyter lab $dirname are running successfully"
done
else
(jupyter-lab --port=${dic[$1]} --notebook-dir=$root_dir$1) > $log_dir$1.log 2>&1 &
sleep 3
cat $log_dir$1.log
echo "jupyter lab $1 is running successfully"
fi

# 关闭脚本
#!/bin/bash

if [ $1 == "all" ]
then
list=("zl" "xj" "zgp" "xxc")
for dirname in ${!list[@]}
do
# 获取进程pid
PID=$(pgrep -f dir=/jupyter/${list[$dirname]})
if [ $PID ]
then
kill -n 15 $PID
echo "已终止${list[$dirname]} $PID"
else
echo "未找到${list[$dirname]}相关进程"
fi
done
else
# 获取进程pid
PID=$(pgrep -f dir=/jupyter/$1)
if [ $PID ]
then
kill -n 15 $PID
echo "已终止$1 $PID"
else
echo "未找到$1相关进程"
fi
fi

6、代码提示

① 先去Node官网下载安装包,解压安装到指定位置

1
2
3
4
5
6
7
8
9
10
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xJvf node-xxx.tar.xz -C /usr/local/lib/nodejs
vim ~/.profile

# 追加环境变量
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH
# 刷新bash环境
source ~/.profile
# 验证安装
node -v

② 关闭所有jupyter进程

③ 激活base环境,安装jupyterlab-lsp插件

1
2
3
4
conda activate
pip install jupyterlab-lsp
pip install python-lsp-server[all]
pip install nbclassic==0.2.8

④ 重启jupyter,看到左下角状态栏有✔Fully字样即为成功

⑤ 去除下划线纠错标识,工具栏 - Settings - Advanced Settings,开启json视图,添加下方键值对即可关闭:

1
{"ignoreMessagesPatterns": [".*"]}