Installing PyTorch
Many scientific computing libraries do not immediately support the newest version of Python, so the latest release is not always the best choice for PyTorch.
Why the Newest Python Is Not Always the Right Choice
When people set up a new machine for machine learning, a natural instinct is to install the newest version of everything. For Python, that instinct can cause trouble when the next step is installing PyTorch. Many scientific computing libraries do not support the newest version of Python immediately after it is released, so the latest release is not automatically the most suitable one for PyTorch.
This article answers one practical question: which Python version should you use when installing PyTorch? The short answer is a version that is one or two releases older than the latest one. The reasons behind that guideline are what make it worth understanding, so we will look at what happens to the install at each stage rather than just memorizing a rule.
Choosing a Python Version Before You Install
The guideline is to use a Python version that is one or two releases older than the latest release. The reference point is always the latest release, and the two versions just below it are the ones to consider for installing PyTorch. The source uses the release 3.13 as its example. When the latest release is 3.13, the source recommends Python 3.11 or 3.12 for installing PyTorch.
The reason for the window is that PyTorch and its scientific dependencies need time to support a newly released Python. A release one or two steps back has usually had that time, so the libraries you need are more likely to be ready for it. The newest release may simply not have the matching support yet.
Choosing a version for a new machine
You are setting up a new machine for machine learning and the newest Python release available to you is 3.13. Which version should you install PyTorch into?
- 1Identify the latest release
The latest release in this example is 3.13, and it is the reference point.
- 2Count back one or two releases
One release back is 3.12 and two releases back is 3.11. Both fall inside the recommended window.
- 3Avoid the newest release for this step
Installing PyTorch into 3.13 is the instinct the guideline warns against, because the newest release may not yet match what PyTorch and its dependencies support.
- 4Pick a version from the window
Choose Python 3.11 or 3.12 as the environment you install PyTorch into.
Install PyTorch into Python 3.11 or 3.12 rather than 3.13.
Mistakes That Come From Always Installing the Newest Python
Common mistake Installing PyTorch into the newest Python release by default
What it looks likeA new machine gets the latest Python, and PyTorch is installed into that same environment without any thought about the version.
Why it failsThe source explains that many scientific computing libraries do not immediately support the newest version of Python, so the install can run into trouble at the point where PyTorch is installed.
Do this insteadDecide on the Python version first, and prefer a version one or two releases older than the latest.
Common mistake Treating the Python version as unrelated to the PyTorch install
What it looks likeThe user focuses only on the PyTorch command and never checks which Python environment it runs in.
Why it failsThe Python environment is part of the outcome. The default command assumes the environment already has what it needs, such as pip.
Do this insteadCheck the Python version and the environment before running the install command.
Common mistake Assuming the newest release always means the best setup for machine learning
The root of these mistakes is one habit: treating the newest release as the default answer. The guideline replaces that habit with a deliberate choice, made before the install begins.
What the Default Install Command Selects
Most PyTorch setups begin in a terminal with one line. Here is the default command. Notice that it contains no mention of CUDA, GPUs, or versions.
- The command names only the package torch.
- It contains no build choice, so the selection is made by the machine and the environment.
What do you think happens?
The command contains no mention of CUDA or GPUs. What decides which PyTorch build you get?
Reveal answer
The machine and the Python environment the command runs in
The default command does not ask which build you want. It checks what your computer offers, and it assumes the Python environment has the dependencies it needs, such as pip.
The selection is conditional. If your computer has a CUDA-compatible GPU, the install proceeds with the version of PyTorch that uses GPU acceleration through CUDA. CUDA is a technology that allows for GPU acceleration, so this build can hand heavy numerical work to the graphics card. If the machine has no CUDA-compatible GPU, the CPU path is the one to expect, and the GPU build is not selected.
CPU-Only and Full PyTorch Builds
PyTorch comes in two versions. The leaner version supports only CPU computing. The full version supports both CPU and GPU computing. The distinction is about what the installed library is able to run on, and it is the reason the choice matters before you ever write a line of model code.
Read the two columns as a subset relationship. The lean build covers CPU computing only. The full build covers CPU computing and also GPU computing, so the lean build is contained within the full build's capabilities, and the extra part of the full build is the GPU support.
Using the Official Menu for a CUDA Command
The default command is convenient, but it is often better to name the CUDA version you want PyTorch to be compatible with. The official PyTorch website provides installation commands for different operating systems through a selection menu. The menu is the authoritative source for the exact command to run, so copy the command it produces rather than typing one from memory.
- 1Choose the latest stable release.
- 2If your graphics card supports CUDA, choose a CUDA version that is compatible with that card.
- 3If you do not have an NVIDIA card that supports CUDA, choose the CPU version instead.
- 4Copy the command the menu produces for your choices, because the exact text can differ between operating systems.
Pinning an Exact PyTorch Version
Sometimes you want a specific release rather than whatever is current. The source's examples were written with PyTorch 2.4.0, and it recommends installing that exact version to guarantee compatibility with its examples. The pinning syntax attaches a version after a double equals sign, so the general form is package name, two equals signs, then the version.
- torch is the package name.
- == pins the install to the version that follows.
- 2.4.0 is the exact version the source's examples were written with.
Combining pinning with the official menu is a two-step process. The menu gives you a command tailored to your operating system and CUDA choice, and that command begins with the word torch. Replace torch with torch==2.4.0 in that command and keep everything else the same. The result matches your machine and targets the exact version.
- Only the package name changes.
- The placeholder stands for the remaining options the menu produced for your system.
When an installation produces a build you did not expect, the fastest diagnosis is to walk the path back in order. Ask which command you ran, whether it contained a pinned version, which operating system and CUDA choices produced that command, and what hardware the machine has. Each question corresponds to one decision described above, so the first answer that does not match your expectation shows where the install left the path you intended.
Practice: Tracing an Unexpected Build
A colleague says they ran the pinned command pip install torch==2.4.0 on a machine with a CUDA-compatible GPU, but they expected a GPU build and got a CPU-only result. Using the walk-back method, list the four questions you would ask in order, and say which one is most likely to reveal the cause.
Hints
- Start with the command that was actually run and check whether it was the pinned form.
- Then ask which menu choices produced the command and what hardware the machine has.
- Consider whether the Python environment had the dependencies it needed, such as pip.
Choose a Python version for a machine whose latest release is 3.13, and justify your choice in one sentence using the one or two releases older rule.
Hints
- Count back from the latest release.
- The source recommends 3.11 or 3.12 in this case.
Key Takeaways
- The newest Python release is not automatically the best environment for PyTorch, because many scientific libraries take time to support it; choose a version one or two releases older than the latest, such as 3.11 or 3.12 when the latest is 3.13.
- The default command pip install torch does not ask which build you want. It selects a GPU-enabled CUDA build when the machine has a CUDA-compatible GPU and the Python environment has what it needs, such as pip.
- The leaner CPU-only build supports only CPU computing, while the full build supports both CPU and GPU computing.
- For a specific CUDA setup, use the official PyTorch menu: choose the latest stable release, then a compatible CUDA version or the CPU version, and copy the command it produces.
- To pin an exact version, use pip install torch==<version>; when an install goes wrong, walk the path back through the command, the pin, the OS and CUDA choices, and the hardware.