Skip to content

Instantly share code, notes, and snippets.

@mohanrajreese
Last active May 12, 2024 04:33
Show Gist options
  • Save mohanrajreese/a0405698427aa8e2f106b815bde2cebe to your computer and use it in GitHub Desktop.
Save mohanrajreese/a0405698427aa8e2f106b815bde2cebe to your computer and use it in GitHub Desktop.

Linux based Python enviroment for Windows systems using WSL

The following document will guide you through to install WSL, Ubuntu distro, python,pyenv and poetry

Install WSL and Linux Distro for Windows Machine

If you are using Linux based system skip these steps for installing wsl & Ubuntu

  1. First you need to Make sure your Windows version is at least Windows 10, version 2004. You can check your Windows version by going to Settings > System > About.

  2. Open PowerShell as an administrator and run the following command to enable the WSL feature:

  Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  1. Restart your computer when prompted.

  2. You can now install everything you need to run WSL with a single command. Open PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting Run as administrator

wsl --install
  1. Then restart your computer.

  2. To install Ubuntu-20.04 use the following command in powershell or command prompt then restart

wsl --install Ubuntu-20.04
  1. If you want to install additional distributions from inside a Linux/Bash command line (rather than from PowerShell or Command Prompt), you must use .exe in the command:
wsl.exe --install -d <Distribution Name>
  1. You can list your installed Linux distributions and check the version of WSL each is set to by entering the command in PowerShell or Windows Command Prompt:
wsl -l -v
  1. To set the default version WSL 2 when a new Linux distribution is installed, use the command:
wsl --set-default-version 2
  1. To set the default Linux distribution used with the wsl command, enter:
wsl -s <DistributionName>

or

wsl --setdefault <DistributionName>

replacing <DistributionName> with the name of the Linux distribution you would like to use.

Install python 3.10 in WSL Ubuntu

To install Python 3 in WSL Ubuntu, follow these steps:

  1. Open the terminal in WSL Ubuntu.

  2. To update the package list run the following command

sudo apt-get update
  1. To install Python 3 run the command
sudo apt-get install python3 
  1. Verify the installation by running the command and it should display the version of Python 3 that you have installed.
python3 --version
  1. If you want to install pip for Python 3, run the command
sudo apt-get install python3-pip
  1. Verify the installation by running the command and it should display the version of pip that you have installed.
pip3 --version
  1. You now have Python 3 and pip installed in WSL Ubuntu. You can now use Python 3 to write and run your programs.

Install pyenv

  1. Open the terminal in WSL Ubuntu.

  2. To update the package list Run the command

sudo apt-get update
  1. To install the necessary dependencies for pyenv run the command
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
  1. to install pyenv run the command
curl https://pyenv.run | bash
  1. Add the following lines to your .bashrc file:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. To apply the changes run the command
source ~/.bashrc
  1. To install Python 3.10.0 run the command
pyenv install 3.10.0
  1. To set Python 3.10.0 as the global version run the command
pyenv global 3.10.0 
  1. To set Python 3.10.0 as the local version run the command
pyenv local 3.10.0 
  1. Verify the installation by running the following command it should display Python 3.10.0
python --version

Install poetry

  1. Open the Windows Terminal or the command prompt and type wsl to enter the Windows Subsystem for Linux.

  2. Once in the Linux environment, update the package list by typing sudo apt-get update and enter your password if prompted.

  3. Install poetry by running the following command

sudo apt-get install poetry
  1. Wait for the installation to complete and then type poetry to confirm that it is installed correctly.

  2. If you see the poetry usage instructions, it means the installation is successful. You can now use poetry to create and manage your Python projects.

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