Python


multiple Python versions on Ubuntu

You can install and manage multiple Python versions on Ubuntu using the pyenv tool or by using the Deadsnakes PPA (Personal Package Archive) with apt. The pyenv method is generally recommended as it is safer and user-level, while the PPA method is good for system-wide access. 

Method 1: Using pyenv (Recommended for development)
pyenv allows you to manage multiple Python versions and switch between them easily without affecting the system's default Python installation. 
  1. Install system dependencies: These packages are necessary to build Python from source, which pyenv does.
    bash
    sudo apt update
    sudo apt install git curl make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
    
  2. Install pyenv: Run the automatic installer script provided by pyenv.
    bash
    curl https://pyenv.run | bash
    
  3. Configure your shell environment: Add the following lines to your ~/.bashrc (or ~/.zshrc if you use Zsh) file to ensure pyenv loads correctly.
    bash
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    
  4. Reload your shell: Close and reopen your terminal, or run the following command to apply the changes to your current session:
    bash
    source ~/.bashrc
    
  5. Install desired Python versions: Now you can install specific Python versions using pyenv. For example, to install Python 3.8.0 and 3.12.0:
    bash
    pyenv install 3.8.0
    pyenv install 3.12.0
    
  6. Switch between versions:
    • To set a global default for your user: pyenv global 3.12.0.
    • To set a local version for a specific project directory: pyenv local 3.8.0. 

Method 2: Using the Deadsnakes PPA (System-wide installation) 
The Deadsnakes PPA offers various Python versions packaged for Ubuntu, allowing installation via apt without manually compiling from source. This method installs Python versions system-wide. 
  1. Add the Deadsnakes PPA:
    bash
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt update
    
  2. Install specific Python versions: Install the desired versions (e.g., Python 3.8 and 3.12).
    bash
    sudo apt install python3.8
    sudo apt install python3.12
    
  3. Verify installations: The executables will be available as python3.8 and python3.12.
    bash
    python3.8 --version
    python3.12 --version
    
  4. Install pip for each version: pip must be installed separately for each version.
    bash
    sudo apt install python3.8-distutils # Install distutils first if needed
    curl -sS https://bootstrap.pypa.io/get-pip.py | python3.8
    
    sudo apt install python3.12-distutils # Install distutils first if needed
    curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
    
     
Important Note: Avoid modifying the default system Python symlink (/usr/bin/python3) directly, as system tools rely on it and changing it can cause your operating system to break. The methods above allow different versions to coexist safely. For project-specific dependencies, always use Python virtual environments

installing masonite on ubuntu 24

Installing the Masonite framework on Ubuntu 24.04 involves setting up Python dependencies, creating a virtual environment, and installing the framework using pip. 

Prerequisites
Before installing, ensure your Ubuntu system is updated and has the necessary development tools, Python, and pip installed. 

bash
sudo apt update
sudo apt install python3-dev python3-pip libssl-dev build-essential python3-venv

Installation Steps
  1. Create a Project Directory
    Navigate to the directory where you want to store your project:
    bash
    mkdir ~/myapp
    cd ~/myapp
    
  2. Set Up a Virtual Environment (Recommended)
    Create and activate a virtual environment to avoid conflicts with system-wide packages:
    bash
    python3 -m venv venv
    source venv/bin/activate
    
  3. Install Masonite
    Install the core Masonite framework:
    bash
    pip install masonite
    
  4. Create a New Project
    Initialize a new Masonite project in the current directory:
    bash
    craft new
    # or to specify a name
    # project start my_project
    
  5. Install Project Dependencies
    Install the dependencies listed in your project's requirements.txt file:
    bash
    pip install -r requirements.txt
    # Alternatively, if you used 'project start'
    # project install
    
  6. Run the Server
    Start the development server to confirm installation:
    bash
    python craft serve
    
    Access your application at http://localhost:8000/. 

Troubleshooting

Running a Python Masonite application in aaPanel

Running a Python Masonite application in aaPanel involves installing the Python manager, setting up a virtual environment, installing dependencies, and configuring a WSGI server (like Gunicorn) behind Nginx. Use the "Python Project" management feature in aaPanel to handle the environment, dependencies, and reverse proxy configuration seamlessly. 

