管理员配置Jupterhub

  1. 在Ubuntu上演示如何部署R语言环境,包括

    1. posit的已经编译了一些R的安装

    2. 系统已经装了R,我额外编译的R如何让大家都能用到

    3. 如何配置RStudio

  2. jupyterhub的配置和使用

    1. 管理员如何配置jupyterhub

    2. 用户如何配置自己的jupyter环境

Ubuntu上的R部署

这一次,我们以从posit上下载预编译的R: https://docs.posit.co/resources/install-r/[1]

比如说Ubuntu 20.04的安装方法如下

sudo apt-get update
sudo apt-get install gdebi-core


export R_VERSION=4.3.1
curl -O https://cdn.rstudio.com/r/ubuntu-2004/pkgs/r-${R_VERSION}_1_amd64.deb
sudo gdebi r-${R_VERSION}_1_amd64.deb

#验证
/opt/R/${R_VERSION}/bin/R --version


但是此时安装的R并不能使用,因为他不在环境变量PATH中,为了让所有人能够使用,也为了方便切换版本,我们通过软连接的方式,将安装的R链接到/usr/local/bin

export R_VERSION=4.3.1

sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript

接下来配置RStudio server, 还是Ubuntu为例。

wget https://download2.rstudio.org/server/focal/amd64/rstudio-server-2023.06.1-524-amd64.deb
sudo gdebi rstudio-server-2023.06.1-524-amd64.deb

由于将R放在了/usr/local/bin下,因此RStudio server可以直接访问到这个R,也就不需要修改配置文件/etc/rstudio/rserver.conf

当然环境配置到此还不够,我们下一步还需要安装很多R包,为了简化这一过程,我将我自己常用的R包汇总到了一个文件中,见https://github.com/xuzhougeng/server-management-for-bioinformatics/blob/main/scripts/common-r-package.r[2]

你需要在服务器下载我的common-r-package.r或者新建一个文件,粘贴上述内容,即可。然后运行如下命令

export R_VERSION=4.3.1

sudo /opt/R/${R_VERSION}/bin/Rscript common-r-package.r

这样子服务器就会运行这个安装脚本。

如果是普通用户,那就是去掉这个sudo,会安装到自己的家目录下

export R_VERSION=4.3.1

/opt/R/${R_VERSION}/bin/Rscript common-r-package.r

JupyterHub

管理员配置Jupterhub

目标是在服务器上配置一个JupyterHub,支持多个用户登录使用。建议在Ubuntu 20.04以上哦

以root权限配置全局的JupyterHub作为入口

首先,我们安装python3和python

sudo apt install python3 python3-pip

然后,需要手动安装npm

wget https://nodejs.org/dist/v18.17.0/node-v18.17.0-linux-x64.tar.xz
tar xf node-v18.17.0-linux-x64.tar.xz
sudo mkdir -p /opt/node
sudo mv node-v18.17.0-linux-x64 /opt/node/18.17.0

