4.7 KiB
This guide walks you through the steps to set up Python and manage virtual environments using pyenv and pyenv-virtualenv on macOS and Linux.
Prerequisites
Before you start, ensure you have the following installed:
macOS
-
Homebrew:
Check if Homebrew is already installed:
brewIf this throws an error:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Follow the instructions provided by the Homebrew installer to add
brewto your path. -
Install required dependencies:
brew install openssl readline sqlite3 xz zlib tcl-tk git libomp
Linux (Debian/Ubuntu-based systems)
- Install build dependencies:
sudo apt update && sudo apt 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 git
Step 1: Install pyenv
-
Run the following command to install
pyenv:curl https://pyenv.run | bashAlternatively, use
wget:wget -qO- https://pyenv.run | bash -
Find out which configuration file your shell is loading. Most common:
~/.bashrc,~/.zshrcor~/.bash_profile.e.g.
echo $SHELL -
Run the command corresponding to your shell configuration file. (
~/.bashrc,~/.zshrc, etc.):echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init --path)"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrcecho 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init --path)"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrc -
Reload your shell:
exec $SHELL -
Verify the installation:
pyenv --version
Step 2: Install pyenv-virtualenv
-
Install the
pyenv-virtualenvplugin:git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv -
Add the following line to your shell configuration file (
~/.bashrc,~/.zshrc, etc.):eval "$(pyenv virtualenv-init -)" -
Reload your shell:
exec $SHELL -
Verify the installation:
pyenv virtualenv --version
Step 3: Install a Python Version and setup a virtual environment
-
Install Python Version 3.11
pyenv install 3.11 -
Create a virtual environment with a name of your choice:
pyenv virtualenv 3.11 <env-name> -
Set the virtual environment as the global Python Environment:
pyenv global <env-name>
You are now ready to install JALE. Installing JALE
Useful pyenv commands
-
List all available Python versions:
pyenv install --list -
Install a specific Python version:
pyenv install <version>Example:
pyenv install 3.11.5 -
Set the global Python version:
pyenv global <version>Example:
pyenv global 3.11.5 -
Verify the installed Python version:
python --version -
Check all installed Python versions:
pyenv versions -
Create a virtual environment:
pyenv virtualenv <python-version> <env-name>Example:
pyenv virtualenv 3.11.5 myenv -
Activate the virtual environment:
pyenv activate <env-name>Example:
pyenv activate myenv -
Deactivate the virtual environment:
pyenv deactivate -
List all virtual environments:
pyenv virtualenvs -
Set a local Python version or virtual environment for a specific project directory:
pyenv local <version-or-env>Example:
pyenv local myenv -
Remove a virtual environment:
pyenv uninstall <env-name>
Troubleshooting
macOS Specific Issues
- If you use macOS with M1/M2 chips, consider setting additional flags when installing Python:
CFLAGS="-I$(brew --prefix openssl)/include" \ LDFLAGS="-L$(brew --prefix openssl)/lib" \ pyenv install <version>
Linux Specific Issues
- If you encounter build issues while installing Python, ensure all required dependencies (listed in prerequisites) are installed.