in

How to Install PyTorch on Windows and Linux: The Complete Guide

Hello there! PyTorch is one of the most popular deep learning frameworks used by data analysts and AI researchers globally. As your resident tech geek, I‘m excited to share this comprehensive guide on how to properly install PyTorch on both Windows and Linux.

Whether you‘re looking to follow PyTorch tutorials or power cutting-edge neural network research, having PyTorch correctly setup is crucial. So let‘s dive in!

Why PyTorch is Worth Learning

But first – why use PyTorch in the first place? Well from my experience as a data analyst, PyTorch offers some major advantages:

  • Python-first – PyTorch uses intuitive Python APIs for coding models, unlike frameworks that force proprietary languages. This makes PyTorch easy to learn!

  • Dynamic graphs – The code defines model architecture, allowing flexible debugging and iteration.

  • GPU acceleration – Seamlessly leverage NVIDIA GPUs for major speedups in training models. This enables you to train more complex neural networks faster.

PyTorch usage has skyrocketed in recent years. According to a 2020 Kaggle survey, it is now the 2nd most popular ML framework after TensorFlow:

Kaggle framework popularity

So learning PyTorch is a valuable investment in your data science skills. Now let‘s get it installed!

Prerequisites to Install PyTorch

Before we can install PyTorch, we need a few things setup on our computer:

  • Python 3.6+ – The core language PyTorch is built on. Download it if not installed.

  • pip – The Python package installer. Comes with most Python installations.

  • CUDA (optional) – The NVIDIA tool for GPU computing. Highly recommended for increased training speeds. Install guide here.

  • Anaconda (recommended) – A Python distribution for data science. Provides conda for managing packages/environments. Get it here.

Once you have those basics covered, we can move on to the fun part – installing PyTorch!

Step 1 – Create Conda Environment

Now as a data analyst, I always recommend installing Python packages like PyTorch in a dedicated Conda environment.

Conda allows you to create isolated "virtual environments" for each of your projects. This avoids version conflicts between dependencies.

To create a Conda environment called pytorch:

conda create -n pytorch python=3.7

Then activate it using:

conda activate pytorch

The environment is now ready for installing PyTorch!

Step 2 – Install PyTorch

Head over to the PyTorch website and hover over Get Started > Locally to pick your OS.

You‘ll see a table like this:

PyTorch Install Table

Fill in your preferences for compute language, package manager, CUDA version etc. It will generate the exact install command you need.

For example on Linux, to install CPU-only PyTorch:

pip install torch torchvision torchaudio

And for CUDA-enabled PyTorch:

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

The full install takes a few minutes. Grab some coffee while you wait!

Step 3 – Verify the Install

With PyTorch installed, let‘s make sure it works properly.

Open up a Python shell and run:

import torch
print(torch.__version__) 

It should print out your PyTorch version without errors.

We can also test CUDA support:

import torch
torch.cuda.is_available()

This returns True if CUDA is enabled.

And we‘re all set! PyTorch is ready to power your ML projects.

Docker Alternative

If you don‘t want to install locally, PyTorch offers prebuilt Docker images:

  • pytorch/pytorch – CPU-only PyTorch
  • pytorch/pytorch:latest-cuda – CUDA-enabled PyTorch

For example, to grab the CUDA image:

docker pull pytorch/pytorch:latest-cuda

Then start it with:

docker run --rm -it --gpus all pytorch/pytorch:latest-cuda

Now you have a Docker container with PyTorch and CUDA all setup!

Detailed Linux Install

Let‘s go through installing PyTorch on Ubuntu Linux step-by-step:

Install System Dependencies

PyTorch requires some base packages that we need to install first:

sudo apt update
sudo apt install python3 python3-pip python3-dev build-essential cmake libopenblas-dev libblas-dev m4

This covers Python 3, pip, compilers, and math libraries needed.

Optional CUDA dependencies:

sudo apt install nvidia-cuda-toolkit
sudo apt install libcudnn7

Create Conda Environment

Next, make your conda environment:

conda create -n pytorch python=3.7 
conda activate pytorch

This will create an isolated pytorch environment.

Install PyTorch

Now run your chosen install command from pytorch.org:

CPU-only:

pip3 install torch torchvision torchaudio

With CUDA:

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

Once complete, verify the install worked as covered earlier.

And that‘s it for Linux! PyTorch is installed and ready to use.

Step-by-Step Windows Install

If you‘re on Windows, here are the detailed steps to get PyTorch running:

Install Visual Studio Build Tools

Download and run the Visual Studio installer. This provides the necessary C++ compiler toolchain for building Python packages like PyTorch.

Install Python

Download the latest Python 3.x from python.org. Make sure to add Python to your system PATH.

Open cmd.exe and upgrade pip:

python -m pip install --upgrade pip

Create Conda Environment

Launch the Anaconda Prompt and create a conda environment:

conda create -n pytorch python=3.7

Activate it with:

conda activate pytorch 

Install PyTorch

Go to pytorch.org and grab the Windows install command.

For example:

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch 

Run this to install PyTorch and optional CUDA support based on your preferences.

Double check it installed properly as covered earlier.

And now PyTorch is installed on Windows as well!

Verifying the Installation

Once the install finishes, verify PyTorch is importable:

import torch
print(torch.__version__)

You should see your version printed out, like 1.11.0.

For CUDA:

import torch
torch.cuda.is_available() 

Returns True if CUDA is enabled.

If you run into any error, try reinstalling or post on the PyTorch forums for help.

Troubleshooting Tips

If the install isn‘t working, a few things to check:

  • Ensure Python, pip, and other prerequisites are setup
  • Try creating a fresh conda environment
  • Use a prebuilt PyTorch Docker image
  • Search GitHub issues for similar install problems
  • Ask on Stack Overflow if you‘re stuck!

Retrace your steps and confirm each piece is in place. You‘ll be importing PyTorch in no time.

Exciting Next Steps

With PyTorch installed, you unlock a whole new world of possibilities:

  • Follow PyTorch tutorials to gain a strong foundation
  • Build models with CNN and RNN architectures
  • Import datasets and train neural networks
  • Leverage GPUs for hyperparameter tuning at scale
  • Implement papers from arxiv.org to replicate state-of-the-art techniques
  • Compete on Kaggle using your newfound PyTorch skills!

The best way to get comfortable with PyTorch is to start using it. Follow tutorials, build your own datasets, and let me know if you have any questions!

Key Takeaways

Congratulations – you now have PyTorch installed and ready to use!

We covered:

  • Installing on Windows, Linux, and with Docker
  • Creating isolated Conda environments
  • CUDA support for fast GPU computing
  • Verifying the install was successful
  • Fixing common installation issues
  • Next steps to ramp up your PyTorch skills

Now you can start training neural networks in Python right away. PyTorch provides flexibility and cutting-edge capabilities to power your AI projects.

I hope you found this guide helpful! Let me know if you have any other topics you‘d like me to cover. Now go enjoy building something cool with PyTorch!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.