PyTorch Installation Guide – GPU Setup & Troubleshooting

目次

1. What is PyTorch? What you’ll learn in this article

What is PyTorch?

PyTorch is an open-source machine learning framework developed by Facebook (now Meta). It is based on Python, and its code can be written simply and intuitively, making it widely used in data science and deep learning. PyTorch includes tensor computation and automatic differentiation, making it easy to design and train neural networks. It also leverages GPUs for fast processing, delivering performance suited for large datasets and complex models.

What you’ll learn in this

This article will cover the following topics in detail.
  1. How to verify the required environment and tools before installing PyTorch.
  2. Criteria for deciding whether to use pip or Anaconda.</>
  3. Step-by-step installation procedures and configuration.
  4. Troubleshooting and error resolution approaches.
  5. Next steps and recommended resources after installation.
By reading this article, even beginners can smoothly install PyTorch and take the first step in machine learning.

Intended Audience

  • Beginners interested in deep learning but confused about the PyTorch setup process.
  • Intermediate users who understand basic Python operations but lack confidence in library management or environment setup.
  • Engineers or data scientists who want to build high-performance computing environments using GPUs.
In the next section, we will discuss in detail the environment and tools required before installing PyTorch. Make sure to prepare appropriately before starting the installation.

2. Preparation before installation: required environment and tools

Checking the operating environment

Before installing PyTorch, first verify that the system requirements and necessary software are in place. This helps prevent issues that could arise after installation.

1. OS support status

PyTorch runs on the following operating systems:
  • Windows 10 or later
  • macOS 10.15 (Catalina) or later
  • Linux (Ubuntu, CentOS, and other major distributions)

2. Python version

PyTorch supports Python 3.8 and above. Check your current Python version with the following command.
python --version
Alternatively, depending on your environment, use the following.
python3 --version

3. Checking pip version

pip is the Python package management tool. If it’s not the latest version, you may encounter errors when installing PyTorch. Verify it with the following command.
pip --version
Update to the latest version with the following command.
python -m pip install --upgrade pip

Checking GPU environment (optional)

Using a GPU with PyTorch enables faster computation. If you plan to use a GPU, CUDA and cuDNN must be properly configured.

1. Verifying CUDA‑compatible GPU

Check GPU availability with the following command.
nvidia-smi
If GPU information appears in the output, a CUDA‑compatible GPU is recognized.

2. Checking CUDA and cuDNN versions

PyTorch closely aligns with specific CUDA versions. Check the appropriate version on PyTorch’s official download page. You can check the CUDA version with the following command.
nvcc --version

3. Installing CUDA

If CUDA is not installed, download and install it from NVIDIA’s official website.

Note: When GPU is unavailable

Even without a GPU, you can use PyTorch on CPU alone. For small projects or testing, the CPU version is sufficient.

Setting up a virtual environment (recommended)

It is recommended to use a virtual environment to isolate dependencies per project.

1. When using venv

python -m venv pytorch-env
source pytorch-env/bin/activate  # Linux/MacOS
pytorch-envScriptsactivate     # Windows

2. When using Anaconda

conda create -n pytorch-env python=3.10
conda activate pytorch-env

Next steps

With this, the pre‑installation preparation is complete. The next section will detail how to install PyTorch, covering the differences between pip and Anaconda and walking through each installation process.
侍エンジニア塾

3. Comparison of Installation Methods and Selection Criteria

pip vs Anaconda: Which Should You Choose?

There are two main ways to install PyTorch: pip and Anaconda. Let’s look at their characteristics and selection criteria.

1. Installing with pip

pip is Python’s standard package management tool, allowing lightweight and fast installations. Pros:
  • Easy and fast installation.
  • Install only the necessary libraries with minimal overhead.
  • Easy to combine with virtual environments (venv).
Cons:
  • Dependency management can become complex.
  • GPU driver and CUDA compatibility must be managed manually.
