en How to install PyTorch on Windows and Linux

How to install PyTorch on Windows and Linux

This guide provides instructions for installing PyTorch on Windows and Linux using Anaconda to manage package installations.

Installation is done using 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 Tensorflow by Google and others. It’s useful in computer vision and natural language processing, and is used by companies like Tesla to develop autopilot software.

PyTorch is free, open source, licensed under modified BSD, and under the Linux Foundation.

How to install PyTorch on Windows and Linux
How to install PyTorch on Windows and Linux

Prerequisites

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

If you haven’t installed it yet, this guide on how to install Anaconda will walk you through the process. After following that guide, you can proceed to install PyTorch.

Installing PyTorch on Linux

As a good practice, first update your Linux distribution’s software packages. In my case, I’m using Ubuntu and use apt to manage packages, so I use the following command to update:

 $ sudo apt update && apt upgrade 

Once the package update is complete, go to the installation page on the official PyTorch website . Scroll down the page until you find the installation wizard as shown below.

Pytorch table to help generate the correct command to install
Pytorch table to help generate the correct command to install

The wizard allows you to specify system information and settings by clicking through various options, and displays commands that you can use to install PyTorch in your terminal.

After entering your system information, it should look like this:

Screenshot of 2022-11-03-18-44-42
Screenshot of 2022-11-03-18-44-42

I will choose a stable Linux version and use Conda for package management. I also chose to use PyTorch with Python rather than C++/Java. And run PyTorch on the CPU instead of the GPU.

At the bottom of the table there is a command that you can use to install PyTorch, but before you can run this command you need to create an Anaconda virtual environment called pytorch .

Virtual environments allow you to create a project and keep its dependencies isolated from dependencies in other projects, thereby preventing dependency conflicts. One of the benefits of Anaconda is the ease with which you can create and manage virtual environments.

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

 conda create -n pytorch python=3.7

Once the environment is created, activate it using the following command:

 conda activate pytorch

Once the environment is active, install PyTorch by running the command you generated earlier on the PyTorch website.

 conda install pytorch torchvision torchaudio cpuonly -c pytorch

Follow the prompts to install PyTorch. Once completed, restart your terminal session for it to take effect.

Now, to verify that PyTorch is installed correctly, try importing it into your Python interactive shell. Verify that you are in the Pytorch virtual environment using the following command:

 conda activate pytorch

Once you are in the Pytorch virtual environment, enter the following command to open a Python interactive shell.

 python

Once your shell session starts, write the following line of code and press Enter.

 import torch

If Python runs without errors, the installation was successful. However, if you get a “module not found” error, it means something went wrong during installation. Please try reinstalling again.

Installing PyTorch on Windows

First, search for and open the Anaconda Prompt program on your Windows machine. Now run the command.

Once the program opens, use the command to create a virtual environment for your PyTorch installation.

 conda create -n pytorch python=3.7

Once you have created your virtual environment, you can activate it by running the following command:

 conda activate pytorch

Once your virtual environment is active, you can proceed to install PyTorch. First, go to the installation page on the PyTorch website . You can then scroll down to the section of the page where this installation wizard is located.

Installation stand
Installation stand

Select System Information here and the wizard will provide commands to install PyTorch. Select stable releases for Windows, managed by Conda, used through the Python programming language, and run on the CPU. As a result, my table looks like this:

Installation table with data
Installation table with data

Then copy and paste the command into the Anaconda prompt and press Enter.

Once the installation is complete, you can check whether the installation was successful by opening a Python interactive shell and attempting to import PyTorch.

Therefore, start an interactive Python session within the Anaconda prompt.

 python

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

 import torch

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

last word

In this guide, we installed PyTorch on both Windows and Linux using conda. It can be installed via PIP just like a regular PIP package. In both cases, I chose to install a CPU. However, you can still use CUDA, a system toolkit developed by Nvidia that speeds up training by parallelizing operations across GPUs.

Easy-to-understand explanation of “How to install PyTorch on Windows and Linux”! Best 2 videos you must watch

NVIDIA GPU搭載PC 機械学習環境構築 Tensorflow/PyTorch Ubuntu 22.04 LTS
https://www.youtube.com/watch?v=I1JoBawuJtI&pp=ygVGIFdpbmRvd3Mg44GoIExpbnV4IOOBqyBQeVRvcmNoIOOCkuOCpOODs-OCueODiOODvOODq-OBmeOCi-aWueazlSZobD1KQQ%3D%3D
WindowsとLinuxのデュアルブート手順を細かく説明(LinuxMint)
https://www.youtube.com/watch?v=IIJqI9G9N-M&pp=ygVGIFdpbmRvd3Mg44GoIExpbnV4IOOBqyBQeVRvcmNoIOOCkuOCpOODs-OCueODiOODvOODq-OBmeOCi-aWueazlSZobD1KQQ%3D%3D