1

I'm working on a machine M1 on which I need to install some python stuff using pip. This system is on an internal network. For reasons, it doesn't have a DNS server configured, nor can it open connections to arbitrary machines on the Internet. It can basically just connect to the machine I'm contorlling it from, M2, via SSH; and M2 is properly connected to the Internet.

My question: How can I install pip packages from machine M1?

Notes:

  • Both machines run a (slightly dated) Linux distribution. Can provide additional info upon request in the comments.
  • I have root on M1, but a non-root-privileges solution would be better if you can offer one.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Something like [this](https://packaging.python.org/en/latest/guides/hosting-your-own-index/)? – Axe319 May 16 '23 at 12:20
  • Admittedly, I'm not sure how this would work using only IP addresses, however, you could work around your lack of DNS by modifying the hosts file on M1. – Axe319 May 16 '23 at 12:24
  • SOCKS5 proxy: `ssh -D 1080 M2` + `pip install --proxy socks5a://127.0.0.1:1080` like in https://stackoverflow.com/q/22915705/7976758 – phd May 16 '23 at 12:42
  • @phd: "ValueError: Unable to determine SOCKS version from socks5a://127.0.0.1:1080" – einpoklum May 16 '23 at 13:04
  • Sorry, confused by `socks4://` vs `socks4a://`. For SOCKS5 it's `socks5://` vs [`socks5h://`](https://stackoverflow.com/a/61980997/7976758). Try `pip install --proxy socks5h://127.0.0.1:1080`. If still "*Unable to determine SOCKS version*" try `pip install --proxy socks5://127.0.0.1:1080` but then I'd expect problems with DNS. – phd May 16 '23 at 14:23
  • @phd; Make that into an answer perhaps? Also, will that respect non-transparent proxy settings on M2? e.g. ones I have in the `http_proxy` and `https_proxy` environment variables? – einpoklum May 16 '23 at 15:16

2 Answers2

2

Make sure you have PySocks installed (pip show pysocks). If not download PySocks to M2, copy it to the offline host M1 and install using pip install --no-index ./PySocks-1.7.1-py3-none-any.whl

On M1 run a SOCKS5 proxy using SSH:

ssh -D 1080 M2

(1080 is the proxy port, can be any free port from 1 to 65535; please remember on Unix/Linux running servers on ports 1-1023 requires root privileges).

Now use the SOCKS proxy with pip:

pip install --proxy socks5h://127.0.0.1:1080

Please note protocol socks5h; unlike socks5 it makes proxy clients to resolve DNS host names via the proxy.

phd
  • 82,685
  • 13
  • 120
  • 165
1

Have you tried installing them offline ?

You could download the pip packages from M2, then do some USB processing or anything else (SCP should work if SSH does) to transfer these to M1 and install them like explained here : How to install packages offline?.

Specifically:

  1. Create a requirements.txt file with the packages you need and their versions, e.g.

    mypackage
    anotherpackage=0.1.2
    
  2. On machine M2, run:

    pip download -r requirements.txt
    
  3. Copy the files you got, and the requirements file, somewhere on M1, e.g. into /some/path.

  4. On machine M1, run

    pip install --no-index --find-links /some/path -r /some/path/requirements.txt
    

and that should do it.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Will that let me get the entire heap of dependencies for my package(s) of interest in one go? – einpoklum May 16 '23 at 12:47
  • Yes ! The complete reference for `pip download` is available here : https://pip.pypa.io/en/stable/cli/pip_download/ , and it is said : "`pip download` does the same resolution and downloading as `pip install`, but instead of installing the dependencies, it collects the downloaded distributions into the directory provided (defaulting to the current directory)." – The Coding Penguin May 16 '23 at 12:56
  • So, do I `pip download` on M1 or on M2? – einpoklum May 16 '23 at 13:08
  • You `pip download` on the machine that has internet access, that gets you all the packages and dependencies downloaded in a folder that you can compress, send via SCP to M1, and then uncompress and install them there with `pip install` (just as explained in the first link I sent) – The Coding Penguin May 16 '23 at 13:13