Skip to content

Instructions for installing PyTorch #6409

Open

Description

@davidgilbertson
  • I have searched the issues of this repo and believe that this is not a duplicate.

Issue

As mentioned in issue #4231 there is some confusion around installing PyTorch with CUDA but it is now somewhat resolved. It still requires a few steps, and all options have pretty serious flaws. Below are two options that 'worked' for me, on Poetry version 1.2.0.

Option 1 - wheel URLs for a specific platform

  • You will need to pick the specific wheels you want. These are listed here: https://download.pytorch.org/whl/torch_stable.html. E.g. if you want CUDA 11.6, Python 3.10 and Windows, search that page for cu116-cp310-cp310-win_amd64.whl to see the matches for torch, torchaudio and torchvision
  • In your pyproject.toml file add the URLs like:
[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.23.2"
torch = { url = "https://download.pytorch.org/whl/cu116/torch-1.12.1%2Bcu116-cp310-cp310-win_amd64.whl"}
torchaudio = { url = "https://download.pytorch.org/whl/cu116/torchaudio-0.12.1%2Bcu116-cp310-cp310-win_amd64.whl"}
torchvision = { url = "https://download.pytorch.org/whl/cu116/torchvision-0.13.1%2Bcu116-cp310-cp310-win_amd64.whl"}
  • Run poetry update. It will download a lot of data (many GB) and take quite some time. And this doesn't seem to cache reliably (at least, I've waited 30 minutes+ at 56 Mbps three separate times while troubleshooting this, for the exact same wheels)

