Set up Ruby on Rails with rbenv on Ubuntu 22.04 with Best Steps
This tutorial guides you through setting up Ruby on Rails with rbenv on Ubuntu 22.04. Ruby on Rails, often shortened to Rails, is a powerful open-source web application framework written in the Ruby programming language. It provides a structure for building web applications by offering ready-made solutions for common tasks, simplifying development and promoting code reusability. Rails is essentially a RubyGem, a packaged library that extends Ruby’s capabilities.
A Ruby on Rails application is like a toolbox filled with pre-built components. These components streamline repetitive tasks such as creating database tables, generating forms, and building website menus. Rails integrates seamlessly with other web technologies like JavaScript, HTML, and CSS, acting as a back-end or server-side development platform.
The following steps, initially presented on the Orcacore website, detail how to install Ruby on Rails with rbenv on Ubuntu 22.04. rbenv is a command-line tool that simplifies the process of installing, managing, and switching between multiple Ruby environments.
To proceed, you’ll need to log into your Ubuntu 22.04 server as a root user or a non-root user with sudo privileges. Refer to a guide like "Initial Server Setup with Ubuntu 22.04" for assistance. Additionally, ensure that Node.js is installed on your server. The guide "How To Install Node.js on Ubuntu 22.04" using the PPA repository provides instructions for this.
1. Install rbenv on Ubuntu 22.04
Begin by updating your system’s package list:
sudo apt update
Next, install the necessary packages and dependencies required for Ruby on Rails:
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev -y
Download and Install rbenv
Use the following curl command to download the rbenv installer script from GitHub and execute it using bash:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
Now, add ~/.rbenv/bin
to your $PATH
environment variable:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
To enable rbenv to load automatically when you start a new shell, add the following line to your .bashrc
file:
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
Apply the changes by sourcing your .bashrc
file:
source ~/.bashrc
Verify that rbenv is correctly installed by running:
type rbenv
(Image of rbenv tool installation verification)
2. Set up Ruby with ruby-build on Ubuntu 22.04
With rbenv installed, you can now install specific Ruby versions. First, list the available Ruby versions using:
rbenv install -l
(Image of available Ruby versions)
To install Ruby version 3.2.1, execute:
rbenv install 3.2.1
This process may take some time.
Once the installation is complete, set Ruby 3.2.1 as the global default version:
rbenv global 3.2.1
Confirm the Ruby installation and its version:
ruby -v
**Output**
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
3. Set up Rails on Ubuntu 22.04
Gems are the standard way to distribute Ruby libraries. Use the gem
command to manage gems and install Rails. You also need to install Bundler, a tool for managing gem dependencies in your projects.
Run the following commands to configure gem installation and install Bundler:
# echo "gem: --no-document" > ~/.gemrc
# gem install bundler
**Output**
Successfully installed bundler-2.4.7
1 gem installed
Check where gems are being installed:
gem env home
**Output**
/root/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0
Install the latest version of Rails using the gem command:
gem install rails
**Output**
Successfully installed rails-7.0.4.2
Note: To install a specific Rails version, first list available versions and then install the desired one:
# gem search '^rails$' --all
# gem install rails -v 7.0.4
After installing a new Ruby version or a gem that provides commands like Rails, run the following command:
rbenv rehash
Verify the Rails installation:
rails -v
**Output**
Rails 7.0.4.2
4. Update rbenv on Ubuntu 22.04
Update rbenv using git:
# cd ~/.rbenv
# git pull
5. Uninstall Ruby Versions on Ubuntu 22.04
To uninstall a specific Ruby version, use the following command:
rbenv uninstall 3.2.1
6. Uninstall rbenv on Ubuntu 22.04
If you no longer need rbenv, follow these steps to remove it. First, open your ~/.bashrc
file using a text editor like vi:
Find and remove the following lines from the file:
~/.bashrc
...
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Save and close the file.
Then, remove rbenv and all installed Ruby versions:
rm -rf `rbenv root`
For more information, consult the Ruby on Rails Guides.
Conclusion
You have successfully learned how to install Ruby on Rails with rbenv on Ubuntu 22.04. Ruby on Rails is a valuable tool for building web applications efficiently.
Alternative Solutions for Setting Up Ruby on Rails on Ubuntu 22.04
While rbenv is a popular and effective method for managing Ruby versions, alternative approaches exist. Here are two different ways to set up Ruby on Rails on Ubuntu 22.04:
1. Using Ruby Version Manager (RVM)
RVM (Ruby Version Manager) is another widely used tool for managing multiple Ruby environments. It provides similar functionality to rbenv, allowing you to install, manage, and switch between different Ruby versions.
Explanation:
RVM offers a more feature-rich environment management compared to rbenv. It handles gemsets (isolated gem environments for each project) more directly, which can be beneficial for complex projects with different gem dependencies. RVM also automatically handles necessary environment configurations upon installation.
Installation and Usage:
-
Install RVM:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -sSL https://get.rvm.io | bash -s stable
This command downloads and executes the RVM installation script. Follow the on-screen instructions, which typically involve adding RVM to your
.bashrc
or.zshrc
file. After the installation, you will need to close and reopen your terminal or source the file to load RVM:source ~/.bashrc # or ~/.zshrc if you are using Zsh
-
Install Ruby:
rvm install 3.2.1 # Replace with your desired version
This command installs the specified Ruby version.
-
Set a Default Ruby Version:
rvm use 3.2.1 --default # Replace with your desired version
This command sets the specified Ruby version as the default for new terminal sessions.
-
Install Rails:
gem install rails
This installs the Rails gem using the currently active Ruby version. Since RVM manages gemsets, it’s good practice to create a new gemset for each Rails project to isolate dependencies.
rvm gemset create my_rails_project rvm gemset use my_rails_project gem install rails
-
Verify Installation:
ruby -v rails -v
These commands verify that Ruby and Rails are installed correctly.
Code Example (Creating a new Rails application within an RVM gemset):
rvm gemset create my_new_app
rvm gemset use my_new_app
gem install rails
rails new my_new_app
cd my_new_app
bundle install
This sequence of commands creates a new gemset, uses it, installs Rails, generates a new Rails application, navigates to the application directory, and installs the required gems based on the Gemfile
.
2. Using a Containerization Technology (Docker)
Docker allows you to create isolated environments called containers, which package an application and its dependencies together. This ensures consistency across different environments.
Explanation:
Docker provides a highly reproducible and portable environment for your Rails application. It eliminates the "it works on my machine" problem by ensuring that everyone working on the project uses the same environment. This approach isolates the application from the host system, preventing conflicts with other software.
Installation and Usage:
-
Install Docker: Follow the official Docker documentation for installing Docker on Ubuntu 22.04.
-
Create a
Dockerfile
: ADockerfile
is a text file that contains instructions for building a Docker image. Create aDockerfile
in your Rails project directory (or a dedicated directory for Docker configuration):# Use an official Ruby runtime as a parent image FROM ruby:3.2.1 # Install dependencies RUN apt-get update -qq && apt-get install -y --no-install-recommends build-essential nodejs postgresql-client # Set the working directory in the container WORKDIR /app # Copy the Gemfile and Gemfile.lock into the container COPY Gemfile Gemfile.lock ./ # Install gems RUN gem install bundler RUN bundle install # Copy the application code into the container COPY . . # Expose port 3000 EXPOSE 3000 # Set the command to run when the container starts CMD ["rails", "server", "-b", "0.0.0.0"]
-
Build the Docker Image:
docker build -t my-rails-app .
This command builds a Docker image named
my-rails-app
using theDockerfile
in the current directory. -
Run the Docker Container:
docker run -p 3000:3000 my-rails-app
This command runs a Docker container based on the
my-rails-app
image, mapping port 3000 on your host machine to port 3000 in the container.
Code Example (docker-compose.yml for a Rails app with a PostgreSQL database):
version: "3.9"
services:
db:
image: postgres:14
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: rails
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
environment:
RAILS_ENV: development
DATABASE_URL: "postgresql://rails:password@db:5432"
volumes:
db_data:
This docker-compose.yml
file defines two services: db
(a PostgreSQL database) and web
(your Rails application). It links them together and configures the necessary environment variables. To run the application, use docker-compose up --build
. This will build the images and start the containers.
These alternative methods offer different advantages and disadvantages depending on your specific needs and preferences. RVM provides a more integrated Ruby management experience, while Docker offers a highly portable and consistent environment. Choosing the right method depends on your project’s complexity, team size, and deployment strategy. Understanding how to Set up Ruby on Rails with rbenv on Ubuntu 22.04 is still fundamental as it provides a solid base for understanding Ruby environment management.