摘要:
本文手把手教你如何在 Linux 服务器上搭建基于 vLLM 的 Python 虚拟环境,并配置支持远程访问的 Jupyter Lab 服务。从创建虚拟环境到内核注册,再到安全密码设置与后台启动,全程无坑,小白也能轻松掌握!
🧰 一、创建 vLLM 虚拟环境
为了隔离依赖并保持环境干净,我们首先使用 conda
创建一个独立的虚拟环境。
conda create --name vllm python=3.12
确认安装后即可完成虚拟环境的创建。
🔌 二、激活虚拟环境
conda activate vllm
进入 vllm
环境后,我们就可以开始安装相关工具包了。
📦 三、安装 vLLM 和 Jupyter 支持
接下来我们安装 vLLM
及其配套的 Jupyter 工具包:
pip install vllm
pip install ipykernel jupyter
vllm
:用于大模型推理加速。ipykernel
:将虚拟环境注册为 Jupyter 内核。jupyter
:提供 Jupyter Lab 服务支持。
🔌 四、将虚拟环境注册为 Jupyter 内核
为了让 Jupyter Lab 能识别我们刚创建的 vllm
环境,需要注册内核:
python -m ipykernel install --user --name vllm --display-name "vllm"
--name
:内核名称--display-name
:在 Jupyter UI 中显示的名称
🔒 五、配置 Jupyter 安全密码
出于安全性考虑,我们需要对 Jupyter 的登录密码进行加密处理。
在 Python 命令行中执行如下命令生成加密密码:
from jupyter_server.auth import passwd
passwd()
系统会提示你输入并确认密码,完成后会输出一段加密字符串,请复制保存好。
📄 六、生成并配置 Jupyter Lab 配置文件
生成默认配置文件:
jupyter lab --generate-config
打开配置文件进行编辑:
vim ~/.jupyter/jupyter_lab_config.py
找到或添加以下配置项:
c.ServerApp.allow_origin = '*' # 允许跨域访问
c.ServerApp.allow_remote_access = True # 允许远程访问
c.ServerApp.ip = '0.0.0.0' # 绑定所有IP
c.ServerApp.open_browser = False # 不自动打开浏览器
c.ServerApp.password = 'sha1:xxxxxx...' # 替换为你刚刚生成的加密密码
c.ServerApp.port = 8002 # 设置访问端口
▶️ 七、后台启动 Jupyter Lab 服务
为了避免终端关闭导致服务中断,我们使用 nohup
后台运行:
nohup jupyter lab --allow-root > jupyterlab.log 2>&1 &
--allow-root
:允许 root 用户运行(视情况使用)> jupyterlab.log 2>&1 &
:将日志写入文件并在后台运行
你可以通过 tail -f jupyterlab.log
查看启动日志。
🌐 八、访问 Jupyter Lab 并使用 vLLM 内核
在浏览器中访问:
http://你的服务器IP:8002
输入你设置的密码,进入 Jupyter Lab 页面。新建 Notebook 时,选择 vllm
内核即可使用 vLLM 进行大模型开发和调试。
🎉 结语:开启你的 AI 开发之旅!
恭喜你完成了 vLLM + Jupyter Lab 的完整部署!现在你可以安全地远程访问并高效进行大模型推理开发了。
如果你觉得这篇文章对你有帮助,欢迎点赞收藏,也欢迎留言交流你在使用过程中的问题或心得。
祝你工作顺利,代码无 Bug,模型不掉链子!🚀
标签:
#vLLM部署教程
#Jupyter远程访问
#Python虚拟环境配置