Step-by-Step Deployment Guide
  1. Install Python Manager:
    • Log in to aaPanel.
    • Go to Software Store, find Python Project Manager, and install it.
  2. Upload Project Files:
    • Use the aaPanel File Manager to upload your Masonite project files to a directory (e.g., /www/wwwroot/masonite-app).
  3. Configure Project in Python Manager:
    • Open Python Project Manager.
    • Click Add Project.
    • Project Name: Enter a name.
    • Path: Select the folder where you uploaded the Masonite app.
    • Python Version: Select Python 3.8+ (recommended for Masonite).
    • Framework: Choose "Masonite" (or "Flask/WSGI" if Masonite isn't listed, as it's WSGI compatible).
    • Startup File/Script: Point to wsgi.py (usually app/wsgi.py or similar).
    • Port: Set a port (e.g., 8000).
    • Select "Install Dependencies" to install from requirements.txt.
    • Ensure Binding Domain is filled to map your domain to the app.
  4. Configure Environment Variables & Database:
    • In the project settings in the Python Manager, ensure the .env file is configured correctly for your database (MySQL/PostgreSQL) and that the APP_URL matches your domain.
  5. Run Migrations & Start:
    • Use the terminal in aaPanel or the command line in the project manager to run Masonite commands:
      bash
      craft migrate
      
    • Restart the application in the Python Manager to apply changes.
  6. Setup Nginx Reverse Proxy:
    • The Python Manager should automatically create an Nginx configuration that proxies requests from port 80/443 to your Python app port (e.g., 8000). 
If the app fails to start, check the logs in the Python Project Manager to resolve dependency or environment issues

Creating new masonite project using python 3.8.5

First confirm the version

/www/server/python_manager/versions/3.8.5/bin/python3 --version

Setup the python 3.8.5 virtual environment

/www/server/python_manager/versions/3.8.5/bin/python3 -m venv <environmentname>

Activate the envronment

source <environmentname>/bin/activate

Install the core Masonite framework

pip install masonite

Create your project 
project start <projectfoldername>
cd <projectfoldername>
project install

Run your project

python craft serve

 

You can create this in the same way by switching to another Python version available on your system.

Deploying a Masonite Application on aaPanel Using Gunicorn and tmux

Deploying a Masonite application on a VPS managed with aaPanel is straightforward when done step by step. This guide walks you through setting up your environment, running Masonite with Gunicorn, and keeping the application alive using tmux, along with configuring a reverse proxy in aaPanel.


Step 1: Create a Static Website (MySQL Optional)

Start by creating a static website in aaPanel.
If your Masonite project requires a database, you may also create a MySQL database at this stage. Otherwise, MySQL is optional.

This step mainly helps aaPanel manage the domain and web root.


Step 2: Remove the Default index.html

After the site is created, navigate to the website’s root directory and remove the default index.html file created by aaPanel.
This ensures it does not conflict with your Masonite application.


Step 3: Upload Your Masonite Project Files

Upload all your Masonite project files into the website directory using FTP, SFTP, or the aaPanel file manager.

Make sure your project structure is intact, including the wsgi.py (or equivalent) entry file.


Step 4: Install tmux from aaPanel Terminal

Open the aaPanel Terminal and switch to the Super User (root) shell.
Install tmux, which will help keep Gunicorn running even after you close the terminal.


yum install tmux -y # or (for Ubuntu/Debian) apt install tmux -y

Step 5: Navigate to Your Project Folder

Still in the super user shell, move to your Masonite project directory:


cd /www/wwwroot/your-domain.com

Step 6: Initialize Python 3.11.4 Environment

Initialize and activate a Python 3.11.4 virtual environment for your project.

Refer to previously Creating new masonite project using python 3.8.5 for setting up Python 3.11.4 virtual environment.

Once activated, ensure Python and pip are pointing to the correct environment.


Step 7: Install Gunicorn

With the virtual environment active, install Gunicorn:


pip install gunicorn

Gunicorn will act as the WSGI server for your Masonite application.


Step 8: Run Gunicorn

Start your Masonite application using the following command:


gunicorn wsgi:application --bind your-domain.com:8010

Step 9: Verify the Application

At this point, check if your Masonite application is running correctly.

Yes, it is working — because you know your code better than anyone else 🙂

If there are issues, check logs and fix them before proceeding.


Step 10: Run Gunicorn in a Detached tmux Session

To keep Gunicorn running in the background, create a new tmux session:


tmux new -s gunicorn_session

Run Gunicorn again inside the tmux session:


gunicorn wsgi:application --bind your-domain.com:8010

Detach from the session without stopping Gunicorn:

Press: Ctrl + B, then D

Your application will continue running in the background.


Step 11: Configure Reverse Proxy in aaPanel

Now go back to aaPanel → Website Settings → Reverse Proxy and add a new proxy rule:

Save the configuration.


Step 12: Access Your Masonite Application

Open your domain in the browser.
You will now see your Masonite application running successfully on aaPanel, served through Gunicorn and managed via reverse proxy.