conda一键安装脚本

conda一键安装脚本特点

  • 🚫 禁止以 root 用户运行
  • 📦 检查安装包是否已存在
  • 🧠 自动接受 ToS 协议
  • ⚡ 配置清华镜像源
  • 🐍 自动创建并激活 Python 3.10 环境 py310

清华conda源禁止root安装提示403

  • 支持ubuntu/centos系列操作系统

完整脚本

install_conda_with_py310.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
set -eo pipefail

echo "=== Conda 一键安装脚本(含清华源、ToS 接受、py310 环境创建)==="

# 0. 检查是否为 root 用户
if [[ "$EUID" -eq 0 ]]; then
echo "❌ 本脚本禁止以 root 用户运行,请使用普通用户执行。"
exit 1
fi

# 1. 检查 wget 是否安装
echo "[1/7] 检查 wget..."
command -v wget &>/dev/null || { echo "正在安装 wget..."; sudo dnf install -y wget || sudo apt install -y wget; }

# 2. 下载 Miniconda 安装包
CONDA_INSTALLER="Miniconda3-latest-Linux-x86_64.sh"

#INSTALL_URL="https://repo.anaconda.com/miniconda/${CONDA_INSTALLER}"

INSTALL_URL="https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/${CONDA_INSTALLER}"

echo "[2/7] 检查安装包..."
if [[ -f "$CONDA_INSTALLER" ]]; then
echo " => 安装包已存在,跳过下载"
else
wget -c "$INSTALL_URL" -O "$CONDA_INSTALLER"
fi

# 3. 安装 Miniconda
INSTALL_PREFIX="$HOME/miniconda3"
if [[ -d "$INSTALL_PREFIX" ]]; then
echo " => 已安装 Miniconda 于 $INSTALL_PREFIX,跳过安装"
else
echo "[3/7] 安装 Miniconda 到 $INSTALL_PREFIX"
bash "$CONDA_INSTALLER" -b -p "$INSTALL_PREFIX"
echo 'export PATH="$HOME/miniconda3/bin:$PATH"' >> ~/.bashrc
export PATH="$HOME/miniconda3/bin:$PATH"
fi

# 4. 初始化 Conda
echo "[4/7] 初始化 Conda..."
conda init bash || true
source ~/.bashrc

# 5. 自动接受 TOS
echo "[5/7] 自动接受 Anaconda Terms of Service..."
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

# 6. 配置清华源
echo "[6/7] 配置清华 TUNA 镜像..."
cat > ~/.condarc <<EOF
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
EOF

# 7. 创建并激活 py310 环境
echo "[7/7] 创建 Conda 环境 py310(python=3.10)..."
if conda env list | grep -qE '^\s*py310\s'; then
echo " => 环境 py310 已存在,跳过创建"
else
conda create -n py310 python=3.10 -y
fi

echo "✅ 激活环境 py310"
conda activate py310

echo "✅ Conda 安装 & py310 环境已完成。当前 Python 版本:$(python --version)"

使用方式

1
2
chmod +x install_conda_with_py310.sh
./install_conda_with_py310.sh

执行结果示例

1
2
3
4
(base) $ ./install_conda_with_py310.sh

激活环境 py310
(py310) $