完整脚本:
1.0
bash
#!/bin/bash
set -e
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠️ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
if ! command -v conda &> /dev/null; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p $HOME/miniconda3
rm ~/miniconda.sh
export PATH="$HOME/miniconda3/bin:$PATH"
eval "$($HOME/miniconda3/bin/conda shell.bash hook)"
else
echo "已检测到 Miniconda,跳过安装"
eval "$(conda shell.bash hook)"
fi
# 4. 删除已有 zhy 环境(如果存在)
if conda env list | grep -q "^zhy"; then
echo "--- 删除已有 Conda 环境 'zhy' ---"
conda env remove -n zhy -y
fi
# 5. 创建 Python 3.11 Conda 环境
echo "--- 创建 Conda 环境 'zhy' ---"
conda create -y -n zhy python=3.11
# 6. 激活环境
echo "--- 激活 Conda 环境 'zhy' ---"
conda activate zhy
# 7. 升级 pip
echo "--- 升级 pip ---"
pip install --upgrade pip
# 8. 安装 PyTorch + torchvision + torchaudio (CUDA 12.8)
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
pip install torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 --index-url https://download.pytorch.org/whl/cu128
# 9. 安装 Python 库
echo "--- 安装 python-docx 和 pillow ---"
pip install python-docx pillow
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate zhy"
- #! /bin/bash
告诉linux系统使用/bin/bash这个程序执行,如果写的是#! /bin/sh 那系统会用sh执行 - set -e
脚本中命令以执行失败退出时
2.0
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 初始化 conda,无论新安装还是已存在都要执行
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 4. 自动接受 Anaconda TOS
echo "--- 接受 Anaconda TOS ---"
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main || true
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r || true
# 5. 删除已有 zhy 环境(如果存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "--- 删除已有 Conda 环境 '$ENV_NAME' ---"
conda env remove -n "$ENV_NAME"
fi
# 6. 创建 Python 3.11 Conda 环境
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
# 7. 激活环境
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 8. 升级 pip
echo "--- 升级 pip ---"
pip install --upgrade pip
# 9. 安装 PyTorch + torchvision + torchaudio (CUDA 12.8)
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
pip install torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 --index-url https://download.pytorch.org/whl/cu128
# 10. 安装 Python 库
echo "--- 安装 python-docx 和 pillow ---"
pip install python-docx pillow
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate $ENV_NAME"
3.0
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 初始化 conda,无论新安装还是已存在都要执行
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 4. 自动接受 Anaconda TOS
echo "--- 接受 Anaconda TOS ---"
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main || true
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r || true
# 5. 配置 Conda 源(网络不可达时使用清华镜像)
function test_conda_network() {
python - <<END
import urllib.request
try:
urllib.request.urlopen("https://repo.anaconda.com/pkgs/main/linux-64/repodata.json", timeout=5)
exit(0)
except:
exit(1)
END
}
if ! test_conda_network; then
echo "--- 网络不可达官方源,切换到清华镜像 ---"
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --set show_channel_urls yes
else
echo "--- 官方源可用,使用官方源 ---"
fi
# 6. 删除已有 zhy 环境(如果存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "--- 删除已有 Conda 环境 '$ENV_NAME' ---"
conda env remove -n "$ENV_NAME"
fi
# 7. 创建 Python 3.11 Conda 环境
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
# 8. 激活环境
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 9. 升级 pip
echo "--- 升级 pip ---"
pip install --upgrade pip
# 10. 配置 pip 镜像(网络不可达官方 PyPI 时使用清华镜像)
function test_pypi_network() {
python - <<END
import urllib.request
try:
urllib.request.urlopen("https://pypi.org/simple", timeout=5)
exit(0)
except:
exit(1)
END
}
PIP_INSTALL="pip install"
if ! test_pypi_network; then
echo "--- 网络不可达官方 PyPI,使用清华镜像安装 Python 包 ---"
PIP_INSTALL="pip install -i https://pypi.tuna.tsinghua.edu.cn/simple"
else
echo "--- 官方 PyPI 可用 ---"
fi
# 11. 安装 PyTorch + torchvision + torchaudio (CUDA 12.8)
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
$PIP_INSTALL torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 --index-url https://download.pytorch.org/whl/cu128
# 12. 安装 Python 库
echo "--- 安装 python-docx 和 pillow ---"
$PIP_INSTALL python-docx pillow
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate $ENV_NAME"
4.0
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 4. 初始化 conda shell 并生效
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 5. 将 conda 永久加入 ~/.bashrc
if ! grep -q "conda.sh" "$HOME/.bashrc"; then
echo "--- 将 Conda 添加到 ~/.bashrc ---"
echo "# >>> Conda 初始化 >>>" >> "$HOME/.bashrc"
echo "export PATH=\"$MINICONDA_DIR/bin:\$PATH\"" >> "$HOME/.bashrc"
echo "eval \"\$($MINICONDA_DIR/bin/conda shell.bash hook)\"" >> "$HOME/.bashrc"
echo "# <<< Conda 初始化 <<<" >> "$HOME/.bashrc"
fi
# 6. 自动接受 Anaconda TOS
echo "--- 接受 Anaconda TOS ---"
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main || true
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r || true
# 7. 配置 Conda 源(网络不可达时使用清华镜像)
function test_conda_network() {
python - <<END
import urllib.request
try:
urllib.request.urlopen("https://repo.anaconda.com/pkgs/main/linux-64/repodata.json", timeout=5)
exit(0)
except:
exit(1)
END
}
if ! test_conda_network; then
echo "--- 网络不可达官方源,切换到清华镜像 ---"
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --set show_channel_urls yes
else
echo "--- 官方源可用,使用官方源 ---"
fi
# 8. 删除已有 zhy 环境(如果存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "--- 删除已有 Conda 环境 '$ENV_NAME' ---"
conda env remove -n "$ENV_NAME"
fi
# 9. 创建 Python 3.11 Conda 环境
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
# 10. 激活环境(直接在脚本中生效)
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 11. 升级 pip
echo "--- 升级 pip ---"
pip install --upgrade pip
# 12. 配置 pip 镜像(网络不可达官方 PyPI 时使用清华镜像)
function test_pypi_network() {
python - <<END
import urllib.request
try:
urllib.request.urlopen("https://pypi.org/simple", timeout=5)
exit(0)
except:
exit(1)
END
}
PIP_INSTALL="pip install"
if ! test_pypi_network; then
echo "--- 网络不可达官方 PyPI,使用清华镜像安装 Python 包 ---"
PIP_INSTALL="pip install -i https://pypi.tuna.tsinghua.edu.cn/simple"
else
echo "--- 官方 PyPI 可用 ---"
fi
# 13. 安装 PyTorch + torchvision + torchaudio (CUDA 12.8)
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
$PIP_INSTALL torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 --index-url https://download.pytorch.org/whl/cu128
# 14. 安装 Python 库
echo "--- 安装 python-docx 和 pillow ---"
$PIP_INSTALL python-docx pillow
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate $ENV_NAME"
echo "或者直接打开新终端即可使用 conda 命令。"
5.0 开发环境搭建完整版
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 4. 初始化 conda shell 并生效
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 5. 将 conda 永久加入 ~/.bashrc
if ! grep -q "conda.sh" "$HOME/.bashrc"; then
echo "--- 将 Conda 添加到 ~/.bashrc ---"
echo "# >>> Conda 初始化 >>>" >> "$HOME/.bashrc"
echo "export PATH=\"$MINICONDA_DIR/bin:\$PATH\"" >> "$HOME/.bashrc"
echo "eval \"\$($MINICONDA_DIR/bin/conda shell.bash hook)\"" >> "$HOME/.bashrc"
echo "# <<< Conda 初始化 <<<" >> "$HOME/.bashrc"
fi
# 6. 配置 Conda 使用清华镜像
echo "--- 配置 Conda 镜像源为清华 ---"
conda config --remove-key channels 2>/dev/null || true
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --set show_channel_urls yes
# 7. 删除已有 zhy 环境(如果存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "--- 删除已有 Conda 环境 '$ENV_NAME' ---"
conda env remove -n "$ENV_NAME"
fi
# 8. 创建 Python 3.11 Conda 环境(使用清华镜像)
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
# 9. 激活环境
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 10. 升级 pip
echo "--- 升级 pip ---"
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
# 11. 安装 PyTorch + torchvision + torchaudio (CUDA 12.8) 使用国内源
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
pip install torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 \
--index-url https://download.pytorch.org/whl/cu128
# 12. 安装 Python 库(使用清华 PyPI 镜像)
echo "--- 安装 python-docx 和 pillow ---"
pip install python-docx pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate $ENV_NAME"
echo " source ~/.bashrc"
echo "或者直接打开新终端即可使用 conda 命令。"
6.0 开发环境搭建脚本一键运行版 Docker
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 4. 初始化 conda shell 并生效
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 5. 将 conda 永久加入 ~/.bashrc
if ! grep -q "conda.sh" "$HOME/.bashrc"; then
echo "--- 将 Conda 添加到 ~/.bashrc ---"
echo "# >>> Conda 初始化 >>>" >> "$HOME/.bashrc"
echo "export PATH=\"$MINICONDA_DIR/bin:\$PATH\"" >> "$HOME/.bashrc"
echo "eval \"\$($MINICONDA_DIR/bin/conda shell.bash hook)\"" >> "$HOME/.bashrc"
echo "# <<< Conda 初始化 <<<" >> "$HOME/.bashrc"
fi
# 6. 配置 Conda 使用清华镜像
echo "--- 配置 Conda 镜像源为清华 ---"
conda config --remove-key channels 2>/dev/null || true
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --set show_channel_urls yes
# 7. 删除已有 zhy 环境(如果存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "--- 删除已有 Conda 环境 '$ENV_NAME' ---"
conda env remove -n "$ENV_NAME"
fi
# 8. 创建 Python 3.11 Conda 环境(使用清华镜像)
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
# 9. 激活环境
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 10. 升级 pip
echo "--- 升级 pip ---"
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
# 11. 安装 PyTorch + torchvision + torchaudio (CUDA 12.8) 使用国内源
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
pip install torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 \
--index-url https://download.pytorch.org/whl/cu128
# 12. 安装 Python 库(使用清华 PyPI 镜像)
echo "--- 安装 python-docx 和 pillow ---"
pip install python-docx pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
# 13. 安装 Docker(如果未安装)
if ! command -v docker &>/dev/null; then
echo "--- 安装 Docker ---"
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
else
echo "已检测到 Docker,跳过安装。"
fi
# 14. 启动 Docker 服务
echo "--- 启动 Docker 服务 ---"
sudo service docker start
# 15. 配置 Docker 镜像加速(daemon.json)
echo "--- 配置 Docker 镜像加速 ---"
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json >/dev/null <<EOF
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.1panel.live",
"https://hub.rat.dev"
]
}
EOF
sudo service docker restart
# 16. 加载本地镜像并启动 docker compose(如果存在 project_images.tar)
if [ -f "./project_images.tar" ]; then
echo "--- 加载本地镜像 ---"
sudo docker load -i ./project_images.tar
echo "--- 启动 Docker Compose ---"
sudo docker compose up -d
else
echo "未找到 ./project_images.tar,跳过镜像加载和 Compose 启动。"
fi
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate $ENV_NAME"
echo " source ~/.bashrc"
echo "或者直接打开新终端即可使用 conda 命令。"
7.0 开发环境搭建脚本一键运行版 跳过重复安装
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 4. 初始化 conda shell 并生效
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 5. 将 conda 永久加入 ~/.bashrc
if ! grep -q "conda.sh" "$HOME/.bashrc"; then
echo "--- 将 Conda 添加到 ~/.bashrc ---"
echo "# >>> Conda 初始化 >>>" >> "$HOME/.bashrc"
echo "export PATH=\"$MINICONDA_DIR/bin:\$PATH\"" >> "$HOME/.bashrc"
echo "eval \"\$($MINICONDA_DIR/bin/conda shell.bash hook)\"" >> "$HOME/.bashrc"
echo "# <<< Conda 初始化 <<<" >> "$HOME/.bashrc"
fi
# 6. 配置 Conda 使用清华镜像(若已配置则跳过)
echo "--- 配置 Conda 镜像源为清华 ---"
if ! conda config --show channels | grep -q "tuna.tsinghua"; then
conda config --remove-key channels 2>/dev/null || true
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --set show_channel_urls yes
fi
# 7. 创建 Python 3.11 Conda 环境(如果未存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "Conda 环境 '$ENV_NAME' 已存在,跳过创建。"
else
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
fi
# 8. 激活环境
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 9. 升级 pip(必要时)
echo "--- 升级 pip ---"
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
# 10. 安装 PyTorch(仅当未安装)
if python -c "import torch" &>/dev/null; then
echo "PyTorch 已安装,跳过。"
else
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
pip install torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 \
--index-url https://download.pytorch.org/whl/cu128
fi
# 11. 安装 Python 库(仅当缺失)
for pkg in python-docx pillow; do
if python -c "import ${pkg//-/_}" &>/dev/null; then
echo "$pkg 已安装,跳过。"
else
echo "安装 $pkg..."
pip install "$pkg" -i https://pypi.tuna.tsinghua.edu.cn/simple
fi
done
# 12. 安装 Docker(如果未安装)
if ! command -v docker &>/dev/null; then
echo "--- 安装 Docker ---"
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
else
echo "已检测到 Docker,跳过安装。"
fi
# 13. 启动 Docker 服务
echo "--- 启动 Docker 服务 ---"
sudo service docker start
# 14. 配置 Docker 镜像加速(仅当未配置)
DOCKER_DAEMON="/etc/docker/daemon.json"
if [ ! -f "$DOCKER_DAEMON" ] || ! grep -q "daocloud" "$DOCKER_DAEMON"; then
echo "--- 配置 Docker 镜像加速 ---"
sudo mkdir -p /etc/docker
sudo tee "$DOCKER_DAEMON" >/dev/null <<EOF
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.1panel.live",
"https://hub.rat.dev"
]
}
EOF
sudo service docker restart
else
echo "Docker 镜像加速已配置,跳过。"
fi
# 15. 加载本地镜像并启动 docker compose(如果存在 project_images.tar)
if [ -f "./project_images.tar" ]; then
echo "--- 加载本地镜像 ---"
sudo docker load -i ./project_images.tar
echo "--- 启动 Docker Compose ---"
sudo docker compose up -d
else
echo "未找到 ./project_images.tar,跳过镜像加载和 Compose 启动。"
fi
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " conda activate $ENV_NAME"
echo " source ~/.bashrc"
echo "或者直接打开新终端即可使用 conda 命令。"
8.0 开发环境搭建脚本一键运行版 安装软件 后台运行
bash
#!/bin/bash
set -e
# 检测是否以 sudo 运行
if [ "$EUID" -eq 0 ]; then
echo "⚠ 警告:请不要使用 sudo 运行此脚本!"
echo "请使用普通用户执行: ./install_dev_env.sh"
exit 1
fi
echo "=== 开始安装 AI 开发环境(无人交互版) ==="
# 1. 更新系统
echo "--- 更新系统 ---"
sudo apt update -y
sudo apt upgrade -y
# 2. 安装基础依赖
echo "--- 安装基础依赖 ---"
sudo apt install -y libfmt-dev libopencv-dev python3.11-dev ffmpeg wget curl build-essential
# 3. 安装 Miniconda(如果未安装)
MINICONDA_DIR="$HOME/miniconda3"
if [ ! -d "$MINICONDA_DIR" ]; then
echo "--- 安装 Miniconda ---"
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p "$MINICONDA_DIR"
rm ~/miniconda.sh
else
echo "已检测到 Miniconda 目录 $MINICONDA_DIR,跳过安装。"
fi
# 4. 初始化 conda shell 并生效
export PATH="$MINICONDA_DIR/bin:$PATH"
eval "$($MINICONDA_DIR/bin/conda shell.bash hook)"
# 5. 将 conda 永久加入 ~/.bashrc
if ! grep -q "conda.sh" "$HOME/.bashrc"; then
echo "--- 将 Conda 添加到 ~/.bashrc ---"
echo "# >>> Conda 初始化 >>>" >> "$HOME/.bashrc"
echo "export PATH=\"$MINICONDA_DIR/bin:\$PATH\"" >> "$HOME/.bashrc"
echo "eval \"\$($MINICONDA_DIR/bin/conda shell.bash hook)\"" >> "$HOME/.bashrc"
echo "# <<< Conda 初始化 <<<" >> "$HOME/.bashrc"
fi
# 6. 配置 Conda 使用清华镜像(若已配置则跳过)
echo "--- 配置 Conda 镜像源为清华 ---"
if ! conda config --show channels | grep -q "tuna.tsinghua"; then
conda config --remove-key channels 2>/dev/null || true
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --set show_channel_urls yes
fi
# 7. 创建 Python 3.11 Conda 环境(如果未存在)
ENV_NAME="zhy"
if conda env list | grep -q "^$ENV_NAME"; then
echo "Conda 环境 '$ENV_NAME' 已存在,跳过创建。"
else
echo "--- 创建 Conda 环境 '$ENV_NAME' ---"
conda create -n "$ENV_NAME" python=3.11 -y
fi
# 8. 激活环境
echo "--- 激活 Conda 环境 '$ENV_NAME' ---"
conda activate "$ENV_NAME"
# 9. 升级 pip(必要时)
echo "--- 升级 pip ---"
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
# 10. 安装 PyTorch(仅当未安装)
if python -c "import torch" &>/dev/null; then
echo "PyTorch 已安装,跳过。"
else
echo "--- 安装 PyTorch 2.8.0 + torchvision 0.23.0 + torchaudio 2.8.0 (CUDA 12.8) ---"
pip install torch==2.8.0+cu128 torchvision==0.23.0+cu128 torchaudio==2.8.0 \
--index-url https://download.pytorch.org/whl/cu128
fi
# 11. 安装 Python 库(仅当缺失)
for pkg in python-docx pillow; do
if python -c "import ${pkg//-/_}" &>/dev/null; then
echo "$pkg 已安装,跳过。"
else
echo "安装 $pkg..."
pip install "$pkg" -i https://pypi.tuna.tsinghua.edu.cn/simple
fi
done
# 12. 安装 Docker(如果未安装)
if ! command -v docker &>/dev/null; then
echo "--- 安装 Docker ---"
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
else
echo "已检测到 Docker,跳过安装。"
fi
# 13. 启动 Docker 服务
echo "--- 启动 Docker 服务 ---"
sudo service docker start
# 14. 配置 Docker 镜像加速(仅当未配置)
DOCKER_DAEMON="/etc/docker/daemon.json"
if [ ! -f "$DOCKER_DAEMON" ] || ! grep -q "daocloud" "$DOCKER_DAEMON"; then
echo "--- 配置 Docker 镜像加速 ---"
sudo mkdir -p /etc/docker
sudo tee "$DOCKER_DAEMON" >/dev/null <<EOF
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.1panel.live",
"https://hub.rat.dev"
]
}
EOF
sudo service docker restart
else
echo "Docker 镜像加速已配置,跳过。"
fi
# 15. 加载本地镜像并启动 docker compose(如果存在 project_images.tar)
if [ -f "./project_images.tar" ]; then
echo "--- 加载本地镜像 ---"
sudo docker load -i ./project_images.tar
echo "--- 启动 Docker Compose ---"
sudo docker compose up -d
else
echo "未找到 ./project_images.tar,跳过镜像加载和 Compose 启动。"
fi
# 16. 安装并安全配置 xplan-view
DEB_PKG="./xplan-view_amd64_beta_0.0.4.deb"
XPLAN_DIR="/opt/xplan-view"
# 安装 .deb 包
if dpkg -l | grep -q "xplan-view"; then
echo "xplan-view 已安装,跳过安装。"
else
if [ -f "$DEB_PKG" ]; then
echo "--- 安装 xplan-view ---"
sudo dpkg -i "$DEB_PKG" || sudo apt-get install -f -y
else
echo "未找到 $DEB_PKG,跳过安装。"
fi
fi
# 配置系统库路径
if [ ! -f /etc/ld.so.conf.d/xplan-view.conf ]; then
echo "--- 配置系统库路径 $XPLAN_DIR ---"
echo "$XPLAN_DIR" | sudo tee /etc/ld.so.conf.d/xplan-view.conf
sudo ldconfig
else
echo "系统库路径 $XPLAN_DIR 已配置,跳过。"
fi
# 自动检测 PyTorch 库路径并加入系统库路径
TORCH_LIB_DIR=$(python -c "import torch, os; print(os.path.join(os.path.dirname(torch.__file__), 'lib'))" || echo "")
if [ -n "$TORCH_LIB_DIR" ] && [ -d "$TORCH_LIB_DIR" ]; then
if [ ! -f /etc/ld.so.conf.d/torch.conf ]; then
echo "--- 配置 PyTorch 库路径 $TORCH_LIB_DIR ---"
echo "$TORCH_LIB_DIR" | sudo tee /etc/ld.so.conf.d/torch.conf
sudo ldconfig
fi
fi
# 重命名原始二进制,防止递归调用
if [ -f "$XPLAN_DIR/xplan-view" ] && [ ! -f "$XPLAN_DIR/xplan-view.real" ]; then
sudo mv "$XPLAN_DIR/xplan-view" "$XPLAN_DIR/xplan-view.real"
sudo chmod +x "$XPLAN_DIR/xplan-view.real"
fi
# 创建安全启动脚本
STARTUP_SCRIPT="/usr/local/bin/xplan-view"
if [ ! -f "$STARTUP_SCRIPT" ]; then
echo "--- 创建安全启动脚本 $STARTUP_SCRIPT ---"
sudo tee "$STARTUP_SCRIPT" >/dev/null <<EOF
#!/bin/bash
export LD_LIBRARY_PATH="$XPLAN_DIR:\$LD_LIBRARY_PATH"
exec "$XPLAN_DIR/xplan-view.real" "\$@"
EOF
sudo chmod +x "$STARTUP_SCRIPT"
else
echo "xplan-view 启动脚本已存在,跳过。"
fi
# 安装完成后自动运行
echo "--- 启动 xplan-view ---"
"$STARTUP_SCRIPT" &
echo "=== AI 开发环境安装完成 ==="
echo "要使用环境,请执行:"
echo " source ~/.bashrc"
echo " conda activate $ENV_NAME"