在Conda环境中安装Pytorch的.whl包教程

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

在 Conda 环境中安装 PyTorch 的 .whl 包教程

​ 本教程将指导你在 Conda 环境中通过手动下载和安装 PyTorch 的 .whl 文件来安装 PyTorch。这种方法适用于无法直接通过 conda installpip install 获取 PyTorch 的场景(如离线环境或特定版本需求)。

whl链接汇总:

torch

torchvision

一、前提条件

  • 已安装 Anaconda 或 Miniconda。
  • 了解你的系统环境,包括:
    • 操作系统(Windows、Linux 或 macOS)
    • Python 版本(例如 3.8、3.9 等)
    • 是否需要 GPU 支持(需要 CUDA 和 cuDNN 版本匹配)
  • 确保有足够的磁盘空间存储 .whl 文件和安装后的 PyTorch。

二、安装torch

1. 创建或激活 Conda 环境

首先,创建一个新的 Conda 环境或激活已有的环境,以确保 PyTorch 安装在一个隔离的环境中。

# 创建一个新环境(例如 Python 3.9)
conda create -n pytorch_env python=3.9

# 激活环境
conda activate pytorch_env

2. 确定所需的 PyTorch 版本

访问 PyTorch 官方网站(https://pytorch.org) PyTorch 的历史版本页面(https://pytorch.org/get-started/previous-versions/)查找适合你的系统配置的 .whl 文件。需要确认以下信息:

  • PyTorch 版本(例如 2.0.1、1.13.1 等)
  • Python 版本(例如 cp39 表示 Python 3.9)
  • 操作系统(例如 linux_x86_64、win_amd64、macosx_10_9_x86_64 等)
  • CUDA 版本(如果需要 GPU 支持,例如 cu118 表示 CUDA 11.8;若无 GPU,选择 cpu)

例如:

  • CPU 版本:torch-2.0.1-cp39-cp39-linux_x86_64.whl
  • GPU 版本(CUDA 11.8):torch-2.0.1+cu118-cp39-cp39-linux_x86_64.whl

3. 下载 .whl 文件

从 PyTorch 官方提供的下载链接或历史版本页面下载对应的 .whl 文件。例如:

  • 打开 https://download.pytorch.org/whl/torch/
  • 根据你的系统和需求选择合适的 .whl 文件,下载到本地(例如 ~/Downloads/torch-2.0.1-cp39-cp39-linux_x86_64.whl)。

注意:如果需要其他依赖(如 torchvisiontorchaudio),也需要下载对应的 .whl 文件,并确保版本兼容。

4. 安装 .whl 文件

在激活的 Conda 环境中,使用 pip 安装下载的 .whl 文件。

# 确保已激活 Conda 环境
conda activate pytorch_env

# 使用 pip 安装 .whl 文件(替换为你的文件路径)
pip install ~/Downloads/torch-2.0.1-cp39-cp39-linux_x86_64.whl

提示:如果需要安装多个 .whl 文件(例如 torchvision),按依赖顺序逐个安装:

pip install ~/Downloads/torchvision-0.15.2-cp39-cp39-linux_x86_64.whl

5. 验证安装

安装完成后,验证 PyTorch 是否正确安装并可用。

# 进入 Python 解释器
python

# 输入以下代码
import torch
print(torch.__version__)  # 输出 PyTorch 版本
print(torch.cuda.is_available())  # 检查是否支持 GPU(返回 True 表示可用)

示例输出:

2.0.1
True  # 如果支持 GPU

6. 解决常见问题

  • 版本不匹配:确保 Python 版本、CUDA 版本和 .whl 文件匹配。例如,cp39 对应 Python 3.9,cu118 对应 CUDA 11.8。

  • 缺少依赖

    :PyTorch 可能需要其他依赖(如

    numpy
    

    )。如果报错,尝试安装:

    pip install numpy
    
  • GPU 支持问题:如果 torch.cuda.is_available() 返回 False,检查 CUDA 和 cuDNN 是否正确安装,或者确认是否下载了 CPU 版本的 .whl 文件。

  • 离线安装:如果在离线环境中,确保所有依赖的 .whl 文件都已下载并按顺序安装。

7. (可选)安装其他 PyTorch 生态库

如果你需要 torchvisiontorchaudio 或其他相关库,重复步骤 3 和 4,下载并安装对应的 .whl 文件。例如:

pip install ~/Downloads/torchvision-0.15.2-cp39-cp39-linux_x86_64.whl

三、安装torch 的对应 torchvision 版本

Torch 版本TorchVision 版本Torchaudio 版本支持的 Python 版本支持的 CUDA 版本
2.5.10.20.12.5.1>=3.9, < 3.13(3.12)[9/10/11/12]12.4 / 12.1 / 11.8
2.5.00.20.02.5.0>=3.9, < 3.13(3.12)12.4 / 12.1 / 11.8
2.4.10.19.12.4.1>=3.8, < 3.13(3.12)[8/9/10/11/12]12.4 / 12.1 / 11.8
2.4.00.19.02.4.0>=3.8, < 3.13(3.12)12.4 / 12.1 / 11.8
2.3.10.18.12.3.1>=3.8, < 3.13(3.12)[8/9/10/11/12]12.1 / 11.8
2.3.00.18.02.3.0>=3.8, < 3.13(3.12)12.1 / 11.8
2.2.20.17.22.2.2>=3.8, < 3.12 [8/9/10/11]12.1 / 11.8
2.2.10.17.12.2.1>=3.8, < 3.1212.1 / 11.8
2.2.00.17.02.2.0>=3.8, < 3.1212.1 / 11.8
2.1.20.16.22.1.2>=3.8, < 3.12(3.10)[8/9/10/11]12.1 / 11.8
2.1.10.16.12.1.1>=3.8, < 3.12(3.10)12.1 / 11.8
2.1.00.16.02.1.0>=3.8, < 3.12(3.10)12.1 / 11.8
2.0.00.15.02.0.0>=3.8, < 3.12(3.8)[8/9/10/11]11.8 / 11.7
1.13.10.14.10.13.1>=3.7.2, <=3.10(3.8)[7/8/9/10]11.7 / 11.6
1.13.00.14.00.13.0>=3.7.2, <=3.10(3.8)11.7 / 11.6
1.12.10.13.11.12.1>=3.7, <=3.10(3.8)[7/8/9/10]11.6 / 11.3 / 10.2
1.12.00.13.01.12.0>=3.7, <=3.10(3.8)11.6 / 11.3 / 10.2
1.11.00.12.01.11.0>=3.7, <=3.10(3.8)11.3 / 10.2
1.10.10.11.20.10.1>=3.6, <=3.9(3.8)[6/7/8/9]11.3 / 10.2
1.10.00.11.00.10.0>=3.6, <=3.9(3.8)11.3 / 10.2
1.9.10.10.10.9.1>=3.6, <=3.9(3.8)[6/7/8/9]11.1 / 10.2
1.9.00.10.00.9.0>=3.6, <=3.9(3.8)11.1 / 10.2
1.8.10.9.10.8.1>=3.6, <=3.9(3.8)[6/7/8/9]11.1 / 10.2
1.8.00.9.00.8.0>=3.6, <=3.9(3.8)11.1 / 10.2
1.7.10.8.20.7.2>=3.6(3.6)11.0 / 10.2 / 10.1
1.7.00.8.00.7.0>=3.6(3.6)11.0 / 10.2 / 10.1

1.访问 PyTorch 官网:

  • 打开浏览器,访问 PyTorch 官方安装页面:https://pytorch.org/get-started/locally/

2.使用“Get Started”安装向导:

  • 在页面上,你会看到一个交互式的安装命令生成器,标题为“Start Locally”。
  • 根据你的系统配置选择以下选项:
    • PyTorch Build:选择 Stable(稳定版)或 Preview (Nightly)(预览版)。
    • Your OS:选择你的操作系统(例如 Linux、Windows、MacOS)。
    • Package:选择包管理器(例如 pipconda)。
    • Language:选择 Python 版本(你的环境是 Python 3.10)。
    • Compute Platform:选择计算平台(例如 CUDA 12.1,与你的 torch==2.5.1+cu121 匹配,或 CPU)。

3.查看生成的安装命令:

  • 选择完选项后,页面会生成一个安装命令,例如:

    pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu121
    
  • 这个命令会明确列出与 torch==2.5.1 兼容的 torchvision 版本(例如 torchvision==0.20.1)。

4.确认 torchtorchvision 版本对应关系:

  • 根据你的 environment.yml 文件,torch==2.5.1+cu121 已经安装。官网显示 torch==2.5.1 对应的 torchvision 版本是 0.20.1(与 CUDA 12.1 兼容)。https://pytorch.org/get-started/previous-versions/
  • 如果你选择了不同的 CUDA 版本或其他平台(如 CPU),官网会列出相应的 torchvision 版本。

5.查看历史版本(可选):

  • 如果需要查找其他 torch 版本的对应 torchvision 版本,滚动到页面底部的“Previous PyTorch Versions”部分。

  • 点击链接(例如 https://pytorch.org/get-started/previous-versions/),会列出历史版本的安装命令,明确显示每个 torch 版本对应的 torchvision 版本。例如:

    # CUDA 12.1
    pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu121
    

6.直接访问 torchvision GitHub(高级用户):

  • 如果官网信息不够详细,可以访问 torchvision 的 GitHub 仓库:https://github.com/pytorch/vision
  • READMERELEASE.md 文件中,会有版本兼容性表格,列出 torchtorchvision 的对应关系。
  • 例如,vision/RELEASE.md 可能提到 torch==2.5.1 对应 torchvision==0.20.1

四、验证安装

  • environment.yml 显示 torch==2.5.1+cu121,Python 3.10,CUDA 12.1。

  • 根据 PyTorch 官网,torch==2.5.1+cu121 对应的 torchvision 版本是 0.20.1+cu121

  • 安装命令:

    pip install torchvision==0.20.1 --index-url https://download.pytorch.org/whl/cu121
    

安装 torchvision==0.20.1 后,运行以下代码确认:

import torch
import torchvision
print(torch.__version__)  # 应输出 2.5.1+cu121
print(torchvision.__version__)  # 应输出 0.20.1+cu121

五、注意事项

  • Conda 与 pip 混合使用:在 Conda 环境中使用 pip 安装 .whl 文件是安全的,但避免在同一环境中混用 conda installpip install 安装 PyTorch,以免出现依赖冲突。

  • 存储空间:PyTorch 的 .whl 文件和安装后的库可能占用较大空间(GPU 版本可能超过 1GB)。

  • 网络问题:如果无法访问 PyTorch 官网,可尝试从镜像源(如 https://mirrors.tuna.tsinghua.edu.cn/pytorch/)下载 .whl 文件。

  • CUDA 匹配:确保 torchvision 的 CUDA 版本(+cu121)与 torch 一致,否则可能导致不兼容。

  • PyPI 索引:使用 --index-url https://download.pytorch.org/whl/cu121 确保从 PyTorch 官方索引下载,普通 PyPI 可能缺少 CUDA 版本。

  • Python 版本: Python 3.10 与 torch==2.5.1torchvision==0.20.1 兼容,无需调整。

您可能感兴趣的与本文相关的镜像

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ac157ol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值