Ruby on Rails - Installation and Setup
Introduction
Installing and setting up Ruby on Rails involves a few steps that are straightforward but crucial for getting your development environment ready. This guide will walk you through the process of installing Ruby, Rails, and the necessary dependencies.
Key Points:
- Ruby on Rails requires Ruby, a package manager, and a database to function correctly.
- This guide will cover the installation steps for different operating systems.
- After installation, you'll be ready to create and run your first Rails application.
Installing Ruby
Using RVM (Ruby Version Manager)
# Install RVM and Ruby
\curl -sSL https://get.rvm.io | bash -s stable --ruby
source ~/.rvm/scripts/rvm
rvm install 3.0.0
rvm use 3.0.0 --default
# Verify the installation
ruby -v
Using rbenv
# Install rbenv and ruby-build
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
# Install Ruby
rbenv install 3.0.0
rbenv global 3.0.0
# Verify the installation
ruby -v
Installing Rails
# Install Rails
gem install rails
# Verify the installation
rails --version
Setting Up a New Rails Application
Create a New Rails Application
# Create a new Rails application
rails new myapp
# Navigate into the application directory
cd myapp
# Start the Rails server
rails server
# Access the application in your browser at http://localhost:3000
Configuring Your Database
Rails by default uses SQLite for its database. However, you can configure it to use other databases like PostgreSQL or MySQL.
PostgreSQL Example
# Add the pg gem to your Gemfile
gem 'pg'
# Install the pg gem
bundle install
# Configure your database in config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: myapp
password: password
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Running Your First Rails Application
Once everything is set up, you can start your Rails server and see your application in action. Run the following command to start the server:
# Start the Rails server
rails server
# Access the application in your browser at http://localhost:3000
Conclusion
By following this guide, you have successfully installed and set up Ruby on Rails. You are now ready to start developing your Rails applications and exploring the powerful features that Rails offers.