Log inSign up

Python dependency management with Poetry

Install Poetry with pipx install poetry, then use poetry add to record dependencies in pyproject.toml and poetry.lock. On the server, poetry sync --only main builds an environment that matches the lock file exactly, and your systemd unit calls the programs inside the project .venv folder.

Topic: Python · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does Poetry do that pip alone does not?

Poetry keeps the list of what your project needs separate from the exact versions that were resolved, and it creates the virtual environment for you. You declare ranges such as flask (>=3.0,<4.0) in pyproject.toml; Poetry resolves the full tree, writes every package and hash to poetry.lock, and installs from that lock everywhere. Commit both files, and a laptop, a CI runner and the production server end up with identical libraries.

Poetry 2 also reads the standard [project] table from PEP 621, so the metadata is not locked into one tool. The groups feature splits runtime packages from development ones like pytest or linters, which keeps the production install lean.

How do you install Poetry on the server?

Install it as an isolated tool with pipx, so Poetry never shares an environment with the projects it manages. You need Python 3.9 or later, which every current image provides; on Rocky Linux and AlmaLinux 9 a newer interpreter from AppStream is still a good idea for the projects themselves.

# Ubuntu and Debian
sudo apt update
sudo apt install -y pipx python3-venv
# Rocky and Alma
sudo dnf install -y epel-release
sudo dnf install -y pipx python3.12

pipx ensurepath
exec $SHELL -l
pipx install poetry
poetry --version

The project also publishes its own installer, curl -sSL https://install.python-poetry.org | python3 -, which places Poetry under ~/.local/share/pypoetry. Pick one method and stay with it, because each has its own upgrade command: pipx upgrade poetry for pipx, poetry self update for the official installer. For pipx, pipx itself is covered in installing Python 3, pip and venv.

How should Poetry be configured for servers?

Tell Poetry to create the environment inside the project folder. A .venv next to the code has a predictable path, which makes systemd units and cleanup simple.

poetry config virtualenvs.in-project true
poetry config --list | grep virtualenvs

Without this setting Poetry keeps environments in ~/.cache/pypoetry/virtualenvs with a hashed folder name. That is fine on a workstation but awkward when a service file has to point at the interpreter. The setting is stored per user in ~/.config/pypoetry/config.toml; run it again as each user that builds projects.

How do you start a new project or adopt an existing one?

Use poetry new for a fresh package and poetry init inside a folder that already has code. Since Poetry 2, poetry new uses a src layout by default.

cd ~/apps
poetry new inventory-api
cd inventory-api
tree -L 3 .        # sudo apt install tree, or dnf install tree
cat pyproject.toml

A typical Poetry 2 file looks like this:

[project]
name = "inventory-api"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = []

[tool.poetry]
packages = [{ include = "inventory_api", from = "src" }]

[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

For a web application that is never published as a package, add package-mode = false under [tool.poetry]. Poetry then only manages dependencies and does not try to install the project itself, which avoids errors about a missing package folder.

How do you add, group and lock dependencies?

Run poetry add for runtime libraries and poetry add --group dev for tools only needed while developing. Each call updates both the TOML file and the lock.

poetry env use python3.12      # pick the interpreter once per project
poetry add flask gunicorn
poetry add --group dev pytest ruff
poetry show --tree
poetry show --outdated
poetry update flask            # move one package within its range
poetry lock                    # refresh the lock after editing pyproject.toml by hand
poetry check --lock

In Poetry 2, poetry lock keeps existing pins where it can; add --regenerate to resolve everything from scratch. Use poetry remove to drop a library, and it disappears from both files.

How do you run code inside the environment?

Prefix commands with poetry run, or activate the environment directly. The old poetry shell command moved to a plugin in Poetry 2; the built in replacement prints an activation command for you to evaluate.

poetry run python -c "import flask; print(flask.__version__)"
poetry run pytest
eval "$(poetry env activate)"
python --version
deactivate
poetry env info --path

Because the environment lives in .venv, you can also call .venv/bin/python directly, which is what scripts and services should do.

How do you deploy a Poetry project on the server?

Copy or clone the code with both pyproject.toml and poetry.lock, then install exactly what the lock says, skipping the dev group. The sync command also removes packages that are no longer listed, so the environment cannot drift. If the code lives in Git, the Git deploy hooks guide shows how to trigger this on push.

sudo mkdir -p /srv/inventory-api
sudo chown deploy:deploy /srv/inventory-api
git clone https://git.example.net/team/inventory-api.git /srv/inventory-api
cd /srv/inventory-api
poetry sync --only main
.venv/bin/gunicorn --version

Then describe the service with systemd, pointing straight at the environment:

sudo tee /etc/systemd/system/inventory-api.service >/dev/null <<'EOF'
[Unit]
Description=Inventory API
After=network.target

[Service]
User=deploy
WorkingDirectory=/srv/inventory-api
ExecStart=/srv/inventory-api/.venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 'inventory_api.app:create_app()'
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now inventory-api
curl -I http://127.0.0.1:8000/

Put Nginx in front for public traffic, as shown in deploying Flask with Gunicorn and Nginx.

What if a server or container only understands requirements.txt?

Export the lock to a plain requirements file with the export plugin, then install it with pip. This is useful for container builds where you do not want Poetry inside the image.

pipx inject poetry poetry-plugin-export
poetry export --only main -f requirements.txt --output requirements.txt
head requirements.txt

The exported file includes hashes, so pip install -r requirements.txt verifies every download. If you installed Poetry with the official script, add the plugin with poetry self add poetry-plugin-export instead.

How do you fix common Poetry problems?

  • Wrong Python version in .venv: run poetry env remove --all, then poetry env use python3.12 and install again.
  • Lock file out of date: the message about pyproject.toml changing significantly means someone edited the TOML without locking. Run poetry lock and commit the result.
  • Build fails on a native package: install compilers and headers (build-essential python3-dev or gcc python3-devel) and retry.
  • Installs are slow or killed on a 1 GB plan: the resolver and wheel builds can use a lot of memory; add swap, or resolve on your workstation and only run poetry sync on the server.
  • Command not found after install: run pipx ensurepath and open a new login shell.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2small apps with room for the resolver1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4several projects and CI style builds2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Manage Python projects with Poetry

Should I commit poetry.lock?

Yes, for applications. The lock file is what makes the server install the same versions you tested. Libraries published for others often commit it too, since it only affects development.

Do I still need venv if I use Poetry?

Poetry creates and manages the virtual environment for you. With virtualenvs.in-project enabled it is the .venv folder in the project.

What replaced poetry shell?

In Poetry 2 use eval "$(poetry env activate)", or install the poetry-plugin-shell plugin to get the old command back.

Poetry or uv?

Both manage pyproject.toml projects with a lock file. Poetry has a long history and plugin ecosystem; uv is newer and very fast and can also download interpreters. Either works on our servers; stay consistent within a team.

Can Poetry use a Python version the system does not have?

No, Poetry needs the interpreter to exist. Install it first through AppStream, deadsnakes, uv or a source build, then point Poetry at it with poetry env use.