Python
- multiple Python versions on Ubuntu
- installing masonite on ubuntu 24
- Running a Python Masonite application in aaPanel
- Creating new masonite project using python 3.8.5
- Deploying a Masonite Application on aaPanel Using Gunicorn and tmux
multiple Python versions on Ubuntu
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. 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. - Install system dependencies: These packages are necessary to build Python from source, which
pyenvdoes.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 - Install
pyenv: Run the automatic installer script provided bypyenv.curl https://pyenv.run | bash - Configure your shell environment: Add the following lines to your
~/.bashrc(or~/.zshrcif you use Zsh) file to ensurepyenvloads correctly.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 - Reload your shell: Close and reopen your terminal, or run the following command to apply the changes to your current session:
source ~/.bashrc - 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:pyenv install 3.8.0 pyenv install 3.12.0 - 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.
- To set a global default for your user:
apt without manually compiling from source. This method installs Python versions system-wide. - Add the Deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update - Install specific Python versions: Install the desired versions (e.g., Python 3.8 and 3.12).
sudo apt install python3.8 sudo apt install python3.12 - Verify installations: The executables will be available as
python3.8andpython3.12.python3.8 --version python3.12 --version - Install
pipfor each version:pipmust be installed separately for each version.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
/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 environmentsinstalling masonite on ubuntu 24
pip. pip installed. sudo apt update
sudo apt install python3-dev python3-pip libssl-dev build-essential python3-venv
- Create a Project Directory
Navigate to the directory where you want to store your project:mkdir ~/myapp cd ~/myapp - Set Up a Virtual Environment (Recommended)
Create and activate a virtual environment to avoid conflicts with system-wide packages:python3 -m venv venv source venv/bin/activate - Install Masonite
Install the core Masonite framework:pip install masonite - Create a New Project
Initialize a new Masonite project in the current directory:craft new # or to specify a name # project start my_project - Install Project Dependencies
Install the dependencies listed in your project'srequirements.txtfile:pip install -r requirements.txt # Alternatively, if you used 'project start' # project install - Run the Server
Start the development server to confirm installation:
Access your application atpython craft servehttp://localhost:8000/.
- Command
craftnot found: If thecraftcommand is not recognized, ensure you are in the virtual environment. Alternatively, try running it withpython craft serve. - Permission Errors: If you did not use a virtual environment, use the
--userflag:pip install masonite --user
Running a Python Masonite application in aaPanel
- Install Python Manager:
- Log in to aaPanel.
- Go to Software Store, find Python Project Manager, and install it.
- Upload Project Files:
- Use the aaPanel File Manager to upload your Masonite project files to a directory (e.g.,
/www/wwwroot/masonite-app).
- Use the aaPanel File Manager to upload your Masonite project files to a directory (e.g.,
- 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(usuallyapp/wsgi.pyor 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.
- Configure Environment Variables & Database:
- In the project settings in the Python Manager, ensure the
.envfile is configured correctly for your database (MySQL/PostgreSQL) and that theAPP_URLmatches your domain.
- In the project settings in the Python Manager, ensure the
- Run Migrations & Start:
- Use the terminal in aaPanel or the command line in the project manager to run Masonite commands:
craft migrate - Restart the application in the Python Manager to apply changes.
- Use the terminal in aaPanel or the command line in the project manager to run Masonite commands:
- 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).
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.
Step 5: Navigate to Your Project Folder
Still in the super user shell, move to your Masonite project directory:
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:
Gunicorn will act as the WSGI server for your Masonite application.
Step 8: Run Gunicorn
Start your Masonite application using the following command:
-
wsgi:applicationrefers to your Masonite WSGI entry point -
8010is the internal port Gunicorn will listen on
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:
Run Gunicorn again inside the tmux session:
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:
-
Target URL:
http://127.0.0.1:8010 -
Important: Do not add a trailing slash (
/)
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.