How to Install Python Dependencies in Ubuntu Linux

Managing dependencies for any software project is necessary to ensure smooth execution; otherwise, we end up with a broken app. Similarly, developers working on Python applications require various libraries and packages, known as Dependencies, to complete their projects’ functionality and run them correctly.

In this article, we go through the steps to learn how to install the dependencies required by a Python project or application on Ubuntu Linux.

What do we need?

  • Python installation
  • Ubuntu 24.04, 22.04, 20.04 or any other version
  • sudo rights
  • Internet connection

Installing Dependencies required by a Python app on Ubuntu

Step 1: APT package Update

Run the terminal app on your Ubuntu system and type the below-given command to update the package repository cache and security patches; it will also install the latest versions of packages already installed on Ubuntu.

sudo apt update && sudo apt upgrade

Step 2: Install Python and Pip

I assume you have already installed Python on your Ubuntu system; however, to confirm, we can execute the given command to ensure Python is on our system.

sudo apt install python3

Now, to manage and install the Python-required dependencies, we can use its package manager – Pip (Python’s package installer). It will not be on your Ubuntu system by default and needs to be installed manually using the given command:

sudo apt install python3-pip
Instaling Python PIP on Ubuntu

Step 3: Verify the Installation

To verify that Python and Pip have been installed correctly, check their versions:

python3 --version
pip3 --version

Step 4: Create a Virtual Environment (Optional)

Although it is a purely optional step in this article, it is recommended that those who create a new Python project create a virtual environment. A virtual environment provides an isolated environment, allowing users to manage different projects’ dependencies separately.

To create a virtual environment, first install the venv module:

sudo apt install python3-venv

Then, create a virtual environment in your project directory. The name of the virtual environment we have used is “myenv“; you can use whatever you want.

python3 -m venv myenv

Activate the virtual environment:

source myenv/bin/activate

Step 5: Install Python Dependencies

Ubuntu Linux and other operating systems using the PIP can install multiple Python dependencies in a single command. To do so, we can create a file, “requirements.txt,” and list the names of all dependencies required for our project inside it. Then, we can save the file and execute the PIP command as given below to install them.

pip install -r requirements.txt

This command will download and install all the packages listed in the requirements.txt file.

Step 6: Install Individual Packages

Use the given command syntax if you need to install individual Python dependency packages using PIP. For example, to install the requests library, run:

pip install requests
Install Python dependencies on Ubuntu

Step 7: Freeze Dependencies (Optional)

What if you have installed all required dependencies for your Python project on Ubuntu and now want to share it with others or deploy it for production without changing the dependencies version? In simple words, you have tested a version of the Python program with a particular set of dependencies. Now, at the time of deployment, the project must use the same version of libraries we used to test our app, not an updated one. In that case, we can freeze the installed packages and their versions in a requirements.txt file.

This command will generate a requirements.txt file listing all installed packages and their versions.

pip freeze > requirements.txt

Step 8: Deactivate the Virtual Environment (Optional)

Once you are done with working, in case you need to deactivate the current Python Virtual environment and return to the system’s global environment, use:

deactivate 

Conclusion

If you already have the Python and PIP package manager on your Ubuntu system, managing and installing dependencies for your project will not be a Sisyphean task. Nevertheless, it is recommended that you ensure an isolated Python virtual environment for your project for better handling and a cleaner approach.

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.