Skip to content
Home

Install `Poetry` in Ubuntu

Introduction

Poetry is a tool for dependency management and packaging in Python. It offers a lockfile to ensure repeatable installs, and can build your project for distribution.

Below are the instructions for Ubuntu. Complete instructions list for other methods here: https://python-poetry.org/docs/#installing-with-the-official-installer


Install pyenv to manage python versions. Here is the complete source

  1. Installing via the recommended way

    curl https://pyenv.run | bash
  2. Setup shell environment by adding the commands to the .bashrc

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
  3. Restart shell

    exec "$SHELL"
  4. (optional) Build environment for Python

    sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
    libbz2-dev libreadline-dev libsqlite3-dev curl git \
    libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
  5. Install a python version

    pyenv install -l # gives the list of all available versions
    pyenv install 3.12 # install the selected version
  6. Set the version

    pyenv global 3.12 # setting the python environment
  7. Uninstall python version

    pyenv uninstall 3.12 # removes this version
  8. (optional) Update pyenv

    pyenv update
  9. (optional) Uninstall pyenv: To completely uninstall Pyenv, remove all Pyenv configuration lines from your shell startup configuration, and then remove its root directory. This will delete all Python versions that were installed under the $(pyenv root)/versions/ directory:

    rm -rf $(pyenv root)

Instructions for poetry installation

  1. Download the Poetry install script and execute

    curl -sSL https://install.python-poetry.org | python3 -
  2. Add Poetry to Path

    The installer creates a poetry wrapper in a well-known, platform-specific directory:

    • $HOME/.local/bin on Unix.
    • %APPDATA%\Python\Scripts on Windows.
    • $POETRY_HOME/bin if $POETRY_HOME is set.
    # TODO: check if the `poetry` is actually installed in the path
    echo 'export PATH="$PATH:$HOME/.local/bin"' >> ~/.bashrc
    # To apply the changes
    source ~/.bashrc
  3. Use Poetry

    poetry --version
  4. Autocompletion

    poetry completions bash >> ~/.bash_completion
  5. Update Poetry (optional)

    poetry self update
  6. Uninstall Poetry (optional)

    curl -sSL https://install.python-poetry.org | python3 - --uninstall
    curl -sSL https://install.python-poetry.org | POETRY_UNINSTALL=1 python3 -

Settings for fish shell configuration

In the config.fish file, add the following code snippet to recognize pyenv and poetry

# pyenv settings
#------------------------
# Set pyenv root directory
set -x PYENV_ROOT $HOME/.pyenv
# Add pyenv to PATH
set -x PATH $PYENV_ROOT/bin $PATH
# Initialize pyenv
if test -d $PYENV_ROOT
status --is-interactive; and source (pyenv init --path | psub)
source (pyenv init - | psub)
end
# poetry settings
#------------------------
# Add Poetry to PATH
set -x PATH $HOME/.local/bin $PATH

Bonus