Skip to content

Instantly share code, notes, and snippets.

@monkut
Created February 13, 2019 13:19
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save monkut/35c2ef098b871144b49f3f9979032cee to your computer and use it in GitHub Desktop.
Save monkut/35c2ef098b871144b49f3f9979032cee to your computer and use it in GitHub Desktop.
Add python to Windows Subsystem for Linux (WSL) [ubuntu]

Install Windows Subsystem for Linux (WSL) [ubuntu]

Follow this guide to install WSL:

https://docs.microsoft.com/en-us/windows/wsl/install-win10

Prepare WSL (ubuntu)

  1. Get your build tools and python required libraries installed:

    See: https://stackoverflow.com/questions/8097161/how-would-i-build-python-myself-from-source-code-on-ubuntu/31492697

    sudo apt update && sudo apt install -y build-essential git libexpat1-dev libssl-dev zlib1g-dev \
    libncurses5-dev libbz2-dev liblzma-dev \
    libsqlite3-dev libffi-dev tcl-dev linux-headers-generic libgdbm-dev \
    libreadline-dev tk tk-dev
    
  2. Install pyenv (which will install python):

    See: https://github.com/pyenv/pyenv-installer

    curl https://pyenv.run | bash
    
  3. After install, it tells you to add the following to your ~/.bashrc file, go do that:

    # Load pyenv automatically by adding
    # the following to ~/.bashrc:
    
    export PATH="/home/{USERNAME}/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    
    
  4. Install the latest 'common' versions of python through pyenv:

    # list available python versions
    # pyenv install --list
    pyenv install 3.6.8
    pyenv install 3.7.2
    
  5. Install/Upgrade base libraries into both versions of python:

    pyenv global 3.6.8
    pip install pip --upgrade
    pip install pipenv
    
    pyenv global 3.7.2
    pip install pip --upgrade
    pip install pipenv
    

-- Go to work!

@filipebezerra
Copy link

I had an issue trying to install python through pyenv install --list

zsh: command not found: pyenv

I changed my ~/.zshrc from:

export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

To

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Original thread: pyenv/pyenv-virtualenv#233 (comment)

@rokdd
Copy link

rokdd commented Oct 8, 2020

same issue like filipebezerra !

@AlinMH
Copy link

AlinMH commented Nov 22, 2020

@filipebezerra @RoKondo

If you're using zsh just put this line in ~/.zshrc:

plugins=(pyenv)

Reference:
https://www.karpton.com/articles/how-to-install-pyenv

@rokdd
Copy link

rokdd commented Nov 25, 2020

plugins=(pyenv)

Perfect thank you!

@Bishwas-py
Copy link

How about this. SUPER SIMPLE (wsl install pip)

@caniko
Copy link

caniko commented Jul 6, 2021

My which python points to my pyenv-win installation on the Windows-side. Any solutions?

@dipi-tech
Copy link

My which python points to my pyenv-win installation on the Windows-side. Any solutions?

Facing the same issue, Were you lucky with any resolutions?

@kingaj12
Copy link

I had a problem with line endings

/usr/bin/env: ‘bash\r’: No such file or directory

The advice in pyenv issue #1725 helped me.

@rzsgrt
Copy link

rzsgrt commented Oct 25, 2021

My which python points to my pyenv-win installation on the Windows-side. Any solutions?

I face this problem and remove all pyenv-win and pyenv wsl then resinstall pyenv wsl

@rzsgrt
Copy link

rzsgrt commented Oct 25, 2021

I already install pyenv on wsl and create new env using pyenv virtualenv 3.7.4 env-name.
After that i

  • pyenv activate env-name
  • pip install notebook
  • jupyter-notebook

access notebook. but it says i should apt-install jupyter notebook (basically no jupyter notebook). Even type python doesn't launch python. if i type python3 i got python version 3.8.10 (which is i didn't voluntarily install).
if i check pyenv which python it shows /home/User/.pyenv/versions/env-name/bin/python

Anyone have a guide how to use pyenv on wsl?

@jier
Copy link

jier commented Jan 28, 2022

@rzsgrt I am facing this issue with WSL2, have you found a work-around? Specifically with the python command. It gives me the system python even after activating it. pyenv is the latest version.

@jier
Copy link

jier commented Jan 28, 2022

echo -e "\n\n# pyenv environment variables\nexport PYENV_ROOT=\"\$HOME/.pyenv\"\nexport PATH=\"\$PYENV_ROOT/bin:\$PATH\"\n\n# pyenv initialization\nif command -v pyenv 1>/dev/null 2>&1; then\n eval \"\$(pyenv init --path)\"\nfi\n\n" >> ~/.bashrc
This command solved it for me. Apparently setting the path as @filipebezerra showed does not work properly on WSL2

@vitasiku
Copy link

vitasiku commented Feb 3, 2022

@jier thanks. Your suggestion worked for me.

@bashguru
Copy link

bashguru commented Feb 5, 2022

Here is what worked for me, thank you @jier. I just added the virtualenv initialization too.

pyenv environment variables

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

pyenv initialization

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
  eval "$(pyenv virtualenv-init -)"
fi

@alextosta
Copy link

That workaround worked for me too @bashguru, thank you!

@mohanrajreese
Copy link

im facing this issue on wsl ubuntu
/mnt/c/Users/lenovo/.pyenv/pyenv-win/bin/pyenv: 3: cygpath: not found /mnt/c/Users/lenovo/.pyenv/pyenv-win/bin/pyenv: 3: exec: cmd: not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment