How To Install Streamlit on Ubuntu 24.04 LTS
Streamlit has emerged as a game-changer in the realm of data science and machine learning, offering a seamless way to transform Python scripts into interactive web applications. If you’re running Ubuntu 24.04 LTS (Long Term Support), this guide will walk you through the installation process, ensuring you can harness the power of Streamlit for your projects. Ubuntu 24.04 LTS, the latest iteration of this popular Linux distribution, provides a robust and stable environment for development. This article provides a detailed, step-by-step approach to installing Streamlit on Ubuntu 24.04 LTS, covering everything from initial prerequisites to troubleshooting common issues. Whether you’re a seasoned developer or just starting, you’ll find valuable insights here to get Streamlit up and running smoothly.
What is Streamlit?
Streamlit is an open-source Python library that simplifies the creation and deployment of web applications for data science and machine learning. Instead of wrestling with complex front-end frameworks, Streamlit allows you to build interactive applications using pure Python. With Streamlit, you can quickly prototype, test, and deploy your models, making it an invaluable tool for data scientists, machine learning engineers, and researchers.
Key features of Streamlit include:
- Widget Support: Streamlit provides a wide array of widgets like buttons, sliders, and text inputs, enabling users to interact with your applications dynamically.
- Live Updates: Streamlit apps automatically update whenever you modify your code, providing a real-time development experience.
- Easy Deployment: Streamlit Cloud offers a platform to easily deploy your applications, making them accessible to a broader audience.
Prerequisites
Before installing Streamlit, ensure your Ubuntu 24.04 LTS system meets the following prerequisites:
System Requirements:
- Minimum 2GB RAM: Streamlit and its dependencies require sufficient memory to run efficiently.
- Minimum 5GB Storage: Ensure you have enough disk space for the installation and your project files.
Operating System:
- Ubuntu 24.04 LTS: This guide is specifically tailored for Ubuntu 24.04 LTS. While the steps might be similar for other versions, some commands may vary.
Python:
- Supported Versions: Streamlit supports Python versions 3.9 to 3.13.
- Check Python Version: Open a terminal and run
python3 --version
to verify your installed Python version.
Package Managers:
- pip (Recommended): Python’s package installer, pip, is essential for installing Streamlit and its dependencies.
- Install pip: If pip is not already installed, use the following command:
sudo apt update && sudo apt install python3-pip -y
.
Python Environment Manager:
- venv (Recommended): Using a virtual environment is highly recommended to isolate project dependencies.
Code Editor (Optional):
- VS Code: While not mandatory, a good code editor like VS Code can significantly enhance your development experience.
Step-by-Step Installation Guide
Follow these steps to install Streamlit on your Ubuntu 24.04 LTS system:
Updating the System
Start by updating your system’s package lists to ensure you have the latest versions of all software. This also ensures the dependencies required for Streamlit are the newest available.
- Open a terminal.
- Run the following commands:
sudo apt update sudo apt upgrade -y
Creating a Virtual Environment
A virtual environment isolates project dependencies, preventing conflicts between different projects. Create a virtual environment for your Streamlit project to keep its dependencies separate from other Python projects.
- Navigate to your project directory:
cd myproject
- Create the virtual environment:
python3 -m venv .venv
Activating the Virtual Environment
Activate the virtual environment to ensure all subsequent installations are contained within it. This step is crucial for isolating your Streamlit project’s dependencies.
- Activate the environment:
source .venv/bin/activate
- Verify activation by checking for the environment name in parentheses before the terminal prompt, like this:
(.venv) user@ubuntu:~/myproject$
.
Installing Streamlit
With the virtual environment activated, install Streamlit using pip. This command fetches the latest version of Streamlit and installs it along with its dependencies.
- Install Streamlit using pip:
pip install streamlit
- Alternatively, you can use:
python -m pip install streamlit
Verifying the Installation
Verify that Streamlit is installed correctly by running the Streamlit Hello app. This built-in app confirms that Streamlit is functioning as expected.
- Run the Streamlit Hello app:
streamlit hello
- Alternatively, use:
python -m streamlit hello
- The Streamlit Hello app should open in a new tab in your web browser.
Creating a Simple “Hello World” App
Create a basic Streamlit app to ensure you can run your own code. This “Hello World” app is a simple way to confirm that Streamlit is correctly set up and running.
- Create a file named
app.py
:nano app.py
- Add the following code:
import streamlit as st st.write("Hello world")
- Save the file and exit the editor.
Running the “Hello World” App
Run your “Hello World” app using the streamlit run
command. This starts the Streamlit server and displays your app in a web browser.
- Run the app:
streamlit run app.py
- Alternatively, use:
python -m streamlit run app.py
- Access the app in your browser using the URL provided in the terminal (usually
http://localhost:8501
). - To stop the server, press
Ctrl+C
in the terminal.
Deactivating the Virtual Environment
Once you’re done working on your Streamlit project, deactivate the virtual environment. Deactivating ensures that your system’s global Python environment remains unaffected.
- Deactivate the environment:
deactivate
Running Streamlit Apps
Once Streamlit is installed, you can customize how your apps run using various command-line options.
Running on a Specific Port
To run your Streamlit app on a specific port, use the --server.port
option.
streamlit run app.py --server.port 80
Running in the Background Using Tmux
To keep your Streamlit app running even after you close your terminal, use Tmux, a terminal multiplexer.
- Install Tmux:
sudo apt install tmux
- Create a new Tmux session:
tmux new -s StreamlitSession
- Run your Streamlit app within the Tmux session.
- Detach from the session: Press
Ctrl+b
, thend
. - Reattach to the session:
tmux attach -t StreamlitSession
Troubleshooting Common Issues
Encountering issues during installation or while running Streamlit apps is not uncommon. Here are some common problems and their solutions:
“Streamlit command not found”
- Ensure Streamlit is Installed: Verify that Streamlit is installed in the active virtual environment.
- Check Virtual Environment: Make sure the virtual environment is activated.
- Update pip: Ensure pip is up to date:
pip install --upgrade pip
Port Already in Use
- Identify the Process: Find the process using the port (default is 8501):
sudo netstat -tulnp | grep :8501
- Kill the Process: Terminate the process:
sudo kill <PID>
- Run on a Different Port: Alternatively, run Streamlit on a different port using the
--server.port
option.
Browser Not Opening Automatically
- Manually Open the URL: If the browser doesn’t open automatically, manually enter the URL provided in the terminal (usually
http://localhost:8501
) into your browser.
Import Errors
- Install Dependencies: Ensure all required dependencies are installed:
pip install -r requirements.txt
Alternative Installation Methods
Besides using pip and venv, Streamlit can also be installed using other methods.
Anaconda
Anaconda is a popular Python distribution that comes with its own package manager, conda. You can install Streamlit using Anaconda by following these steps:
- Create a new conda environment:
conda create --name streamlit_env python=3.9
- Activate the environment:
conda activate streamlit_env
- Install Streamlit:
conda install -c conda-forge streamlit
- Run the Streamlit Hello app to verify the installation.
Streamlit Community Cloud
Streamlit Community Cloud offers a hassle-free way to deploy and share your Streamlit apps without worrying about local installations. It integrates seamlessly with GitHub Codespaces, allowing you to develop and deploy your apps directly from the cloud.
Streamlit in Snowflake
Streamlit in Snowflake enables you to code and run Streamlit apps directly within the Snowflake environment, providing a secure and controlled environment for your data applications.
Congratulations! You have successfully installed Streamlit. Thanks for using this tutorial for installing the Streamlit on Ubuntu 24.04 LTS system. For additional or useful information, we recommend you check the official Streamlit website.