Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Install Python Packages Hosted On Private GitHub

Unfortunately, GitHub does not provide the ability to create a token that is restricted to a specific respository. This is unlike GitLab where one could create a project access token.

Thus, for this tutorial, we will make use of "deploy keys" that are SSH keys we have added to GitHub for specific packages.

Prerequisites / Assumptions

  1. We are assuming you already have a private GitHub respository for a python package.
  2. Your repository needs to be set up correctly as a python package, with its setup.py at the root/top level of your GitHub repository. You cannot have it within a subfolder.
  3. You need the ability to create GitHub deploy keys on the repository.

Steps

Create a GitHub deploy key for your package.

Add the SSH key to your SSH agent, so that it will automatically be used when applicable.

ssh-add my-deploy-key

The following examples are lines you would add to your requirements.txt file, or passed to a pip command. Each of them require the #egg=my-package-name where my-package-name is the name of your package.

Specific Hash

git+ssh://git@github.com/github-username/repo-name.git@41b95ec#egg=my-package-name

Specific Branch

Here we use a branch called my-branch-name

git+ssh://git@github.com/github-username/repo-name.git@my-branch-name#egg=my-package-name

Tag

Here we use use a tagged version where the tag is called my-tag-name

git+ssh://git@github.com/github-username/repo-name.git@my-tag-name#egg=egg=my-package-name

Install Command

Once you have the relevant line you can install through a requirements file with:

pip install --requirement requirements.txt

... or you can install directly with:

pip3 install $LOCATION

References

Last updated: 29th July 2021
First published: 29th July 2021