Comprehensive Guide to Installing Poetry on Ubuntu and Managing Python Projects

Ronak Jain
3 min readOct 8, 2023

--

Python developers often find managing dependencies and projects a challenging task. Poetry, a robust dependency management and packaging tool, simplifies this process by providing an intuitive interface and powerful features. In this comprehensive guide, we will walk you through the detailed steps to install Poetry on Ubuntu, manage Python environments using Pyenv, and cover all the essential commands and configurations in Poetry.

Photo by Florian Olivo on Unsplash

Prerequisites

Before we begin, ensure you have the following prerequisites:

  • Ubuntu operating system installed on your machine.
  • Python installed (preferably the latest version).
  • Basic knowledge of the terminal and Python programming.

Installing Poetry on Ubuntu

Step 1: Install Poetry

Open a terminal window and run the following command to install Poetry:

curl -sSL https://install.python-poetry.org | python3 -

This command downloads and installs Poetry on your system.

Step 2: Verify Installation

To confirm that Poetry is installed correctly, run:

poetry --version

You should see the installed version of Poetry displayed in the terminal.

Managing Python Environments with Pyenv

Pyenv is a fantastic tool for managing multiple Python versions on your system. Let’s install Pyenv and configure it to work seamlessly with Poetry.

Step 1: Install Pyenv

Install Pyenv using the following commands:

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
source ~/.bashrc

Step 2: Install Python Versions

Install the desired Python version(s) using Pyenv:

pyenv install 3.8.6  # Example Python version

Step 3: Set Local and Global Python Versions

Set a local Python version for your project:

cd your_project_directory
pyenv local 3.8.6 # Replace with your desired Python version

Set a global Python version for your system:

pyenv global 3.8.6  # Replace with your desired Python version

Creating and Managing Python Projects with Poetry

Now that we have Poetry and Pyenv set up, let’s dive into the essential commands and configurations in Poetry.

Step 1: Initialize a Poetry Project

Navigate to your project directory and run:

poetry init

Step 2: Add Dependencies

Add project dependencies using the poetry add command. For example, to add the Requests library:

poetry add requests

Poetry will automatically update pyproject.toml and create a poetry.lock file, ensuring consistent dependency management.

Step 3: Install Dependencies

To install project dependencies, run:

poetry install

This command reads pyproject.toml and installs the specified dependencies along with their correct versions, ensuring consistency across different environments.

Step 4: Creating Virtual Environments

Poetry manages virtual environments for your projects. To activate the virtual environment, use:

poetry shell

This command drops you into a shell within the virtual environment, allowing you to work in an isolated Python environment specific to your project.

Step 5: Running Scripts

Poetry simplifies running scripts in your project. For example, if you have a script named main.py, you can run it using:

poetry run python main.py

This ensures that the script runs in the project’s virtual environment, avoiding conflicts with system-wide Python installations.

Step 6: Updating Dependencies

To update your project’s dependencies, use the following command:

poetry update

This updates your poetry.lock file with the latest compatible versions of your project dependencies.

Step 7: Publishing Packages

If you’re developing a Python package and want to publish it, Poetry simplifies the process. First, configure your PyPI credentials:

poetry config pypi-token.pypi <your-token>

Then, publish your package:

poetry publish --build

This command builds your package and uploads it to PyPI, making it available for others to install and use.

Conclusion:

Congratulations! You have successfully installed Poetry on Ubuntu, configured Python environments using Pyenv, and learned how to create and manage Python projects with Poetry. By following these detailed steps, you can streamline your Python development workflow and focus on writing great code. Happy coding!

Before you go

Thank you for reading. I hope you enjoyed it. Don’t forget to follow me on GitHub| Medium | Linkedin

If this post was helpful, please give this blog a clap to show me your support! It helps a lot!

Ronak Jain

--

--

Ronak Jain

Python | Data Science | SQL | Machine Learning | OCR | Quantum Computing |