Note that each subsequent poetry update will do another huge download and you'll see this message:

  • Updating torch (1.12.1+cu116 -> 1.12.1+cu116 https://download.pytorch.org/whl/cu116/torch-1.12.1%2Bcu116-cp310-cp310-win_amd64.whl)
  • Updating torchaudio (0.12.1+cu116 -> 0.12.1+cu116 https://download.pytorch.org/whl/cu116/torchaudio-0.12.1%2Bcu116-cp310-cp310-win_amd64.whl)
  • Updating torchvision (0.13.1+cu116 -> 0.13.1+cu116 https://download.pytorch.org/whl/cu116/torchvision-0.13.1%2Bcu116-cp310-cp310-win_amd64.whl)

Option 2 - alternate source

[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.23.2"
torch = { version = "1.12.1", source="torch"}
torchaudio = { version = "0.12.1", source="torch"}
torchvision = { version = "0.13.1", source="torch"}

[[tool.poetry.source]]
name = "torch"
url = "https://download.pytorch.org/whl/cu116"
secondary = true

This seems to have worked (although I already had the packages installed) but it reports errors like Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pillow/, but I think they get installed anyway (maybe a better message would be "Can't access pillow at 'https://download.pytorch.org/whl/cu116', falling back to pypi")

Also, if you later go on to do, say poetry add pandas (a completely unrelated library) you'll get a wall of messages like:

Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pandas/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pandas/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pytz/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/python-dateutil/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/numpy/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pillow/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/requests/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/typing-extensions/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/certifi/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/urllib3/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/idna/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/charset-normalizer/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/python-dateutil/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/six/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pytz/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/six/

This happens with or without secondary = true in the source config.

Maintainers: please feel free to edit the text of this if I've got something wrong.

Activity

  1. dimbleby commented on Sep 5, 2022

    @dimbleby
    Contributor

    Failure to cache during resolution is covered by #2415, poetry's insistence on checking all sources for all packages is discussed at #5984

  2. variolam commented on Sep 6, 2022

    @variolam

    Solution 1 seems infeasible when working in a team with machines on different operating systems, due to the need of providing the complete URL of the wheel including operating system and exact version number.

    Solution 2 seems to work, but it results in downloading every single PyTorch version that can found, independent on the operating system. I'm running Windows and the download looks like this:

    image

    A single installation takes around 15-20 minutes at ~250 Mbps.

  3. neersighted commented on Sep 6, 2022

    @neersighted
    Member

    Poetry will always download wheels for every platform when you install -- this is because there is no other way to get package metadata from a repository using PEP 503's API.

  4. Queuecumber commented on Sep 6, 2022

    @Queuecumber

    Can you elaborate a little on what metadata is needed and why downloading every conceivable version of a package yields that metadata? As mentioned this leads to a ~20min install for one package

  5. radoering commented on Sep 6, 2022

    @radoering
    Member

    Solution 1 seems infeasible when working in a team with machines on different operating systems, due to the need of providing the complete URL of the wheel including operating system and exact version number.

    It's not convenient, but it should be feasible with multiple constraints dependencies.

  6. neersighted commented on Sep 6, 2022

    @neersighted
    Member

    Poetry requires the package's core metadata aka the METADATA file (most critically this includes dependencies), as well as the bdist/sdist itself for hashing purposes. Note that PEP 658 is a standard for serving the METADATA file that is implementable by third-party repositories, and PEP 691 specifies a (potentially) richer JSON API (including hashes) that third-party repositories could likely implement.

    However, Poetry is unlikely to grow support for these new APIs until PyPI does, and I think third party repos are unlikely to implement it before PyPI. Eventually support for these APIs will allow for feature and performance parity in Poetry between PyPI and third-party repositories. Until then, we are stuck with the legacy HTML API, which requires us to download every package when generating a lock file for the first time.

    After your cache is warm you will not need to download again, and on other platforms you will only download the necessary files as the metadata is captured in the lock file.

  7. Queuecumber commented on Sep 6, 2022

    @Queuecumber

    What I'm not understanding is that poetry knows I'm on Linux with python 3.8 but it still downloads

    https://download.pytorch.org/whl/cu116/torch-1.12.1%2Bcu116-cp37-cp37-win_amd64.whl
    

    Or does that wheel not contain the core metadata that is needed?

  8. Queuecumber commented on Sep 6, 2022

    @Queuecumber

    Also there seems to be a second problem going on here unless I've misunderstood the documentation

    I have this in my pyproject.toml

    [[tool.poetry.source]]
    name = "torchcu116"
    url = "https://download.pytorch.org/whl/cu116"
    default = false
    secondary = true
    

    i.e. secondary = true

    Yet poetry is asking that repository for every package I try to install. I thought from the documentation that secondary meant it would go to pypi for any package unless specifically asked to go to that custom repository.

  9. neersighted commented on Sep 6, 2022

    @neersighted
    Member

    What I'm not understanding is that poetry knows I'm on Linux with python 3.8 but it still downloads

    https://download.pytorch.org/whl/cu116/torch-1.12.1%2Bcu116-cp37-cp37-win_amd64.whl
    

    Or does that wheel not contain the core metadata that is needed?

    Poetry constructs a universal lock file -- we write hashes to the lock file for all supported platforms. Thus on the first machine you generate a lock file, you will download a wheel for every supported platform. There is no way to write hashes to the lock file for those foreign/other platform versions without downloading them first.

    If you want to reduce the scope of this a bit, you can tighten your Python constraint. There is a prototype of a new feature at #4956 (though it needs resurrection, design, and testing work) to add arbitrary markers to let a project reduce its supported platforms as an opt-in.

    Also there seems to be a second problem going on here unless I've misunderstood the documentation

    I have this in my pyproject.toml

    [[tool.poetry.source]]
    name = "torchcu116"
    url = "https://download.pytorch.org/whl/cu116"
    default = false
    secondary = true
    

    i.e. secondary = true

    Yet poetry is asking that repository for every package I try to install. I thought from the documentation that secondary meant it would go to pypi for any package unless specifically asked to go to that custom repository.

    I think it might be you misreading -- that is the intended and documented behavior. There is a proposal to introduce new repository types at #5984 (comment) as the current secondary behavior covers multiple use cases poorly, while being ideal for none of them.

  10. Queuecumber commented on Sep 6, 2022

    @Queuecumber

    Poetry constructs a universal lock file

    OK this makes sense now, thanks for the explanation, looking forward to that PR hopefully being merged eventually

    I think it might be you misreading

    I was misreading I see that this is intended behavior

    the current secondary behavior covers multiple use cases poorly

    I agree with that and I hope that these new repository types can be implemented

  11. ZetiMente commented on Sep 24, 2022

    @ZetiMente

    Can't wait for option 2 to have good performance !

  12. neersighted commented on Sep 25, 2022

    @neersighted
    Member

    Please 👍 on issues instead of commenting me too -- it keeps the notifications down and still shows interest. Thanks!

  13. 135 remaining items

  14. brian316 commented on Aug 2, 2024

    @brian316

    this seems to work for me

    poetry 1.8.3

    [tool.poetry.dependencies]
    torch = { version = "2.1.0", source = "pytorch" }
    
    [[tool.poetry.source]]
    name = "pytorch"
    url = "https://download.pytorch.org/whl/cu121"
    priority = "explicit"
  15. havardox commented on Aug 18, 2024

    @havardox

    Anyone got a working pyproject.toml for ROCm?

  16. extraymond commented on Aug 23, 2024

    @extraymond

    Anyone got a working prproject.toml for ROCm?

    After some trial and error I think I know why it sometimes works and sometimes won't when using on rocm.

    poetry works, but pytorch didn't have prebuild wheels for every combination of torch/rocm/python.
    You can manually check that here: https://download.pytorch.org/whl/torch

    For example, only torch 2.4 are build for rocm6.1, the old one are kept in rocm6.0 and rocm5.6.
    Let's say you want torch=^2.0 and torchvision installed, which translates to finding torch on the 2.0 series and whatever torchvision that works with torch. I think the resolver will do the following:

    1. find the torch version available in that source
    2. cpu and cuda version is in the same source, they are only differentiate with each other by markers like +cpu +rocm6.1
    3. the resolver find the highest available version with ^2.0 and found one build for cuda
    4. it tried to resolve the torchvision version compatible with torch
    5. and tried to find the rocm variant which aren't available

    So when using rocm, I think manually check the version available in the source and directly specify it will works better.

    Here's my declaration:

    [tool.poetry.dependencies]
    torch = { version = "^2.0", source = "torch-rocm" }
    torchvision = { version = "^0.16", source = "torch-rocm" }
    
    [[tool.poetry.source]]
    name = "torch-rocm"
    url = "https://download.pytorch.org/whl/rocm5.6"
    priority = "explicit"

    be sure to check the python version marker too! if one aren't build for rocm you will face the same issue.

  17. fede-bello commented on Sep 25, 2024

    @fede-bello

    I have play around on install PyTorch on my poetry project.

    Solution about It only work on 2.2.2

    after that it would just show message Unable to find installation candidates for torch (2.3.1)

    The configuration i am using

    • Mac OS 12
    • Python 3.12
    • Poetry 1.8

    I'm having the same issue here. When running just poetry install I'm not having any issue, but when specifying cpu or gpu to install pytorch I'm getting:

      RuntimeError
    
      Unable to find installation candidates for torch (2.4.1+cpu)
    
      at ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/chooser.py:74 in choose_for
           70│
           71│             links.append(link)
           72│
           73│         if not links:
        →  74│             raise RuntimeError(f"Unable to find installation candidates for {package}")
           75│
           76│         # Get the best link
           77│         chosen = max(links, key=lambda link: self._sort_key(package, link))
           78│
    
    Cannot install torch.
    
      - Installing torch (2.4.1+cu118): Failed
    
      RuntimeError
    
      Unable to find installation candidates for torch (2.4.1+cu118)
    
      at ~/.local/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/chooser.py:74 in choose_for
           70│
           71│             links.append(link)
           72│
           73│         if not links:
        →  74│             raise RuntimeError(f"Unable to find installation candidates for {package}")
           75│
           76│         # Get the best link
           77│         chosen = max(links, key=lambda link: self._sort_key(package, link))
           78│
    
    Cannot install torch.
    
  18. r-Larch commented on Sep 26, 2024

    @r-Larch

    When trying @tfha approach I get MemoryError so I was needed to use --no-cache.

    You find the url for the right wheels file from pytorch getting started. [...]

    You first change the source for where poetry tries to find wheels for pytorch with:

    poetry source add --priority=explicit pytorch https://download.pytorch.org/whl/cu118

    [...]

    You can then continue to install PyTorch with CUDA support by running the following command in the terminal:

    poetry add torch --source pytorch

    Produces:

    Using version ^2.4.1+cu118 for torch
    
    Updating dependencies
    Resolving dependencies... (1.3s)
    
    Package operations: 0 installs, 1 update, 0 removals
    
      - Updating torch (2.4.1 -> 2.4.1+cu118): Failed
    
      MemoryError
    
      at msgpack\\_packer.pyx:241 in msgpack._cmsgpack.Packer._pack_inner
    
    Cannot install torch.
    

    Solution:

    Found here #7250 (comment)
    Adding --no-cache solves the issue.

    poetry add torch --source pytorch --no-cache
  19. clarkh-ncino commented on Oct 3, 2024

    @clarkh-ncino

    If you have an intel chip mac, any version past 2.2.2 is unsupported without leveraging conda pytorch/pytorch#114602

  20. rbudnar commented on Oct 9, 2024

    @rbudnar

    The solutions described above here and here mostly work, but I noticed that if I re-run the install command (with or without with the sync flag) again after an install, poetry will flip the install to the other version regardless of which flags I've passed. For example, I cloned the repo provided by @lucaspar and simply ran the suggested command over and over:

    poetry install --sync -E cuda --with cuda
    

    And poetry 1.8.3 would toggle between the CPU and GPU versions indefinitely:
    image

  21. leolcao commented on Dec 24, 2024

    @leolcao

    for my side, I find poetry can not use some mirror url, like the aliyun mirror, I don't know it is poetry's issue or not.

    my pyproject.toml

    [tool.poetry]
    name = "torch-samples"
    version = "0.1.0"
    description = ""
    authors = ["Leo Cao"]
    readme = "README.md"
    package-mode = false
    
    [tool.poetry.dependencies]
    python = "3.10.11"
    
    # torch = { url = "https://mirrors.aliyun.com/pytorch-wheels/cu121/torch-2.3.1+cu121-cp310-cp310-win_amd64.whl" }
    # torchvision = { url = "https://mirrors.aliyun.com/pytorch-wheels/cu121/torchvision-0.18.1+cu121-cp310-cp310-win_amd64.whl" }
    
    # aliyun-torch not work
    torch = { version = "2.3.1+cu121", source = "sjtu-torch" }
    torchvision = { version = "0.18.1+cu121", source = "sjtu-torch" }
    
    # lock the numpy in 1.x
    numpy = "1.26.4"
    
    [tool.poetry.group.dev.dependencies]
    pyinstaller = "6.11.1"
    
    [[tool.poetry.source]]
    name = "tsinghua"
    url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
    priority = "primary"
    
    [[tool.poetry.source]]
    name = "torch"
    # https://download.pytorch.org/whl/torch_stable.html
    url = "https://download.pytorch.org/whl/cu121/"
    priority = "supplemental"
    
    [[tool.poetry.source]]
    name = "aliyun-torch"
    # https://mirrors.aliyun.com/pytorch-wheels/torch_stable.html
    url = "https://mirrors.aliyun.com/pytorch-wheels/cu121/"
    priority = "supplemental"
    
    [[tool.poetry.source]]
    name = "sjtu-torch"
    # https://mirror.sjtu.edu.cn/pytorch-wheels/torch_stable.html
    url = "https://mirror.sjtu.edu.cn/pytorch-wheels/cu121/"
    priority = "supplemental"
    
    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"

    If i use the source aliyun-torch, poetry install will throw the error message:

    Installing dependencies from lock file
    
    Finding the necessary packages for the current system
    
      IndexError
    
      Repository "aliyun-torch" does not exist.
    
      at D:\LeoCao\.pipx\venvs\poetry\Lib\site-packages\poetry\repositories\repository_pool.py:145 in _get_prioritized_repository
          141│     def _get_prioritized_repository(self, name: str) -> PrioritizedRepository:
          142│         name = name.lower()
          143│         if self.has_repository(name):
          144│             return self._repositories[name]
        → 145│         raise IndexError(f'Repository "{name}" does not exist.')
          146│
          147│     def add_repository(
          148│         self,
          149│         repository: Repository,
    

    But, I can use the url way to set the aliyun source, like the commented parts from above pyproject.toml.
    does anyone know why? the poetry version is 1.8.4.

  22. Node-serialportainsley commented on Feb 7, 2025

    @Node-serialportainsley
  23. psibre commented on Feb 7, 2025

    @psibre

    Observe this https://pythoner.space/questions/error-generated-due-to-unsupported-locale-setting-in-a-dockerfile-built-with-python crucial insight

    Problems (and solutions!) for setting the locale inside Docker containers are easy to find on the web, e.g., https://stackoverflow.com/a/28406007.

    That has nothing at all to do with this issue, or even Python in general.

  24. 0x26res commented on Feb 19, 2025

    @0x26res

    I got it working on Linux + CUDA at least (not yet tested on Windows and macOS). Note that NumPy isn't installed automatically and one has to explicitly add the dependency to the TOML file (as stated in the PyTorch documentation).

    [tool.poetry.dependencies]
    python = "^3.10,<3.12"
    torch = { version = "^2.1.0+cu118", source = "pytorch" }
    torchvision = { version = "^0.16.0+cu118", source = "pytorch" }
    numpy = "^1.26.1"

    [[tool.poetry.source]]
    name = "pytorch"
    url = "https://download.pytorch.org/whl/cu118"
    priority = "explicit"
    The Python version should be be constrained to be inferior to 3.12 to satisfy NumPy requirements.

    Note the explicit priority for the pytorch source which allows using the given source only for the packages with the source mentioned explicitly. This avoids some of the expensive dependency resolution work.

    I emptied all the Poetry caches, removed the lockfile and poetry lock took less than a minute to resolve dependencies on a fast internet connection.

    I tested that Pytorch was installed correctly and that CUDA was supported with the following command:

    poetry run python -c "import torch; torch.zeros(1, device='cuda')"
    According to the poetry.lock file, the installation should work for Linux x86_64 and Windows amd64 platforms. I don't think this would be compatible out of the box with macOS however since there is no CUDA macOS wheels. I'll try to specify a constrain for macOS in the TOML and i'll test this etting on multiple platforms.

    EDIT

    Indeed, this works for Linux and Windows but not macOS. The following TOML configuration works for the three platforms (Linux+CUDA, Windows+CUDA, macOS+MPS):

    [tool.poetry.dependencies]
    python = "^3.9,<3.12"
    torch = [
    { version = "^2.0.0", source = "pytorch", platform = "!=darwin"},
    { version = "^2.0.0", source = "pypi", platform = "darwin"},
    ]
    torchvision = [
    { version = "^0.15.0", source = "pytorch", platform = "!=darwin"},
    { version = "^0.15.0", source = "pypi", platform = "darwin"},
    ]
    numpy = "^1.26.1"

    [[tool.poetry.source]]
    name = "pytorch"
    url = "https://download.pytorch.org/whl/cu118"
    priority = "explicit"
    I was able to test this successfully on the three platforms using the same lockfile.

    Has anyone managed to get this to work using the PEP 621 way of specifying dependencies, and what would the syntax be?

  25. amadou-6e commented on Mar 26, 2025

    @amadou-6e

    In the age of machine learning, not being able to install something as basic as torch, would make poetry obsolete when compare with conda where this is a one liner

  26. MaKaNu commented on Mar 26, 2025

    @MaKaNu
    Contributor

    @amadou-6e I would not compare poetry with conda. The issue here is more on metas way of distributing the software. The same issue appears on other package managers, which provide dependency resolving similar to poetry.

    Just because one library is weird how they distribute does not make this tool obsolete.

  27. bverhoeve commented on Jun 3, 2025

    @bverhoeve

    I've been struggling with this for a while as well now. We use the CPU version of torch as our use case is quite limited. My current pyproject.toml file looks as follows:

    torch = [
      { version = "2.6.0", markers = "sys_platform == 'darwin'" },
      { version = "2.6.0", source = "pytorch_cpu", extras=["cpu"], markers = "platform_machine == 'x86_64' and sys_platform != 'darwin'"},
    ]
    
    [[tool.poetry.source]]
    name = "pytorch_cpu"
    url = "https://download.pytorch.org/whl/cpu"
    priority = "explicit"
    

    When generating a poetry.lock file, everything works fine. When installing using poetry install on macos I consistently run into the following error:

    Package operations: 0 installs, 23 updates, 0 removals
    
      - Updating torch (2.6.0 -> 2.6.0+cpu): Failed
    
      RuntimeError
    
      Unable to find installation candidates for torch (2.6.0+cpu)
    
      at ~/.local/pipx/venvs/poetry/lib/python3.13/site-packages/poetry/installation/chooser.py:74 in choose_for
           70│
           71│             links.append(link)
           72│
           73│         if not links:
        →  74│             raise RuntimeError(f"Unable to find installation candidates for {package}")
           75│
           76│         # Get the best link
           77│         chosen = max(links, key=lambda link: self._sort_key(package, link))
           78│
    
    Cannot install torch.
    

    So even though pyproject.toml indicates that the CPU version is NOT to be used on darwin (it isn't even present in the torch repo), Poetry somehow insists on installing the cpu extras. Any ideas?

    EDIT: just using torch = 2.6.0 does work although that does install the CUDA dependencies as well even when not used on Intel silicon

  28. Rishabh-Choudhry commented on Jul 16, 2025

    @Rishabh-Choudhry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/docsDocumentation issues/improvementsstatus/triageThis issue needs to be triaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions