How to Install PyTorch on Windows and Linux

In this tutorial, I will walk you through installing PyTorch on Windows and Linux using Anaconda to manage package installation.

Installation will be done using the conda command line tool built into Anaconda.

PyTorch is a machine learning library written in Python and based on the Torch framework. It was developed by Facebook and is comparable to Google's Tensorflow. It is useful in computer vision and natural language processing and has been used by companies such as Tesla to develop autopilot software.

PyTorch is free and open source, licensed under a modified BSD license and part of the Linux Foundation.

Prerequisites

To follow this guide, you need to have Anaconda installed on the machine you are working on.

If you don't already have it installed, this Anaconda installation guide will guide you through the entire process. By following this guide, you can proceed with installing PyTorch.

Installing PyTorch on Linux

It is recommended to start by updating the software packages on your Linux distribution. In my case, I'm using Ubuntu and I can manage my packages, so I'll use the following command to update:

$ sudo apt update && apt upgrade

When you are done updating the packages, go to the installation page of the official PyTorch website. Scroll down the page until you find the setup wizard, which looks like this:

Using this wizard, you will be able to click through various options to provide your system information and preferences, and in turn, you will be given a command that you can use in your terminal to install PyTorch.

After providing information about my system, it looks like this:

I have chosen stable Linux and will be using Conda to manage my packages. I also chose to use PyTorch with Python rather than C++/Java. And I will be running my PyTorch on the CPU, not the GPU.

At the bottom of the table is a command that I can use to install PyTorch, but before running this command, I would like to create an Anaconda virtual environment called pytorch.

Virtual environments allow you to create projects and keep their dependencies isolated from the dependencies of other projects, which prevents dependency conflicts. One of the benefits of Anaconda is that it helps you easily create and manage virtual environments.

To create a virtual environment with a version of Python 3.7, I will enter the following command:

conda create -n pytorch python=3.7

After creating the environment, I activate it with the following command:

conda activate pytorch

Once the environment is active, I will run the command generated earlier on the PyTorch website to install PyTorch.

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Follow the instructions to install PyTorch. After that, I will restart my terminal session for the changes to take effect.

Now, to make sure PyTorch is installed correctly, we will try to import it into an interactive Python shell. Make sure you are in the Pytorch virtual environment using the command:

conda activate pytorch

Once you're in the Pytorch virtual environment, open an interactive Python shell by typing:

python

After starting a shell session, write the following line of code and press ENTER.

import torch

If Python runs without errors, then the installation was successful. But if you get the "Module not found" error, then something went wrong during the installation. You can try reinstalling it again.

Installing PyTorch on Windows

To get started, on your Windows computer, find the Anaconda Prompt program and open it. This is where we will run the commands.

Once the program is open, we are going to create a virtual environment for our PyTorch installation using the command.

conda create -n pytorch python=3.7

After creating the virtual environment, we can activate it by running the following command:

conda activate pytorch

Once the virtual environment is active, we can proceed with installing PyTorch. Let's start by going to the installation page of the PyTorch website. We can then scroll down to the section of the page where this setup wizard is located:

Here we select information about our system and the wizard will give us the command to install PyTorch. I'm going to opt for a stable version for Windows, managed by Conda, used through the python programming language, and running on a CPU. As a result, my table will look like this.

Then copy the command, paste it into the Anaconda prompt and press ENTER.

Once the installation is complete, we can check if it was successful by opening an interactive Python shell and trying to import PyTorch.

So, on the Anaconda command line, start an interactive Python session.

python

Once the session is started, import PyTorch using the following line of code:

import torch

If this action completes without errors, then the installation was successful.

Final words

In this guide, we have installed PyTorch on both Windows and Linux using conda. It can be installed via PIP like a normal PIP package. In both cases, I chose the processor setting. However, you can still use CUDA, a system toolkit developed by Nvidia that speeds up learning by parallelizing operations across GPUs.

Related posts