PLEX - Install Script On Ubuntu 24.4
From IT-Arts.net
install_plex.sh
#!/bin/bash
# --------------------------------------------------------
# Advanced Plex Media Server Installation Script for Ubuntu 24.04
# This script installs Plex, sets up the server, configures a firewall (UFW),
# and creates a dedicated plex user to run Plex Media Server.
# --------------------------------------------------------
# Function to display messages
function info() {
echo -e "\033[1;32m[INFO]\033[0m $1"
}
function warn() {
echo -e "\033[1;33m[WARN]\033[0m $1"
}
function error() {
echo -e "\033[1;31m[ERROR]\033[0m $1"
}
# --------------------------------------------------------
# Step 1: Update System and Install Dependencies
# --------------------------------------------------------
info "Updating system packages..."
sudo apt update -y
sudo apt upgrade -y
info "Installing required dependencies (curl, apt-transport-https, ca-certificates, and UFW)..."
sudo apt install -y curl apt-transport-https ca-certificates ufw
# --------------------------------------------------------
# Step 2: Create a Plex User (Optional)
# --------------------------------------------------------
info "Creating a dedicated Plex user for security purposes..."
# Check if the user 'plex' exists, if not create it
if ! id -u plex &>/dev/null; then
sudo useradd -r -s /usr/sbin/nologin plex
info "Plex user created successfully."
else
info "Plex user already exists, skipping creation."
fi
# --------------------------------------------------------
# Step 3: Download and Install Plex Media Server
# --------------------------------------------------------
info "Downloading the latest Plex Media Server package..."
PlexDownloadURL="https://downloads.plex.tv/plex-media-server-new/latest/debian/plexmediaserver_1.29.1.5841-7d5b2ff69_amd64.deb"
PlexDebPackage="/tmp/plexmediaserver.deb"
# Download the Plex server package
curl -fsSL "$PlexDownloadURL" -o "$PlexDebPackage"
# Check if download was successful
if [[ ! -f "$PlexDebPackage" ]]; then
error "Failed to download Plex Media Server. Please check the URL or your internet connection."
exit 1
fi
info "Installing Plex Media Server..."
# Install the downloaded .deb package
sudo dpkg -i "$PlexDebPackage"
# Fix any missing dependencies
info "Fixing missing dependencies..."
sudo apt --fix-broken install -y
# --------------------------------------------------------
# Step 4: Configure Plex Media Server as a System Service
# --------------------------------------------------------
info "Enabling Plex Media Server to start at boot..."
sudo systemctl enable plexmediaserver
info "Starting Plex Media Server..."
sudo systemctl start plexmediaserver
# --------------------------------------------------------
# Step 5: Configure Firewall (UFW) for Plex
# --------------------------------------------------------
info "Configuring UFW firewall to allow Plex traffic (port 32400)..."
# Allow Plex port through firewall
sudo ufw allow 32400/tcp
# Enable UFW if it's not enabled
if ! sudo ufw status | grep -q "active"; then
info "UFW is not enabled. Enabling UFW now..."
sudo ufw enable
else
info "UFW is already active."
fi
# Reload UFW to apply the changes
sudo ufw reload
# --------------------------------------------------------
# Step 6: Clean Up
# --------------------------------------------------------
info "Cleaning up installation files..."
# Remove the downloaded .deb package to save space
rm -f "$PlexDebPackage"
# --------------------------------------------------------
# Step 7: Post-Installation Configuration
# --------------------------------------------------------
info "Plex Media Server installation complete!"
info "You can now access Plex through your web browser at: http://localhost:32400/web"
# --------------------------------------------------------
# Step 8: Provide Additional Information to the User
# --------------------------------------------------------
info "For first-time setup, go to the Plex web interface and sign in to your Plex account."
info "You can also access your Plex Media Server from any device in your network using the IP address of your server."
info "For remote access, ensure that your router has port forwarding configured for port 32400."
# --------------------------------------------------------
# Optional: Set Permissions (if needed)
# --------------------------------------------------------
info "If you need to grant specific permissions to your media folders for Plex, use the following command:"
info "sudo chown -R plex:plex /path/to/your/media/folder"
# --------------------------------------------------------
# Final Notes
# --------------------------------------------------------
info "You can now enjoy your Plex Media Server!"
info "For further configuration, visit: https://support.plex.tv/."
# End of script
exit 0
