Setting up Ubuntu for HuggingFace

Just wanted to document steps I took to successfully setup an Ubuntu machine for using models on HuggingFace. Refer to for installation instructions for using Transformers module.

Ubuntu comes installed with Python3. I started with creating a new environment for the project. I switched to the folder where I was going to create projects/files for using HuggingFace

Creating new Python Environment

  1. python3 -m venv .env
  2. source .env/bin/activate

Adding the new environment to Jupyter

Jupyter does not use the new environment created above. We need to add the environment to Jupyter. But before that we need to install ipykernel

  1. python3 -m pip install ipykernel
  2. python3 -m ipykernel install –user –name=myenv –display-name=“HuggingFace Env”
    Here name can be anything, you don’t have to crate the environment with that name.
  3. Launch “jupyter notebook”, create a notebook and switch kernel to “HuggingFace Env” as specified in the display-name above

Installing PyTorch

Go to and select appropriate options in “Start Locally” section to see the command to be executed for installing PyTorch. I used the following command (for CPU only option on Linux –

python3 -m pip install torch torchvision torchaudio –index-url

Install Transformers

python3 -m pip install transformers

Verify Setup

Run following code, either in Jupyter notebook or from a .py file –

import torch
from transformers import pipeline

sentiment_analyzer = pipeline(“sentiment-analysis”)
result = sentiment_analyzer(“this setup is working!”)

-Ram Kulkarni

Leave a Reply