Recommended Use Cases:
  • For lightweight scripts or projects.
  • When you want to add PyTorch to an existing Python environment.

2. Installing with Anaconda

Anaconda is a Python distribution specialized for data science, making environment management easy. Pros:
  • Intuitive and easy virtual environment management.
  • Automatic dependency resolution reduces installation issues.</>
  • Works well with Jupyter Notebook and data analysis tools.
Cons:
  • Large installation size and longer setup time.
  • Commands may feel complex for beginners.
Recommended Use Cases:
  • Ideal for machine learning and data analysis projects.
  • When you are concerned about dependency or package management.

3. Summary of Selection Criteria

The table below provides a quick comparison of selection criteria between pip and Anaconda.
ItempipAnaconda
Installation speedFastSlightly slower
Dependency managementManual management requiredAutomatically resolved
Beginner-friendlinessSimple but configuration can be a bit trickyEasy management, beginner-friendly
Virtual environment managementRequires venv or virtualenvEasily managed with built-in conda command
Project scaleSuitable for small projectsSuitable for large projects

4. Which Should You Choose?

  • When you want a quick try: pip is recommended. It can be added quickly to an existing Python environment, making it ideal for those who want to start with a minimal setup.
  • When building a full‑scale project: Anaconda is recommended. It is optimal for cases that require robust dependency management and large‑scale environment setup.

Next Steps

At this point, you should understand the differences and selection criteria between pip and Anaconda. The next section will detail the installation procedures for each. We’ll walk through setting up a PyTorch environment using actual installation commands.

4. PyTorch Installation Guide (Practical Edition)

1. Install with pip (Fastest Method)

pip is simple and fast for installing, making it ideal when you want to start with a minimal setup.

1-1. CPU Version Installation

Run the following command to install PyTorch.
pip install torch torchvision torchaudio

1-2. GPU Version Installation (CUDA Support)

If you plan to use a GPU, you need to install the version matching your CUDA version. For CUDA 11.8 support, run the following command.
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

1-3. Verify Installation

After installation, you can check the version and CUDA availability with the following code.
import torch
print(torch.__version__)  # display version
print(torch.cuda.is_available())  # check GPU support
Example output:
2.0.1+cu118
True

2. Install with Anaconda (Recommended Method)

Anaconda makes virtual environment management easy and is handy when you want to isolate environments per project.

2-1. Create and Activate a Virtual Environment

Create a new environment using Python 3.10.
conda create -n pytorch-env python=3.10
conda activate pytorch-env

2-2. CPU Version Installation

Install with the following command.
conda install pytorch torchvision torchaudio cpuonly -c pytorch

2-3. GPU Version Installation (CUDA Support)

If you plan to use a GPU, use the installation command for CUDA 11.8 support.
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

2-4. Verify Installation

Run the following code to check the PyTorch version and GPU detection status.
import torch
print(torch.__version__)  # display version
print(torch.cuda.is_available())  # check GPU support
Example output:
2.0.1+cu118
True

3. Troubleshooting

3-1. Dependency Errors with pip

Error Message:
ERROR: Could not find a version that satisfies the requirement torch
Solution:
pip install --upgrade pip
Update to the latest version and retry.

3-2. Package Resolution Errors with Anaconda

Error Message:
PackagesNotFoundError: The following packages are not available
Solution: Check the channel specifications and be sure to include the official -c pytorch and -c nvidia.

4. Best Practices for Environment Setup

  • Use Virtual Environments: Separate environments per project to avoid dependency conflicts.
  • Package Management Tips: Do not mix pip and conda; maintain consistency per environment.
  • Record Dependencies: Export and back up dependencies after installation.
For pip:
pip freeze > requirements.txt
For Anaconda:
conda list --export > environment.yml

Next Steps

The PyTorch installation is now complete. The next section will detail how to configure CUDA and cuDNN for GPU operation. Optimize post‑installation settings to achieve a faster computational environment.
侍エンジニア塾

5. Setting up CUDA and cuDNN (GPU usage)

