pip.conf, index-url and extra-index-url: install from a private index safely

Serving developers since 2016 Founder-run, no venture capital MFA, SSO and audit log on every plan

Every feature on every plan, with unlimited users. 14 days free. No credit card required.

Start free trial

Starting at $99/month billed annually.

index-url replaces PyPI as pip’s index. extra-index-url adds another index beside it. pip treats every index it knows as equal, which is exactly where dependency confusion starts.

Where pip.conf lives

pip reads up to four config locations in order, and each one overrides the ones before it:

Level Linux macOS Windows
Global /etc/pip.conf /Library/Application Support/pip/pip.conf C:\ProgramData\pip\pip.ini
User $HOME/.config/pip/pip.conf (and the older $HOME/.pip/pip.conf) $HOME/Library/Application Support/pip/pip.conf if that pip folder exists, otherwise $HOME/.config/pip/pip.conf %APPDATA%\pip\pip.ini
Site $VIRTUAL_ENV/pip.conf $VIRTUAL_ENV/pip.conf %VIRTUAL_ENV%\pip.ini
File you name PIP_CONFIG_FILE PIP_CONFIG_FILE PIP_CONFIG_FILE

Environment variables override every file, and command line options override environment variables.

[global]
index-url = https://pypi.example.com/simple/

You can also let pip write the file for you and show which files it loaded:

pip config set global.index-url https://pypi.example.com/simple/
pip config list
pip config debug

In CI, skip the config file and use an environment variable. Every pip option maps to PIP_ plus its long name in capitals:

export PIP_INDEX_URL="https://pypi.example.com/simple/"

index-url versus extra-index-url

  • index-url is the single index pip uses. It defaults to https://pypi.org/simple. Point it at your own index, and pip stops talking to PyPI.
  • extra-index-url adds additional indexes on top of index-url.

The catch is how pip searches across multiple indexes. In pip’s own words: “There is no priority in the locations that are searched. Rather they are all checked, and the ‘best’ match for the requirements…” is selected. Here, “best” means the highest version.

If your private package is acme-core 1.4.0 on your index and someone publishes acme-core 99.0.0 on PyPI, pip installs theirs. The pip documentation puts it plainly:

Using the --extra-index-url option to search for packages which are not in the main repository (for example, private packages) is unsafe. This is a class of security issue known as dependency confusion: an attacker can publish a package with the same name to a public index, which may then be chosen instead of your private package.

An exact == pin doesn’t save you when both indexes have that version. For the full breakdown, including what JFrog and Sonatype sell for it, see One pip index URL for your packages and PyPI’s.

Keep credentials out of the URL

pip accepts https://user:token@host/simple/, but that leaves your token sitting in pip.conf, your requirements file, and your logs. Put it in ~/.netrc instead, readable only by you (chmod 600 ~/.netrc), and pip looks it up by the index’s host name:

machine pypi.example.com
login you@example.com
password YOUR_TOKEN

pip can also read credentials from keyring with --keyring-provider import or subprocess.

One flag that won’t fix a 401 is --trusted-host. It tells pip to trust a host “even though it does not have valid or any HTTPS.” That’s about certificates, not your login.

Two safe shapes

One index that serves both. Point index-url at a single index that holds your private packages and proxies PyPI, and never set extra-index-url. pip then has only one place to look for every name.

One index at a time. If your private index can’t serve PyPI’s packages, split the install into three steps so no command ever sees two indexes:

pip install -r requirements.txt
pip install -r requirements-private.txt
pip install --no-deps -e .
requirements-private.txt
--index-url https://pypi.example.com/simple/
acme-core==1.4.0
acme-pipeline==2.0.0

Public packages install from PyPI first. Private packages install from your index alone next. Last, your own project installs without its dependencies. Keep private names out of requirements.txt, or step one goes looking for them on PyPI.

In a Dockerfile

Pass the netrc as a build secret, never as a build argument. Build arguments stay in the image.

# syntax=docker/dockerfile:1
FROM python:3.13-slim
WORKDIR /app
COPY requirements.txt .
RUN --mount=type=secret,id=netrc,target=/root/.netrc \
pip install --no-cache-dir --index-url https://pypi.example.com/simple/ -r requirements.txt
COPY . .
docker build --secret id=netrc,src=$HOME/pypi.netrc .

Keep pypi.netrc outside your build context, holding only the one entry the build needs.

This is where CloudRepo helps

A PyPI group in CloudRepo (early access) puts your hosted Python repository and a cached proxy of PyPI behind one index-url, so extra-index-url never appears in your config. If a package name exists in your hosted repository, the group always serves it from there, never from PyPI.

[global]
index-url = https://your-org.mycloudrepo.io/repositories/python-group/simple/

More: Install Python packages from CloudRepo, PyPI groups, One pip index URL for your packages and PyPI’s.

Every feature on every plan, with unlimited users

14 days free. No credit card required.

Starting at $99/month billed annually.

Chris Shellenbarger, founder of CloudRepo

Founder-run since 2016, with no venture capital. A real person answers within one business day. Usually it's me.

Chris, founder of CloudRepo