How to Install PyTorch on Ubuntu 20.04
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
This guide shows you how to install PyTorch, a Python framework, on an Ubuntu 20.04 Linode. PyTorch provides support for a variety of math-intensive applications that run on GPU and CPU hardware. Linode offers dedicated CPU instances and GPU instances that you can use to run PyTorch-based projects.
What is PyTorch?
PyTorch allows popular Python-based apps to access GPU hardware to speed up machine learning, AI, and a large number of supported PyTorch ecosystem apps. PyTorch can also be used with CPUs as you set up your applications for use. Moving the application compute logic to a GPU hardware instance allows processing at a far faster output than CPU instances permit. For production workloads, GPU instances provide much higher speed and parallelism benefits.
PyTorch Installation Steps
Prerequisites
The instructions below install PyTorch and Anaconda on an Ubuntu 20.04 instance. For the best results, use a Linode GPU instance with sufficient memory and storage to accomplish your task. Up to 96GB of memory and 7TB of storage are available.
Optimizing a task may also require using external data sources. If using external data sources and data sets, like Linode Object Storage, you should prepare them ahead of setting up your PyTorch GPU instance.
Update your Ubuntu 20.04 instance. The base packages and libraries must be updated first.
sudo apt update
Then the updates must be installed and upgraded.
sudo apt upgrade
If the instance to be used supports GPU/NVIDIA CUDA cores, and the PyTorch applications that you’re using support CUDA cores, install the NVIDIA CUDA Toolkit.
sudo apt install nvidia-cuda-toolkit
For full instructions, see Installing the NVIDIA CUDA Toolkit.
NoteThe NVIDIA CUDA Toolkit is not needed on CPU-only (non-GPU) instances.
Use Conda to Install PyTorch
Anaconda is a package manager for Python and R. The steps in this section uses Anaconda to install PyTorch.
In your home directory, create a 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
Then, execute the script.
sudo Anaconda3-2020.11-Linux-x86_64.sh
Scroll through the license agreement and agree to it by entering
Yes
. Indicate the destination directory for Anaconda. The default directory is~/anaconda
.The installer prompts you
to initialize Anaconda3 by running conda init
. We recommend enteringyes
(if you enterno
, conda cannot modify your shell scripts).You are now ready to install PyTorch and PyTorch tools using Anaconda. From the
~/anaconda
directory install PyTorch:conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
Note
Using Anaconda to install PyTorch, installs the NVIDIA CUDA Toolkit. For instances that install CPU-only versions of PyTorch, skip to the Use Pip to Install PyTorch section.During installation, you are prompted to install new packages. Type
y
to install them. Your output displays a similar output:Collecting package metadata (current_repodata.json): done Solving environment: done ... The following packages will be UPDATED: conda 4.10.1-py38h06a4308_1 --> 4.10.3-py38h06a4308_0 Proceed ([y]/n)? y Downloading and Extracting Packages openh264-2.1.0 | 722 KB | ##################################### | 100% torchaudio-0.9.1 | 4.4 MB | ##################################### | 100% cudatoolkit-10.2.89 | 365.1 MB | ##################################### | 100% libiconv-1.15 | 721 KB | ##################################### | 100% libidn2-2.3.2 | 81 KB | ##################################### | 100% conda-4.10.3 | 2.9 MB | ##################################### | 100% nettle-3.7.3 | 809 KB | ##################################### | 100% ninja-1.10.2 | 1.4 MB | ##################################### | 100% libunistring-0.9.10 | 536 KB | ##################################### | 100% libtasn1-4.16.0 | 58 KB | ##################################### | 100% torchvision-0.10.1 | 28.7 MB | ##################################### | 100% lame-3.100 | 323 KB | ##################################### | 100% gnutls-3.6.15 | 1.0 MB | ##################################### | 100% pytorch-1.9.1 | 706.8 MB | ##################################### | 100% ffmpeg-4.3 | 9.9 MB | ##################################### | 100% Preparing transaction: done Verifying transaction: done
Use Pip to Install PyTorch
If you don’t have access to Anaconda, PyTorch can be installed with Python Pip. Learn about Pip and Python programming environments in our Using Pipenv to Manage Python Packages and Versions guide.
To install Pip, use the following command:
sudo apt install python3-pip
Then, 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
To install PyTorch using GPU/NVIDIA instances, use the following command:
pip3 install -f torch torchvision
Test your PyTorch Installation
Use the steps below to ensure that you have a working PyTorch installation.
Enter the Python interpreter.
python3
The prompt should change to the python interpreter:
>>>
Import the PyTorch library functions.
>>> import torch >>>
Note
If the torch library cannot be found, python returns an error message indicatingnot-found
.Determine if PyTorch is using a GPU:
>>>print (torch.cuda.is_available) true
If the output returns
false
, there may be one of several conditions to fix:- Ensure that you are using a GPU instance.
- Check your server logs for errors during the installation of any of the software components, especially PyTorch and the NVIDIA CUDA Toolkit
Determine if your server’s CUDA cards were found.
>>>print (torch.cuda.device_count) >>>2
The output should determine the number of physical cards that were found.
Uninstall PyTorch
The steps in this section shows you how to use Anaconda to uninstall PyTorch.
Remove PyTorch from your server with the command below. Any datasets must also be removed independently from removing PyTorch.
conda remove pytorch
Note
You can also use theuninstall
command to remove PyTorch libraries. Any datasets must also be removed independently from removing PyTorch.Caution
When using theuninstall
command, the Linode may also be deleted, but it cannot be recovered once deleted.Remove Anaconda from your system.
rm -rf ~/anaconda
Caution
The above command is dangerous, and must refer specifically to the directory where anaconda was installed. In the above example, Anaconda was installed in the/home/<user>/anaconda
directory. Adjust the command to ensure the directory deleted is indeed theanaconda
directory.Remove the Anaconda installation script:
rm /home/<user>/Downloads/Anaconda3-2020.11-Linux-x86_64.sh
This page was originally published on