This section provides a detailed guide on the steps required to set up CUDA and cuDNN for using GPUs with PyTorch. Leveraging GPUs can significantly boost computation speed, benefiting training and inference of machine learning models.

1. What is CUDA?

CUDA (Compute Unified Device Architecture) is a parallel computing platform provided by NVIDIA. It maximizes the computational power of GPUs to efficiently handle large-scale calculations.

2. What is cuDNN?

cuDNN (CUDA Deep Neural Network library) is an NVIDIA library optimized for deep learning. It is designed to enable frameworks such as PyTorch to perform fast neural network operations.

1. CUDA installation steps

1-1. Verify GPU

First, check whether your PC has a CUDA‑capable GPU.
nvidia-smi
If this command displays the GPU model and CUDA driver version, CUDA is available.

1-2. Verify CUDA compatibility

Check the CUDA version compatible with PyTorch on the official page below. PyTorch official download page

1-3. Download and install CUDA

  1. From NVIDIA’s CUDA download page, select the OS and version you need.
  2. Follow the instructions to download the installer and run the installation.

1-4. Verify installation

Use the following command to verify that CUDA is installed correctly.
nvcc --version
Example output:
Cuda compilation tools, release 11.8, V11.8.89

2. cuDNN installation steps

2-1. Download cuDNN

  1. Download from NVIDIA Developer site’s cuDNN page.
  2. An NVIDIA account is required; log in or register.

2-2. Installation steps

  1. Extract the downloaded ZIP file.
  2. Copy the extracted files into the CUDA installation directory.
