ubuntu 22.04 anaconda comfyui安装
背景:
戴尔R740服务器,安装了proxmox操作系统,配置了显卡直通。创建了一个ubuntu 22.04 VM虚拟机实例,并安装了显卡驱动与cuda等相关配置:
接下来准备搭建一套comfyui的环境,前段时间B站,抖音各种刷到相关视频,自己也想玩一下!
ubuntu 22.04 anaconda comfyui
可以参考官方网站:https://docs.comfy.org/zh-CN/installation/manual_install
对于 ComfyUI 的安装, 主要分为几个步骤
- 创建一个虚拟环境(避免污染系统级 Python 环境)
- 克隆 ComfyUI 代码仓库
- 安装依赖
- 启动 ComfyUI
安装anaconda并配置虚拟环境
独立的虚拟环境是必要的,因为 ComfyUI 的依赖可能会与系统上的其他依赖冲突,也可以避免对系统级 Python 环境的污染。这里选择了使用anaconda安装python环境:
下载安装anaconda
通过访问:https://repo.anaconda.com/archive/ 选择对应安装程序:
下载并运行安装脚本:(在ubuntu 22.04 VM虚拟机实例上,这里使用了xshell远程连接)
wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh
bash Anaconda3-2024.10-1-Linux-x86_64.sh
全部默认yes yes 一路回车:
anaconda加入系统环境变量:
其实这里已经加入了 <font style="color:rgb(77, 77, 77);">~/.bashrc</font>
文件来激活安装
source ~/.bashrc
conda list
安装后的一些配置:
鉴于源的访问问题,配置源为清华源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/conda config --set show_channel_urls yespip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
创建comfyui的 python虚拟环境:
创建python 3.12的comfyui的运行环境,并进入该运行环境:
conda create -n comfyui python=3.12
conda activate comfyui
验证python的版本:
ubuntu系统的一些其他配置:
使用如下命令查看当前的分区状况:
lsblk
我在创建VM实例的时候明明分配了1.5T这里的根/默认的只有100G,需要处理一下(估计后续1.5T也不够需要扩容磁盘):
查看当前PV配置:
使用**pvdisplay **查看当前PV配置:
pvdisplay
分配剩余空间给根目录:
将卷组ubuntu-vg(VG)中所有剩余空间(+100%FREE
)分配给名为ubuntu-lv
的逻辑卷(LV),并使用r**esize2fs **命令调整文件系统以匹配新容量:
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
df -Th /
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
验证扩容:
使用如下命令验证lv扩容成功:
df -h
lsblk
comfyui的安装
参照官方网站:https://docs.comfy.org/zh-CN/installation/manual_install.
克隆仓库:
这里使用git克隆的方式安装(默认git已经安装),个人喜欢目录使用/data安装软件:
mkdir /data
cd /data
git clone https://github.com/comfyanonymous/ComfyUI.git
安装GPU 及 ComfyUI 依赖
可以参照github仓库README.md,安装GPU依赖:
cd /data/ComfyUI
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu128
安装 ComfyUI 依赖:
pip install -r requirements.txt
注意:执行命令的目录,必须是comfyui 的相对路径下,如我的目录绝对路径是/data/ComfyUI!
启动 ComfyUI
进入ComfyUI目录,执行如下启动命令,等待comfyui启动:
cd /data/ComfyUI
python main.py --port 15070 --listen 0.0.0.0
通过浏览器访问安装comfyui应用的VM实例主机的15070端口,可以看到如下页面:
运行默认的工作流,会报如下错误:
模型缺失,需要下载该模型到 models目录下,我这里直接使用了魔搭社区
通过该社区这里下载了模型:https://www.modelscope.cn/models/stabilityai/stable-diffusion-2-inpainting/files(否则就要通过科学上网,这里就偷懒了)
右击模型文件右侧下载,复制连接。获得下载地址,下载模型到Comfyui模型目录下:
cd /data/ComfyUI/models/checkpoints/
wget https://www.modelscope.cn/models/stabilityai/stable-diffusion-2-inpainting/resolve/master/512-inpainting-ema.ckpt
kill掉comfyui进程 ,重启comfyui程序:
一般的方式是ps -aux获取进程号,然后kill -9杀掉进程
也可以通过下面的方式杀死进程:
kill -9 $(ps aux | grep 'python main.py --port 15070 --listen 0.0.0.0' | grep -v grep | awk '{print $2}')
然后重新启动comfyui:
python main.py --port 15070 --listen 0.0.0.0
刷新浏览器重新运行工作量可以正常运行:
其他的一些配置
将comfyui注册为系统服务
每次使用命令启动comfyui的方式很不方便对服务的管理与维护,现在我们将comfyui注册为系统服务,通过systemctl管理comfyui:
vi /etc/systemd/system/comfyui.service
[Unit]
Description=ComfyUI Stable Diffusion Service
After=network.target
StartLimitIntervalSec=60[Service]
Type=simple
User=root
WorkingDirectory=/data/ComfyUI
Environment="PATH=/root/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/bin/bash -c 'source /root/anaconda3/etc/profile.d/conda.sh && conda activate comfyui && exec python main.py --port 15070 --listen 0.0.0.0'
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=comfyui
ProtectSystem=full
PrivateTmp=true
NoNewPrivileges=true
LimitNOFILE=65536[Install]
WantedBy=multi-user.target
重新加载 systemd 管理的服务单元文件,并尝试启动comfyui:
systemctl daemon-reload
systemctl start comfyui.service
通过如下命令确认服务正常启动:
ps -aux|grep python
systemctl status comfyui.service
并进一步使用浏览器可以正常访问服务:
注意:这里的15070端口可以通过个人喜好修改service文件中中启动命令的端口!
comfyui安装常用插件:
为了方便使用comfyui,需要安装一些常用的插件,如:comfyui-manager ComfyUI-Custom-Scripts等插件,请注意:插件的安装目录是ComfyUI 目录下的custom_nodes目录,ComfyUI-Manager 下载到本地目录名需要替换成小写,具体可以参照github文档!
cd /data/ComfyUI/custom_nodes
git clone https://github.com/ltdrdata/ComfyUI-Manager comfyui-manager
git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts.git
重启comfyui服务,等待服务重启:
systemctl restart comfyui.service
systemctl status comfyui.service
journalctl -u comfyui -f
通过浏览器访问comfyui,主要看ComfyUI-Manager这个插件,后续的插件模型可以通过这里去下载:
点击Costom Nodes manager 可以搜索下载插件,其他英文名词可以自行翻译:
代理相关
由于comfyui下载插件很多插件要放我github,模型的下载也需要访问HuggingFace。我局域网有一台cvm安装了科学上网工具,就在安装comfyui的设备上面需要加入变量的方式科学上网:
export http_proxy=http://192.168.0.31:20171export http_proxys=http://192.168.0.31:20171
不使用了 还要unset 。直接将其封装了一个service:
cat /etc/systemd/system/proxy.service
[Unit]
Description=Proxy Environment Variables Service
Documentation=https://example.com
After=network.target[Service]
Type=oneshot
RemainAfterExit=yes
Environment="PROXY_URL=http://192.168.0.31:20171"ExecStart=/bin/sh -c "\systemctl set-environment http_proxy=${PROXY_URL} && \systemctl set-environment https_proxy=${PROXY_URL} && \systemctl set-environment HTTP_PROXY=${PROXY_URL} && \systemctl set-environment HTTPS_PROXY=${PROXY_URL} && \echo 'Proxy enabled: ${PROXY_URL}'"ExecStop=/bin/sh -c "\systemctl unset-environment http_proxy && \systemctl unset-environment https_proxy && \systemctl unset-environment HTTP_PROXY && \systemctl unset-environment HTTPS_PROXY && \echo 'Proxy disabled'"ExecReload=/bin/sh -c "\systemctl set-environment http_proxy=${PROXY_URL} && \systemctl set-environment https_proxy=${PROXY_URL} && \systemctl set-environment HTTP_PROXY=${PROXY_URL} && \systemctl set-environment HTTPS_PROXY=${PROXY_URL} && \echo 'Proxy reloaded: ${PROXY_URL}'"# 解决status卡住问题的配置
StandardOutput=journal
TimeoutStopSec=5[Install]
WantedBy=multi-user.target
重新加载 systemd 管理的服务单元文件,并尝试启动proxy:
systemctl daemon-reload
systemctl start proxy.service
通过如下命令确认服务正常启动:
systemctl status proxy.service
关闭代理服务则输入stop 关闭proxy服务:
systemctl stop proxy.service
systemctl status proxy.service
当然关于模型的下载也可以使用ModelScope魔搭社区等实现。