Welcome to our comprehensive guide on installing PyTorch, a powerful Python framework, on an Ubuntu 20.04. PyTorch is widely used for a variety of math-intensive applications, particularly in the fields of machine learning and artificial intelligence. By following the steps outlined in this guide, you will be able to set up PyTorch and leverage its capabilities for your projects.
1. What is PyTorch?
PyTorch is a popular Python-based framework that enables developers to leverage GPU hardware for accelerated computing. It is widely used in machine learning, artificial intelligence, and a variety of other math-intensive applications. PyTorch provides a rich ecosystem of tools and libraries that facilitate the development and deployment of complex models.
2. Prerequisites
Before installing PyTorch on your Ubuntu 20.04, ensure that you have met the following prerequisites:
- An Ubuntu 20.04 instance.
- Up-to-date base packages and libraries. To update, run the following commands:
sudo apt update sudo apt upgrade
- (Optional) If you plan to utilize GPU instances and your applications support CUDA cores, you will need to install the NVIDIA CUDA Toolkit. Run the following command to install the toolkit:
sudo apt install nvidia-cuda-toolkit
3. Installing PyTorch with Anaconda
Anaconda is a powerful package manager for Python and R, and we will use it to install PyTorch.
- Create a directory for Anaconda and navigate to it:
mkdir anaconda cd ~/anaconda
- Download the Anaconda installation script using
wget
:
wget https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh
- Provide execute permission to the script:
chmod +x ./Anaconda3-2020.11-Linux-x86_64.sh
- Run the script to install Anaconda. Scroll through the license agreement and enter ‘Yes’ to agree. When prompted for the installation directory, you can choose the default
~/anaconda
or specify a different location:
sudo ./Anaconda3-2020.11-Linux-x86_64.sh
- After installation, initialize Anaconda by running the following command and agreeing to the prompt:
conda init
- Install PyTorch and its dependencies using the
conda
package manager:
conda install pytorch torch vision torch audio cudatoolkit=10.2 -c pytorch
During the installation, you may be prompted to install additional packages. Type ‘y’ to proceed with the installation.
Congratulations, you have successfully installed PyTorch with Anaconda on your Ubuntu 20.04!
4. Installing PyTorch with Pip
If you do not have access to Anaconda, you can still install PyTorch using the Python package manager, Pip. Follow these steps to install PyTorch with Pip:
- Install Pip using the following command:
sudo apt install python3-pip
- Use Pip to install PyTorch with CPU support only:
pip3 install torch==1.9.1+cpu torchvision==0.10.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
- If you are using a GPU/NVIDIA instance and wish to utilize it with PyTorch, use the following command instead:
pip3 install -f torch torchvision
5. Testing your PyTorch Installation
To ensure that PyTorch has been installed successfully, perform the following steps:
- Enter the Python interpreter by running the command:
python3
- Import the PyTorch library functions:
import torch
- Verify if PyTorch is using a GPU by running the following command:
print(torch.cuda.is_available())
If the output is True
, it means that PyTorch is configured to use a GPU. If it is False
, ensure that you are using a GPU instance and that PyTorch was installed with GPU support.
- Determine the number of CUDA cards detected by running:
print(torch.cuda.device_count())
The output will indicate the number of physical GPUs available.
6. Uninstalling PyTorch
If, for any reason, you need to uninstall PyTorch, follow these steps:
- Remove PyTorch and any associated dependencies using the
conda
package manager:
conda remove pytorch
- If you used Pip for the installation, use the following command to uninstall PyTorch:
pip3 uninstall torch torchvision
- Optionally, remove Anaconda from your system by deleting the Anaconda directory:
rm -rf ~/anaconda
Conclusion
In this guide, we have walked you through the process of installing PyTorch on an Ubuntu 20.04. Whether you chose to use Anaconda or Pip, you can now harness the power of PyTorch for your machine learning and AI projects. Remember to test your installation to ensure everything is working correctly.
If you have any further questions or need assistance, don’t hesitate to reach out to our support team. Happy coding!
At Shape.host, we offer reliable and scalable Cloud VPS hosting services. Our optimized infrastructure ensures high performance and security for your applications. If you’re looking for a powerful hosting solution, visit Shape.host to explore our services.