如何在阿里云ECS实例上配置深度学习环境?

在阿里云ECS实例上配置深度学习环境,可以按照以下步骤进行:


1. 选择并创建ECS实例

  • 镜像选择:推荐使用 Ubuntu 20.04/22.04CentOS 7/8(兼容性较好)。
  • 实例规格:根据需求选择GPU实例(如 gn6ign7i 等),若仅学习可用CPU实例。
  • 系统盘:建议至少 50GB(深度学习库和数据集占用空间大)。
  • 安全组:开放SSH(22端口)、Jupyter Notebook(8888端口)等必要端口。

2. 连接ECS实例

  • 通过SSH连接:
    ssh root@<你的ECS公网IP>

3. 安装基础依赖

Ubuntu/Debian

apt update && apt upgrade -y
apt install -y python3-pip python3-dev git wget curl

CentOS

yum update -y
yum install -y python3-pip python3-devel git wget curl

4. 安装NVIDIA驱动(仅GPU实例)

  • 自动安装(推荐):
    ubuntu-drivers autoinstall
    reboot
  • 验证驱动
    nvidia-smi

5. 安装CUDA和cuDNN

  • CUDA
    • 访问 NVIDIA官网 选择版本(如CUDA 11.8)。
      wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
      sudo sh cuda_11.8.0_520.61.05_linux.run
  • cuDNN
    • 下载对应版本并安装(需注册NVIDIA账号)。

6. 安装深度学习框架

PyTorch

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

TensorFlow

pip3 install tensorflow[and-cuda]

验证安装

python3 -c "import torch; print(torch.cuda.is_available())"
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

7. 配置Jupyter Notebook(可选)

pip3 install jupyter
jupyter notebook --generate-config
jupyter notebook password  # 设置密码
  • 远程访问
    修改配置文件 ~/.jupyter/jupyter_notebook_config.py

    c.NotebookApp.ip = '0.0.0.0'
    c.NotebookApp.open_browser = False
    c.NotebookApp.port = 8888
  • 后台运行
    nohup jupyter notebook --allow-root &

8. 环境优化建议

  • 持久化环境:使用 condavirtualenv 管理环境。
  • 数据存储:挂载阿里云 NASOSS 存储数据集。
  • 监控:安装 gpustathtop 等工具监控资源。

常见问题

  • GPU不可用:检查驱动/CUDA版本与框架的兼容性。
  • pip速度慢:更换阿里云镜像源:
    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

通过以上步骤,你可以在阿里云ECS上快速搭建深度学习环境。根据实际需求调整版本和配置,并注意定期更新驱动和库以保持兼容性。

云服务器