Creating a Virtual Environment - rapmd73/JackrabbitRelay GitHub Wiki
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
apt upgrade
1. 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.
apt update
2. 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.
apt install python3-venv
3. 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.
python3 -m venv /home/RAPMD
4. 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 thevenv
module to create the environment./home/RAPMD
: The directory where the virtual environment will be created.
The choice of /home/RAPMD
is specific to Jackrabbit Relay 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 Jackrabbit Relay and the user's software, offering a sense of structure that aligns with their approach to software deployment.
source /home/RAPMD/bin/activate
5. 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
.bashrc
:
Steps to Add Activation to -
Open the
.bashrc
file in a text editor:nano ~/.bashrc
-
Scroll to the end of the file and add the following line:
source /home/RAPMD/bin/activate
-
Save and exit the editor (e.g.,
CTRL+O
,Enter
,CTRL+X
innano
, 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 Jackrabbit Relay 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 Jackrabbit Relay Install and Setup.