In this guide, I am going to go through installing PyTorch on Windows and Linux, using Anaconda to manage the installation of the packages.

The installation will be done using the conda command-line tool built into Anaconda.

PyTorch is a machine learning library written in Python and is based on the Torch framework. It was developed by Facebook and is comparable to the likes of Tensorflow by Google. It is useful in the fields of 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 the modified BSD, and is under the Linux Foundation.

Prerequisites

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

If you do not already have it installed, this guide on how to install Anaconda will walk you through the whole process. After following that guide, you can proceed to install PyTorch.

Installing PyTorch on Linux

As a good practice, begin by updating software packages in your Linux distribution. In my case, I am using Ubuntu and apt to manage my packages, so I will use the following command to update:

$ sudo apt update && apt upgrade 

When you are done updating the packages, go to the official PyTorch website installation page. Scroll down the page until you find the installation wizard that looks like the one below:

<img alt="Pytorch table to help you generate the correct command to install" data- data-src="https://kirelos.com/wp-content/uploads/2022/11/echo/Screenshot-from-2022-11-03-18-38-36.png" data- decoding="async" height="348" src="data:image/svg xml,” width=”832″>

Using this wizard, you will be able to click on the different options to provide your system information and preferences, and in turn, you will get a command you can use in your terminal to install PyTorch.

After providing my system information, this is what it looks like:

<img alt="Screenshot-from-2022-11-03-18-44-42" data- data-src="https://kirelos.com/wp-content/uploads/2022/11/echo/Screenshot-from-2022-11-03-18-44-42.png" data- decoding="async" height="348" src="data:image/svg xml,” width=”832″>

I opted for the stable Linux version, and I 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 a CPU as opposed to a GPU.

At the bottom of the table is the command I can use to install PyTorch, but before running this command, I would want 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, thus preventing dependency conflicts. One of the benefits of Anaconda is that it helps you create and manage virtual environments easily.

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

conda create -n pytorch python=3.7

After the environment is created, I will activate it using 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 prompts to install PyTorch. Once done, I will restart the terminal session to take effect.

Now to verify that PyTorch was correctly installed, we are going to try and import it in the Python interactive shell. Make sure you are in the Pytorch virtual environment using the command:

conda activate pytorch

Once you are in the Pytorch virtual environment, open the python interactive shell by typing the command:

python

Once the shell session starts, write the following line of code and press ENTER

import torch

If Python runs without errors, then the installation was successful. But if you got a Module Not Found error, it means something went wrong during installation. You may try reinstalling it again.

Installing PyTorch on Windows

To begin, on your Windows machine, search for the Anaconda Prompt program and open it. This is where we are going to be running 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 to install PyTorch. We begin by going to the PyTorch website installations page. After which, we can scroll down to the section of the page where this installation wizard is located:

<img alt="Installation-table" data- data-src="https://kirelos.com/wp-content/uploads/2022/11/echo/Installation-table.png" data- decoding="async" height="309" src="data:image/svg xml,” width=”816″>

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

<img alt="installation-table-with-data" data- data-src="https://kirelos.com/wp-content/uploads/2022/11/echo/installation-table-with-data.png" data- decoding="async" height="300" src="data:image/svg xml,” width=”806″>

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

After installation completes, we can verify if it was successful by opening the Python interactive shell and trying to import PyTorch.

So inside the Anaconda prompt, start an interactive Python session.

python

After the session starts, 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 installed PyTorch on both Windows and Linux using conda. It is possible to install it via PIP like a normal PIP package. In both cases, I opted for the CPU installation. However, you can still use CUDA, which is a system toolkit developed by Nvidia that speeds up training by parallelizing operations across GPUs.