sudo ln -s /opt/node/18.17.0/bin/* /usr/local/bin

安装configurable-http-proxy

sudo npm install -g  configurable-http-proxy --registry=http://registry.npmmirror.com 
sudo ln -s /opt/node/18.17.0/bin/configurable-http-proxy /usr/local/bin

最后配置jupyterhub

# 安装jupyterhub
sudo python3 -m pip install jupyterhub  -i https://pypi.mirrors.ustc.edu.cn/simple/

 

hub是一个中心,具体需要调用jupterlab和notebook,因此建议安装

# 安装jupter lab 和 notebook
sudo python3 -m pip install notebook  -i https://pypi.mirrors.ustc.edu.cn/simple/
sudo python3 -m pip install jupyterlab  -i https://pypi.mirrors.ustc.edu.cn/simple/

测试下安装状态

jupyterhub -h
configurable-http-proxy -h

Jupyterhub的启动有两种

1、基于命令行参数

sudo jupyterhub --ip 0.0.0.0 --port 8989

2、基于配置文件

第一步,生成配置文件

jupyterhub --generate-config

第二步,修改配置文件,主要就是ip和端口

# 设置访问方式
c.JupyterHub.bind_url = 'http://0.0.0.0:8989'

第三步,启动

sudo jupyterhub -f  jupyterhub_config.py

为了方便使用,我们还需要将jupyterhub变成系统服务

第一步,编辑文件

sudo vim /etc/systemd/system/jupyterhub.service

内容如下

[Unit]
Description=Jupyterhub service
After=syslog.target network.target

[Service]
ExecStart=/usr/local/bin/jupyterhub -f /etc/jupyterhub/config.py

[Install]
WantedBy=multi-user.target

还需要复制之前的配置文件

sudo mkdir -p /etc/jupyterhub
sudo cp  jupyterhub_config.py /etc/jupyterhub/config.py

第三步,启动服务

sudo systemctl daemon-reload

# 启动服务
sudo service jupyterhub start
  
# 看状态
sudo systemctl status jupyterhub.service

参考:https://github.com/jupyterhub/jupyterhub[3]

用户管理自己的Jupyter

我们需要掌握的一个知识点:jupyterhub会去$HOME/.local/share/jupyter/kernels 查找记录的kernels。

因此,在任何 Python[4] 环境,无论是系统级、用户级、venv[5] 还是 Conda[6] 环境,我们都可以通过ipykernel手动添加到 Jupyter[7] 的 Kernels 列表。以conda为例(关于conda,一定要去阅读我的https://github.com/xuzhougeng/server-management-for-bioinformatics/blob/main/conda-for-software-management.md[8])

以conda为例,我们可以为conda默认环境添加

pip install ipykernel -i https://pypi.mirrors.ustc.edu.cn/simple/
python -m ipykernel install --user --name="base" --display-name="base"
# Installed kernelspec base in /home/用户名/.local/share/jupyter/kernels/base

也可以新建一个python3.10的环境

conda create -n python3.10 python=3.10 ipython ipykernel

为其增加kernel

python -m ipykernel install --user --name="python310" --display-name="Python 3.10"
# Installed kernelspec python310 in /home/用户名/.local/share/jupyter/kernels/python310

之后,我们就可以在服务器看到这些内容(我配置了不少内容,包括R,还有一个单细胞课程用到的palantir)

管理员配置Jupterhub_第1张图片

如果你需要安装特定的python酷库,一种是在jupyter的notebook中选择好kernel,然后执行下面的命令

import sys
sys.executable
# 下面必须在jupyter中运行,例如palantir
!{sys.executable} -m pip install  palantir

当然,我更推荐推荐启动对应conda环境, 安装你的库。

conda activate python310
pip install palantir

插件

创建一般指的就是jupyterlab的插件,因为notebook就是一个book而已。

jupyterhub的插件功能通过pip的方式进行安装,例如python补全功能(注意pip得是对应的环境下的pip,在notebook中的pip可能是全局pip)

pip install -U jupyterlab-lsp  -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install -U "python-lsp-server[all]"  -i https://pypi.mirrors.ustc.edu.cn/simple/

部分拓展需要用nodejs (≥18),因此需要额外安装你一个node。

git clone https://gitee.com/mirrors/nvm.git
cd nvm
# 启动nvm
. ./nvm.sh
# 安装node=18
nvm install 18

安装的node要添加到环境变量PATH中

目前的语言模型比较火热,jupyterhub上有一个对应的插件,叫做jupyterhub AI。他的安装也非常的简便。

首先启动,我的一个环境

conda activate python3.10

安装插件

pip install jupyter_ai

然后打开jupyterhub

%load_ext jupyter_ai_magics

案例

%%ai chatgpt
Please generate the Python code to solve the 2D Laplace equation in cartesian coordinates.
Solve the equation on the square domain X=(0,1) and y=(0,1) with vanishing boundary conditions.
Plot the solutionu using Matplotlib.
Please also provide ean explanation.

此时会报错

管理员配置Jupterhub_第2张图片

原因,还没有给token,你需要一个OPENAI官方的token

%env OPENAI_API_KEY = OPENAI官方的token

但是依旧可能出现问题,因为服务器没有穿越能力

管理员配置Jupterhub_第3张图片

这个时候,你可以考虑使用代理线路(进行从另一个地方转发请求,如果担心被盗用,这个连接也提供了自建的方案)

%env OPENAI_API_BASE = https://api.openai-proxy.com/v1

感谢:https://www.openai-proxy.com/[9]

你可能感兴趣的:(r,llinux,jupyter,postgresql,数据库)