To install and run a Streamlit app on an Azure Virtual Machine, first ensure the VM has Python 3.7+ installed by connecting via SSH. Then, use pip to install the streamlit package in your desired environment. Finally, launch the application using the streamlit run command while configuring the Azure Network Security Group to allow inbound traffic on port 8501.
Preparing the Azure Virtual Machine
Before diving into the software installation, you must ensure that your Azure Virtual Machine (VM) is properly configured to accept incoming connections. Streamlit applications typically run on port 8501 by default. If this port is blocked by the firewall or network security rules, users will not be able to access your dashboard from their local machines. Start by navigating to the Azure Portal and selecting your virtual machine. Under the "Settings" menu, locate the "Networking" blade. Here, you will see a list of inbound port rules. Click on "Add inbound port rule" and create a new rule with the following specifications:- Source: Any (or specify your IP for tighter security)
- Destination port ranges: 8501
- Protocol: TCP
- Action: Allow
- Name: Allow-Streamlit-Port
Installing Python and Dependencies
Streamlit requires Python 3.7 or higher. Most modern Azure Linux distributions come with Python pre-installed, but it is best practice to verify the version and install any missing dependencies. Connect to your VM using SSH from your terminal:ssh username@your-vm-ip-address
Once connected, check your Python version:
python3 --version
If Python is not installed or is outdated, install it using your distribution's package manager. For Ubuntu/Debian systems, use:
sudo apt update
sudo apt install python3-pip python3-dev
For CentOS/RHEL systems, use:
sudo yum install python3 pip
After ensuring Python is available, it is highly recommended to use a virtual environment to isolate your project dependencies. This prevents conflicts with system-wide packages and makes deployment cleaner. Create a virtual environment named env:
python3 -m venv env
source env/bin/activate
With the environment activated, you can proceed to install Streamlit itself. This process is straightforward and relies on pip, the standard package installer for Python. You can refer to our guide on running applications installed via pip for more details on managing executable scripts within virtual environments.
Installing Streamlit
With your virtual environment active, install the Streamlit library by running the following command:pip install streamlit
This command downloads the latest stable version of Streamlit along with all its core dependencies, such as Flask, Tornado, and NumPy. The installation may take a few minutes depending on your internet connection speed.
If your Streamlit app relies on specific data science libraries like Pandas, Matplotlib, or Scikit-learn, install them now while the virtual environment is still active:
pip install pandas matplotlib scikit-learn
Ensuring these dependencies are present before launching the app prevents runtime errors related to missing modules. It is also good practice to freeze your requirements into a requirements.txt file for reproducibility:
pip freeze > requirements.txt
Running the Streamlit Application
Now that the software is installed, you need to serve your application. Place your main Streamlit script, typically namedapp.py, in a directory on the VM. Navigate to that directory in your SSH session.
To start the server, use the streamlit run command followed by the path to your script:
streamlit run app.py
By default, Streamlit binds to localhost (127.0.0.1), which means it is only accessible from within the VM itself. To make it accessible from the outside world, you must explicitly bind it to the public interface. Use the --server.address flag:
streamlit run app.py --server.address 0.0.0.0
The output will display the local URL (http://localhost:8501) and the public URL if detected. Copy the public IP address provided in the terminal output. Open your web browser and navigate to http://your-vm-ip:8501. Your Streamlit dashboard should now be live and interactive.
Managing Long-Running Processes
While the command above works for testing, closing your SSH session will terminate the Streamlit process. To keep the app running in the background, you can use tools likenohup, screen, or systemd services. Using nohup is a simple method for basic setups:
nohup streamlit run app.py --server.address 0.0.0.0 &
This detaches the process from the terminal, allowing it to continue running even after you log out. For production-grade deployments, consider setting up a systemd service unit file to manage startup, shutdown, and automatic restarts on failure.
Troubleshooting Common Issues
If you encounter issues accessing the app, verify the following:- Firewall Rules: Ensure port 8501 is open in both the Azure NSG and the OS-level firewall (e.g.,
ufworfirewalld). - Virtual Environment: Confirm that the virtual environment is activated when running the
streamlit runcommand. Installed packages inside an inactive venv will not be found. - Permissions: Ensure the user running the command has read access to the
app.pyfile.
Frequently Asked Questions
Can I run multiple Streamlit apps on the same VM?
Yes, you can run multiple apps simultaneously by assigning each app a different port number. Use the--server.port flag when launching each instance, such as --server.port 8502 for the second app, and ensure those ports are also allowed in your Azure Network Security Group.
How do I secure my Streamlit app with HTTPS?
Streamlit supports SSL certificates directly. You can enable HTTPS by providing the paths to your certificate and key files using the--server.sslCertFile and --server.sslKeyFile flags during the streamlit run command. Alternatively, place a reverse proxy like Nginx in front of Streamlit to handle SSL termination.
What should I do if I get a "ModuleNotFoundError" after installation?
This error usually indicates that the virtual environment was not activated when you ran the Streamlit command. Deactivate the current shell, activate your virtual environment again usingsource env/bin/activate, and then re-run the streamlit run command to ensure it uses the correct isolated Python environment.