How To Install Streamlit on Rocky Linux 9
In this tutorial, we will show you how to install Streamlit on Rocky Linux 9. Streamlit is an open-source Python framework that makes it incredibly easy to build and share beautiful, custom web applications for machine learning and data science. It allows you to transform data scripts into shareable web apps in minutes, without needing any front-end development experience. For data scientists and machine learning engineers, Streamlit bridges the gap between model development and deployment. This comprehensive guide will walk you through the process of installing Streamlit on Rocky Linux 9.
Rocky Linux 9, known for its stability and enterprise-grade performance, is an excellent platform for deploying data science tools. By following this tutorial, you’ll have Streamlit up and running, ready to create interactive data apps. Let’s dive in!
Prerequisites
Before you begin the installation process, ensure you have the following prerequisites in place:
- Rocky Linux 9 Server: A running instance of Rocky Linux 9. You can use a virtual machine or a physical server.
- User Account with Sudo Privileges: A non-root user account with
sudo
privileges to execute administrative commands. - Internet Connection: A stable internet connection to download packages and dependencies.
Having these prerequisites ensures a smooth and successful installation of Streamlit. Now, let’s proceed with the installation of Python and Pip.
Installing Python and Pip
Streamlit requires Python 3.9 or higher. Rocky Linux 9 usually comes with Python pre-installed, but it’s essential to verify the version and install Pip, the Python package manager.
Checking Python Version
First, open a terminal and check if Python is already installed by running the following command:
python3 --version
If Python is installed, the version number will be displayed. If not, or if the version is below 3.9, proceed with the installation.
Installing Python 3.9 or Later
Use the DNF package manager to install Python 3. DNF (Dandified Yum) is the default package manager in Rocky Linux, making software installation straightforward.
sudo dnf install python3
This command installs Python 3 and its core components. After the installation, verify the version again:
python3 --version
Make sure the version is 3.9 or greater to ensure compatibility with Streamlit.
Installing Pip
Pip is a package management system used to install and manage software packages written in Python. It simplifies the process of installing, upgrading, and removing Python packages. Install Pip using the following command:
sudo dnf install python3-pip
This command installs Pip for Python 3. Once the installation is complete, verify it by checking the Pip version:
pip3 --version
The output should display the Pip version number, confirming successful installation. With Python and Pip installed, the next step is to create a virtual environment.
Creating a Virtual Environment (Recommended)
A virtual environment is a self-contained directory that isolates Python package installations for different projects. It prevents conflicts between package versions and ensures that each project has its required dependencies [1]. Using a virtual environment is highly recommended for Streamlit projects.
Installing the venv
Module
The venv
module is used to create virtual environments. Install it using DNF:
sudo dnf install python3-venv
This command installs the venv
module, allowing you to create isolated environments for your Streamlit applications.
Creating a New Virtual Environment
Navigate to your desired project directory and create a new virtual environment. For example, to create an environment named streamlit_env
, use the following command:
python3 -m venv streamlit_env
This command creates a directory named streamlit_env
containing the necessary files for the virtual environment. Now, activate the environment.
Activating the Virtual Environment
To activate the virtual environment, use the source
command:
source streamlit_env/bin/activate
Once activated, the environment name (streamlit_env
) will appear in parentheses before your terminal prompt. This indicates that you are working within the virtual environment. To deactivate the environment when you’re finished, simply type:
deactivate
Using a virtual environment keeps your Streamlit project isolated and organized. Next, you’ll install Streamlit itself.
Installing Streamlit
With the virtual environment activated, you can now install Streamlit using Pip. Ensure that your virtual environment is active before running the installation command.
Using Pip to Install Streamlit
Run the following command to install Streamlit:
pip install streamlit
Pip will download and install Streamlit and its dependencies. The installation process may take a few minutes, depending on your internet connection. Once the installation is complete, you’ll see a message confirming the successful installation.
Addressing Potential Installation Issues
Sometimes, installation issues may arise due to various reasons. Here are a few common problems and their solutions:
- Permissions Errors: If you encounter permission errors, it might be due to insufficient privileges. Ensure you are using
sudo
where necessary or that your user has the required permissions. - Dependency Conflicts: Dependency conflicts can occur if there are conflicting versions of required packages. Using a virtual environment helps mitigate this issue. If conflicts persist, try upgrading Pip or individual packages.
- Firewall Issues: Firewall settings might prevent Pip from downloading packages. Ensure that your firewall allows outbound connections on port 443 (HTTPS).
By addressing these potential issues, you can ensure a smooth Streamlit installation.
Verifying the Streamlit Installation
After installing Streamlit, it’s essential to verify that the installation was successful. This ensures that Streamlit is correctly installed and ready to use.
Checking the Streamlit Version
To check the Streamlit version, run the following command:
streamlit --version
This command displays the installed Streamlit version number. If the command is not recognized, ensure that your virtual environment is activated and that Streamlit is correctly installed.
Running the Streamlit Hello App
Streamlit comes with a “Hello” app that you can run to verify the installation. Use the following command:
streamlit hello
Alternatively, you can use the long-form command:
python -m streamlit hello
This command launches the Streamlit Hello app in your default web browser. If the app runs successfully, it confirms that Streamlit is correctly installed and functioning.
Accessing the Streamlit App in a Web Browser
Once the Streamlit app is running, it will provide a local URL in the terminal (e.g., http://localhost:8501
). Open this URL in your web browser to access the app. If you are running Streamlit on a remote server, you might need to configure firewall settings to allow access to the specified port (usually 8501).
Creating a Simple Streamlit App
Now that Streamlit is installed and verified, let’s create a simple Streamlit application to understand how it works. This will give you a basic understanding of Streamlit’s structure and capabilities.
Creating a New Directory
First, create a new directory for your Streamlit app and navigate into it:
mkdir streamlit_app && cd streamlit_app
This command creates a new directory named streamlit_app
and changes your current directory to it.
Creating a Python File
Create a Python file (e.g., app.py
) where you’ll write your Streamlit app code:
nano app.py
This command opens the nano
text editor, allowing you to create and edit the app.py
file.
Writing the Streamlit App Code
Inside the app.py
file, add the following code:
import streamlit as st
st.title('My First Streamlit App')
st.write('Hello, world!')
This simple app imports the Streamlit library, sets a title, and displays “Hello, world!” [1, 4]. Save the app.py
file by pressing Ctrl+X
, then Y
, and finally Enter
.
Running the Streamlit App
With the app.py
file created, you can now run your Streamlit app. This will launch the app in your web browser.
Using the streamlit run
Command
Run the Streamlit app using the following command:
streamlit run app.py
Alternatively, you can use the long-form command:
python -m streamlit run app.py
Streamlit will start a local server and open the app in your default web browser. You should see the title “My First Streamlit App” and the text “Hello, world!” displayed.
Accessing the App in a Web Browser
If the app doesn’t automatically open in your browser, check the terminal output for the local URL (e.g., http://localhost:8501
) and open it manually. If you are running the app on a remote server, ensure that the necessary ports are open and accessible.
Troubleshooting Common Issues
If you encounter issues running the app, consider the following:
- Port Conflicts: If port 8501 is already in use, Streamlit will attempt to use a different port. Check the terminal output for the correct URL.
- File Permissions: Ensure that the
app.py
file has the necessary read permissions. - Virtual Environment: Make sure that your virtual environment is activated before running the app.
Configuring Streamlit
Streamlit can be configured to customize various aspects of its behavior. The primary method for configuration is through the config.toml
file, which allows you to set global settings for your Streamlit apps.
Understanding the Streamlit Configuration File
The Streamlit configuration file (.streamlit/config.toml
) is located in the .streamlit
directory in your home directory. If the directory or file doesn’t exist, you can create it manually.
Customizing Streamlit Settings
Open the config.toml
file using a text editor:
nano ~/.streamlit/config.toml
Here are some common settings you might want to customize:
- Set Server Address and Port:
[server] address = "0.0.0.0" port = 8080
This sets the server to listen on all available network interfaces and port 8080.
- Configure Theme Options:
[theme] base = "light" # Can be "light" or "dark" primaryColor = "#F63366" secondaryBackgroundColor = "#E14594" textColor = "#262730"
This configures the theme of your Streamlit app.
Applying Configuration Changes
After making changes to the config.toml
file, save the file and restart your Streamlit app for the changes to take effect. Streamlit automatically reloads the configuration file on startup.
Using Streamlit with Data Science Libraries
Streamlit’s power lies in its ability to integrate with popular data science libraries. This allows you to create interactive data visualizations and dashboards with ease.
Installing Data Science Libraries
Install common data science libraries such as Pandas, NumPy, Matplotlib, and Seaborn using Pip:
pip install pandas numpy matplotlib seaborn
These libraries provide powerful tools for data manipulation, analysis, and visualization.
Demonstrating Library Usage
Update your app.py
file with the following code to demonstrate the use of these libraries:
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
st.title('Data Science with Streamlit')
# Create a Pandas DataFrame
data = pd.DataFrame({
'x': np.arange(10),
'y': np.random.randn(10)
})
# Display the DataFrame
st.write("### DataFrame")
st.dataframe(data)
# Create a Matplotlib chart
st.write("### Matplotlib Chart")
fig, ax = plt.subplots()
ax.plot(data['x'], data['y'])
st.pyplot(fig)
This code creates a Pandas DataFrame, displays it using st.dataframe
, and generates a Matplotlib chart, which is then displayed using st.pyplot
. Run the app to see the results.
Deploying Streamlit Apps
Once you’ve created your Streamlit app, you might want to deploy it so that others can access it. There are several deployment options available, each with its own advantages and considerations.
Deployment Options
- Streamlit Community Cloud: Streamlit’s own cloud platform, which offers a simple and free way to deploy Streamlit apps [2, 15].
- Heroku: A popular platform for deploying web applications. It offers a free tier and is relatively easy to set up.
- AWS, Google Cloud, Azure: Cloud platforms that provide a wide range of services, including app deployment. These options offer more control and scalability but require more configuration.
- Docker: Containerization platform that allows you to package your Streamlit app and its dependencies into a container, making it easy to deploy on any system that supports Docker.
Deploying to Streamlit Community Cloud
To deploy to Streamlit Community Cloud, follow these steps:
- Create a GitHub Repository: Upload your Streamlit app code to a public GitHub repository.
- Sign Up for Streamlit Community Cloud: Go to Streamlit Community Cloud and sign up using your GitHub account.
- Deploy the App: Click on “New app” and select your GitHub repository, branch, and the main Python file (e.g.,
app.py
). - Configure Dependencies: Streamlit Cloud automatically detects and installs the required dependencies from your
requirements.txt
file.
Your app will be deployed and accessible via a public URL. This is a straightforward method for sharing your Streamlit apps with others.
Troubleshooting Common Issues
During the installation and usage of Streamlit, you may encounter some common issues. Here are some troubleshooting tips to help you resolve them:
- ModuleNotFoundError: This error occurs when a required Python module is not installed. Ensure that you have installed all the necessary dependencies using Pip.
- Streamlit Command Not Found: If the
streamlit
command is not recognized, ensure that your virtual environment is activated and that Streamlit is correctly installed. - Port Already in Use: If Streamlit fails to start due to a port conflict, try changing the port in the
config.toml
file or closing the application that is using the port. - App Not Displaying Correctly: If your app is not displaying correctly in the browser, clear your browser cache and try again. Also, check the browser’s developer console for any JavaScript errors.
Congratulations! You have successfully installed Streamlit. Thanks for using this tutorial for installing the Streamlit on Rocky Linux 9 system. For additional or useful information, we recommend you check the official Streamlit website.