PyTorch is a powerful Python framework that enables developers to leverage GPU hardware for accelerated machine learning and AI applications. In this step-by-step guide, we will walk you through the process of installing PyTorch on an Ubuntu 20.04. Whether you are a seasoned developer or just getting started with PyTorch, this guide will help you set up your environment and get you up and running quickly.
1. Introduction to PyTorch
PyTorch is a widely-used Python framework that provides support for a variety of math-intensive applications, particularly those that involve machine learning and AI. It allows developers to access GPU hardware, enabling faster processing and parallelism benefits. PyTorch is known for its user-friendly interface, flexibility, and excellent community support. It is widely adopted by researchers and developers alike due to its extensive ecosystem and wide range of applications.
2. Updating Ubuntu 20.04
To ensure that your Ubuntu 20.04 instance is up to date, it is essential to update the base packages and libraries before proceeding with the PyTorch installation. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
These commands will update your Ubuntu instance with the latest packages and perform any necessary upgrades.
3. Installing the NVIDIA CUDA Toolkit
If your instance supports GPU/NVIDIA CUDA cores and the PyTorch applications you plan to use also support CUDA cores, it is recommended to install the NVIDIA CUDA Toolkit. This toolkit provides the necessary libraries and development tools for GPU acceleration. To install the NVIDIA CUDA Toolkit, run the following command:
sudo apt install nvidia-cuda-toolkit
Please note that the NVIDIA CUDA Toolkit is not required for CPU-only (non-GPU) instances.
For a more detailed guide on installing the NVIDIA CUDA Toolkit.
4. Using Conda to Install PyTorch
Anaconda is a popular package manager for Python and R, and we will use it to install PyTorch. Follow the steps below to install Anaconda and PyTorch using Conda:
- Create a directory in your home directory to install Anaconda and move into 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
- Give execute permission to the script:
chmod +x. /Anaconda3-2020.11-Linux-x86_64.sh
- Execute the script:
sudo ./Anaconda3-2020.11-Linux-x86_64.sh
Follow the prompts in the installation script, and when asked to initialize Anaconda3 by running conda init, type ‘yes’ and press enter.
- Once the installation is complete, you can install PyTorch and its dependencies using the following command:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
During the installation process, you may be prompted to install additional packages. Type ‘y’ to proceed with the installation.
Congratulations! You have now successfully installed PyTorch using Conda.
5. Using Pip to Install PyTorch
If you do not have access to Anaconda, or if you prefer using Pip, you can still install PyTorch using the following steps:
- Install Pip by running the following command:
sudo apt install python3-pip
- Once Pip is installed, you can use it 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 have a GPU/NVIDIA instance and wish to utilize it for PyTorch, you can use the following command instead:
pip3 install torch torchvision
6. Testing your PyTorch Installation
After installing PyTorch, it is crucial to verify that the installation was successful and that PyTorch is functioning correctly. Follow these steps to test your PyTorch installation:
- Open a terminal and enter the Python interpreter:
python3
- Import the PyTorch library functions:
import torch
If there are no errors, and the prompt changes to >>>
, it means that PyTorch has been successfully imported.
- Determine 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 using a GPU. If the output is False
, it indicates that either you are not using a GPU instance or there was an issue during the installation.
- To check the number of CUDA cards found on your server, run the following command:
print(torch.cuda.device_count())
The output should display the number of physical cards that were found.
7. Uninstalling PyTorch
In some cases, you may need to uninstall PyTorch from your system. Here are the steps to uninstall PyTorch using Anaconda:
- Remove PyTorch from your server by running the following command:
conda remove pytorch
Please note that if you have any datasets associated with PyTorch, you will need to remove them independently.
- Remove Anaconda from your system by executing the following command:
rm -rf ~/anaconda
Please exercise caution when using the above command, as it will permanently delete the Anaconda directory. Adjust the command based on your installation directory.
- If you downloaded the Anaconda installation script, you can remove it using the following command:
rm /home/<user>/Downloads/Anaconda3-2020.11-Linux-x86_64.sh
8. Conclusion
Congratulations! You have successfully installed PyTorch on your Ubuntu 20.04. Whether you used Conda or Pip, you are now ready to develop and run powerful machine learning and AI applications using PyTorch. Make sure to explore the vast ecosystem and community support that PyTorch offers to further enhance your projects.
If you have any further questions or need assistance, feel free to reach out to the Shape.host Support team or visit our Community Site. We are committed to providing you with reliable and scalable Cloud Hosting solutions to empower your business.
9. Frequently Asked Questions
Q1: Can I install PyTorch on a CPU-only instance?
Yes, you can install PyTorch on a CPU-only instance using Pip. Simply follow the instructions provided in section 6 of this guide.
Q2: What are the benefits of using GPU instances for PyTorch?
GPU instances offer significant advantages for PyTorch applications, such as faster processing speeds and enhanced parallelism. This is particularly beneficial for computationally intensive tasks like training deep neural networks.
Q3: Can I uninstall PyTorch without removing other packages installed with Anaconda?
Yes, you can uninstall PyTorch without removing other packages installed with Anaconda. When running the command conda remove pytorch
, Anaconda will handle the removal of PyTorch specifically.