Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created January 20, 2025 02:13
Show Gist options
  • Save codelynx/83fe36c839aa48988ec0f2518614db0b to your computer and use it in GitHub Desktop.
Save codelynx/83fe36c839aa48988ec0f2518614db0b to your computer and use it in GitHub Desktop.
#!/bin/bash
# ================================
# Script: setup.sh
# Description: Installs essential developer tools and Swift compiler on Ubuntu 24.04.1 LTS ARM64.
# Usage: sudo bash setup.sh
# ================================
# Exit immediately if a command exits with a non-zero status
set -e
# -------------------------------------------------
# Step 0: Define Variables
# -------------------------------------------------
OS_TYPE=$(uname -s)
ARCH_TYPE=$(uname -m)
SWIFT_VERSION="6.0.3"
SWIFT_TARBALL_NAME="swift-${SWIFT_VERSION}-RELEASE-ubuntu24.04-aarch64.tar.gz"
SWIFT_URL="https://download.swift.org/swift-${SWIFT_VERSION}-release/ubuntu2404-aarch64/swift-${SWIFT_VERSION}-RELEASE/${SWIFT_TARBALL_NAME}"
SWIFT_INSTALL_DIR="/opt/swift"
SWIFT_FOLDER="swift-${SWIFT_VERSION}-RELEASE-ubuntu24.04-aarch64"
SWIFT_FINAL_PATH="${SWIFT_INSTALL_DIR}/${SWIFT_FOLDER}/usr/bin/swift"
PROFILE_FILE="$HOME/.bashrc"
echo "Checking system and architecture..."
# Check if OS is Linux
if [ "$OS_TYPE" != "Linux" ]; then
echo "[x] This script only supports Linux. Detected OS: $OS_TYPE" ; exit 1
fi
# Check if architecture is ARM64 (aarch64)
# Note: some systems might report "arm64" or "aarch64" interchangeably
if [ "$ARCH_TYPE" != "aarch64" ] && [ "$ARCH_TYPE" != "arm64" ]; then
echo "[x] This script only supports ARM64 (aarch64) architecture. Detected ARCH: $ARCH_TYPE" ; exit 1
fi
echo "system: $OS_TYPE ($ARCH_TYPE)"
# -------------------------------------------------
# Step 1: Update Package Lists
# -------------------------------------------------
echo "πŸ”„ Updating package lists..."
sudo apt-get update -y
# -------------------------------------------------
# Step 2: Install Essential Developer Tools
# -------------------------------------------------
echo "πŸ› οΈ Installing essential developer tools..."
ESSENTIAL_TOOLS=(
build-essential
git
curl
wget
vim
nano
htop
tmux
tree
pkg-config
cmake
make
gcc
g++
clang
lldb
libicu-dev
libcurl4-openssl-dev
libssl-dev
libxml2-dev
)
for TOOL in "${ESSENTIAL_TOOLS[@]}"; do
if dpkg -s "$TOOL" &>/dev/null; then
echo "[o] $TOOL is already installed."
else
echo "[x] $TOOL is not installed. Installing..."
sudo apt-get install -y "$TOOL" && echo "[o] $TOOL has been installed." || echo "[x] Failed to install $TOOL."
fi
done
# -------------------------------------------------
# Step 3: Install Swift Compiler
# -------------------------------------------------
echo "πŸ“¦ Installing Swift Compiler..."
if command -v swift &>/dev/null; then
echo "[o] Swift is already installed."
swift --version
else
echo "[x] Swift is not installed. Proceeding with installation."
# Navigate to /tmp directory
cd /tmp
echo "[o] Downloading Swift from $SWIFT_URL..."
wget "$SWIFT_URL" -O swift.tar.gz && echo "[o] Downloaded Swift tarball successfully." || {
echo "[x] Failed to download Swift tarball."
exit 1
}
# Create installation directory if it doesn't exist
sudo mkdir -p "$SWIFT_INSTALL_DIR"
echo "[o] Extracting Swift tarball to $SWIFT_INSTALL_DIR..."
sudo tar xzf swift.tar.gz -C "$SWIFT_INSTALL_DIR" && echo "[o] Extracted Swift tarball successfully." || {
echo "[x] Failed to extract Swift tarball."
exit 1
}
echo "[o] Cleaning up downloaded files..."
rm swift.tar.gz && echo "[o] Cleanup successful." || {
echo "[x] Failed to clean up."
exit 1
}
# -------------------------------------------------
# Check Actual Swift Binary for Architecture
# -------------------------------------------------
if [[ -f "$SWIFT_FINAL_PATH" ]]; then
echo "[o] Verifying Swift binary architecture..."
# 'file' the actual 'swift' binary:
SWIFT_BIN_INFO=$(file "$SWIFT_FINAL_PATH")
echo "$SWIFT_BIN_INFO"
if echo "$SWIFT_BIN_INFO" | grep -q "aarch64"; then
echo "[o] Swift binary is ARM64-compatible."
# Create or update symbolic link
echo "[o] Creating symbolic link for Swift..."
sudo ln -sf "$SWIFT_FINAL_PATH" /usr/local/bin/swift && echo "[o] Symbolic link created successfully." || {
echo "[x] Failed to create symbolic link."
exit 1
}
# Verify Swift installation
if command -v swift &>/dev/null; then
echo "[o] Swift has been successfully installed."
swift --version
else
echo "[x] Swift installation failed."
exit 1
fi
else
echo "[x] The Swift binary is not ARM64; removing it..."
sudo rm -rf "$SWIFT_INSTALL_DIR/$SWIFT_FOLDER"
exit 1
fi
else
echo "[x] Swift binary not found at $SWIFT_FINAL_PATH"
exit 1
fi
fi
# -------------------------------------------------
# Step 4: Add Swift to PATH Permanently (Optional)
# -------------------------------------------------
echo "πŸ”§ Configuring environment variables..."
if grep -q "export PATH=${SWIFT_INSTALL_DIR}/${SWIFT_FOLDER}/usr/bin:\$PATH" "$PROFILE_FILE"; then
echo "[o] Swift path is already configured in $PROFILE_FILE."
else
echo "export PATH=${SWIFT_INSTALL_DIR}/${SWIFT_FOLDER}/usr/bin:\$PATH" >> "$PROFILE_FILE"
echo "[o] Swift path has been added to $PROFILE_FILE."
source "$PROFILE_FILE" && echo "[o] Reloaded $PROFILE_FILE." || echo "[x] Failed to reload $PROFILE_FILE."
fi
# -------------------------------------------------
# Step 5: (Optional) Install Node.js
# -------------------------------------------------
echo "πŸ“¦ Installing Node.js..."
if command -v node &>/dev/null; then
echo "[o] Node.js is already installed."
else
echo "[x] Node.js is not installed. Installing..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - && echo "[o] Node.js repository setup successfully." || {
echo "[x] Failed to set up Node.js repository."
exit 1
}
sudo apt-get install -y nodejs && echo "[o] Node.js has been successfully installed." || {
echo "[x] Failed to install Node.js."
exit 1
}
# Verify Node.js installation
if command -v node &>/dev/null; then
echo "[o] Node.js version: $(node --version)"
else
echo "[x] Node.js installation verification failed."
fi
fi
echo "βœ… Developer essential tools and Swift installation complete."
# Show installed versions
echo "πŸ” Installed Tools and Versions:"
echo "---------------------------------"
echo -n "Swift: " && swift --version
echo -n "Clang: " && clang --version | head -n1
echo -n "LLDB: " && lldb --version | head -n1
echo -n "Git: " && git --version
echo -n "Node.js: " && node --version || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment