Data analysis via machine learning is becoming increasingly important in the modern world. PyTorch is a machine learning Python library, developed by the Facebook AI Research (FAIR) group, that acts as a high-level interface for developers to create applications like natural language processors. In this tutorial, you are going to learn how to install PyTorch via Anaconda and PIP.
- These instructions are being performed on an Ubuntu dedicated cloud server as the root user.
- This installation does not include support for GPU acceleration.
- Python 3.6 or higher is recommended.
Step 1: Install Anaconda
Anaconda is a popular package management system for data scientists working with Python. The reason for this is because it comes pre-baked with full data science packages. This means developers have less work to do in the software dependency department. It is the recommended package management interface for PyTorch.
First, as a best practice, ensure all packages are up to date with the below command.
root@ubuntu1604:~# apt-get update -y
After this, you will need to download and run the bash installation script for Anaconda.
root@ubuntu1604:~# curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
Once the installation script is finished downloading, run it and follow the prompts. You are going to use the defaults.
root@ubuntu1604:~# bash Miniconda3-latest-Linux-x86_64.sh
To start using Anaconda, you will need to refresh the terminal.
root@ubuntu1604:~# source ~/.bashrc
You’ll notice (base) now precedes your normal command prompt. This means Anaconda’s base environment is active. By default, it is auto-activated each time you enter a new shell session. It is optional to turn this behavior off by using the following command.
(base) root@ubuntu1604:~# conda config --set auto_activate_base false
For the purposes of this tutorial, you want to allow Anaconda’s base environment to continue to be active.
Step 2: Install PyTorch
Now that you have Anaconda installed and activated, it’s time to install PyTorch Ubuntu.
(base) root@ubuntu1604:~# conda install pytorch torchvision cpuonly -c pytorch
You’ll notice a prompt during installation. Enter y to finish the installation.
Proceed ([y]/n)? y
Verify PyTorch is Installed
Finally, it’s time to verify that PyTorch is installed and available to use. To do so, you will need to drop into a Python REPL (Read–Eval–Print Loop).
(base) root@ubuntu1604:~# Python Python 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Once you are in the REPL, you can paste in the following snippet.
import torchPIP x = torch.rand(3, 7) print(x)
The output should look something like this.
>>> import torch >>> >>> x = torch.rand(3, 7) >>> print(x) tensor([[0.2833, 0.7547, 0.1444, 0.6286, 0.4838, 0.1576, 0.3350], [0.8544, 0.5095, 0.0808, 0.4367, 0.4174, 0.4669, 0.6155], [0.4436, 0.7904, 0.8911, 0.2073, 0.5987, 0.3607, 0.4366]]) >>>
To exit the Python shell, hold the ctrl key and press the D key (Ctrl+D).
Now that you are back at the command prompt, you can deactivate the Anaconda base environment if you use the below command.
(base) root@ubuntu1604:~# conda deactivate root@ubuntu1604:~#
Install via PIP
Step 1: Install python3-venv
If you don’t need all of the additional packages that come along with Anaconda, you can install PyTorch Ubuntu using PIP, the Python package manager, in a virtual Python environment. To ensure that the installation of PyTorch, and its dependencies, has no adverse effect on your system’s Python installation, it’s advisable to install it in a virtual Python environment.First, you need to install the python3-venv package to make it possible to create a virtual Python environment.
root@ubuntu1604:~# apt-get install -y python3-venv
Step 2: Prepare the Environment
To start, make a directory to house your project and change into it using the cd command.
root@ubuntu1604:~# mkdir pytorch_awesome root@ubuntu1604:~# cd pytorch_awesome root@ubuntu1604:~/pytorch_awesome#
Now it’s time to create the virtual Python environment where you will install PyTorch Ubuntu.
root@ubuntu1604:~/pytorch_awesome# python3 -m venv pytorch-awesome
Next, you need to activate the virtual Python environment that you just created.
root@ubuntu1604:~/pytorch_awesome# source pytorch-awesome/bin/activate (pytorch-awesome) root@ubuntu1604:~/pytorch_awesome#
You can now see (pytorch-awesome) precedes our normal shell prompt because our newly created virtual Python environment is activated.
Step 3: Install PyTorch
While the new Python virtual environment is active, you can install PyTorch Ubuntu.
(pytorch-awesome) root@ubuntu1604:~/pytorch_awesome# pip install torch==1.3.0+cpu torchvision==0.4.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
You can test if PyTorch is properly installed the same way you did with the Anaconda installation. First, drop into a Python shell.
(pytorch-awesome) root@ubuntu1604:~/pytorch_awesome# Python Python 3.5.2 (default, Oct 8 2019, 13:06:37) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Now copy and paste this snippet into the Python shell and hit enter.
import torch x = torch.rand(3, 7) print(x)
The output will look something like this.
>>> import torch >>> import torch >>> >>> x = torch.rand(3, 7) >>> print(x) tensor([[0.0416, 0.9980, 0.2793, 0.8503, 0.0285, 0.8286, 0.8091], [0.8038, 0.7944, 0.0110, 0.1239, 0.0611, 0.9727, 0.6899], [0.3036, 0.0378, 0.1660, 0.7076, 0.5073, 0.0686, 0.9490]]) >>>
To exit the Python shell, hold the ctrl key and press the D key (Ctrl+D).
Once you are back at your command prompt, you can deactivate the Python virtual environment.
(pytorch-awesome) root@ubuntu1604:~/pytorch_awesome# deactivate root@ubuntu1604:~/pytorch_awesome#
How to Uninstall PyTorch
You can uninstall PyTorch using two methods.
Method 1: Using Anaconda
You can use the conda command to remove PyTorch if you have installed it on Anaconda. Type the following conda command into your anaconda terminal.
conda remove Pytorch
PyTorch will be successfully removed from the anaconda packages.
Method 2: Using PIP
If you have installed PyTorch using PIP, you can uninstall using pip commands. The pip command depends on the Python version installed. If you have Python 3.xx installed, then the pip3 command needs to be used. For Python 2.xx, you can use the pip command. In this case, Python 3.xx is installed; hence, you can uninstall PyTorch using the following command.
pip3 uninstall torch
PyTorch is an artificial intelligence framework built on the Torch library and used for applications like computer vision and natural language processing. In this tutorial, you have seen how to install and uninstall PyTorch using different methods. Hope the article was helpful.
Would you like to know more about PyTorch or how it can benefit you? Reach out to one of our experienced system administrators today, and we can provide information on how this technology can have a positive impact on your dedicated server setup!
Once you get your app set up and ready for production, our HIPAA compliant web hosting service can ensure further security and privacy on your server.Contact us to speak with one of our knowledgeable sales team of experienced Hosting Advisors today!
What is PyTorch?
What are the prerequisites for PyTorch installation on an ubuntu server?
How can you test the PyTorch installation?
What are the various methods to install PyTorch Ubuntu?
How to uninstall PyTorch?
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.