Complete Hugging Face Authentication Guide

Table of Contents

  1. Why Authentication is Needed

  2. Installing the Hugging Face CLI

  3. Authentication Methods Overview

  4. Step-by-Step Setup

  5. How Token Detection Works

  6. Environment Variables Explained

  7. Choosing the Right Method

  8. Troubleshooting

  9. Security Best Practices

  10. Quick Reference


Why Authentication is Needed

Some models on Hugging Face (including Llama models from Meta) require:

  1. User authentication - to verify who's downloading the model

  2. License acceptance - to agree to the model's terms of use

  3. Access requests - some models need approval before download

This is Meta's way of tracking usage and ensuring compliance with their license terms.


Installing the Hugging Face CLI

Before you can authenticate, you need to install the huggingface_hub package which provides the CLI tools.

Installation Methods

Option 1: Using pip (Recommended)

Option 2: Using pip with --user flag (if you get permission errors)

Option 3: Using conda/mamba

Option 4: With --break-system-packages (Ubuntu/Debian if needed)

Verify Installation

After installation, verify the installation:

You should see output like:

Note: The main command is huggingface-cli, but most subcommands use the shorter hf prefix (like hf auth login, hf whoami).

If Command Not Found

If you get "command not found", make sure pip's bin directory is in your PATH:

Linux/Mac - Temporary:

Linux/Mac - Permanent:

Alternative method if PATH issues persist:


Authentication Methods Overview

There are three main ways to authenticate with Hugging Face:

MethodBest ForToken LocationPersistence
hf auth loginLocal development, personal useSaved to diskPermanent (until logout)
Environment VariableServers, CI/CD, DockerEnvironmentSession/permanent depending on setup
Explicit token= ArgumentTesting, multiple accountsIn codePer function call

Token Detection Priority

When you use Hugging Face libraries, they check for authentication in this order:

  1. Token passed as token= argument (highest priority - overrides everything)

  2. HF_TOKEN environment variable

  3. HUGGING_FACE_HUB_TOKEN environment variable (legacy)

  4. Saved token from hf auth login

  5. No authentication (will fail for gated models)

Important: If HF_TOKEN is set in your environment, you do not need to pass token= as an argument - the library will automatically use it!


Step-by-Step Setup

Step 1: Create a Hugging Face Account

If you don't have one already:

  1. Go to https://huggingface.co/join

  2. Sign up with email or GitHub account

  3. Verify your email address

Step 2: Create an Access Token

