25th Anniversary Savings | 25% Off Dedicated Servers*Shop Now
25th Anniversary Savings | 25% Off VPS Hosting* †††Shop Now
Dedicated Hosting Deals | From $99/moShop Now
Earn hosting credits and a chance to win an Amazon gift card when you refer friends to Liquid Web!Read our promo rules

Installing PyTorch on Ubuntu: a Guide

Reading Time: 4 minutes

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 group, that acts as a high-level interface for developers to create applications like natural language processors. In this tutorial, we are going to cover how to install PyTorch via Anaconda and PIP.

How to Install PyTorch on Ubuntu

Pre-flight Check

  • 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.5 or higher is required.

Install via Anaconda

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:

root@ubuntu1604:~# apt-get update -y

After this, we need to download and run the bash installation script for Anaconda.

Running a bash script downloaded directly from the internet is not recommended. Be sure you trust the source providing the installation script before proceeding. The link in the below curl command is the latest version of the command line installer for Linux at the time this article was written.
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. We are going to use the defaults.

root@ubuntu1604:~# bash Miniconda3-latest-Linux-x86_64.sh

To start using Anaconda, we will need to refresh the terminal:

root@ubuntu1604:~# source ~/.bashrc

(Optional). 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. To turn this behavior off, we can run this command:

(base) root@ubuntu1604:~# conda config --set auto_activate_base false

For the purposes of this tutorial, we want to allow Anaconda’s base environment to continue to be active.

Step 2: Install PyTorch

Now that we have Anaconda installed and activated, it’s time to install PyTorch.

(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, we need to drop into a Python repl (a 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 we are in the repl, we can paste in the following snippet

import torch
x = torch.rand(3, 7)

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 we are back at the command prompt, we can deactivate the Anaconda base environment if we use

(base) root@ubuntu1604:~# conda deactivate

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 using Pip, the Python Package manager, in a virtual Python environment. To ensure that the installation of PyTorch and it’s dependencies has no adverse effect on your system’s Python installation, it’s advisable to install it in a virtual Python environment.

First, we 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

Now it’s time to create the virtual Python environment where we will install PyTorch

root@ubuntu1604:~/pytorch_awesome# python3 -m venv pytorch-awesome

.Next, we need to activate the virtual Python environment we just created.

root@ubuntu1604:~/pytorch_awesome# source pytorch-awesome/bin/activate
(pytorch-awesome) root@ubuntu1604:~/pytorch_awesome#

We 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, we can install PyTorch.

(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

We can test PyTorch is properly installed the same way we did with the Anaconda installation. First drop into a Python shell

The Python version you see within the initial output from the Python shell might be slightly different than mine
(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)

The output will look something like this:

>>> 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 (Ctrk+D).

Once we are back at our command prompt, we can deactivate the Python virtual environment.

(pytorch-awesome) root@ubuntu1604:~/pytorch_awesome# deactivate

Join Us!

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.

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions Team or an experienced Hosting Advisors today!

Avatar for Justin Palmer

About the Author: Justin Palmer

Justin Palmer is a professional application developer with Liquid Web

Latest Articles

How to Install Adminer MySQL Database Management Tool on AlmaLinux

Read Article

What is CGI-Bin and What Does it Do?

Read Article

Top 10 Password Security Standards

Read Article

Top 10 Password Security Standards

Read Article

How to Use the WP Toolkit to Secure and Update WordPress

Read Article