How to install Poetry on Ubuntu 22.04?
Overview
Poetry is a manager of dependencies in Python that is designed to make your project more flexible, efficient, and maintainable. It allows you to easily upgrade and downgrade Python project dependencies at any time. It offers simple and various tools and a consistent interface for you to work easily in a virtual environment.
This tutorial will help you with the installation of Poetry on Ubuntu 22.04, create a Poetry project, and manage its dependencies.
Prerequisites
There are certain prerequisites that need to be met before you begin.
Ubuntu 20.04 LTS configured on your system.
Python 3 configuration programming environment.
Non-root sudo user privileges
Get Started
Step 1: Installation The Poetry website offers an official installation script that is used to set up Poetry.
Run this command to initiate the download of the installation script and Poetry installation.
curl -sSL https://install.python-poetry.org | python3 -

Once the installation is complete, modify your PATH environment variable so that you can use Poetry from the command line. For this, open the ~/.bashrc file using nano.
nano ~/.bashrc
At the end of the file, add the following path:
export PATH="/home/ubuntu/.local/bin:$PATH"
Incorporate and save the changes into your current session:
source ~/.bashrc
Print the current version of Poetry to verify if you can run its commands:
poetry --version
Step 2: Project Setup A new Poetry project offers an ideal virtual environment for installing and managing your dependencies.
Create a new project, followed by the project name.
poetry new demo

Enter into the project directory:
cd demo
You can now view the recently created boilerplate files for your project.

Step 3: Managing Dependencies
Poetry utilizes an auto-generated file to store all the project dependencies, known as pyproject.toml.
Open the pyproject file to examine it:
nano pyproject.toml
In addition to the dependencies listed under tool.poetry.dependencies, tool.poetry also contains metadata that you can modify by adding your own data, such as your name, email address, or project details. Also, your project currently only depends on Python itself and has no other dependencies. Poetry, on the other hand, can perform addition and direct installation of new dependencies to your project via the command line.

Now, run the following command to add and install the dependencies concurrently as well as initialize the virtual environment.
poetry add requests

You can view the recent updates in the file:

Poetry uses a lock file to maintain consistency between the dependencies that are currently installed and the ones that are listed in your pyproject.toml file. During installation, Poetry reads directly from this file, so it is advisable to not make any changes to this file to avoid any errors / warnings. As a result, you should use the remove command to get rid of any dependency or modify its version.
poetry remove requests
After removing requests, you can re-add it as a pinned dependency. With dependencies, you frequently want to maintain a library for your project at a particular version. This can help you maintain consistency in your project because compatibility problems arise frequently as libraries age and change.
Pin requests to a different version with the following command:
poetry add [email protected]

You can view the recent updates in the file:

Conclusion
With the help of this tutorial, you were able to successfully install Poetry, create a project in it, and manage all project dependencies by adding, installing, or removing them.
Last updated
Was this helpful?