1

I am using a private Github repo in my project as a library. This works fine in my local environment but when I push this to a remote host, it won't install because the remote host doesn't have access to the repo.

Adding the credentials to the url like this works fine: git+https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/user/project.git@{version}

However, my problem is that whenever I run pip freeze > requirements.txt to update my libraries, the repo's URL is updated and the credentials are stripped out.

Is there a way to set my git credentials as an environment variable in my remote environment so that pip can install the library using the "normal" github link?

Dave Cook
  • 617
  • 1
  • 5
  • 17
  • 1
    Does this answer your question? [Is it possible to use pip to install a package from a private GitHub repository?](https://stackoverflow.com/questions/4830856/is-it-possible-to-use-pip-to-install-a-package-from-a-private-github-repository) – sahasrara62 Mar 17 '23 at 21:19

1 Answers1

2

Yes, you can set your Git credentials as environment variables in your remote environment so that pip can install the library using the "normal" GitHub link. Here are the steps you can follow:

Set up a Git credential helper on your remote host. You can do this by running the following command in your terminal:

git config --global credential.helper cache

This will tell Git to cache your credentials for a period of time so that you don't have to enter them every time you interact with the remote repository.

Set the following environment variables on your remote host:

export GITHUB_USER=<your_github_username> export GITHUB_TOKEN=<your_github_personal_access_token>

You can generate a personal access token by following the instructions here: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

Be sure to keep your token secure and never share it with anyone.

Update your requirements.txt file to use the "normal" GitHub link instead of the URL with credentials. For example:

git+https://github.com/user/project.git@{version}

Make sure to remove any credentials from the URL.

Install the library using pip on your remote host:

pip install -r requirements.txt

Pip should now be able to install the library using the "normal" GitHub link and the credentials will be retrieved from the environment variables you set.

Note: If you are using a virtual environment on your remote host, you will need to set the environment variables within the virtual environment using the source command before running pip install.

Amir C
  • 23
  • 2