Home How to install a pip Package from a git Repository (https and ssh)
Post
Cancel

How to install a pip Package from a git Repository (https and ssh)

I recently came across a very helpful Python package on Github, that I wanted to use for the project I’m currently working on. The package was also available on the Python Package Index PyPI, allowing me to integrate it into my project using pip. However, I needed to change the package’s code. One way to use the package with the changed code is to integrate it as a git submodule into my current project and not use pip. However, I wanted to keep the convenience of pip such that my co-workers don’t have to mess with git submodules and can just run:

1
python -m pip install -r requirements.txt

And pip allows you to install packages from remote git repositories. If you want to install the package foobar through pip you would normally execute the following command:

1
python -m pip install foobar

This command installs the most recent version available for your Python version. However, you can also install the foobar package from its GitHub repository and even specify the commit you would like to install by running the following command:

1
python -m pip install git+https://github.com/foobar/foobar.git@91f9ad741b03d8587a5b52612c805c658e4f1d84

This command uses https as a protocol to connect to GitHub. But you can also use the ssh protocol, which might be necessary if you got a private GitLab instance, for example. The next command shows you how to install a Python package through pip using ssh:

1
python -m pip install git+ssh://[email protected]/foobar/foobar.git@91f9ad741b03d8587a5b52612c805c658e4f1d84

To successfully run this command, you need to set up an ssh-key before authenticating yourself. Finally, to add such a git repository to your requirements.txt you insert the ssh or https link into it like this:

1
2
3
matplotlib==3.4.3
numpy>=1.21.2
git+ssh://[email protected]/foobar/foobar.git@91f9ad741b03d8587a5b52612c805c658e4f1d84

And this is how you can use pip to install packages directly from git repositories.

If you want to learn how to set up and run multiple Python versions on your system, check out this article: How to Install Multiple Python Versions on your Computer and use them with VSCode together with the videos on Youtube.

This post is licensed under CC BY 4.0 by the author.