Example:
sudo cp cuda/include/* /usr/local/cuda/include/
sudo cp cuda/lib64/* /usr/local/cuda/lib64/

2-3. Set environment variables

Set the environment variables using the following commands.
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

2-4. Verify configuration

Run the following command to confirm that cuDNN is installed correctly.
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
Example output:
#define CUDNN_MAJOR 8
#define CUDNN_MINOR 0
#define CUDNN_PATCHLEVEL 5

3. Test PyTorch and CUDA compatibility

3-1. Verify with Python code

Use the following code to check if PyTorch is linked with CUDA.
import torch
print(torch.cuda.is_available())  # If True, CUDA is recognized
print(torch.cuda.get_device_name(0))  # Display GPU model name
Example output:
True
NVIDIA GeForce RTX 3060

3-2. Troubleshooting errors

Error example 1:
torch.cuda.is_available() -> False
Solution:
  • Check the version compatibility between CUDA and PyTorch and install the appropriate versions.
  • Update the NVIDIA driver to the latest version.
Error example 2:
RuntimeError: CUDA error: invalid device function
Solution:
  • If the CUDA version is outdated, update to the latest version.
  • Also verify the cuDNN version and ensure compatibility.

Next steps

The CUDA and cuDNN setup is now complete. In the next section, we will detail test procedures to verify that PyTorch is installed correctly and explain how to address common errors. Let’s continue reviewing troubleshooting points.

6. Verifying the Installation

This section explains how to verify that PyTorch has been installed correctly. It also introduces common issues and errors along with their solutions.

1. Verifying the Installation with Python Code

1-1. Basic Functionality Check

import torch
print("PyTorch Version:", torch.__version__)  # version check
Running this code will display the installed PyTorch version. Example output:
PyTorch Version: 2.0.1

1-2. Checking GPU Support

print("CUDA Available:", torch.cuda.is_available())  # GPU support availability
Example output:
CUDA Available: True
If ‘True’ is displayed, the GPU is recognized correctly.

1-3. Checking Detailed GPU Information

if torch.cuda.is_available():
    print("Device Name:", torch.cuda.get_device_name(0))  # display GPU model name
    print("CUDA Version:", torch.version.cuda)  # display CUDA version
Example output:
Device Name: NVIDIA GeForce RTX 3060
CUDA Version: 11.8

2. Running Simple Code for Verification

Tensor Generation and Computation

import torch

# generate a random 3x3 matrix
x = torch.rand(3, 3)
print("Random Tensor:")
print(x)

# matrix addition
y = torch.rand(3, 3)
z = x + y
print("Addition Result:")
print(z)
Example output:
Random Tensor:
tensor([[0.2567, 0.8954, 0.6543],
        [0.1234, 0.4567, 0.7890],
        [0.9876, 0.5432, 0.1123]])

Addition Result:
tensor([[0.6789, 1.3456, 1.1234],
        [0.3456, 0.7890, 1.5678],
        [1.4321, 0.9876, 0.5432]])
If this code runs without errors, PyTorch is installed correctly.

3. Common Errors and Solutions

3-1. When PyTorch Cannot Be Imported

Error message:
ModuleNotFoundError: No module named 'torch'
Cause: PyTorch may not be installed correctly, or the virtual environment may not be active. Solution:
  1. Reactivate the virtual environment.
conda activate pytorch-env
  1. Reinstall it.
pip install torch torchvision torchaudio

3-2. When the GPU Is Not Recognized

Error message:
CUDA Available: False
Cause:
  • CUDA is not installed.
  • The CUDA version is incompatible with PyTorch.
  • The GPU driver is outdated.
Solution:
  1. Check the NVIDIA driver version and update to the latest.
nvidia-smi
  1. Check the CUDA version.
nvcc --version
  1. Reinstall drivers or CUDA as needed.

4. Troubleshooting Tips

  1. Refer to the official documentation: If a problem occurs, consult the PyTorch official support page.
  2. Search the error message: Googling the exact error message often leads to solutions on forums or GitHub Issues.
  3. Align versions: Compatibility among CUDA, cuDNN, and PyTorch versions is crucial. Refer to the official compatibility matrix to verify the latest versions.

Next Steps

With this, PyTorch is correctly installed and basic functionality verification is complete. The next section will delve deeper into handling common issues. You’ll learn specific approaches for error situations and how to leverage support resources.

7. Common Issues and Solutions

In this section, we provide a detailed explanation of common errors that occur during PyTorch installation and usage, along with their solutions.

1. Installation Errors and Solutions

1-1. Dependency Mismatch Error

Error Message:
ERROR: Could not find a version that satisfies the requirement torch
Cause:
  • The pip or Python version is outdated, causing incompatibility with packages required by PyTorch.
  • The specified CUDA version is not compatible with the selected PyTorch version.
Solution:
  1. Update pip and Python to the latest versions.
python -m pip install --upgrade pip
  1. Recheck the environment’s dependencies.
pip check
  1. Obtain the appropriate installation command from the official site and try again.

1-2. Virtual Environment Errors

Error Message:
ModuleNotFoundError: No module named 'torch'
Cause: The virtual environment may not be activated, or it could be corrupted. Solution:
  1. Reactivate the virtual environment.
conda activate pytorch-env
  1. Recreate the virtual environment.
conda create -n pytorch-env python=3.10
conda activate pytorch-env
conda install pytorch torchvision torchaudio -c pytorch

2. GPU Configuration Errors and Solutions

2-1. GPU Not Detected

Error Message:
torch.cuda.is_available() -> False
Cause:
  • CUDA is not installed correctly.
  • The GPU driver version is outdated.
  • The PyTorch and CUDA versions are incompatible.
Solution:
  1. Check the NVIDIA driver version and update to the latest.
nvidia-smi
  1. Verify the CUDA version.
nvcc --version
  1. Reinstall the driver and CUDA if necessary.

2-2. CUDA Error

Error Message:
RuntimeError: CUDA error: invalid device function
Cause: The CUDA and PyTorch versions do not match or are incompatible. Solution:
  1. Check the official compatibility matrix for CUDA and PyTorch versions.
  2. Install using a compatible version.
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

3. Runtime Errors and Solutions

3-1. Out-of-Memory Error

Error Message:
RuntimeError: CUDA out of memory.
Cause: Using large models or batch sizes can exhaust GPU memory. Solution:
  1. Reduce the batch size.
  2. Delete unnecessary variables to free memory.
del variable_name
torch.cuda.empty_cache()
  1. Monitor memory usage and optimize the code accordingly.

3-2. Slow Performance

Cause: The processing may be happening on the CPU instead of the GPU. Solution:
  1. Check whether the GPU is recognized.
print(torch.cuda.is_available())
  1. Explicitly specify the device in the code.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)

4. Other Issues and Solutions

4-1. When a PyTorch Update Is Needed

Solution: Update to the latest version.
pip install --upgrade torch torchvision torchaudio

4-2. Using Support Forums

  • Official Documentation: See the support page on the PyTorch official site.
  • Community Forums: Ask questions on Stack Overflow or PyTorch Discuss.

Next Steps

So far, you have learned about common errors during installation and runtime, and how to address them. This enables you to handle issues flexibly during environment setup and program execution. In the next section, we will recap the key points as a summary of the article and introduce the next learning steps with PyTorch. We will also add advice to encourage reader action.

8. Summary and Next Steps

In the articles so far, we have detailed the installation of PyTorch, environment setup, and troubleshooting. In this section, we review the key points and introduce the next steps for leveraging PyTorch.

1. Review of This Article’s Key Points

1. What is PyTorch?

  • A framework optimized for machine learning and deep learning, supporting fast GPU computation.

2. Pre‑Installation Preparation

  • You need to verify the OS and Python versions and prepare for CUDA and cuDNN installation.
  • Using a virtual environment is recommended to simplify dependency management.

3. Installation Procedures

  • pip: Lightweight and quick to install.
  • Anaconda: Easy environment management, suitable for large‑scale projects.

4. Configuring CUDA and cuDNN

  • If you plan to use a GPU, you must verify compatibility and install CUDA and cuDNN.

5. Verifying Installation and Troubleshooting

  • After installation, check the version and GPU support, and we also provide solutions for any errors that arise.

2. Next Steps Using PyTorch

Once PyTorch is correctly installed, the next step is to build actual models and work on machine learning or deep learning projects. Below are some recommended topics for the next steps.

2-1. Building Your First Neural Network

Create a simple classification model to get comfortable with PyTorch. Please try the following. Sample Code: Linear Regression Model
import torch
import torch.nn as nn
import torch.optim as optim

# Prepare data
x = torch.rand(100, 1)
y = 2 * x + 1 + 0.1 * torch.randn(100, 1)

# Define model
model = nn.Linear(1, 1)
loss_fn = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Training loop
for epoch in range(1000):
    y_pred = model(x)
    loss = loss_fn(y_pred, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

# Verify results
print(model.weight.item(), model.bias.item())
This code is an example of training a simple linear regression model, allowing you to verify PyTorch’s basic functionality.

2-2. Applied PyTorch Projects

  1. Creating an Image Classification Model
  • Practice an image classification task using a CNN (Convolutional Neural Network).
  1. Building a Natural Language Processing Model
  • Try text classification or translation models using RNNs or Transformers.
  1. Leveraging Pre‑trained Models
  • Build advanced applications using pre‑trained models such as ResNet or BERT.

3. Learning Resources

3-1. Official Documentation and Tutorials

3-2. Online Courses and Video Learning

  • PyTorch courses are available on online learning platforms such as Coursera and Udemy.
  • YouTube also offers free practical videos, which are recommended.

3-3. Hands‑On Projects and Sample Code

    • Referencing projects and sample code published on GitHub can help you learn practical usage.
4. Conclusion In this article, we thoroughly explained how to install and configure PyTorch, provided troubleshooting tips, and suggested next learning steps. This enables you to start machine learning projects with PyTorch smoothly. As the next step, work on building real models and applying pre‑trained models to further enhance your machine learning skills. If you have any questions or feedback, please feel free to reach out in the comments!