Guide: Setting Up Pyenv and Poetry on Linux-Based Systems¶
Introduction¶
Managing Python environments and dependencies effectively is essential
for software development, particularly when working with multiple
projects. This guide explains how to install and configure pyenv
and
Poetry
on Linux-based systems (e.g., Linux or macOS).
!!! abstract "Why Use Pyenv with Poetry?" Poetry requires a Python
version manager like pyenv
to ensure compatibility and flexibility
across different Python versions. Using pyenv
, you can isolate
Python versions per project, making dependency management with
Poetry seamless and conflict-free.
Setting Up Pyenv on Linux-Based Systems¶
pyenv
simplifies managing multiple Python versions, enabling you to
switch between them easily. Follow the steps below to set up pyenv
and
configure it for use with Poetry.
Step 1: Install Prerequisites¶
Before installing pyenv
, install the essential system packages:
sudo apt install curl git-core gcc make zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libssl-dev
Why These Dependencies?
These packages provide the necessary tools and libraries for
building and managing Python versions with pyenv
.
Step 2: Clone the pyenv
Repository¶
Clone the official pyenv
repository into your home directory:
Keep pyenv
Updated
To ensure you have access to the latest features and Python
versions, periodically update your cloned repository: bash cd
$HOME/.pyenv && git pull
Step 3: Update Shell Configuration¶
To make pyenv
accessible, modify your shell configuration file:
- Open the
.bashrc
file (or.zshrc
if using Zsh) with a text editor:
- Append the following lines to enable
pyenv
:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
- Save changes and exit the editor (
Ctrl+X
,Y
,Enter
in nano).
Step 4: Apply Changes¶
Reload the shell configuration to apply the changes:
Shell Configuration Applied
Your shell now recognizes pyenv
. Use pyenv --version
to confirm
the installation.
Configuring Pyenv for Each Project¶
Step 1: Install Multiple Python Versions¶
Using pyenv
, install all required Python versions for your projects:
You can verify the installed versions with:
Managing Many Python Versions
If storage space becomes a concern, uninstall unused Python versions with:
Step 2: Set the Python Version Per Project¶
For each project, specify the required Python version:
- Navigate to the project directory:
- Use
pyenv
to set the local Python version:
This creates a .python-version
file in the project directory,
ensuring the specified version is activated when you work in the
directory.
Simplify Environment Setup
The .python-version
file allows collaborators using pyenv
to
automatically use the correct Python version when they clone your
repository.
Installing and Configuring Poetry¶
Poetry simplifies dependency management and project setup. Using pipx
,
you can install Poetry in an isolated environment to avoid conflicts.
Step 1: Check or Install pipx
¶
Ensure pipx
is installed and updated:
If not installed, install or upgrade it:
Step 2: Install Poetry¶
Install Poetry using pipx
:
Poetry Installed
Run poetry --version
to confirm that Poetry is installed successfully.
Step 3: Update Poetry (Optional)¶
Keep Poetry up-to-date with:
Specifying Python Compatibility in pyproject.toml
¶
While pyenv
controls the actual Python version used in your
environment, you can specify Python compatibility in the
pyproject.toml
file for consistency across collaborators. For example:
Python Compatibility Only
This configuration ensures collaborators use a compatible Python version but does not enforce the specific interpreter used in your virtual environment.
Setting Up a Virtual Environment with Poetry¶
- Create Virtual Environment: Create an isolated environment for your project using Poetry:
Why Use poetry env use
?
This command ensures that Poetry uses the exact Python
interpreter managed by pyenv
, avoiding conflicts with the
system Python.
- Verify Python Version: Confirm Poetry is set to the desired Python version with:
- Install Dependencies: At your project root (where
pyproject.toml
is), run:
- Activate Environment: Enter the environment with:
- Check Installation: Ensure all dependencies are installed:
Conclusion¶
By combining pyenv
and Poetry with project-specific configurations, you can efficiently manage Python versions and project dependencies.
Consistency Across Projects
Using pyenv local
ensures each project has its own Python version,
while poetry env use
guarantees that dependencies align with the
project's interpreter. This setup reduces conflicts, simplifies
environment management, and enhances productivity, especially when
working on multiple projects with different Python version
requirements.