Install PyTorch on Ubuntu: Complete Guide
To install PyTorch in Ubuntu, you can use either conda or pip. Here are the steps to install PyTorch using conda.
- First, make sure you have installed conda. If you haven’t installed conda, you can follow these steps for installation:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc
- Create a new conda environment and activate it.
conda create -n myenv
conda activate myenv
- Install PyTorch using conda.
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
Once the installation is complete, you can use PyTorch in your Python scripts. Here is a simple example:
import torch
x = torch.tensor([1.0, 2.0, 3.0])
y = torch.tensor([4.0, 5.0, 6.0])
z = x + y
print(z)
Running the above code will output the sum of two tensors, indicating that PyTorch has been successfully installed and utilized.
You can also install PyTorch using pip. Here are the steps to install PyTorch using pip:
- First, make sure you have installed pip. If not, you can install it using the following command:
sudo apt update
sudo apt install python3-pip
- Install PyTorch using pip.
pip install torch torchvision torchaudio
After installation is complete, you can use PyTorch as shown above.
I hope this can assist you in installing and using PyTorch on Ubuntu.