Explain Codes LogoExplain Codes Logo

Is it possible to use pip to install a package from a private GitHub repository?

python
pip-install
github-repositories
personal-access-tokens
Anton ShumikhinbyAnton Shumikhin·Nov 25, 2024
TLDR

Yes, it is completely possible! Make your pip access a private GitHub repository by creating a Personal Access Token (PAT) on GitHub with repo scope. Use this shortened code:

pip install git+https://<token>@github.com/<owner>/<repo>.git

Just replace <token>, <owner>, and <repo> with your PAT, repository owner's name, and repository name.

Creating and Using Personal Access Tokens

Tokens are the backbone for authorization! A Personal Access Token (PAT) acts like an ID card for pip. Follow these steps to create one:

  1. Check out your GitHub settings.
  2. Find the Developer settings section, not your horoscope.
  3. Click Generate new token under Personal access tokens, just like generating positive vibes.
  4. Ensure that the repo scope is checked. This is not a scope for hunting, just package hunting.

This formulates your token, well, without any magic potions involved.

Delving deeper: Custom setups and corner cases

Managing Private Dependencies

If you have other private repos that your project is dependent on, include them in your requirements.txt. Unleash this magical bash incantation:

git+https://<token>@github.com/<owner>/<repo>@<branch>#egg=<package_name>

Cloning for CI/CD Pipelines

For servers without SSH access, such as CI/CD pipelines, use PAT:

pip install git+https://<token>@github.com/<owner>/<repo>.git

Deploy Key: One Key to Rule an Entire Repository!

Deploy keys are similar to extremely exclusive VIP passes for a single repo. They provide read-only or write authorization:

  1. Add the deploy key in the Settings of the repo under Deploy keys.
  2. Register the key with ssh-agent
    ssh-add ~/.ssh/your-deploy-key
    
  3. Install with the SSH URL:
    pip install git+ssh://[email protected]/<owner>/<repo>.git
    

Let's Get into the SSH-key Groove

Ready to dial into the SSH groove? Set up the SSH keys, and add them to your GitHub account. Then use git+ssh:

pip install git+ssh://[email protected]/<owner>/<repo>.git#egg=<package_name>

Also, usage of OAuth tokens or PAT in the URL or environment is prevalent.

Using Environment Variables: Because Safety First!

Use environment variables to prevent hardcoding tokens, especially if your codebase is public or you're working on CI/CD:

# One does not simply hard-code a GitHub token! export GITHUB_TOKEN=<your_token> pip install git+https://$GITHUB_TOKEN:[email protected]/<owner>/<repo>.git

Troubleshooting: When Lifelines are Necessary

Permission Denied: More Like Access Denied!

Getting "Permission denied"? Check:

  • The validity of your SSH keys.
  • If your PAT is valid.
  • The access rights and ownership of your repository.

Correct Syntax: It's all in the Details!

Ensure that the pip commands have the correct syntax:

  • Convert ":" to "/" in the SSH Git URL when using pip commands.
  • Add @<tag_name> after the repo URL in the pip command to select the correct branch, tag, or commit.

The "-e" Switch: Edit and Go!

Use editable install ("-e") when you require frequent changes during development. It links directly to the source directory:

pip install -e git+https://<token>@github.com/<owner>/<repo>.git#egg=<package_name>

All changes will reflect without needing to reinstall.

Mastering the Seamless Installation

Using Virtual Environments

Try working within a virtual environment to avoid global conflicts:

python -m venv myenv source myenv/bin/activate

Linking Dependencies

If your dependencies have VCS URLs, use --process-dependency-links:

pip install --process-dependency-links -r requirements.txt

This flag helps pip to resolve dependencies from sources like git, mercurial, svn, and bzr.