Skip to content

Creating a Virtual Environment

Rose Heart edited this page Jan 24, 2025 · 3 revisions

Creating a Virtual Environment

Modern Linux distributions prioritize security and stability, restricting modifications to critical system areas, particularly when it comes to installing Python modules in global directories. This is essential to prevent unintended changes that might disrupt system operations. To overcome this limitation, Python Virtual Environments are employed, allowing developers to create isolated spaces for each project. These environments ensure that project dependencies do not interfere with the system’s Python installation.

This guide outlines the steps for setting up a Python virtual environment, emphasizing a philosophy that avoids placing non-system files in system areas. This approach not only adheres to best practices for security but also makes it easier to manage a VPS, especially with regard to backups.

The Instructions

These instructions are for Ubuntu 20.04, but should easily be adaptable to other methodoloies. These instructions only need to be done ONCE

1. apt upgrade

The apt upgrade command upgrades all installed packages to their latest versions. This step ensures that your system is up-to-date, especially for system libraries and tools that may interact with Python and other programming languages.

2. apt update

The apt update command fetches the latest package index files from the system’s configured package sources. By running this command, you ensure that your package manager is aware of the latest updates and available software.

Running apt update prior to upgrading packages ensures you have the most up-to-date information, allowing you to upgrade the system packages efficiently.

3. apt install python3-venv

This command installs the python3-venv package, which is necessary to create Python 3 virtual environments. While the venv module is part of the Python standard library, it is not always installed by default in some Linux distributions.

Installing python3-venv ensures that you can create isolated Python environments for your projects without interference from the system’s global Python setup.

4. python3 -m venv /home/RAPMD

This command creates a Python virtual environment within the directory /home/RAPMD. Here's a breakdown of the components:

  • python3: Specifies that Python 3 should be used.
  • -m venv: Runs the venv module to create the environment.
  • /home/RAPMD: The directory where the virtual environment will be created.

The choice of /home/RAPMD is specific to Companion and the unique software philosophy behind it. The directory is intentionally selected to reflect the purpose of the software, ensuring that non-system files are stored outside of critical system areas. This not only adheres to a principle of separating software from system files but also helps make managing your VPS easier, particularly when performing backups. For clarity, /home/RAPMD is uniquely associated with Companion and the user's software, offering a sense of structure that aligns with their approach to software deployment.

5. source /home/RAPMD/bin/activate

After creating the virtual environment, the source command is used to activate it. This modifies the shell session to use the Python interpreter and packages contained within the virtual environment instead of the system-wide Python installation.

Activating the environment ensures that all Python commands (e.g., python or pip) will be directed to the virtual environment, keeping the project dependencies separate from the global Python installation.

Automating Activation

To make it easier to use the virtual environment every time you open a terminal session, you can add the following line to the end of your .bashrc file:

source /home/RAPMD/bin/activate

Steps to Add Activation to .bashrc:

  1. Open the .bashrc file in a text editor:

    nano ~/.bashrc
  2. Scroll to the end of the file and add the following line:

    source /home/RAPMD/bin/activate
  3. Save and exit the editor (e.g., CTRL+O, Enter, CTRL+X in nano, or your perfered keys for a modified nano).

Effect:

Once added, the virtual environment will automatically activate every time a new terminal session starts. This removes the need to manually activate the environment each time, allowing for a seamless development experience.


By following these instructions, you will have set up an isolated Python environment, avoiding potential conflicts with the system-wide Python setup. The choice of storing the virtual environment in /home/RAPMD reflects the specific philosophy behind Companion and the user's software, ensuring better VPS management and facilitating easier backups by keeping non-system files separate from critical system directories.

Adding the activation command to .bashrc automates the process, ensuring that the virtual environment is always ready when you open a new terminal session, streamlining your development workflow.

Now you can follow the rest of the Companion Setup and Configuration.