WSL: I can't use pip on WSL Ubuntu

Please fill out the below information:

  • Your Windows build number: 10.0.17763.475 / WSL Ubuntu 18.04.2

  • What you’re doing and what’s happening: Type sudo pip3 install [package]

  • What’s wrong / what should be happening instead: Following error message comes out when trying to install packages via pip.

Collecting [package]
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/[package]/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/[package]/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/[package]/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/[package]/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/[package]/
  Could not find a version that satisfies the requirement flask-restful (from versions: )
No matching distribution found for [package]

None of these solutions work.

  • Re-install pip
  • Re-install WSL
  • sudo pip3 install [package]
  • pip3 install [package] with root account
  • sudo -H pip3 install [package]
  • pip install --user [package]

These commands works normally.

  • python -V (3.6)
  • pip3 --version (9.0.1)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 15
  • Comments: 60 (1 by maintainers)

Commits related to this issue

Most upvoted comments

This worked for me. sudo apt-get update sudo apt-get upgrade sudo apt install python3-pip

For me it turns out that it was the keychain/dbus that prevented pip from running. It should be possible to disable it using

PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring pip install <PACKAGE>

but that didn’t work for me either. I ended up uninstalling python3-keyring, which solved the issue:

sudo apt remove python3-keyring

Edit: If it worked for you, just leave a reaction to the comment to avoid filling the issue with just quotes of my comment 😅

This could work

sudo apt -y purge python-pip
sudo python -m pip uninstall pip
sudo apt -y install python-pip
pip install --upgrade pip
echo "export PATH=\"${HOME}/.local/bin:$PATH\"" >>"${HOME}"/.bashrc

Solution

# Step 1: sudo apt install python3-pip

# Step 2: pip3 --version

# Step 3: pip3 install <packagename> example: pip3 install notebook

If I manually set nameservers, all works properly.

My /etc/wsl.conf

[network]
generateResolvConf = false

And the /etc/resolv.conf

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false

nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 172.23.0.1

Where 1.1.1.1 is cloudflare, 8.8.8.8 and 8.8.4.4 are google, while the 172.23.0.1 is the WSL default one

Ok, so this issue was silent and closed for more than a year and now suddenly it garners a lot of attention again.

Simply updating and upgrading Ubuntu seems to be all that’s required this time.

In my case the error message was slightly different, the connection was broken by NewConnectionError as opposed to ProtocolError. Maybe this helps people find this issue and workarounds more quickly.

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 
'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f81caa39bb0>: Failed to establish a new connection: [Errno -2] Name or service not known')': /simple/qiskit/

I was getting this with a new conda environment created with python=3 (which gave 3.9). Using conda create -n myEnvironment python=3.8 resolved this.

Hello everybody,

To use pip3 on WSL2 you need to force using the “.exe” binary on your command line

pip            pip.exe     pip3         pip3.8         pip3.8.exe     pip3.exe
^ not work     ^ work      ^ not work   ^ not work     ^ work         ^ work

You need to install Python under windows before

me@computer:~$ whereis pip3.8
pip3: /usr/bin/pip3 /usr/local/bin/pip3 /usr/local/bin/pip3.8 /opt/az/bin/pip3 /opt/az/bin/pip3.6 /mnt/c/Python38/Scripts/pip3.exe /usr/share/man/man1/pip3.1.gz
me@computer:~$ whereis pip3.8.exe
pip3.8: /usr/local/bin/pip3.8 /mnt/c/Python38/Scripts/pip3.8.exe

@MatheusRV pip install --upgrade pip gives me same error message 😢

Within conda environment, I get this error when I use pip for the first time. To fix it

  1. Install a python package using conda e.g. conda install matplotlib
  2. Then use pip install for other packages.

Env: Win11 WSL2 / ubuntu 22.04 Firewall and proxy: off

I was having trouble with this also.

I would run this command:

$ python3 -m pip --verbose install pyuic5-tool

and nothing would happen.

So after some reading of similar situations, I TEMPORARILY disabled the firewall on Public Networks in Windows Defender.

image

Once the firewall was disabled I reran the pip command and everything worked as it should.

I am unaware of a way to manually allow WSL2 python3 through the firewall.

I looked into changing the /etc/wsl.conf file but in my installation of WSL2 there is no such file.

Hope this helps.

Try using sudo apt-get update && upgrade before installation. If you’re still having issue, check this forum post to install pip in wsl.

This worked for me. sudo apt-get update sudo apt-get upgrade sudo apt install python3-pip

Thanks, this worked for me

The cause was my Kaspersky app. If I turn off it, everything works well. I’ve raised this issue to Kaspersky.

This worked for me. sudo apt-get update sudo apt-get upgrade sudo apt install python3-pip

This didn’t work. Had to:

  • open powershell on windows
  • run wsl --update

For me it turns out that it was the keychain/dbus that prevented pip from running. It should be possible to disable it using

PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring pip install <PACKAGE>

but that didn’t work for me either. I ended up uninstalling python3-keyring, which solved the issue:

sudo apt remove python3-keyring

Edit: If it worked for you, just leave a reaction to the comment to avoid filling the issue with just quotes of my comment 😅

Save me from a big headache, big thanks!

