1、系統安裝
Ubuntu20.04系統安裝,使用系統盤安裝
查看ubuntu系統版本
lsb_release -a
:顯示發行版名稱、版本號及代號
(base) root@ai-System-Product-Name:/media/ai/wh/clash-for-linux-master# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal
查看Ubuntu系統內核版本
uname -r
:直接顯示內核版本號
(base) root@ai-System-Product-Name:/media/ai/wh/clash-for-linux-master# uname -r
5.15.0-139-generic
2、NVIDIA驅動安裝
Ubuntu操作系統安裝Nvidia GPU驅動-物理機-用戶指南-實例-GPU驅動安裝說明 - 天翼云
Ubuntu操作系統安裝Nvidia GPU驅動
驗證驅動是否正常安裝
nvidia-smi
安裝cuna工具包(一般情況選擇帶有系統版本的CUDA就能直接安裝完)
CUDA Toolkit Archive | NVIDIA Developer
檢查 CUDA Toolkit 是否安裝成功
nvcc --version
操作系統及軟件相關 | 安裝 | 檢查命令 |
系統驅動 | uname -r? | |
C/C++編譯運行環境 | 檢查命令 ?gcc --version?、?g++ --version?、?cmake --version? | |
?Pytorch | Python -c "import torch"? | |
Langchain | Python -c "import langchain"? | |
?Stable - diffusion | 通過查看代碼倉庫有無文件判斷 | |
Lora微調框架 | 通過查看代碼倉庫有無文件判斷 | |
?Dockers容器組件 | 檢查命令 ?sudo systemctl status docker?、?Docker --version? | |
Python | python --version? |
?2.1 Pytorch安裝
PyTorch官網下載鏈接:Start Locally | PyTorch
離線“:https://download.pytorch.org/whl/torch/
首先更新系統并安裝必要的工具和依賴:
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev -y
確保pip是最新版本:
pip3 install --upgrade pip
進入虛擬環境,直接安裝
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
2.2 安裝Miniconda的步驟
Miniconda - Anaconda
下載Miniconda安裝腳本
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh
運行安裝腳本
bash Miniconda3-latest-Linux-x86_64.sh
按照提示進行操作:
-
按Enter查看許可協議
-
輸入
yes
接受許可條款 -
選擇安裝位置(默認在
~/miniconda3
) -
是否初始化Miniconda(建議選擇
yes
)
激活conda
source ~/.bashrc
驗證安裝
conda –version
配置環境(可選)
conda config --set auto_activate_base false
添加清華鏡像源(國內推薦)
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 --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
創建和管理環境
conda create -n xx_conda python=3.9conda activate xx_conda
conda activate xx_conda
卸載 Miniconda(如需)
rm -rf ~/miniconda3
清理 ~/.bashrc
中的 Conda 初始化代碼(刪除相關行)
模型下載
pip install modelscope
modelscope download --model Valdemardi/DeepSeek-R1-Distill-Qwen-32B-AWQ
3、編譯開發組件安裝
apt update 安裝基本開發組件
組件名稱 | 安裝命令 | 檢查命令 |
GUN調試工具(gdb) | ?sudo apt install gdb? | gdb --version? |
內存調試分析工具(valgrind) | sudo apt install valgrind? | valgrind --version????????? |
格式化C/C++代碼的工具(clang - format) | ?sudo apt install clang - format? | ?clang - format --version |
?C/C++代碼格式化和美化工具(astyle) | sudo apt install astyle? | astyle --version? |
版本控制工具組件(git) | ?sudo apt install git? | git --version? |
ssh設備訪問工具 | ?sudo apt install openssh - client?(客戶端 ), sudo apt install openssh - server?(服務端 ) | ssh -V? |
網絡工具組件(scp是ssh附帶工具 ) | ?scp -V? | |
下載網絡內容的非交互工具(wget) | ?sudo apt install wget? | wget --version? |
容器鏡像倉庫工具組件(docker) | sudo apt-get update && sudo apt-get install docker - ce docker - ce - cli containerd.io? | docker --version? |
系統分析與監控工具組件(性能分析 = perf) | sudo apt install linux - tools - common linux - tools - generic? | perf --version? |
系統性能分析(sysstat) | sudo apt install sysstat? | sar -V? |
增強的交互式系統監控工具(htop) | sudo apt install htop? | ?htop --version? |
網絡帶寬工具組件(nload) | sudo apt install nload? | nload --version? |
文本編輯工具(vim) | sudo apt install vim? | ?vim --version? |
命令文本編輯器工具(nano) | ?sudo apt install nano? | nano --version? |
開發庫和依賴工具組件(build - essential/tree ) | sudo apt install build - essential tree? | ?tree --version? |
樹狀圖列出目錄內容工具(tree) | ?tree --version? | |
基本的網絡診斷工具(ping) | 一般系統自帶,若缺失sudo apt install iputils - ping? | ping -V? |
?ifconfig | 一般系統自帶,若缺失sudo apt install net - tools? | ifconfig |
4、AI生態框架安裝
框架名稱 | |
PyTorch | |
LangChain | Introduction | 🦜?🔗 LangChain |
Stable Diffusion | Stable Diffusion API Docs | Stable Diffusion API Documentation |
SAM | GitHub - facebookresearch/segment-anything: The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model. GitCode - 全球開發者的開源社區,開源代碼托管平臺 |
Lora微調組件 |
4.1 LangChain-ChatChat 本地部署文檔
一、系統要求
-
操作系統:Ubuntu 20.04+
-
Python:推薦 3.9 或 3.10
-
內存:建議 ≥ 8GB
-
GPU(可選):用于部署大語言模型(如 Qwen、ChatGLM)
-
Conda(推薦)或 venv 用于隔離環境
二、環境準備
1. 安裝基礎依賴
sudo apt update
sudo apt install -y git wget curl build-essential
2. 安裝 Anaconda 或 Miniconda(推薦)
從官網下載安裝:https://docs.conda.io/en/latest/miniconda.html
3. 創建并激活環境
conda create -n chatchat python=3.10 -y conda activate chatchat
三、克隆項目
git clone https://github.com/chatchat-space/langchain-chatchat.git
cd langchain-chatchat
?
四、安裝依賴
pip install -r requirements.txt
安裝較慢時可使用國內鏡像:
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
五、下載語言模型
這里為了方便調用選擇小模型
手動下載
從 Hugging Face 或模型官方倉庫下載模型文件
六、配置啟動參數
編輯 .env
文件(或創建)設置默認參數:
LLM_MODEL=Qwen3-0.6B-Base
VECTOR_STORE_TYPE=faiss
EMBEDDING_MODEL=text2vec-base-chinese
七、運行服務
項目使用 FastAPI + Web UI + 后端服務,推薦使用一鍵腳本:
# 啟動服務(Web UI + FastAPI 后臺)
bash start.sh
等待約 30~60 秒后,打開瀏覽器訪問:
http://localhost:7860
八、測試功能
-
上傳 PDF 文檔、TXT、DOCX
-
輸入自然語言提問
-
支持多模型切換
-
支持上下文記憶、插件功能、對話歷史
4.2 SAM 部署流程文檔
一、系統要求
-
操作系統:Ubuntu 20.04
-
Python:推薦使用 Python 3.9+
-
顯卡驅動:NVIDIA GPU + CUDA 11.7 或以上
-
內存:≥8GB
-
顯卡顯存:≥6GB(建議使用 12GB 以上顯存)
二、環境準備
1. 安裝系統依賴
sudo apt update
sudo apt install -y git wget unzip build-essential libgl1-mesa-glx
2. 創建 Python 虛擬環境
# 安裝 Python 虛擬環境工具(可選)
sudo apt install python3.9-venv# 創建并激活虛擬環境
python3.9 -m venv sam_env
source sam_env/bin/activate
三、安裝 PyTorch
請根據你的 CUDA 版本從 PyTorch 獲取對應安裝指令。以下為示例:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
四、克隆 SAM 倉庫
git clone https://github.com/facebookresearch/segment-anything.git
cd segment-anything
pip install -e .
五、下載 SAM 模型權重
可以從 Meta 官方提供的地址下載權重模型(需要梯子):
# 創建模型目錄
mkdir -p ~/models/sam# 示例下載 ViT-H SAM 模型
wget https://dl.fbaipublicfiles.com/s????????egment_anything/sam_vit_h_4b8939.pth -P ~/models/sam
測試部署(命令行接口)
創建一個測試腳本 run_sam.py
:
import torch
from segment_anything import sam_model_registry, SamPredictor
import cv2# 加載模型
sam = sam_model_registry["vit_h"](checkpoint="/home/yourname/models/sam/sam_vit_h_4b8939.pth")
sam.to("cuda")predictor = SamPredictor(sam)# 加載圖像
image = cv2.imread("test.jpg")
predictor.set_image(image)# 示例點
input_point = [[500, 375]]
input_label = [1]masks, scores, logits = predictor.predict(point_coords=input_point,point_labels=input_label,multimask_output=True,
)# 保存掩碼圖
import matplotlib.pyplot as plt
import numpy as npfor i, mask in enumerate(masks):plt.imsave(f"mask_{i}.png", mask, cmap="gray")
運行:
python run_sam.py
七、(可選)部署為 Web 服務
你可以使用 gradio
快速部署一個 Web UI:
pip install gradio opencv-python
# 創建 web_app.py
import gradio as gr
import cv2
from segment_anything import sam_model_registry, SamPredictorsam = sam_model_registry["vit_h"](checkpoint="/home/yourname/models/sam/sam_vit_h_4b8939.pth")
sam.to("cuda")
predictor = SamPredictor(sam)def segment(image, x, y):predictor.set_image(image)masks, _, _ = predictor.predict(point_coords=[[x, y]], point_labels=[1], multimask_output=False)return masks[0]gr.Interface(fn=segment,inputs=["image", gr.Number(label="X"), gr.Number(label="Y")],outputs="image").launch()
運行:
python web_app.py
八、常見問題
問題 | 解決方法 |
---|---|
RuntimeError: CUDA out of memory | 使用較小的模型(如 ViT-B)、減小圖像尺寸 |
cv2.error: OpenCV | 確保圖像路徑正確,格式為 RGB |
下載權重失敗 | 使用代理或科學上網工具 |
九、總結
部署 SAM 的關鍵在于:
-
安裝兼容的 CUDA + PyTorch 環境;
-
下載官方模型權重;
-
使用
segment_anything
提供的 API 接口進行加載與預測; -
可選:集成 Web 服務或 API 接口用于后續調用。
4.3 Stable Diffusion 部署文檔
使用 CompVis 官方倉庫(或 Stability AI 的擴展版):
git clone https://github.com/CompVis/stable-diffusion.git
cd stable-diffusion
五、安裝依賴
pip install -r requirements.txt
如果你看到 transformers
、diffusers
缺失,可以補裝:
pip install diffusers transformers accelerate
六、下載模型權重
下載文件
https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt
七、運行圖像生成(命令行)
python scripts/txt2img.py \
? ? --prompt "a fantasy castle in the sky" \
? ? --plms \
? ? --ckpt models/ldm/stable-diffusion-v1/model.ckpt \
? ? --config configs/stable-diffusion/v1-inference.yaml \
? ? --outdir outputs/ \
? ? --n_samples 1 \
? ? --n_iter 1 \
? ? --H 512 --W 512 \
? ? --ddim_steps 50
?
八、輸出結果查看
圖片會保存在 outputs/
文件夾中。
九、測試示例
python scripts/txt2img.py \
? --prompt "a cyberpunk cityscape at night, neon lights, rain" \
? --ckpt models/ldm/stable-diffusion-v1/model.ckpt \
? --config configs/stable-diffusion/v1-inference.yaml \
? --outdir outputs/
?
十、常見問題解決
問題 | 解決方案 |
---|---|
CUDA out of memory | 嘗試減小圖像尺寸(如 384x384)、使用 --n_samples 1 |
No module named xformers | 可選模塊,pip install xformers (需編譯) |
權重文件太大下載失敗 | 可手動從瀏覽器下載 .ckpt 文件,放入指定目錄 |
十一、可選擴展
-
使用
diffusers
+transformers
替代原生結構,部署更輕量 -
部署 Gradio Web UI 示例(需要我可補充)
-
Docker 化部署
-
使用自動 Web UI(如 A1111 或 ComfyUI)
總結
Stable Diffusion 原生部署流程為:
-
安裝環境(Python + PyTorch)
-
克隆項目并安裝依賴
-
下載模型權重
-
使用
txt2img.py
腳本生成圖像
4.4 Lora微調框架安裝
5、AI加速框架安裝
加速組件 | |
vLLM | vLLM - vLLM |
?vllm安裝
6、KVM虛擬化和Docker容器安裝
容器化組件 | |
KVM虛擬化 | |
Docker | https://docs.docker.com/ |
docker-compose | |
Harbor鏡像倉庫 | Harbor docs | Harbor 2.4 Documentation? |
6.1 Dockers容器組件安裝
sudo apt update
sudo apt upgrade -y
lsb_release -a
三、安裝 Docker
1. 安裝必要的依賴
在安裝 Docker 之前,我們需要安裝一些必要的依賴包。運行以下命令:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
2. 添加 Docker 的官方 GPG 密鑰
通過以下命令添加 Docker 的官方 GPG 密鑰,以確保下載的軟件包的完整性:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
?
3. 添加 Docker 的 APT 源
接下來,我們需要添加 Docker 的 APT 源到您的系統中:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
4. 更新 APT 包索引
添加源后,更新 APT 包索引:
sudo apt update
5. 安裝 Docker CE
現在可以安裝 Docker 社區版(Docker CE):
sudo apt install docker-ce
6. 驗證 Docker 是否安裝成功
安裝完成后,您可以運行以下命令來驗證 Docker 是否成功安裝:
sudo systemctl status docker
如果 Docker 正在運行,您將看到類似以下的輸出:
● docker.service - Docker Application Container Engine
? ?Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
? ?Active: active (running)
四、啟動與測試 Docker
1. 啟動 Docker 服務
如果 Docker 服務沒有自動啟動,可以手動啟動它:
sudo systemctl start docker
2. 運行 Hello World 容器
您可以使用以下命令來測試 Docker 是否工作正常:
sudo docker run hello-world
【Docker】在 Ubuntu 22.04 以下版本上安裝 Docker 的詳細指南_ubuntu 安裝docker-CSDN博客
6.2 KVM虛擬化
如何在 Ubuntu 22.04 服務器上安裝和配置 KVM?_ubuntu 22.04 kvm-CSDN博客
檢查 CPU 是否支持虛擬化
egrep -c '(vmx|svm)' /proc/cpuinfo
安裝 KVM 及相關組件
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
-
qemu-kvm
: 提供虛擬化支持 -
libvirt-*
: 提供虛擬機管理 API -
virt-manager
: 圖形化虛擬機管理工具(可選)
檢查安裝結果
sudo systemctl status libvirtd
查看是否已啟動。啟動命令為:
sudo systemctl enable --now libvirtd
查看當前用戶是否可以使用 KVM
groups
是否屬于 libvirt
和 kvm
組,如果沒有就添加你自己進去:
sudo usermod -aG libvirt,kvm $USER
然后重新登錄或重啟。
創建和運行虛擬機(兩種方式)……
方法一:圖形界面(推薦初學者)
virt-manager
會打開圖形界面,點擊 “創建虛擬機” 并加載 ISO 鏡像安裝系統。
方法二:命令行
sudo virt-install \
? --name ubuntu-vm \
? --ram 4096 \
? --vcpus 2 \
? --disk path=/var/lib/libvirt/images/ubuntu-vm.qcow2,size=20 \
? --os-type linux \
? --os-variant ubuntu20.04 \
? --cdrom /path/to/ubuntu.iso \
? --network network=default \
? --graphics vnc
?
虛擬磁盤說明
KVM 默認使用 .qcow2
格式的虛擬磁盤(支持壓縮、快照)。
配置文件路徑
-
虛擬機鏡像:
/var/lib/libvirt/images/
-
網絡配置:
/etc/libvirt/qemu/networks/
-
虛擬機 XML 配置:
/etc/libvirt/qemu/
常用管理命令(命令行)
virsh list --all ? ? ? ? ? ?# 列出所有虛擬機
virsh start ubuntu-vm ? ? ? # 啟動虛擬機
virsh shutdown ubuntu-vm ? ?# 關閉虛擬機
virsh destroy ubuntu-vm ? ? # 強制關閉虛擬機
virsh undefine ubuntu-vm ? ?# 刪除虛擬機配置(不會刪除磁盤文件)