Last active
August 19, 2025 13:16
-
-
Save thatandyrose/571b2fea9f3695eb29cb9006cff07d7b to your computer and use it in GitHub Desktop.
rails setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# =========================================== | |
# Rails Development Environment Setup Script | |
# =========================================== | |
# This script sets up a complete development environment for the Rails application | |
# Designed for Ubuntu 22.04 LTS (but should work on 20.04+ as well) | |
# Usage: chmod +x setup_dev_environment.sh && ./setup_dev_environment.sh | |
set -euo pipefail # Exit on error, undefined vars, pipe failures | |
IFS=$'\n\t' # Secure Internal Field Separator | |
# Logging function | |
log() { | |
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a /tmp/setup.log | |
} | |
error() { | |
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $1" | tee -a /tmp/setup.log | |
} | |
warning() { | |
echo "[$(date +'%Y-%m-%d %H:%M:%S')] WARNING: $1" | tee -a /tmp/setup.log | |
} | |
# Function to check if command exists | |
command_exists() { | |
command -v "$1" >/dev/null 2>&1 | |
} | |
# Function to detect init system | |
detect_init_system() { | |
if ps -p 1 -o comm= | grep -q "systemd"; then | |
echo "systemd" | |
elif ps -p 1 -o comm= | grep -q "pod-daemon\|docker"; then | |
echo "docker" | |
else | |
echo "other" | |
fi | |
} | |
# Function to start service based on init system | |
start_service() { | |
local service_name="$1" | |
local init_system="$2" | |
case "$init_system" in | |
"systemd") | |
sudo systemctl start "$service_name" 2>/dev/null || warning "Failed to start $service_name with systemctl" | |
sudo systemctl enable "$service_name" 2>/dev/null || warning "Failed to enable $service_name with systemctl" | |
;; | |
"docker") | |
sudo service "$service_name" start 2>/dev/null || warning "Failed to start $service_name with service command" | |
;; | |
*) | |
warning "Unknown init system, cannot start $service_name automatically" | |
;; | |
esac | |
} | |
# Function to add environment variables to shell profile | |
add_to_profile() { | |
local var="$1" | |
local profile_file="$HOME/.bashrc" | |
if ! grep -q "$var" "$profile_file"; then | |
echo "$var" >> "$profile_file" | |
log "Added $var to $profile_file" | |
fi | |
} | |
log "Starting Rails development environment setup..." | |
log "Log file: /tmp/setup.log" | |
# =========================================== | |
# 1. UPDATE SYSTEM AND INSTALL BASIC PACKAGES | |
# =========================================== | |
log "Updating system packages..." | |
sudo apt-get update -y | |
sudo apt-get upgrade -y | |
log "Installing basic development tools..." | |
sudo apt-get install -y \ | |
curl \ | |
wget \ | |
git \ | |
unzip \ | |
build-essential \ | |
checkinstall \ | |
libx11-dev \ | |
libxext-dev \ | |
zlib1g-dev \ | |
libpng-dev \ | |
libjpeg-dev \ | |
libfreetype6-dev \ | |
libxml2-dev \ | |
libssl-dev \ | |
libreadline-dev \ | |
libyaml-dev \ | |
libsqlite3-dev \ | |
sqlite3 \ | |
libxml2-dev \ | |
libxslt1-dev \ | |
libcurl4-openssl-dev \ | |
libffi-dev \ | |
software-properties-common \ | |
gnupg \ | |
lsb-release \ | |
ca-certificates \ | |
apt-transport-https | |
# =========================================== | |
# 2. INSTALL POSTGRESQL | |
# =========================================== | |
log "Installing PostgreSQL..." | |
sudo apt-get install -y postgresql postgresql-contrib libpq-dev | |
# Detect init system and start PostgreSQL service | |
INIT_SYSTEM=$(detect_init_system) | |
log "Detected init system: $INIT_SYSTEM" | |
start_service "postgresql" "$INIT_SYSTEM" | |
# Create database user | |
log "Setting up PostgreSQL user..." | |
sudo -u postgres psql -c "CREATE USER vagrant WITH PASSWORD 'vagrant';" || warning "PostgreSQL user 'vagrant' may already exist" | |
sudo -u postgres psql -c "ALTER USER vagrant CREATEDB;" || warning "Failed to grant CREATEDB to vagrant user" | |
sudo -u postgres psql -c "ALTER USER vagrant WITH SUPERUSER;" || warning "Failed to grant SUPERUSER to vagrant user" | |
# Add database environment variables | |
add_to_profile "export DATABASE_USERNAME='vagrant'" | |
add_to_profile "export DATABASE_PASSWORD='vagrant'" | |
add_to_profile "export DATABASE_HOST='localhost'" | |
add_to_profile "export DATABASE_PORT='5432'" | |
# =========================================== | |
# 3. INSTALL REDIS | |
# =========================================== | |
log "Installing Redis..." | |
sudo apt-get install -y redis-server | |
# Start and enable Redis service | |
start_service "redis-server" "$INIT_SYSTEM" | |
# Configure Redis to start automatically (only for systemd) | |
if [ "$INIT_SYSTEM" = "systemd" ]; then | |
sudo systemctl daemon-reload 2>/dev/null || warning "Failed to reload systemd daemon" | |
fi | |
# =========================================== | |
# 4. INSTALL NODE.JS | |
# =========================================== | |
log "Installing Node.js via NodeSource..." | |
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - | |
sudo apt-get install -y nodejs | |
# =========================================== | |
# 5. INSTALL RUBY VIA RBENV | |
# =========================================== | |
log "Installing rbenv and Ruby..." | |
# Install rbenv | |
if ! command_exists rbenv; then | |
git clone https://github.com/rbenv/rbenv.git ~/.rbenv | |
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build | |
# Add rbenv to PATH | |
add_to_profile 'export PATH="$HOME/.rbenv/bin:$PATH"' | |
add_to_profile 'eval "$(rbenv init -)"' | |
# Initialize rbenv in current session | |
export PATH="$HOME/.rbenv/bin:$PATH" | |
eval "$(rbenv init -)" | |
fi | |
# Install Ruby 3.1.3 | |
log "Installing Ruby 3.1.3..." | |
rbenv install 3.1.3 || warning "Ruby 3.1.3 may already be installed" | |
rbenv global 3.1.3 | |
# Install bundler | |
log "Installing bundler..." | |
gem install bundler | |
rbenv rehash | |
# =========================================== | |
# 6. INSTALL IMAGEMAGICK | |
# =========================================== | |
log "Installing ImageMagick..." | |
sudo apt-get install -y imagemagick libmagickwand-dev | |
# Fix ImageMagick policy for PDF processing (common Rails issue) | |
log "Configuring ImageMagick policy..." | |
if [ -f /etc/ImageMagick-6/policy.xml ]; then | |
sudo mv /etc/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml.backup | |
fi | |
# =========================================== | |
# 7. INSTALL HEROKU CLI | |
# =========================================== | |
log "Installing Heroku CLI..." | |
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh | |
# =========================================== | |
# 9. APPLICATION-SPECIFIC SETUP | |
# =========================================== | |
log "Setting up application-specific environment..." | |
# Add application environment variables | |
add_to_profile "export ADMIN_EMAIL='[email protected]'" | |
add_to_profile "export REPLIES_ADMIN_EMAIL='[email protected]'" | |
add_to_profile "export PORT='3000'" | |
add_to_profile "export RAILS_ENV='test'" | |
# =========================================== | |
# 10. SETUP PROJECT DEPENDENCIES | |
# =========================================== | |
if [ -f "Gemfile" ]; then | |
log "Installing Ruby gems..." | |
# Source the updated profile to get rbenv | |
source ~/.bashrc | |
bundle install || warning "Bundle install failed - may need to run manually after setup" | |
fi | |
# =========================================== | |
# 11. DATABASE SETUP | |
# =========================================== | |
if [ -f "config/database.yml" ]; then | |
log "Setting up database..." | |
# Source the updated profile | |
source ~/.bashrc | |
# Create databases | |
bundle exec rails db:create || warning "Database creation failed - may need to run manually" | |
bundle exec rails db:migrate || warning "Database migration failed - may need to run manually" | |
# Seed database if seeds exist | |
if [ -f "db/seeds.rb" ]; then | |
bundle exec rails db:seed || warning "Database seeding failed - may need to run manually" | |
fi | |
fi | |
# =========================================== | |
# 12. FINAL CONFIGURATION | |
# =========================================== | |
log "Performing final configuration..." | |
# Reload systemd daemon (only for systemd systems) | |
if [ "$INIT_SYSTEM" = "systemd" ]; then | |
sudo systemctl daemon-reload 2>/dev/null || warning "Failed to reload systemd daemon" | |
fi | |
# Start all services | |
start_service "postgresql" "$INIT_SYSTEM" | |
start_service "redis-server" "$INIT_SYSTEM" | |
# Set proper permissions | |
chmod +x bin/* 2>/dev/null || warning "No bin directory found or permission setting failed" | |
# =========================================== | |
# 13. CLEANUP | |
# =========================================== | |
log "Cleaning up temporary files..." | |
sudo apt-get autoremove -y | |
sudo apt-get autoclean | |
# =========================================== | |
# SETUP COMPLETE | |
# =========================================== | |
log "Development environment setup complete!" | |
echo | |
echo "=== SETUP SUMMARY ===" | |
echo "Ruby version: $(rbenv version)" | |
echo "Node.js version: $(node --version)" | |
echo "PostgreSQL: $(psql --version | head -1)" | |
echo "Redis: $(redis-server --version)" | |
echo | |
echo "=== NEXT STEPS ===" | |
echo "1. Restart your terminal or run: source ~/.bashrc" | |
echo "2. Navigate to your project directory" | |
echo "3. Run: bundle install" | |
echo "4. Run: rails db:create db:migrate db:seed" | |
echo "5. Start the application: rails server" | |
echo | |
echo "=== USEFUL COMMANDS ===" | |
echo "• Start Rails server: rails server" | |
echo "• Start with Foreman: foreman start" | |
echo "• Kill process on port: killatport <port>" | |
echo "• Run tests: bundle exec rspec" | |
echo "• Rails console: rails console" | |
echo | |
echo "=== TROUBLESHOOTING ===" | |
echo "• If commands aren't found, restart your terminal" | |
echo "• Check setup log: cat /tmp/setup.log" | |
echo "• Database issues: Check PostgreSQL service status" | |
echo "• Permission issues: Check file ownership in project directory" | |
if [ "$INIT_SYSTEM" = "docker" ]; then | |
echo "• Running in Docker container - services may need manual start" | |
echo "• Try: sudo service postgresql start && sudo service redis-server start" | |
fi | |
echo | |
log "Setup script completed successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment