Install PyTorch on Windows, Linux and macOS in 2025: Step by Step

Community Article Published November 12, 2025

Getting PyTorch installed is the first step, not a stumbling block. Before you even open a terminal, the most critical decision you'll make is choosing between a CPU-only build or a GPU-accelerated one. This single choice sets the course for the entire installation and fundamentally defines your project's performance ceiling.

Your Foundation for a Smooth PyTorch Install

If you're just dipping your toes into PyTorch or working on a machine without a dedicated NVIDIA GPU, the CPU version is the way to go. It's a straightforward install and gets you coding quickly.

But for any serious deep learning, whether you're training models or running heavy inference, a GPU isn't just nice to have; it's essential. The right GPU can slash training times from days to hours. This path requires an NVIDIA GPU that supports CUDA, the parallel computing platform that unlocks all that power for PyTorch.

This simple decision tree breaks down the choice.

pytorch-decision-tree

As you can see, having an NVIDIA GPU is the main fork in the road that determines which installation command you'll need and what comes next.

CPU vs GPU PyTorch Build Decision Matrix

Still on the fence? This table breaks down the decision into a few key factors to help you pick the right version for your needs.

Factor CPU-Only Version GPU (CUDA) Version
Primary Use Case Learning PyTorch, running small models, preprocessing, inference on non-NVIDIA hardware. Serious model training, large-scale inference, and any computationally intensive task.
Hardware Requirement Any modern computer. A CUDA-compatible NVIDIA GPU.
Installation Complexity Very simple. Usually a one-line command. More involved. Requires compatible NVIDIA drivers and the CUDA Toolkit.
Performance Sufficient for basic tasks but extremely slow for training deep neural networks. Orders of magnitude faster for deep learning tasks. Non-negotiable for production.
Best For Beginners, students, quick prototyping, or deployment on CPU-only servers. AI researchers, ML engineers, and anyone training models with significant data.

Ultimately, the choice comes down to your workload. For learning and light tasks, CPU is fine. For building and training, GPU is the only practical option.

Core Prerequisites to Consider

Once you've picked your hardware path, you just need a couple more things in place. Make sure you have a compatible version of Python installed. PyTorch keeps up with the latest stable releases. You'll also need a package manager. You can use either:

  • pip: The standard Python package installer. It's simple and gets the job done.
  • conda: Part of the Anaconda distribution. It's the preferred choice in the data science world because its environment management is excellent for preventing nasty dependency conflicts.

The framework's growth has been explosive, cementing it as a leader in the AI community. According to a recent Linux Foundation report, PyTorch now holds a 63% adoption rate for model training, making it the most popular framework in the world.

Getting these basics right is key. If you want to back up and learn more about the framework itself, check out our complete guide for beginners on what PyTorch is.

Platform-Specific Installation Walkthroughs

platform-specific-installation

Alright, let's get down to business and turn theory into action. Here are the exact commands you'll need to get PyTorch up and running. The right command for you depends entirely on your operating system and your package manager of choice (either pip or conda). I’ll give you copy-paste-ready instructions for every major platform.

A quick word of advice: many data scientists prefer conda because its robust environment management can save you from a world of dependency pain later on. On the other hand, pip is more straightforward if you’re already confident about your environment setup.

Installing PyTorch on Linux and macOS

pytorch-on-linux

For anyone on a Linux distro like Ubuntu/Debian or on a Mac, the process is practically the same. The commands below will install the latest stable version of PyTorch.

Using pip Just pop open your terminal and run this command for a standard CPU-only installation. It’s the fastest way to get started.

pip3 install torch torchvision torchaudio

Using conda If you're in the conda camp, the command looks a bit different. Conda is great because it pulls packages from its own channels, which helps make sure everything in your data science stack plays nicely together.

conda install pytorch torchvision torchaudio -c pytorch

You'll notice these commands don't just grab the core torch library. They also pull in torchvision and torchaudio. These are two official companion libraries packed with datasets, pre-trained models, and other tools designed for computer vision and audio tasks. Installing them now just saves you a step down the road.

Installing PyTorch on Windows

pytorch-on-windows

The process for Windows is just as simple, and these commands work in both Command Prompt and PowerShell. The official PyTorch website even has a handy configurator that builds the perfect command for your specific setup.

Using pip For a standard CPU build on Windows, the pip command is identical to what you'd use on Linux and macOS.

pip3 install torch torchvision torchaudio

Using conda Likewise, the conda command stays consistent across all platforms, which is one reason it’s such a reliable choice no matter what OS you're on.

conda install pytorch torchvision torchaudio -c pytorch

This cross-platform consistency is a huge plus for the PyTorch ecosystem. It really simplifies the pytorch install process, especially for teams where developers are working on different machines.

Pro Tip on Reproducibility For any serious project, never use a generic install command. An unpinned pip install torch might work perfectly today but completely break your project a month from now when a new version drops.

Always pin your versions to ensure your code is reproducible. A much safer command looks like this: pip3 install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1. This guarantees anyone setting up your project gets the exact same dependencies.

Decoding the Install Command

Let's quickly break down a more complex, GPU-focused command to see what each piece does. Take this command for a CUDA 12.1 setup using pip:

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

  • pip3 install torch...: This is just the standard instruction to install the packages.
  • --index-url ...: This part is critical. It tells pip to ignore the default package index and instead look for packages in a special location: PyTorch's official repository for pre-compiled binaries (known as wheels).
  • /whl/cu121: This last bit specifies the CUDA version. Pip will now search for and download the PyTorch build that is specifically compatible with CUDA 12.1.

If you needed the build for CUDA 11.8, you'd simply change cu121 to cu118. This flag is the key to matching PyTorch to your system's NVIDIA driver capabilities, a topic we’ll dive into next.

Unlocking GPU Power with NVIDIA CUDA

nvidia-cuda

Moving from CPU to GPU training is the single biggest performance leap you can make in deep learning. Models that would take days to train on a CPU can often converge in just a few hours on a GPU. But this acceleration isn't automatic; it depends on a specific software stack with NVIDIA's CUDA platform at its heart.

The whole setup boils down to a critical relationship between three pieces: your NVIDIA driver, the CUDA Toolkit version PyTorch was built with, and the PyTorch package itself. Getting this alignment wrong is the number one reason a GPU-based pytorch install fails, leaving developers stuck with cryptic errors. Let's demystify this stack so you can avoid the usual headaches.

Checking Your Hardware and Driver Compatibility

Before you install a single thing, you need to confirm you have a CUDA-compatible NVIDIA GPU. Good news is, most NVIDIA GPUs from the last several years are supported.

The fastest way to check is by opening your terminal or command prompt and running the NVIDIA System Management Interface command:

nvidia-smi

If this command runs successfully, it will spit out a table with your GPU's name, the driver version, and the highest CUDA version your driver supports. That last piece of info is pure gold. it tells you the maximum CUDA version you can target. For instance, if nvidia-smi shows "CUDA Version: 12.1", you can install a PyTorch build for CUDA 12.1 or any version below it, but not 12.2 or higher.

If the command fails, it means your NVIDIA drivers are either missing or installed incorrectly. You'll need to fix that before you can go any further. This step is non-negotiable.

The Driver, The Toolkit, and PyTorch

This is where most installations go sideways. A lot of people mistakenly think they need to manually install the exact CUDA Toolkit version that PyTorch requires. In reality, modern PyTorch distributions (whether from pip or conda) come with their own necessary CUDA runtime libraries bundled right inside the package.

Here’s the key takeaway:

You only need a compatible NVIDIA driver on your system. You do not need a separate, system-wide CUDA Toolkit installation unless you're compiling custom CUDA code from source.

The PyTorch install command handles the rest by pulling in the correct libraries. Your only job is to make sure your driver is new enough to support the CUDA version that the PyTorch build requires.

For example, to run a PyTorch version built for CUDA 12.1, you don't need the full toolkit installed on your machine. What you absolutely do need is an NVIDIA driver that supports at least CUDA 12.1. This subtle distinction trips up countless developers.

Troubleshooting Common CUDA Errors

Even with a careful setup, you might hit a snag. One of the most frequent and frustrating errors is CUDA error: no kernel image is available for execution. This almost always means there's a mismatch between your GPU's architecture (its Compute Capability) and the version PyTorch was compiled for.

Here are a few actionable steps to resolve this and other common problems:

  • Verify GPU Detection: Run a quick Python script to see if PyTorch can even see your GPU. import torch print(torch.cuda.is_available()) If this returns False, your installation is definitely not configured correctly for your GPU.
  • Check All Versions: Meticulously re-check your NVIDIA driver version, the CUDA version in your PyTorch install command (cu121, cu118, etc.), and the PyTorch version itself. A tiny mismatch here is the most likely culprit.
  • Consider cuDNN: For optimal performance, especially with convolutional neural networks, you'll also want cuDNN. Our guide on what cuDNN is and how to install it provides a detailed walkthrough of this essential library.

Mastering this setup is an incredibly valuable skill in a rapidly expanding field. The global machine learning market, which leans heavily on frameworks like PyTorch, is projected to hit $192 billion in 2025. That's a 29.7% annual increase, fueled by the accessibility of these powerful open-source tools. You can find more details in this machine learning market analysis.

Using Docker for a Clean and Portable Setup

An illustration showing the Docker logo alongside the PyTorch logo, symbolizing a containerized setup.

If you've ever wrestled with dependency conflicts or felt the pain of an environment breaking after a system update, you know the struggle is real. Docker offers a powerful way out of that mess. Instead of installing PyTorch directly on your machine, you run it inside a lightweight, isolated container.

This approach gives you a perfectly clean, reproducible, and portable environment every single time. It completely sidesteps the dreaded "it works on my machine" problem, which makes collaborating with teammates a whole lot easier.

The PyTorch team does the heavy lifting by maintaining official images on Docker Hub. These come pre-configured with specific versions of PyTorch, Python, CUDA, and cuDNN, so you can just pull the exact image you need and get going in minutes.

Finding and Pulling the Right Image

The real key to using Docker for your pytorch install is picking the correct image tag. These tags are incredibly descriptive, letting you pinpoint the exact software stack your project requires.

You can browse all the available tags on the official PyTorch Docker Hub page. A typical tag follows a clear structure: pytorch/pytorch:<version>-cuda<cuda_version>-cudnn<cudnn_version>-runtime.

For instance, if you need an image with PyTorch 2.3.1, CUDA 12.1, and Python 3.10, you’d just run this command in your terminal:

docker pull pytorch/pytorch:2.3.1-cuda12.1-cudnn8-runtime

That one command downloads a complete, self-contained environment with all the necessary libraries and dependencies perfectly aligned. From there, you can launch a container from this image to start an interactive shell or even a Jupyter Notebook server.

While Docker provides excellent isolation for individual processes, you'll often need orchestrators to manage complex, multi-container applications. You can learn more by exploring the differences between Kubernetes vs. Docker to see how they fit together in a larger production ecosystem.

Enabling GPU Access Inside Docker

By default, a Docker container can't see your machine's GPU. To bridge that gap and unlock hardware acceleration, you'll need to install the NVIDIA Container Toolkit. This is a non-negotiable utility that lets your containers safely access your system's NVIDIA drivers.

Once the toolkit is installed, you just add a simple flag—--gpus all—when you run your container. This exposes your GPU(s) to the containerized environment, giving PyTorch the hardware access it needs for high-performance training.

Launching a GPU-enabled container is straightforward. This command starts an interactive bash shell inside your PyTorch container with full access to your machine's GPUs, ready for you to start working.

docker run --gpus all -it pytorch/pytorch:2.3.1-cuda12.1-cudnn8-runtime bash

This containerized approach gives you the best of both worlds: the raw power of your local GPU combined with the clean, predictable environment that Docker provides. It’s an incredibly effective strategy for maintaining your sanity across complex deep learning projects.

How to Verify and Troubleshoot Your Installation

Just because you see a "Successfully installed" message doesn't mean you can call it a day. The real test of a successful pytorch install is whether it can actually find and use your hardware, especially the GPU. This is where a quick verification step saves you hours of headaches down the road.

Think of it as the final shakedown for your new environment. A few simple commands now will confirm everything is talking to each other correctly. It's far better to catch a setup problem now than in the middle of debugging a complex model.

First-Line Verification Checks

To get started, fire up a Python interpreter or a new script. These two little snippets are your go-to sanity checks.

  • Check the PyTorch Version: First, make sure the library actually imports and that you've got the version you wanted. import torch print(torch.version)

  • Verify GPU Availability: This is the big one. It’s the single most important check to see if PyTorch can communicate with your NVIDIA GPU. import torch print(torch.cuda.is_available())

If the first command works and the second one prints True, congratulations! You're ready to go. That means PyTorch is installed correctly and has successfully found your CUDA-enabled GPU.

