51

Torch documentation says use

pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

to install the latest version of PyTorch. This works when I do it manually but when I add it to req.txt and do pip install -r req.txt, it fails and says ERROR: No matching distribution.

Edit: adding the whole line from req.txt and error here.

torch==1.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.htmltorch==1.4.0+cpu
ERROR: Could not find a version that satisfies the requirement torch==1.4.0+cpu (from -r requirements.txt (line 1)) (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2, 0.3.1, 0.4.0, 0.4.1, 1.0.0, 1.0.1, 1.0.1.post2, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.4.0)
ERROR: No matching distribution found for torch==1.4.0+cpu (from -r requirements.txt (line 1))
2
  • what is your python version? Commented Mar 29, 2020 at 10:21
  • @NilankaManoj python version: 3.6.9 Commented Mar 29, 2020 at 10:36

6 Answers 6

75

Add --find-links in requirements.txt before torch

--find-links https://download.pytorch.org/whl/torch_stable.html

torch==1.2.0+cpu

Source: https://github.com/pytorch/pytorch/issues/29745#issuecomment-553588171

Sign up to request clarification or add additional context in comments.

1 Comment

Not sure if something changed, but I was able to install the latest version by adding torch and torchvision on separate lines to requirements.txt. Or torch>=2.0.1 for that version or higher. No --find-links required.
29

To get the cuda version that I needed (instead of whatever the repos serve up), I converted the cuda-specific installation command from pytorch:

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

into this at the top of requirements.txt:

--extra-index-url https://download.pytorch.org/whl/cu116
torch
torchvision
torchaudio

Then I do the usual pip install -r requirements.txt and when I import torch and run torch.version.cuda inside python, I get '11.6' as I wanted.

1 Comment

This also works with the cpu only version. The link would be https://download.pytorch.org/whl/cpu. The accepted answer didn't work for me.
12
-f https://download.pytorch.org/whl/torch_stable.html 
torch==1.4.0+cpu 
-f https://download.pytorch.org/whl/torch_stable.html
torchvision==0.5.0+cpu

worked fine for me :)

Comments

10

You can do something like that:

$ pip install -r req.txt --find-links https://download.pytorch.org/whl/torch_stable.html

Just put your PyTorch requirements in req.txt like this:

torch==1.4.0+cpu

torchvision==0.5.0+cpu

2 Comments

It's possible the OP can't use any other command except pip install -r req.txt. I have the same issue; pip install -r requirements.txt is run by another team, not me; all I have control over is the contents of requirements.txt.
For some reason, the version torch==1.2.0+cpu in the accepted answer doesn't work for me, but this one does.
5

For me, this requirement.txt worked for CPU version installation

--extra-index-url https://download.pytorch.org/whl/cpu
torch
torchvision

Comments

0

This -f https://download.pytorch.org/whl/torch_stable.html seems to work only for CPU version.

In most cases, we need to install the CUDA version.

For example, to install torch==2.2.0+cu121, this command line

RUN pip install torch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cu121

It can be converted as a requirements.txt, including something like:

--extra-index-url https://download.pytorch.org/whl/cu121
torch==2.2.0+cu121
torchvision==0.17.0+cu121

This requirements.txt works for both CPU and CUDA PyTorch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.