"
This article is part of in the series
Published: Wednesday 31st January 2024

Install PyTorch Using pip

If you’re getting into deep learning projects, you will need the PyTorch library on your machine. This popular library boasts two prominent features, without which deep learning modeling would be a lot more challenging. 

The core data structure the library offers, Tensor, is easy to migrate to GPUs for the fastest computing. It also features autograd, an automatic differentiation engine that lets you conveniently train neural networks by calculating gradients automatically. 

In this quick guide, we will walk you through installing PyTorch on Windows, macOS, and Linux using pip. We also discuss how you can use Anaconda to install this library on your machine. Let’s begin!

How to Install PyTorch on Windows 

To install PyTorch on Windows, you must ensure that you have Python installed on your system. Additionally, you need will need pip or Anaconda installed to follow along with this tutorial. 

Installing PyTorch on Windows Using pip

To begin, check whether you have Python installed on your machine. Customarily, developers do this by inquiring about the Python version on the command line, like so:

python -V

If the command returns a Python version, you have it installed on your machine, and it is working correctly. 

Next, you need to ensure that the pip package manager is installed on your Windows operating system. Again, you can do this by inquiring about the package manager’s version in the command prompt like this:

pip -V

If you see a version number appear, pip is installed on your system and is functioning correctly. You can finally begin installing PyTorch on your computer. You can do this by running:

pip3 install torch torchvision torchaudio

On running the command, PyTorch will quickly install on your machine. But, like all cautious programmers, it’s best to double-check that the library has indeed been installed correctly.

You can do this by running the following command: 

pip3 show torch

Did the PyTorch version information appear? If yes, PyTorch has been installed on your Windows operating system.

Let’s say some time passes, and you need to uninstall the PyTorch version you have installed. You can proceed with the uninstallation with this command:

pip uninstall torch

Straightforward, isn’t it?

Installing PyTorch on Windows Using Anaconda

Anaconda, which is also referred to as the conda package manager, allows you to install PyTorch with ease similar to that offered by pip. If you haven’t installed Anaconda on your machine, you can find the required files on its official site.

To check whether you have Anaconda installed on your Windows operating system, run the following in the command prompt:

conda -V

Did a version number appear? If yes, Anaconda is installed and working correctly. To install PyTorch, you must run the following command in the command prompt:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

If your machine runs the command without fault, Anaconda will install PyTorch on your computer. 

To verify whether Anaconda has correctly installed the library, you can inquire about the version of PyTorch installed on your machine by running:

conda list -f PyTorch

PyTorch 2.1 is the latest version of the library that’s available at the time of writing. Can you believe this open-source project has been powering many of the world’s neural networks since 2016? 

If the command returns the details of the newest version of PyTorch, you can be sure it’s correctly installed. 

How to Install PyTorch on macOS

In this section, we’ll go over how you can install the popular PyTorch library on macOS. Of course, you will need Python and a package manager, be it pip or Anaconda, installed on your machine beforehand. 

We will also walk you through checking whether you have the prerequisites on your machine to remove the need for second-guessing.

Installing PyTorch on macOS Using pip

Begin by verifying that Python is installed on your machine by running the following command inside a terminal:

python3 –version

After running it, you can proceed to the next step if version information appears. As you may have guessed, you must now check whether pip is installed on your machine. Do it by running this command:

pip3 –version

If you see the details of the installed pip version appear, you are ready to install PyTorch on your machine. All it involves is running the following command in the terminal:

pip3 install torch torchvision torchaudio

To double-check that the library is correctly installed, run this command to check its version:

pip3 show torch

You will see the version information appear, which means you can now use the library in your Python projects.

If you decide to uninstall PyTorch at a later time, you can go ahead with removing the library from your machine by running this command:

pip uninstall torch

You should see a confirmation that the library has been removed when you run the above command. 

Installing PyTorch on macOS Using Anaconda

This is another method of installing PyTorch on macOS. 

To get started, you will need to activate the Anaconda prompt on your machine in case you’ve freshly installed it or it got deactivated. 

Doing this involves running the following command:

conda activate

After you’ve activated Anaconda, it’s time to check the version you have installed on your machine. Run the following:

conda --version

The output should show the version number of the conda package manager. If you get this output, you are ready to install PyTorch with it. Otherwise, you must install the latest Anaconda distribution on your machine. 

The next step is installing the PyTorch library on your machine using the conda package manager:

conda install pytorch torchvision torchaudio -c pytorch

With this, PyTorch should be installed on your machine. You can verify that it was correctly installed by running the following:

conda list -f PyTorch

If the command outputs the version details as expected, PyTorch is ready for you to use.

How to Install PyTorch on Linux

PyTorch is quite easy to install on most Linux distros. You must ensure that you have Glibc installed on your machine, and it needs to be version 2.17 or above. Of course, you also need Python and pip or conda installed, depending on your preference. 

If your machine has the prerequisites, follow the steps below to install PyTorch.

Installing PyTorch on Linux Using pip

Start by checking the Python version that’s currently installed on your machine. This is to double-check that Python is installed and working correctly on your machine. To check the version, run:

python -V

Do any Python version details appear? 

If yes, check that pip is installed on your machine and working correctly with this command:

pip -V

If both commands give version details, you can install the latest PyTorch library by running this simple command in a terminal:

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

This should install PyTorch along with any required dependencies on your machine. To verify a successful installation, run this command and check its version details:

pip3 show torch

The newest version number of the PyTorch library should pop up in the terminal. If it does, your copy of PyTorch is ready to use. 

If you decide to uninstall PyTorch later, you can do it by running this command:

pip uninstall torch

How to Install PyTorch on Linux Using Anaconda

Begin by verifying that the prerequisite – the Anaconda package manager – is installed on your machine. Here’s how:

conda -V

The command above should give you the version details of the installed Anaconda version. If it does, you have Anaconda installed, and you can install PyTorch with this command:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

With this command, the newest version of PyTorch should be installed on your machine. To verify if it has been installed, you can run this command to check the version details:

conda list -f PyTorch