UbuntuUbuntu Based

How To Create Python Virtual Environment on Ubuntu 24.04 LTS

Create Python Virtual Environment on Ubuntu 24.04

In this tutorial, we will show you how to create Python Virtual Environment on Ubuntu 24.04 LTS. Python is an interpreted, high-level, general-purpose programming language that has gained immense popularity since its inception in the late 1980s. Known for its simplicity and readability, Python allows developers to focus on solving problems instead of getting bogged down by complex syntax. It’s widely used in web development, data analysis, artificial intelligence, scientific computing, and automation among other fields. The vast array of libraries and frameworks available further enhance its functionality.

A virtual environment is a self-contained directory that houses a Python project’s dependencies, libraries, and scripts. By creating a virtual environment, developers can maintain multiple projects on the same machine without interference. Each virtual environment has its own Python interpreter and can have different versions of packages installed. This isolation is crucial for preventing version conflicts when working on multiple projects.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step create a Python Virtual Environment on Ubuntu 24.04 (Noble Numbat). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • Basic familiarity with the terminal and command-line interface.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
  • An Ubuntu 24.04 system with root access or a user with sudo privileges.

Create Python Virtual Environment on Ubuntu 24.04

Step 1. Updating the Package Repository.

Execute these commands to update your package list and upgrade installed packages:

sudo apt update
sudo apt upgrade

These commands will fetch the latest package information and upgrade any outdated packages to their latest versions.

Step 2. Installing Python 3.

Ubuntu 24.04 LTS typically comes with Python 3 pre-installed. To check if Python 3 is installed and its version, run:

python3 --version

If Python 3 is not installed, or you want to install the latest version, execute the following commands:

sudo apt install python3 python3-pip

Step 3. Installing Virtual Environment Tools.

To effectively work with virtual environments in Python, it is essential to have the right tools installed. You can choose to use either the built-in venv module or the third-party virtualenv tool. Both serve similar purposes with slight differences.

  • Using the venv Module

The venv module is included in standard Python installations starting from Python 3.3. Here’s how to check and enable it:

Run the following command in the terminal to ensure the venv module is available:

python3 -m venv --help

To create a virtual environment, navigate to your project directory (or create one) and use:

python3 -m venv myenv
  • Using virtualenv

While venv is sufficient for many purposes, virtualenv offers additional features and is often preferred by experienced developers. Here’s how to install and use it:

pip3 install virtualenv

As with venv, navigate to your project folder and execute:

virtualenv myenv

After creating a virtual environment with either tool, you need to activate it to start using it. For venv, use:

source myenv/bin/activate

For virtualenv, the command is the same. After activation, your terminal prompt will change to indicate the active environment.

To verify that you are using the correct Python interpreter within your virtual environment, run:

which python

Step 4. Creating a Python Virtual Environment.

Creating a virtual environment is a straightforward process. This section details the step-by-step instructions for creating and activating a virtual environment using both venv and virtualenv.

  • Using venv

Use the cd command to change to the directory where you want to set up your virtual environment. For example:

cd ~/my_project

Execute the following command:

python3 -m venv myenv

To begin using the virtual environment, run:

source myenv/bin/activate

When finished, you can deactivate the environment by simply entering:

deactivate
  • Using virtualenv

As before, change to your desired project directory:

cd ~/my_project

Use the command:

virtualenv myenv

Activate it with:

source myenv/bin/activate

To deactivate, use:

deactivate

Each method provides you with a clean space to develop without interference from system-wide packages. Choose the one that best fits your project’s needs.

Congratulations! You have successfully created a Python Virtual Environment. Thanks for using this tutorial to create a Python Virtual Environment on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Python website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button