An access token is like a password that lets your computer authenticate with Hugging Face.

  1. Log in to https://huggingface.co

  2. Click your profile picture (top right) → Settings

  3. Go to "Access Tokens" (left sidebar)

  4. Click "New token"

  5. Choose token type:

    • Read - Download models and datasets (recommended for most users)

    • Write - Upload models (only if you're sharing models)

  6. Name your token (e.g., "my-laptop", "research-project")

  7. Click "Generate token"

  8. COPY THE TOKEN - You won't be able to see it again!

    • It looks like: hf_AbCdEfGhIjKlMnOpQrStUvWxYz1234567890

Step 3: Accept Model Licenses

Before you can download gated models like Llama, you must accept their license:

For Llama 3.2-1B:

  1. Go to https://huggingface.co/meta-llama/Llama-3.2-1B

  2. You'll see a message: "Access to this model requires gating"

  3. Click "Request access" or "Accept license"

  4. Fill in the form:

    • Your intended use case

    • Agree to Meta's terms

    • Provide your information

  5. Submit the form

  6. Wait for approval (usually instant, but can take a few minutes)

You'll see a green checkmark when approved: ✓ "You have been granted access to this model"

For Llama 3.2-1B-Instruct:

Step 4: Choose Your Authentication Method

Now you need to authenticate your computer. Choose one of the following methods:


Best for: Personal laptops, interactive work, Jupyter notebooks

Login

What happens:

  1. Terminal prompts: Token:

  2. Paste your access token (the one you copied from Step 2)

    • Note: The token won't show as you paste (for security)

    • Just paste and press Enter

  3. Prompt asks: Add token as git credential? (Y/n)

    • Type Y and press Enter (recommended)

    • This saves your token so you don't need to login again

Expected output:

Alternative: Provide token directly in command

Or add the --add-to-git-credential flag to skip the prompt:

Verify Authentication

Check that you're logged in:

Output should show:

Your Python Code (No Token Needed!)

Once logged in, your Python code needs no token anywhere:

This is the cleanest approach for local development!

Logout (if needed)


Best for: Server deployments, Docker containers, CI/CD pipelines, when you need different tokens

Set Environment Variable

Linux/Mac:

Windows (Command Prompt):

Windows (PowerShell):

Make it permanent (Linux/Mac):

Your Python Code (No Token Argument Needed!)

The library automatically detects and uses HF_TOKEN from the environment!

Alternative: Set in Python Script

Using .env File (Best Practice)

Create a file named .env in your project directory:

IMPORTANT: Add .env to your .gitignore:

In your Python code:

Install python-dotenv:


Method C: Explicit Token Argument (For Special Cases)

Best for: Using multiple tokens, testing different accounts, programmatic switching

Pass Token Explicitly

Use Case: Multiple Accounts


How Token Detection Works

Automatic Detection - You Asked This!

Question: "If I set HF_TOKEN in the environment, do I also need to provide it as an argument?"

Answer: No! The library automatically detects tokens.

Examples Showing Automatic Detection

Example 1: Environment Variable Only

Example 2: After hf auth login

Example 3: Explicit Override

Testing Token Detection

Use this script to see which token will be used:

All Functions Use Automatic Detection

Token detection works for all Hugging Face functions:


Environment Variables Explained

Official Variable Names

Hugging Face recognizes multiple environment variable names for backward compatibility:

Variable NameStatusPriorityRecommendation
HF_TOKEN✅ Current standard1st (Highest)Use this
HUGGING_FACE_HUB_TOKEN⚠️ Legacy2ndWorks but outdated
HUGGINGFACE_TOKEN⚠️ Deprecated3rd (Lowest)Avoid

Why Multiple Names?

Hugging Face evolved their naming over time:

They kept backward compatibility so old code doesn't break.

Priority Example

If you set multiple variables, the first one found wins:

Other Useful Environment Variables

HF_HOME - Set cache location:

HF_HUB_OFFLINE - Use cached models only:

HF_HUB_DISABLE_TELEMETRY - Disable analytics:


Choosing the Right Method

Decision Matrix

ScenarioBest MethodWhy
Personal laptophf auth loginOne-time setup, never worry about tokens
Server deploymentEnvironment variableKeep tokens in deployment config, not code
Docker containerEnvironment variablePass via -e HF_TOKEN=...
CI/CD pipelineEnvironment variableUse secret management
Multiple accountsExplicit token=Programmatically switch tokens
Jupyter notebookhf auth loginClean notebooks without tokens
Team development.env file + .gitignoreSecure, easy to configure

Comparison of Methods

Method 1: hf auth login

Pros:

Cons:

Example:

Method 2: Environment Variable

Pros:

Cons:

Example:

Method 3: Explicit Token

Pros:

Cons:

Example:


Troubleshooting

Error: "huggingface-cli: command not found"

Solution 1: Install the package

Solution 2: Check PATH

Solution 3: Use Python module method

Error: "Access to model requires gating"

You haven't accepted the model's license yet:

  1. Go to the model page (e.g., https://huggingface.co/meta-llama/Llama-3.2-1B)

  2. Click "Request access"

  3. Fill in the form and wait for approval

  4. Look for green checkmark: ✓ "You have been granted access"

  5. Try downloading again

Error: "Invalid token" or "401 Unauthorized"

Your token is wrong, expired, or has insufficient permissions:

  1. Generate a new token at https://huggingface.co/settings/tokens

  2. Make sure you selected "Read" permissions

  3. Login again: hf auth login

  4. Paste the new token

Error: "Repository not found"

Either:

Token Not Persisting

If hf auth login doesn't save your token:

  1. Manually create token file:

    • Linux/Mac: ~/.cache/huggingface/token

    • Windows: C:\Users\YourName\.cache\huggingface\token

  2. Paste your token in the file (just the token, no extra text)

  3. Save and close

Models Downloading to Wrong Location

Check your cache directory:

Change cache location:


Security Best Practices

✅ DO:

  1. Use .env files with .gitignore

  1. Set in shell profile for permanent use

  1. Use hf auth login for personal machines

  1. Use environment variables in deployment

❌ DON'T:

  1. Never hardcode tokens in source code

  1. Never commit tokens to git

  1. Never share tokens

  1. Never use Write tokens unless needed

Token Permissions

When creating tokens, use minimum necessary permissions:

Regenerating Compromised Tokens

If your token is exposed:

  1. Go to https://huggingface.co/settings/tokens

  2. Delete the compromised token

  3. Generate a new token

  4. Update your configuration


Managing Downloaded Models

Where Models Are Cached

Default locations:

View Cache

Shows all downloaded models and their sizes.

Delete Cached Models

Delete specific model:

Or manually:

Change Cache Location

Make it permanent by adding to ~/.bashrc or ~/.zshrc.


Complete Example Workflow

Here's the complete process from installation to running code:

First run: Downloads ~2.5GB model to cache Subsequent runs: Uses cached model, no download needed


Quick Reference

Essential Commands

Environment Variables

Python Code Patterns

Common Model Access URLs

Summary Checklist


Key Takeaways

  1. You do NOT need token= argument if using hf auth login or HF_TOKEN environment variable

  2. HF_TOKEN is the current standard (not HUGGING_FACE_HUB_TOKEN)

  3. Token detection is automatic - library checks environment and saved tokens

  4. For local dev: Use hf auth login (cleanest approach)

  5. For servers: Use HF_TOKEN environment variable

  6. For security: Use .env files and add to .gitignore

  7. Must accept license on model page before downloading

  8. First download caches - subsequent uses are instant

That's everything you need to know about Hugging Face authentication! 🚀