How To Install Ruby On Rails on Ubuntu 24.04 LTS with rbenv
Welcome back to the Greenhost.cloud blog! In today’s post, we’ll guide you through the process of installing Ruby on Rails on Ubuntu 24.04 LTS using rbenv, a popular tool for managing Ruby versions. This setup allows you to easily switch between multiple Ruby versions and maintain a controlled environment for your Rails applications. Let’s get started!
Prerequisites
Before we proceed, ensure you have the following:
- A fresh installation of Ubuntu 24.04 LTS
- A user account with sudo privileges
- Basic familiarity with command-line operations
Step 1: Update Your System
First, open your terminal and update your package lists to ensure you have access to the latest packages.
sudo apt update && sudo apt upgrade -y
Step 2: Install Dependencies
Rails requires several dependencies to function properly. Install them using the following command:
sudo apt install -y curl git build-essential libssl-dev libreadline-dev libcurl4-openssl-dev libxml2-dev libxslt1-dev libsqlite3-dev libffi-dev
Step 3: Install rbenv and ruby-build
3.1 Clone rbenv repository
Clone the rbenv repository from GitHub to your home directory:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
3.2 Set up rbenv in your shell
To enable rbenv in your shell, add the following lines to your ~/.bashrc
or ~/.bash_profile
:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
Apply the changes:
source ~/.bashrc
3.3 Install ruby-build
Next, install ruby-build, which is a plugin for rbenv that allows you to install different Ruby versions. Clone it into the plugins directory:
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Step 4: Install Ruby
At this point, rbenv is set up. Now, you can install Ruby. Check the latest stable version of Ruby on the official Ruby website or use the following command to see a list of available versions:
rbenv install -l
For this tutorial, we’ll install Ruby 3.2.0 as an example:
rbenv install 3.2.0
Set the installed version as the global default:
rbenv global 3.2.0
Verify the installation:
ruby -v
You should see the version of Ruby you just installed.
Step 5: Install Bundler
Bundler is essential for managing your application’s dependencies. Install it with the following command:
gem install bundler
Don’t forget to run:
rbenv rehash
Step 6: Install Rails
With Ruby and Bundler installed, you can now install Rails. Use the following command to install the latest version of Rails:
gem install rails
Again, run rbenv rehash
to make the Rails executable available in your shell:
rbenv rehash
Step 7: Verify Rails Installation
To confirm Rails has been installed correctly, check the version:
rails -v
You should see the installed version of Rails displayed in your terminal.
Step 8: Create Your First Rails Application
Now that you have Ruby on Rails set up, it’s time to create your first Rails application. You can create a new project using the following command:
rails new hello_rails
Navigate into your project directory:
cd hello_rails
Start your Rails server:
rails server
Open your web browser and go to http://localhost:3000
to see your new Rails application running!
Conclusion
Congratulations! You’ve successfully installed Ruby on Rails on Ubuntu 24.04 LTS using rbenv. You can now start building and deploying your Rails applications with ease. If you have any questions or run into issues during your setup, feel free to leave us a comment below.
Happy coding from all of us at Greenhost.cloud!
This blog post outlines clear steps for installing Ruby on Rails on Ubuntu 24.04 LTS, providing both beginners and seasoned developers the information they need to get started. Don’t forget to check out our other posts for more tutorials, tips, and tricks!