Introduction
Streamlit is an open-source python library designed to create custom web applications for machine learning and data science. It is one of the fastest ways to design, build, and deploy data applications. This article explains how to run a Streamlit-based web application on a CentOS 8 Server.
1. Install Prerequisites
- Deploy a Rcs CentOS 8 VPS Instance.
- Login as root.
- Update the CentOS 8 Server.
Update Python
The default CentOS repositories contain Python 3.6.8 only, so we will use Python source code to install the latest version of Python3.
Install the required dependencies to compile the Python source code.
# yum groupinstall 'development tools' -y && yum install wget openssl-devel bzip2-devel libffi-devel xz-devel -y
Download the latest Python3 source code.
# wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
Extract the downloaded python source code.
# tar xvf Python-3.9.6.tgz
Compile the python source code.
# cd Python-3.9.6 && ./configure --enable-optimizations
Install Python 3.9.6.
# make altinstall
Set Python 3.9.6 as default version.
# alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 1 && alternatives --set python3 /usr/local/bin/python3.9 && echo "2" | alternatives --config python
Update pip.
# /usr/local/bin/python3.9 -m pip install --upgrade pip
Set pip from Python 3.9.6 as default pip.
# alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.9 1 && alternatives --set pip /usr/local/bin/pip3.9
Check the current version of Python and pip.
# python -V && pip -V
Change to root directory.
# cd
2. Install Streamlit
Install Streamlit using Python pip.
# pip install streamlit
3. Run Streamlit on CentOS 8
Option 1: Use the Default Port
By default, Streamlit runs on port 8501, so you need to allow port 8501 on the firewall.
# firewall-cmd --permanent --add-port 8501/tcp
Reload firewall-cmd.
# firewall-cmd --reload
When you run Streamlit on a normal SSH Session, the Streamlit process will close once you exit the SSH Session. To run Streamlit when you leave the SSH session, use tmux, a terminal multiplexer. Using a terminal multiplexer will allow you to run the Streamlit process in the background. To create a tmux session, run:
# tmux new -s StreamlitSession
You can change
StreamlitSession
to any session name you prefer. Please see How to Install and Use Tmux for more information.Once you are on the Terminal Multiplexer, you can now run your main python script. Make sure that your python script is in the /root/ directory, then run:
# streamlit run main.py
Change
main.py
to your python script file name.When you run Streamlit for the first time, you will be prompted to enter your email address. If you want to receive Streamlit updates, kindly enter your email address, or you can just press Enter to skip it.
To view your deployed web application, visit
http://server_ipaddress:8501
Option 2: Run Streamlit on HTTP Port
To deploy your web application on HTTP Port (80), allow http port (80) on the firewall first. Run:
# firewall-cmd --permanent --add-service=http
Reload firewall-cmd:
# firewall-cmd --reload
Run your python script with Streamlit:
# streamlit run main.py --server.port 80
Change
main.py
to your python script file name.To view your deployed web application, visit
http://server_ipaddress
Troubleshooting
If you have encountered some issues or if you want to learn more, please visit the Streamlit documentation.