Installing Torch with CUDA from Python


So unfortunately a solid “pip install torch” is not always what you need … in fact, when installing Torch for Cuda its *never* what you need. The reason is that we need a torch version that is compiled specifically to work with our cuda machines.

To make things even more complicated, most “requirements.txt” files do not include a CUDA torch but rather the vanilla. That means when you type “pip install -r requirements.txt”, you’re more than likely going to get the wrong version.

Never fear! It’s an easy solution, although an extra-step solution.

Once your requirements.txt has finished installing, from your command line type:

#Note: change the torch version to whichever you needpip install torch==2.2.0 --index-url https://download.pytorch.org/whl/cu118 --upgrade

What this will do is remove the incorrect torch, and reinstall (upgrade) the existing torch.
This can also be called from inside a .bat file by adding: “call” to the beginning of the line like so:

call pip install torch==2.2.0 --index-url https://download.pytorch.org/whl/cu118 --upgrade

Leave a Reply

Your email address will not be published. Required fields are marked *