Ubuntu20.04系统安装,使用系统盘安装

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.9

conda 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

LangChainIntroduction | 🦜️🔗 LangChain
Stable DiffusionStable 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 np

for 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, SamPredictor

sam = 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 的关键在于:

  1. 安装兼容的 CUDA + PyTorch 环境;

  2. 下载官方模型权重;

  3. 使用 segment_anything 提供的 API 接口进行加载与预测;

  4. 可选:集成 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

如果你看到 transformersdiffusers 缺失,可以补装:

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 原生部署流程为:

  1. 安装环境(Python + PyTorch)

  2. 克隆项目并安装依赖

  3. 下载模型权重

  4. 使用 txt2img.py 脚本生成图像

4.4 Lora微调框架安装

5、AI加速框架安装

加速组件
vLLMvLLM - vLLM

 vllm安装

6、KVM虚拟化和Docker容器安装

容器化组件
KVM虚拟化
Dockerhttps://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

是否属于 libvirtkvm 组,如果没有就添加你自己进去:

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    # 删除虚拟机配置(不会删除磁盘文件)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值