How to Create a Python Virtual Environment on Ubuntu 24.04/22.04/20.04

Creating a Python virtual environment is essential for developers working with Python on Ubuntu systems. This guide demonstrates how to create a Python virtual environment on Ubuntu versions 24.04, 22.04, or 20.04. Virtual environments are key in managing Python projects effectively, ensuring each project has its dependencies, irrespective of the others. This isolation avoids conflicts between projects and streamlines development workflows. Below are some of the key features and benefits of creating Python virtual environments:

  • Isolation of Dependencies: Each project can have its dependencies, preventing interference with other Python projects.
  • Replicability: Virtual environments make replicating project settings on different machines or among team members easy.
  • Versatility in Development: They allow developers to work with different Python versions and libraries simultaneously without hassle.
  • Resource Optimization: By managing dependencies efficiently, virtual environments help better utilize system resources.
  • Simplified Project Management: Streamlines the process of managing project-specific requirements.

This approach to Python development on Ubuntu is practical and aligns with best practices in software development. Whether you are a seasoned developer or just starting out, understanding how to create and manage virtual environments is valuable. Let’s dive into the steps to set up your Python virtual environment on Ubuntu.

Pre-Installation Steps Before Creating Python Virtual Environment

Update Ubuntu

It’s important to update your Ubuntu system to ensure all packages are current. This step is vital for system security and functionality. Open your terminal and execute the following command:

sudo apt update && sudo apt upgrade

This command refreshes your system’s package index and then upgrades all installed packages to their latest versions.

Install Python (Skip if Installed)

Python is the foundation for setting up a virtual environment. Check if Python is already installed by running python3 --version. If it’s not installed, or you need a newer version, install Python with the following command:

sudo apt install python3

This installs the latest Python 3.x version in the Ubuntu repositories.

Install Virtual Environment (virtualenv)

To create isolated Python environments, you need the virtualenv package. This tool is essential for managing separate project environments, each with its own dependencies. Install virtualenv using APT by running:

sudo apt install python3-virtualenv

Upon completion, you have virtualenv installed, ready to create isolated Python environments for different projects.

Create a Virtual Environment with Python

Setting Up the Virtual Environment

Now that you have Python and virtualenv installed, you can create a virtual environment. This environment is a self-contained directory that holds your project’s Python interpreter and any necessary packages. To create a virtual environment, use the following command:

virtualenv env

In this command, virtualenv is the tool you’re using, and env is the name of your virtual environment. You can replace env with any name you prefer for your project’s environment. This flexibility in naming allows for easy identification of different environments for various projects.

Step 2: Activate Your Virtual Environment

Once your virtual environment is set up, the next step is to activate it. Activating the environment allows you to work within it, using its isolated Python interpreter and packages. To activate your virtual environment, use the following command:

source env/bin/activate

Here, source is a shell command that reads and executes commands from the file specified. In this case, env/bin/activate is the file that activates your environment. Remember to replace env with your virtual environment’s name if you named it differently.

Confirming Activation

Upon successful activation, you’ll notice a change in your terminal prompt. It will display the name of your virtual environment, confirming that it’s active. For instance, if your virtual environment is named env, your terminal prompt will look like this:

(env) joshua@ubuntu-linux:~$ 

This change in the prompt indicates that you are now working inside the virtual environment. Any Python packages you install, while the environment is active, will be installed within this isolated environment, not affecting the global Python installation.

Step 3: Deactivate Virtual Environment

To exit the virtual environment and revert to your system’s default Python environment, use the deactivate command:

deactivate

This command ceases the virtual environment’s operation, returning your terminal to the system environment.

Managing Python Packages in Virtual Environments

Installing PIP in Ubuntu

Before installing Python packages in your virtual environment, ensure that PIP, the Python package installer, is installed on your system. PIP is a powerful tool that simplifies the installation and management of Python packages. To install PIP, execute the following command:

sudo apt install python3-pip

This command installs the package specified by “<package_name>.” Replace “<package_name>” with the package name you want to install.

This command installs PIP for Python 3, aligning with the version of Python installed on your Ubuntu system.

Updating PIP

Keeping PIP up-to-date is crucial for security and functionality. To update PIP, run the following command within your virtual environment:

pip install --upgrade pip

This command upgrades PIP to the latest version, ensuring you have access to the latest features and security patches.

Installing a Python Package

With PIP installed and updated, you can install Python packages in your virtual environment. Use the following command, replacing <package_name> with the desired package’s name:

pip install <package_name>

For instance, to install the requests package, you would use:

pip install requests

Searching for Packages

PIP also allows you to search for packages in the Python Package Index (PyPI). To search for a package, use:

pip search <search_query>

Replace <search_query> with keywords or the package name you are searching for. This feature is useful for discovering new packages or finding specific ones that meet your project requirements.

Removing a Python Package

If you need to remove a package from your virtual environment, PIP makes it straightforward. Use the following command:

pip uninstall <package_name>

Replace <package_name> with the name of the package you want to uninstall. For example, to remove requests:

pip uninstall requests

Practical Application in Virtual Environments

These PIP commands are particularly valuable when working within Python virtual environments. By managing packages within these isolated environments, you can tailor the setup to your specific project needs without impacting other projects or the global Python setup on your system. This approach is essential for maintaining clean, efficient, and conflict-free development environments in Python projects.

Conclusion

In this article, we learned how to create a Python virtual environment on Ubuntu in detail, including installing Python and Virtualenv, creating and activating the virtual environment, installing packages, deactivating the virtual environment, and deleting it. With the knowledge of these steps, you can now easily create virtual environments for your projects and keep your dependencies and packages isolated from the system, ensuring the stability and reliability of your projects.

Leave a Comment