If it returns False, don't worry. This is a classic rite of passage, and it's time to do a little troubleshooting.

Solving Common Installation Problems

When your GPU check comes back False, it almost always points to a mismatch somewhere in your software stack. These issues are common, solvable, and something every developer runs into eventually. The error messages can look intimidating, but they typically fall into a handful of predictable categories.

A False return from torch.cuda.is_available() is the classic symptom of a setup issue. The most common cause is a mismatch between the NVIDIA driver on your system and the CUDA version your PyTorch package was built for.

When things go wrong, the error messages are your best clues. I've put together a table of the most common issues you'll encounter after a pytorch install and how to fix them.

Common PyTorch Installation Errors and Fixes

Here's a quick reference for decoding what's wrong and getting back on track.

Error Message Snippet Common Cause Recommended Solution
ModuleNotFoundError: No module named 'torch' PyTorch was installed into a different Python environment than the one you're using. Activate the correct conda or venv environment first. In your IDE, double-check which Python interpreter is selected for your project.
CUDA error: no kernel image is available The PyTorch build you installed doesn't match your GPU's architecture (its Compute Capability). Go back to the PyTorch website and carefully select the wheel for your specific CUDA version and GPU generation. This is common with older GPUs.
RuntimeError: The NVIDIA driver on your system is too old Your installed NVIDIA driver is outdated and doesn't meet the minimum version required by the CUDA toolkit PyTorch is using. Update your NVIDIA drivers to the latest stable version recommended for your graphics card. A system reboot is usually required.
torch.cuda.is_available() returns False This is a general symptom with multiple potential causes, most often a driver/CUDA version conflict or an incorrect install command. First, run nvidia-smi in your terminal to confirm the driver version. Then, rebuild your installation command on the official PyTorch website, ensuring the CUDA version matches what your driver supports.

Most of the time, the fix is as simple as running the correct installation command again after updating a driver or activating the right environment. Getting this right is a fundamental skill that sets you up for much smoother sailing as you build and train models.

Frequently Asked Questions About PyTorch Installation

Even the most straightforward guides can leave a few questions unanswered. Let's tackle some of the common hurdles and tricky points that come up during a PyTorch installation. These are the things that often trip people up, so getting them right from the start saves a lot of headaches.

Can I Have Multiple PyTorch Versions Installed?

Absolutely, and if you're doing any serious work, you'll find it's a must. The cleanest way to manage this is with isolated virtual environments. For every project, you should spin up a dedicated environment using tools like conda or Python's native venv.

This strategy completely sidesteps dependency conflicts. For instance, you could have a legacy project running on torch==1.13.1 in one environment while simultaneously kicking off a new experiment with the latest torch==2.3.1 in another. Both projects stay stable, and you can reproduce their results without any drama.

Do I Need the Full CUDA Toolkit from NVIDIA?

For most people, the answer is a simple no. When you install the GPU-enabled PyTorch package with pip or conda, it arrives with all the CUDA runtime libraries it needs already bundled inside. This self-contained setup is all you need for training models and running inference.

You only need to go through the full, system-wide CUDA Toolkit installation from NVIDIA's site if you're getting into compiling custom C++ or CUDA extensions from source. For everyone else, just make sure you have the right NVIDIA driver installed, and you're good to go.

What Is the Difference Between Torchvision and Torchaudio?

Think of the torch package as the core engine. It gives you the essentials—tensors, automatic differentiation, and the basic machinery for building neural networks. Torchvision and torchaudio are official companion libraries that sit on top of that core, built for specific jobs.

  • torchvision: Your go-to toolkit for computer vision. It's packed with popular datasets, pre-trained model architectures like ResNet, and standard image transformations.
  • torchaudio: Does the same thing but for sound. It provides datasets, models, and transformations specifically designed for audio processing tasks.

Adding them to your install command just means you're equipping yourself with these powerful, domain-specific tools from the get-go. And when it comes to picking the right hardware, comparing vendors is key; our guide on cloud GPU providers for machine learning can help you weigh your options on performance and cost.

The PyTorch ecosystem is intentionally modular. You start with the core torch package for the framework itself, then add libraries like torchvision for specialized tools. This lets you tailor your installation to your project's needs without adding a bunch of bloat, keeping your environments lean and efficient.

Community

Sign up or log in to comment