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: 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: 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: 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: After the environment is created, I will activate it using the following command: Once the environment is active, I will run the command generated earlier on the PyTorch website to install 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: Once you are in the Pytorch virtual environment, open the python interactive shell by typing the command: Once the shell session starts, write the following line of code and press ENTER 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. After creating the virtual environment, we can activate it by running the following command: 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: 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. 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. After the session starts, import PyTorch using the following line of code: 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.

How to Install PyTorch on Windows and Linux - 2How to Install PyTorch on Windows and Linux - 52How to Install PyTorch on Windows and Linux - 87How to Install PyTorch on Windows and Linux - 21How to Install PyTorch on Windows and Linux - 41How to Install PyTorch on Windows and Linux - 20How to Install PyTorch on Windows and Linux - 34How to Install PyTorch on Windows and Linux - 29How to Install PyTorch on Windows and Linux - 83How to Install PyTorch on Windows and Linux - 43How to Install PyTorch on Windows and Linux - 81How to Install PyTorch on Windows and Linux - 55How to Install PyTorch on Windows and Linux - 33How to Install PyTorch on Windows and Linux - 28How to Install PyTorch on Windows and Linux - 82How to Install PyTorch on Windows and Linux - 80How to Install PyTorch on Windows and Linux - 25How to Install PyTorch on Windows and Linux - 6How to Install PyTorch on Windows and Linux - 86How to Install PyTorch on Windows and Linux - 71How to Install PyTorch on Windows and Linux - 30How to Install PyTorch on Windows and Linux - 66How to Install PyTorch on Windows and Linux - 71How to Install PyTorch on Windows and Linux - 91How to Install PyTorch on Windows and Linux - 32How to Install PyTorch on Windows and Linux - 32How to Install PyTorch on Windows and Linux - 88