How to Install PyTorch in Anaconda with Conda or Pip

Here are two ways to install PyTorch in Anaconda.

  1. Using Conda
  2. Using Pip

Method 1: Install PyTorch in Anaconda with Conda

To install PyTorch in Anaconda using the conda package manager, follow these steps:

Open the Anaconda Prompt

  1. On Windows: Search for “Anaconda Prompt” in the Start menu and open it.
  2. On macOS or Linux: Open a terminal.

Set up a new environment (optional but recommended)

You can create a new conda environment for your PyTorch projects to isolate the dependencies from your base environment. You can do this by running:

conda create --name myenv python=3.9

Replace myenv with your desired environment name and 3.9 with your desired Python version.

Activate the environment

If you’ve created a new environment in step 2, activate it using:

conda activate myenv

If installing directly in the base environment, you can skip this step.

Install PyTorch

To install PyTorch using conda, run this command:

conda install pytorch torchvision torchaudio -c pytorch

This command installs PyTorch, torchvision (a package with popular datasets, model architectures, and image transformations for computer vision), and torchaudio (similar to torchvision but for audio tasks).

The -c pytorch specifies the channel from which to install the packages.

Verify the installation

import torch

print(torch.__version__)

That’s it! You should now have PyTorch installed in your Anaconda environment.

Method 2: Install PyTorch in Anaconda with Pip

To install PyTorch in Anaconda using the pip, follow these steps:

Open the Anaconda Prompt

  1. On Windows: Search for “Anaconda Prompt” in the Start menu and open it.
  2. On macOS or Linux: Open a terminal.

Set up a new environment (optional but recommended)

You can create a new conda environment for your PyTorch projects to isolate the dependencies from your base environment. You can do this by running:

conda create --name myenv python=3.9

Replace myenv with your desired environment name and 3.9 with your desired Python version.

Activate the environment

If you’ve created a new environment in step 2, activate it using:

conda activate myenv

If installing directly in the base environment, you can skip this step.

Ensure the pip is up-to-date

Before installing any packages with pip, it’s a good practice to ensure you have the latest version:

pip install --upgrade pip

Install PyTorch using a pip

You can install PyTorch using pip with the following command:

pip install torch torchvision torchaudio

Verify the installation

import torch

print(torch.__version__)

And that’s it! PyTorch should now be installed in your Anaconda environment using pip. Remember that while conda and pip can coexist in a single environment, it’s always a good idea to stick to one package manager to avoid potential conflicts.

If you’re using an Anaconda environment, conda is generally preferred. However, if a package is not available in the conda repositories, pip is a valuable alternative.

Related posts

How to Uninstall PyTorch with Anaconda

Leave a Comment