My pip has become extremely slow at installing packages, and none of the solutions in this thread worked for me. Here is the output for installing flask:

Looking in indexes: https://pypi.org/simple, https://piwheels.org/simple
Looking in links: http://pypi.pinaxproject.com, http://pypi2.pinaxproject.com
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=60.0)")': /simple/flask/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=60.0)")': /simple/flask/
Collecting flask
  Using cached https://piwheels.org/simple/flask/Flask-2.0.2-py3-none-any.whl (95 kB)
Requirement already satisfied: click>=7.1.2 in ./venv/lib/python3.8/site-packages (from flask) (8.0.3)
Requirement already satisfied: itsdangerous>=2.0 in ./venv/lib/python3.8/site-packages (from flask) (2.0.1)
Requirement already satisfied: Werkzeug>=2.0 in ./venv/lib/python3.8/site-packages (from flask) (2.0.2)
Requirement already satisfied: Jinja2>=3.0 in ./venv/lib/python3.8/site-packages (from flask) (3.0.3)
Requirement already satisfied: MarkupSafe>=2.0 in ./venv/lib/python3.8/site-packages (from Jinja2>=3.0->flask) (2.0.1)
Installing collected packages: flask
Successfully installed flask-2.0.2

For me it turns out that it was the keychain/dbus that prevented pip from running. It should be possible to disable it using

PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring pip install <PACKAGE>

but that didn’t work for me either. I ended up uninstalling python3-keyring, which solved the issue:

sudo apt remove python3-keyring

This works for me as well.

This worked for me. sudo apt-get update sudo apt-get upgrade sudo apt install python3-pip

me too! Thank’s guy!

How to achieve this in wsl2? The filesystem seems to work differently… can’t enter the ext4 volume like in wsl1

image

I can pip install when I temporarily turn off the public firewall, but I’d rather not do that. Does anyone know why pip depends on an incoming connection anyway?

Otherwise pip install simply hangs with no output, regardless of verbosity level…

The DNS solution does not work on Windows 10 with WSL2 and Ubuntu 20.04 and at this point you have to do what’s described in this stack overflow post just because the file gets overwritten but it doesn’t fix pip. What works is disabling the firewall which is only a temporary solution.

I have some issues with this too. My issues is pip install take too long to process (no output for 5 minute) after some searching I find that if i disable my Public Network Firewall it seems fine again.

Does anyone know how to allow the pip (wsl) in firewall. so i can turn on my public network firewall again ?

i already try what @Biswa96. but my */LocalState folder is empty. and already try what @iongion solution. but it still does not work.

Hi, for me, the thing that fixed it was disconnecting my VPN connection and just connecting to wifi directly.

For me it turns out that it was the keychain/dbus that prevented pip from running. It should be possible to disable it using

PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring pip install <PACKAGE>

but that didn’t work for me either. I ended up uninstalling python3-keyring, which solved the issue:

sudo apt remove python3-keyring

Edit: If it worked for you, just leave a reaction to the comment to avoid filling the issue with just quotes of my comment 😅

Works even if i use conda instead of system python3. It seems the system python3-keyring did block something. By the way, before trying this, i have 2 conda environments with exactly the same version of python, pip, certifi packages, and one works originally while the other showed the pip networking issue. Now both work! 😃

I now have the same issue, it has worked for a long time but now stopped working with latest updates.

It seems in my Ubuntu WSL, the pip belonging to my Miniconda’s base installation works fine, but when creating a new one conda create -n myenv python=3 and doing conda activate myenv and pip install somepippackage it shows the error.

On win10, Education, ver. 20H2, build 19042.1165, I had network unreachable on pip. After checking and founding ping also returned network unreachable, I discovered that simply stopping the wireless connection and staying on Ethernet was the solution.

Hello, I am having the same issue. Pip hangs on every command except version.

I tried all the solutions here: I reinstalled everything, I turned off the firewall (I have the windows one turned off by default, I use Bitdefender and I also tried turning it off), I uninstalled keyring, I disabled IPv6 per a stack overflow post. Also a wild solution I found was to clear the DISPLAY variable, don’t know how it’s related but I tried with a functioning Xserver and a cleared variable.

My Windows is W10 Home Version 10.0.19042 Build 19042, WSL2 with Ubuntu 20.04 LTS from the Windows Store.

My pip install hangs on this: Starting new HTTPS connection (1): pypi.org:443 If I Ctrl C the operation, the traceback shows that it was hanging in this function:

  File "/usr/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()```

Dis you try what @iongion did?

I has no problème with the network 😉. juste nothing when use “pip3” without “.exe” extension

Hello everybody,

To use pip3 on WSL2 you need to force using the “.exe” binary on your command line

pip            pip.exe     pip3         pip3.8         pip3.8.exe     pip3.exe
^ not work     ^ work      ^ not work   ^ not work     ^ work         ^ work

Dis you try what @iongion did?

If I manually set nameservers, all works properly.

My /etc/wsl.conf

[network]
generateResolvConf = false

And the /etc/resolv.conf

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false

nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 172.23.0.1

Where 1.1.1.1 is cloudflare, 8.8.8.8 and 8.8.4.4 are google, while the 172.23.0.1 is the WSL default one

This is indeed speeding up the PIP related issues