How to Create a Python Virtual Environment on Ubuntu 22.10

December 1, 2022 in Tools | 2 mins read | Tagged:

Python virtual environments allow you to isolate your Python environment and dependencies from your system’s global environment. This is especially useful when working on multiple Python projects that require different versions of packages. In this tutorial, we’ll walk through how to create a Python virtual environment on Ubuntu 22.10.

Step 1: Install Python

Ubuntu 22.10 comes with Python pre-installed, but you may want to make sure you have the latest version installed. To check your version of Python, run the following command in your terminal:

python3 --version

If you need to install Python, run the following command:

sudo apt-get update
sudo apt-get install python3

Step 2: Install Virtualenv

To create a Python virtual environment, we’ll need to install Virtualenv. Run the following command to install Virtualenv:

sudo apt-get install virtualenv

Step 3: Create a Virtual Environment

Now that we have Virtualenv installed, let’s create a new virtual environment for our project. First, navigate to your project directory:

cd /path/to/your/project
Next, create a new virtual environment:
virtualenv venv

This will create a new directory called venv in your project directory. This directory will contain a new, isolated Python environment.

Step 4: Activate the Virtual Environment

To activate the virtual environment, run the following command:

source venv/bin/activate

You should now see the name of your virtual environment in your terminal prompt.

Step 5: Install Packages

Now that your virtual environment is activated, you can install packages using pip, just like you would in your global environment. For example:

pip install numpy

Step 6: Deactivate the Virtual Environment

When you’re finished working in your virtual environment, you can deactivate it by running the following command:

deactivate

This will return you to your global environment.

In this tutorial, we learned how to create a Python virtual environment on Ubuntu 22.10 using Virtualenv. Virtual environments are a powerful tool for managing dependencies and isolating your Python environment. With this knowledge, you can create and work on multiple Python projects with ease.


Back to top