在阿里云ECS(Elastic Compute Service)上搭建 TensorFlow 或 PyTorch 深度学习环境,可以按照以下步骤进行。本指南适用于 Ubuntu 20.04/22.04 系统,并支持使用 GPU 提速(需选择带 GPU 的实例类型,如 ecs.gn6i-c8g1.2xlarge 等)。
🚀 一、准备工作
1. 购买并登录 ECS 实例
- 登录 阿里云控制台
- 创建一个 GPU 实例(如果需要 GPU 计算)
- 实例规格:例如
ecs.gn6i-c8g1.2xlarge(NVIDIA T4 GPU) - 镜像建议:Ubuntu 20.04/22.04 64位
- 安全组:开放 SSH(端口22),必要时开放 Jupyter 端口(如 8888)
- 实例规格:例如
⚠️ 注意:GPU 实例费用较高,请注意按量付费或包年包月策略。
2. 连接 ECS 实例
ssh root@<你的公网IP>
🛠️ 二、基础环境配置
1. 更新系统
sudo apt update && sudo apt upgrade -y
2. 安装 Python 和 pip
sudo apt install python3 python3-pip python3-venv -y
3. 创建虚拟环境(推荐)
python3 -m venv dl_env
source dl_env/bin/activate
📦 三、安装 NVIDIA 驱动和 CUDA(仅 GPU 实例)
方法一:使用阿里云自动安装脚本(推荐新手)
阿里云提供了自动化安装脚本:
wget https://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/install_gpu_driver_ubuntu.sh --no-check-certificate
sudo bash install_gpu_driver_ubuntu.sh
该脚本会自动安装合适的驱动和 CUDA 工具包。
✅ 安装完成后重启:
sudo reboot
方法二:手动安装(高级用户)
-
添加 NVIDIA PPA
sudo add-apt-repository ppa:graphics-drivers/ppa -y sudo apt update -
安装驱动(以 NVIDIA 535 为例)
sudo apt install nvidia-driver-535 -y -
安装 CUDA Toolkit(可选,PyTorch/TensorFlow 通常自带)
参考:NVIDIA CUDA 下载页 -
安装 cuDNN(一般通过 conda 或 pip 自动处理)
-
重启并验证:
nvidia-smi应显示 GPU 信息和驱动版本。
🐍 四、安装深度学习框架
激活虚拟环境:
source dl_env/bin/activate
✅ 安装 PyTorch(支持 GPU)
前往 PyTorch 官网 获取最新命令,例如:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
注:
cu118表示 CUDA 11.8,根据你的 CUDA 版本调整。
验证安装:
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
输出应为 True 表示 GPU 可用。
✅ 安装 TensorFlow(支持 GPU)
TensorFlow 2.x 支持 GPU(需 CUDA 11.2+ 和 cuDNN 8.1+)
pip install tensorflow[and-cuda]
或分别安装:
pip install tensorflow
验证:
python -c "import tensorflow as tf; print(tf.__version__); print(len(tf.config.list_physical_devices('GPU')) > 0)"
💡 提示:TensorFlow 对 CUDA 版本要求较严格,建议使用 Docker 更稳定。
🌐 五、可选:安装 Jupyter Notebook / Lab
pip install jupyter notebook
启动并远程访问:
jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root --no-browser
🔐 安全提示:建议设置密码或使用 SSH 隧道:
ssh -L 8888:localhost:8888 root@<公网IP>
🧰 六、其他常用工具
pip install numpy pandas matplotlib scikit-learn tqdm
🧪 七、测试 GPU 是否正常工作
PyTorch 测试
import torch
print("CUDA available:", torch.cuda.is_available())
print("GPU count:", torch.cuda.device_count())
if torch.cuda.is_available():
print("Current GPU:", torch.cuda.get_device_name(0))
TensorFlow 测试
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
📦 八、使用 Conda(替代 pip,更推荐管理复杂环境)
-
安装 Miniconda:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh -
创建环境并安装 PyTorch(Conda 会自动处理 CUDA 依赖):
conda create -n dl python=3.9 conda activate dl conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
✅ 总结:关键点
| 步骤 | 内容 |
|---|---|
| 1 | 选择 GPU 型号的 ECS 实例 |
| 2 | 安装 NVIDIA 驱动(推荐阿里云脚本) |
| 3 | 使用虚拟环境或 Conda 管理依赖 |
| 4 | 安装 PyTorch/TensorFlow 的 GPU 版本 |
| 5 | 验证 cuda.is_available() 或 GPU 设备列表 |
📚 参考文档
- 阿里云 GPU 驱动安装:https://help.aliyun.com/document_detail/52871.html
- PyTorch 官方安装:https://pytorch.org
- TensorFlow GPU 指南:https://www.tensorflow.org/install/gpu
如有需要,也可以使用 阿里云 AI 开发者平台 PAI 或 容器服务 + Docker 来简化部署。欢迎继续提问具体问题!
CLOUD技术笔记