
How to Install Poetry to Manage Python Dependencies on Ubuntu 24
Poetry is a modern dependency management and packaging tool for Python that’s becoming the go-to choice for developers who want to streamline their project workflows. Unlike traditional tools like pip and virtualenv, Poetry handles dependency resolution, virtual environment management, and package building in one cohesive system. This guide will walk you through installing Poetry on Ubuntu 24, setting up your first project, and understanding why it’s worth switching from your current Python dependency management approach.
Why Poetry Over Traditional Python Package Management
Before diving into installation, it’s worth understanding what makes Poetry different from the standard pip + requirements.txt approach. Poetry uses a pyproject.toml file that serves as a single source of truth for your project’s metadata, dependencies, and build configuration.
Feature | Poetry | pip + requirements.txt | pipenv |
---|---|---|---|
Dependency Resolution | Automatic with conflict detection | Manual resolution required | Automatic but slower |
Lock Files | poetry.lock (deterministic) | pip freeze > requirements.txt | Pipfile.lock |
Virtual Environment | Automatically managed | Manual venv/virtualenv | Automatically managed |
Package Building | Built-in support | Requires setup.py/setuptools | Not supported |
Development Dependencies | Separate dev groups | Separate files needed | Built-in dev dependencies |
Installing Poetry on Ubuntu 24
There are several ways to install Poetry on Ubuntu 24, but the official installer is the most reliable method. This approach ensures you get the latest version and proper isolation from your system Python.
Method 1: Official Installer (Recommended)
The official Poetry installer downloads and sets up Poetry in an isolated environment, preventing conflicts with system packages:
curl -sSL https://install.python-poetry.org | python3 -
After installation, add Poetry to your PATH by adding this line to your ~/.bashrc or ~/.zshrc:
export PATH="$HOME/.local/bin:$PATH"
Reload your shell configuration:
source ~/.bashrc
Verify the installation:
poetry --version
Method 2: Using pip (Not Recommended for Production)
While you can install Poetry via pip, this method can lead to dependency conflicts:
pip3 install --user poetry
Method 3: Using pipx (Alternative Isolated Installation)
If you prefer pipx for managing Python applications:
sudo apt update
sudo apt install pipx
pipx install poetry
Initial Configuration and Setup
Once Poetry is installed, you’ll want to configure it for optimal performance and workflow integration. Here are the essential configuration options:
# Configure Poetry to create virtual environments in project directories
poetry config virtualenvs.in-project true
# Set the cache directory (optional)
poetry config cache-dir ~/.cache/pypoetry
# Configure repository credentials if needed
poetry config repositories.testpypi https://test.pypi.org/legacy/
# View current configuration
poetry config --list
The virtualenvs.in-project setting creates a .venv directory inside your project folder, making it easier to locate and manage virtual environments.
Creating Your First Poetry Project
Let’s create a sample project to demonstrate Poetry’s capabilities:
# Create a new project
poetry new fastapi-demo
cd fastapi-demo
# Or initialize Poetry in an existing project
# poetry init
This creates a project structure like this:
fastapi-demo/
├── fastapi_demo/
│ └── __init__.py
├── tests/
│ └── __init__.py
├── pyproject.toml
└── README.md
The pyproject.toml file contains your project configuration:
[tool.poetry]
name = "fastapi-demo"
version = "0.1.0"
description = ""
authors = ["Your Name "]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.8"
[tool.poetry.group.dev.dependencies]
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Managing Dependencies with Poetry
Adding dependencies is straightforward and automatically updates both pyproject.toml and poetry.lock:
# Add a production dependency
poetry add fastapi uvicorn
# Add development dependencies
poetry add --group dev pytest black flake8
# Add a dependency with version constraints
poetry add "django>=4.0,<5.0"
# Add from a git repository
poetry add git+https://github.com/user/repo.git
# Add a local dependency
poetry add ./my-local-package
After adding FastAPI and uvicorn, your pyproject.toml dependencies section will look like:
[tool.poetry.dependencies]
python = "^3.8"
fastapi = "^0.104.1"
uvicorn = "^0.24.0"
[tool.poetry.group.dev.dependencies]
pytest = "^7.4.3"
black = "^23.11.0"
flake8 = "^6.1.0"
Working with Virtual Environments
Poetry automatically creates and manages virtual environments. Here's how to work with them:
# Install dependencies and create virtual environment
poetry install
# Activate the virtual environment
poetry shell
# Run commands in the virtual environment without activating
poetry run python app.py
poetry run pytest
poetry run black .
# Show virtual environment information
poetry env info
# List available environments
poetry env list
# Remove the current environment
poetry env remove python
You can also specify which Python version to use:
# Use a specific Python version
poetry env use python3.11
poetry env use /usr/bin/python3.12
Real-World Example: FastAPI Application
Let's build a complete FastAPI application to demonstrate Poetry in action:
# Create and set up the project
poetry new fastapi-blog
cd fastapi-blog
poetry add fastapi uvicorn sqlalchemy pydantic-settings
poetry add --group dev pytest httpx black
Create a simple FastAPI application in fastapi_blog/main.py:
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
app = FastAPI(title="Blog API", version="1.0.0")
class Post(BaseModel):
id: int
title: str
content: str
posts_db = [
Post(id=1, title="Getting Started with Poetry", content="Poetry is awesome..."),
Post(id=2, title="FastAPI Best Practices", content="Here are some tips...")
]
@app.get("/")
async def root():
return {"message": "Welcome to the Blog API"}
@app.get("/posts", response_model=List[Post])
async def get_posts():
return posts_db
@app.get("/posts/{post_id}", response_model=Post)
async def get_post(post_id: int):
for post in posts_db:
if post.id == post_id:
return post
return {"error": "Post not found"}
Add a startup script in your pyproject.toml:
[tool.poetry.scripts]
start = "uvicorn fastapi_blog.main:app --reload"
Now you can run your application:
poetry run start
Common Issues and Troubleshooting
Here are the most frequent issues you'll encounter and their solutions:
Poetry Not Found After Installation
If poetry command isn't recognized after installation:
# Check if Poetry binary exists
ls ~/.local/bin/poetry
# Add to PATH permanently
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Dependency Resolution Conflicts
When Poetry can't resolve dependencies:
# Clear cache and retry
poetry cache clear pypi --all
poetry lock --no-update
poetry install
# Force update all dependencies
poetry update
Virtual Environment Issues
If the virtual environment becomes corrupted:
# Remove and recreate environment
poetry env remove python
poetry install
# Or specify exact Python version
poetry env use python3.11
poetry install
SSL Certificate Errors
For corporate networks with SSL issues:
# Configure Poetry to use corporate certificates
poetry config certificates.test-pypi.cert /path/to/ca-bundle.crt
# Or disable SSL verification (not recommended for production)
poetry config repositories.pypi.cert false
Best Practices and Performance Tips
Follow these practices to get the most out of Poetry:
- Always commit poetry.lock: This ensures reproducible builds across environments
- Use dependency groups: Separate development, testing, and documentation dependencies
- Pin critical dependencies: Use exact versions for production-critical packages
- Regular updates: Run poetry update periodically to stay current with security patches
- Configure virtual environments in project: Makes IDE integration easier
Performance optimization settings:
# Speed up dependency resolution
poetry config solver.lazy-wheel true
# Use parallel installation
poetry config installer.parallel true
# Configure cache location on faster storage
poetry config cache-dir /path/to/fast/storage
Integration with Development Tools
Poetry integrates well with popular development tools. Here's how to set up common integrations:
VS Code Integration
Configure VS Code to use Poetry's virtual environment by adding to .vscode/settings.json:
{
"python.defaultInterpreterPath": "./.venv/bin/python",
"python.terminal.activateEnvironment": true
}
Docker Integration
Optimize Docker builds with Poetry using multi-stage builds:
FROM python:3.11-slim as builder
ENV POETRY_NO_INTERACTION=1 \
POETRY_VENV_IN_PROJECT=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache
RUN pip install poetry
COPY pyproject.toml poetry.lock ./
RUN poetry install --only=main && rm -rf $POETRY_CACHE_DIR
FROM python:3.11-slim as runtime
ENV VIRTUAL_ENV=/.venv \
PATH="/.venv/bin:$PATH"
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY . .
CMD ["python", "-m", "fastapi_blog.main"]
Migrating from Other Tools
If you're migrating from existing tools, Poetry provides utilities to ease the transition:
From requirements.txt
# Initialize Poetry in existing project
poetry init
# Poetry will detect requirements.txt and offer to import dependencies
# Or manually add dependencies
cat requirements.txt | xargs -n 1 poetry add
From Pipenv
# Poetry can read Pipfile and Pipfile.lock
poetry init
# Convert Pipfile dependencies manually or use poetry add for each dependency
Poetry represents a significant improvement over traditional Python dependency management tools, offering better dependency resolution, automatic virtual environment management, and integrated package building. The initial setup investment pays off quickly through improved development workflow and reduced dependency-related issues. For more detailed information, check the official Poetry documentation and the PEP 518 specification that defines the pyproject.toml standard.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.