When working with Python projects on VPS, it’s essential to isolate dependencies for each one to avoid version conflicts and maintain clean environments. This is where virtualenv comes in — a tool that helps you create independent Python environments.

In this guide, you’ll learn how to install virtualenv, create a virtual environment, and use it with Python 3.

What is virtualenv?

virtualenv is a tool that allows you to create isolated environments for Python projects. Each environment can have its own Python interpreter and installed packages — without affecting the global Python installation.

✅ Benefits:

  • Avoid dependency conflicts

  • Keep global Python clean

  • Easily manage project-specific libraries

Prerequisites

Make sure you have Python 3 and pip installed. Check by running:

python3 --version
pip3 --version

If not installed, get Python 3 from https://python.org.

Step 1: Install virtualenv

Install virtualenv globally using pip:

pip3 install virtualenv

To verify:

virtualenv --version

Step 2: Create a Virtual Environment

Navigate to your project folder or create a new one:

mkdir myproject
cd myproject

Then create the virtual environment (you can name it anything, commonly it’s called venv or .env):

virtualenv venv

You can also specify Python version explicitly:

virtualenv -p python3.11 venv

This creates a folder named venv/ that contains a local Python interpreter and a local pip.

Step 3: Activate the Environment

On Linux/macOS:

source venv/bin/activate

On Windows:

venv\Scripts\activate

After activation, your shell prompt will change (you’ll see the name of the environment), and any Python packages installed will only apply inside this environment.

Step 4: Install Dependencies

Once activated, you can install packages like usual:

pip install requests

You can freeze the environment’s dependencies into a file:

pip freeze > requirements.txt

Later, you can recreate the same environment on another system with:

pip install -r requirements.txt

Step 5: Deactivate the Environment

When you’re done:

deactivate

This returns you to the global Python environment.