Ruby on Rails, often simply referred to as Rails, is a powerful web application framework written in Ruby. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. If you’re looking to develop web applications using Ruby on Rails, the first step is to set up your development environment. In this guide, we will learn how to install Ruby on Rails on Ubuntu.
Prerequisites
Before we dive into the installation process, there are a few prerequisites that you need to have in place:
- Ubuntu System: This guide assumes that you are using an Ubuntu system. The steps should work on other Debian-based distributions as well, but the focus will be on Ubuntu.
- Terminal Access: You should have access to a terminal and be comfortable running commands in it.
- Internet Connection: You will need an active internet connection to download the necessary packages.
- Basic Knowledge of Linux Commands: Familiarity with basic Linux commands will be helpful, though not strictly necessary.
How to Install Ruby on Rails on Ubuntu: 8 Easy Steps
Step 1: Update Your System
Before installing any new software, it’s always a good idea to update your system to ensure that all your existing packages are up to date. Open your terminal and run the following commands:
sudo apt update sudo apt upgrade
This will update the package list and upgrade all the installed packages to their latest versions.
Step 2: Install Dependencies
Ruby on Rails has a few dependencies that need to be installed before you can proceed with the installation. These include curl
, git
, and build-essential
. Run the following command to install these dependencies:
sudo apt install curl git build-essential libssl-dev libreadline-dev zlib1g-dev
- curl: A command-line tool for transferring data with URLs.
- git: A version control system that is widely used in software development.
- build-essential: A package that includes the GNU compiler collection, GNU debugger, and other development libraries and tools required to compile software.
- libssl-dev: A package that provides the development files for OpenSSL, which is used for secure communication.
- libreadline-dev: A package that provides the development files for the GNU readline library, which is used for command-line editing.
- zlib1g-dev: A package that provides the development files for the zlib compression library.
Step 3: Install Ruby
There are several ways to install Ruby on Ubuntu, but one of the most popular methods is to use a version manager like rbenv
or RVM
. These tools allow you to manage multiple versions of Ruby on the same system easily. In this guide, we’ll use rbenv
.
Install rbenv
First, you need to install rbenv
and its plugin ruby-build
, which is used to compile and install Ruby.
Clone the rbenv repository:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Add rbenv to your PATH:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc source ~/.bashrc
Install ruby-build:
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Verify the installation:
type rbenv
You should see an output similar to:
rbenv is a function
Install Ruby Using rbenv
Now that rbenv
is installed, you can use it to install Ruby.
List available Ruby versions:
rbenv install -l
This will list all the available Ruby versions that you can install.
Install the latest stable version of Ruby:
rbenv install 3.1.2
Replace 3.1.2
with the version you want to install.
Set the global Ruby version:
rbenv global 3.1.2
This sets the default Ruby version for your system.
Verify the installation:
ruby -v
You should see an output similar to:
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
Step 4: Install Rails
With Ruby installed, you can now proceed to install Rails.
Install Rails:
gem install rails
This will install the latest version of Rails.
Verify the installation:
rails -v
You should see an output similar to:
Rails 7.0.4
Step 5: Install JavaScript Runtime
Rails requires a JavaScript runtime for certain features, such as the Asset Pipeline. Node.js is a popular choice for this.
Install Node.js:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs
This will install Node.js version 18.x.
Verify the installation:
node -v
You should see an output similar to:
v18.12.1
Step 6: Install a Database
Rails supports multiple databases, including SQLite, PostgreSQL, and MySQL. By default, Rails uses SQLite, which is a lightweight, serverless database. If you plan to use a different database, you’ll need to install it separately.
Install SQLite
Install SQLite:
sudo apt install sqlite3 libsqlite3-dev
Verify the installation:
sqlite3 --version
You should see an output similar to:
3.37.2 2022-01-06 13:25:41 872ba256cbf61d9290b571c0e6d82a20c224ca3ad82971edc46b29818d5dalt1
Install PostgreSQL (Optional)
If you prefer to use PostgreSQL, you can install it as follows:
Install PostgreSQL:
sudo apt install postgresql postgresql-contrib libpq-dev
Start the PostgreSQL service:
sudo systemctl start postgresql
Enable PostgreSQL to start on boot:
sudo systemctl enable postgresql
Create a PostgreSQL user:
sudo -u postgres createuser -s $USER
Verify the installation:
psql --version
You should see an output similar to:
psql (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
Step 7: Create a New Rails Application
Now that everything is set up, you can create a new Rails application to test your installation.
Create a new Rails application:
rails new myapp
Replace myapp
with the name of your application.
Navigate to the application directory:
cd myapp
Start the Rails server:
rails server
Access the application:
Open your web browser and navigate to http://localhost:3000
. You should see the Rails welcome page.
Step 8: Deploying to Production (Optional)
If you’re planning to deploy your Rails application to a production environment, there are a few additional steps you’ll need to take:
- Install a Web Server: Popular choices include Nginx and Apache.
- Install a Production Database: If you used SQLite during development, you must switch to a more robust database like PostgreSQL or MySQL for production.
- Set Up Environment Variables: Use environment variables to manage sensitive information like database credentials.
- Use a Process Manager: Tools like
systemd
orforeman
can help manage your application processes.
Conclusion
Congratulations! You’ve successfully installed Ruby on Rails on your Ubuntu system. You now have a fully functional Rails development environment and are ready to start building web applications. Whether you’re a beginner or an experienced developer, Rails provides a robust framework that can help you bring your ideas to life.
Remember, this guide covers the basics of setting up Rails on Ubuntu. As you continue your journey with Rails, you’ll likely encounter more advanced topics like deploying to production, optimizing performance, and integrating with other services. But for now, you have everything you need to get started. Happy coding!
Leave a Reply