Master Ruby on Rails with rbenv on Ubuntu 22.04

Master Ruby on Rails with rbenv on Ubuntu 22.04

Introduction

Setting up Ruby on Rails with rbenv on Ubuntu 22.04 provides a powerful environment for web development. By using rbenv, you can easily manage multiple Ruby versions on your Ubuntu server, ensuring that your Ruby on Rails applications run smoothly. This guide will walk you through installing rbenv, setting up Ruby, and configuring Rails, as well as keeping everything up-to-date. Whether you’re a beginner or an experienced developer, this tutorial ensures a solid foundation for building web applications with Ruby on Rails.

What is rbenv?

rbenv is a tool that helps manage different versions of the Ruby programming language on a computer. It allows users to easily switch between Ruby versions for different projects, ensuring that each project uses the correct version. It also helps developers install Ruby and related frameworks like Ruby on Rails, making it easier to set up and maintain a Ruby development environment.

Step 1 – Install rbenv and Dependencies

Imagine you’re setting up Ruby, one of the most flexible programming languages, on your computer. But here’s the thing: Ruby won’t work properly without some important packages. These packages are like the key ingredients in your favorite recipe—they’re the ones that help Ruby do its thing.

The first thing you need to do is update your package list to get the most recent versions of everything you need. You can do this easily by running this simple command:

$ sudo apt update

Now that your package list is up-to-date, it’s time to install the necessary dependencies. These include tools and libraries like Git, Curl, and other development libraries that Ruby needs to run properly. Think of these as the essential building blocks that let Ruby work its magic. To install everything, just run:

$ 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

Once you’ve installed all these dependencies, it’s time to move on to rbenv. This tool is pretty handy because it helps you manage different versions of Ruby. It’s like having a manager who makes sure you’re using the right version of Ruby for each project. Installing rbenv is super easy—it’s done through a script hosted on GitHub. To grab and run the installer, use the curl command like this:

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

Now that rbenv is installed, you’ll need to make sure it’s ready to roll every time you open the terminal. To do this, you’ll need to tell your system where to find rbenv. You can do this by editing your ~/.bashrc file (that’s your shell’s startup file). Just open it and add this line to the end:

$ echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”‘ >> ~/.bashrc

But wait, there’s more! You also need to tell rbenv to start up every time you open a new terminal session. To do that, add this line right below the one you just added:

$ echo ‘eval “$(rbenv init -)”‘ >> ~/.bashrc

Once you’ve made these changes, run this command to apply them to your current terminal session:

$ source ~/.bashrc

You’re almost there! Now, let’s check if everything is working. To make sure rbenv is set up correctly, type this command into your terminal:

$ type rbenv

If everything’s set up right, you should see something like this:

Output
rbenv is a function
rbenv () {
    local command;
    command=”${1:-}”;
    if [ “$#” -gt 0 ]; then shift; fi;
    case “$command” in
        rehash | shell) eval “$(rbenv “sh-$command” “$@”)” ;;
        *) command rbenv “$command” “$@” ;;
    esac
}

And just like that, congratulations! Both rbenv and the ruby-build plugin are installed and ready to go. You’re now all set to move on to the next step, which is installing Ruby itself.

Getting Started with Ruby

Step 2 – Installing Ruby with ruby-build

So, here we are, ready to dive into the world of Ruby! You’ve already installed the ruby-build plugin, and now it’s time to put it to work for you. Think of ruby-build as your personal Ruby installer—it lets you choose and install whichever version of Ruby you need. Since Ruby comes in a few different versions, ruby-build helps you grab exactly the one you want, whether it’s the latest stable version or a more specialized one.

First things first, let’s check out what versions of Ruby are available. To do that, simply run this command:

$ rbenv install -l

This command will give you a list of Ruby versions you can install, including stable releases and some alternative Ruby implementations. You might see something like this pop up in your terminal:

Output

2.7.7
3.0.5
3.1.3
3.2.0
jruby-9.4.0.0
mruby-3.1.0
picoruby-3.0.0
truffleruby-22.3.1
truffleruby+graalvm-22.3.1

That’s a lot to take in, right? The list includes the latest stable Ruby versions, but you’ll also see alternatives like JRuby and TruffleRuby, which could come in handy for specific tasks. If you want to see all the available Ruby versions, including older ones, you can expand your search by running:

$ rbenv install –list-all / -L

For this tutorial, we’re going with Ruby version 3.2.0. To install it, just run this command in your terminal:

$ rbenv install 3.2.0

Here’s the thing: installing Ruby can take a bit of time. It’s not like downloading a quick app—it involves downloading the source code and compiling it. So, be prepared to wait a little while. But don’t worry, it’ll be worth it!

Once the installation is done, we need to set Ruby 3.2.0 as the version your system will use by default. To do that, we’ll use the rbenv global command. Simply run:

$ rbenv global 3.2.0

Now, Ruby 3.2.0 is your default version, so every time you open a new terminal session, this is the one that will be used.

To make sure everything is working as it should, let’s double-check that Ruby was installed correctly and that you’re using the right version. You can do that by running:

$ ruby -v

If everything went smoothly, you’ll see something like this:

Output

ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux]

And just like that, you’ve got Ruby 3.2.0 installed and set as the default on your system! You’re officially ready to start using Ruby to build some amazing projects. Next up: setting up Ruby gems and Rails to complete your development environment. Let’s get going!

For more detailed steps, check out the official Ruby Installation Guide.

Step 3 – Working with Gems

Alright, we’ve made some good progress—your Ruby environment is starting to take shape! Now, let’s dive into the world of gems. You might be wondering, “What exactly are gems?” Well, think of them as little treasure chests filled with reusable pieces of code that you can use in your Ruby projects. These gems are what keep everything running smoothly in a Ruby application. Whether you’re adding a new feature or fixing a bug, gems are the tools that help get it done.

To keep everything in order, Ruby has a command called gem that helps you manage these treasures. With this command, you can install new gems, update the ones you already have, or remove them completely. Since we’re working on setting up a Ruby on Rails environment, managing gems is going to be super important.

But here’s the thing: when you install a gem, Ruby also creates local documentation for it. While this documentation can be helpful, it can also slow things down, especially when you’re installing larger gems. But don’t worry, there’s a trick to speed up the process. You can skip the local documentation by creating a file called ~/.gemrc and adding a special setting. Here’s the command you need to run to set it up:

$ echo “gem: –no-document” > ~/.gemrc

Now that the documentation delay is out of the way, let’s talk about the next important gem we need: Bundler. Bundler is like the manager of all your gems. It makes sure that the right versions of gems are installed, and that your Ruby on Rails project runs smoothly. Since Rails depends on Bundler to manage its own gems, you’ll need to install it before moving forward.

To install Bundler, simply run this command:

$ gem install bundler

Once that’s done, you’ll see an output like this:

Output
Fetching bundler-2.4.5.gemSuccessfully installed bundler-2.4.51 gem installed

Now that Bundler is installed, you can start checking out how your gems are set up and where they’re stored. Ruby provides a handy command called gem env to inspect your gem environment. For example, if you want to know where your gems are stored on your system, just use this:

$ gem env home

You’ll see something like this:

Output
/home/sammy/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0

And just like that, you’ve got all the info you need about your gems’ environment. Your system is now fully set up to work with gems, and Bundler is ready to keep everything in check.

Next up: we’re going to install Rails itself. With your gems all set, you’re one step closer to launching your Ruby on Rails development environment. Get ready to jump into the world of Rails!

Ruby Documentation

Step 4 – Installing Rails

Now that Ruby is all set up, it’s time to bring in the magic of Ruby on Rails! This is where things get exciting. To install Ruby on Rails, we’ll use the gem install command, which is part of the RubyGems package manager. Think of gems as little bundles of code that make your life easier, and RubyGems is the tool that helps you manage them.

The great thing about using the gem command is that it doesn’t just install Rails itself—it also handles all the extra stuff that Rails needs to run properly. This means you don’t have to worry about installing a bunch of other things separately. All you need to do is specify which version of Rails you want to install with the -v flag. For this tutorial, we’re using version 7.0.4, so run this command:


$ gem install rails -v 7.0.4

Here’s the deal: Rails isn’t a tiny little library. It’s a big, complex web development framework, so it comes with a lot of pieces. Don’t be surprised if the installation takes a bit of time. You’ll see a progress bar and eventually a message like this once everything is done:

Output

Successfully installed rails-7.0.4
35 gems installed

That’s your sign that Rails and all the needed gems have been successfully installed. Nice job!

Installing Different Versions of Rails

Now, let’s say you need a version of Rails that’s different from 7.0.4. Maybe you’re working on an older project, or you prefer a previous version. No worries! You can search for all the available versions of Rails using the gem search command:


$ gem search ‘ˆ>rails$’ –all

This will give you a list of every version of Rails that’s available. Let’s say you want to install version 4.2.7 instead. You can simply run:


$ gem install rails -v 4.2.7

Or, if you just want the latest version, leave off the -v flag, and the gem command will grab the newest one for you:


$ gem install rails

Managing Ruby Versions and Shims with rbenv

Now that Rails is installed, we need to make sure everything is working smoothly, and that’s where rbenv comes in. rbenv is a Ruby version manager that helps you juggle multiple versions of Ruby on your system. Think of it like a remote control for your Ruby environments—it ensures you’re always using the right Ruby version for whatever project you’re working on.

To keep track of all these Ruby versions, rbenv creates something called “shims.” Shims are small scripts that act as a go-between for the commands you type and the Ruby versions installed on your system. They make sure the correct Ruby version is being used whenever you run a command.

Whenever you install a new Ruby version or a gem like Rails that adds new commands, you’ll need to update these shims. To do this, just run the rbenv rehash command, which refreshes the shims directory and ensures everything is linked up properly. Here’s the command:


$ rbenv rehash

Verifying the Installation of Rails

At this point, everything should be good to go, but let’s double-check that Rails is working correctly. You can do this by checking the version of Rails installed on your system. Run this command:


$ rails -v

If all went well, you should see something like this:

Output

Rails 7.0.4

And just like that, you’ve successfully installed Ruby on Rails! Your environment is now ready, and you’re all set to start building dynamic, database-driven web applications.

With everything in place, you can dive into your projects. But don’t forget: the next step is making sure your Ruby environment stays up-to-date. Keep an eye on rbenv to manage future Ruby version updates and keep your workflow smooth. Ready to get started? Let’s go!

For more details, refer to the Ruby Installation Documentation.

Step 5 – Updating rbenv

Alright, you’ve got your rbenv set up, but now comes the part that a lot of people forget: keeping it updated. It’s kind of like upgrading your phone’s software—those new updates bring cool features and important bug fixes, along with better performance. The same goes for rbenv. When you install rbenv manually using Git, it’s really important to keep it updated so you can take advantage of all the latest fixes, tweaks, and improvements.

Updating rbenv is pretty simple, though, and you can do it anytime. The first thing you need to do is head over to the directory where rbenv is installed. It’s usually in the ~/.rbenv directory inside your home folder. Think of it like the treasure chest where rbenv is stored on your system. Once you’re in the right place, you’ll only need a couple of commands to get things updated.

First, go to the ~/.rbenv directory by running this:

$ cd ~/.rbenv

Next, you’re going to ask your treasure chest to update itself. Use the git pull command to get the latest changes from the official rbenv repository on GitHub. This command doesn’t just grab the newest version of rbenv; it also applies the updates directly to your local setup. It’s like getting the latest software upgrade without doing much. Just run:

$ git pull

And just like that, you’ve updated rbenv to the latest version, complete with all the new features and fixes. Keeping rbenv updated is one of those “set it and forget it” tasks—doing it regularly makes sure your Ruby environment stays fresh, stable, and running smoothly.

Here’s a pro tip: if you run into any issues with your current version of rbenv or something feels off with your Ruby setup, the first thing you should try is this git pull command. It’s a quick and easy way to resolve problems, get the latest bug fixes, and make sure everything is working just right. It’s like giving your Ruby environment a little tune-up whenever you need it.

Remember, regular updates ensure that your Ruby environment stays up-to-date and stable.

What is rbenv?

Step 6 – Uninstalling Ruby Versions

Picture this: you’re deep into your Ruby on Rails project, testing out different Ruby versions to find the best one for your app. It’s exciting at first—trying out new versions, testing features, and improving performance. But over time, you might start to notice something: your system is slowly getting cluttered with old Ruby versions you don’t even need anymore. These outdated versions can take up valuable disk space and might cause some confusion when managing your Ruby environment. It’s like having a drawer full of random tools you never use but still can’t seem to throw away.

Now it’s time to clean up that clutter. It’s a good idea to regularly uninstall Ruby versions you no longer need—especially as your projects evolve, and you figure out which versions you’re actually using. Luckily, rbenv makes it really easy to tidy things up. All the Ruby versions you’ve installed are stored in the ~/.rbenv/versions directory, and that’s where the magic happens.

If you’ve been juggling several Ruby versions, your versions directory might be getting a bit crowded. The great thing about rbenv is that it offers a simple command to uninstall any versions you don’t need anymore. For example, if you no longer need Ruby 3.2.0, here’s the command to remove it:

$ rbenv uninstall 3.2.0

This command will remove Ruby 3.2.0 from your system. It’s quick, efficient, and a great way to free up space on your disk. It’s also a smart move to make sure your development environment stays organized—only keeping the versions of Ruby you actually need. No more excess baggage!

By uninstalling old Ruby versions, you’re not just freeing up space—you’re also making your system easier to manage. It reduces the risk of accidentally switching to an outdated Ruby version, which could mess with your app’s compatibility or even cause issues with your gems. Think of it like cleaning out your closet. You don’t need those old shoes taking up space when you’ve got newer, better ones to wear, right?

So, with just a few simple commands, you can keep your rbenv environment neat, focused, and free from the clutter of Ruby versions you no longer need. Regular maintenance is key to ensuring you stay on track with the versions you need for your ongoing projects while avoiding potential headaches down the road.

For more details, you can refer to the official Ruby Installation Documentation.

Ruby Installation Documentation

Step 7 – Uninstalling rbenv

Sometimes, you might reach a point where you need a change in your Ruby development setup. Maybe you’ve been using rbenv to manage your Ruby environments, but now you’ve decided it’s time to try something different or simply don’t need it anymore. Whatever the reason, uninstalling rbenv is easy, and I’ll guide you through it step by step.

Step 1: Modify Your Bash Configuration

First, you’ll need to tidy up your shell’s startup file, the ~/.bashrc file. This file is like the manual your system follows every time you open a new terminal. It holds environment settings and commands, like the ones that tell your system to load rbenv when you start working. So, to uninstall rbenv properly, you need to tell your terminal not to load it anymore.

Open the ~/.bashrc file in your text editor of choice. I’ll use the nano editor here (but feel free to swap it out if you prefer another editor). To open the file, type:

nano ~/.bashrc

Once the file is open, you’ll see several lines of code. What you’re looking for are the two lines that set up rbenv in your terminal:


export PATH=”$HOME/.rbenv/bin:$PATH”
eval “$( rbenv init – )”

These lines are what make rbenv available every time you open a terminal. To uninstall rbenv, simply delete these two lines from the ~/.bashrc file.

Step 2: Save and Exit the Editor

Once you’ve deleted those lines, it’s time to save your changes and exit the editor. If you’re using nano, here’s how you do it: press CTRL + X , then hit Y to confirm you want to save the file, and press ENTER to exit. That’s it!

Step 3: Remove rbenv and All Installed Ruby Versions

Now that your terminal won’t be loading rbenv anymore, it’s time to do a little spring cleaning. You need to delete the rbenv directory and any Ruby versions it’s managing. It’s like clearing out your closet and tossing out all the Ruby versions you no longer need.

To do this, run the following command:

rm -rf `rbenv root`

This will remove the entire rbenv directory, including all Ruby versions installed with it. And just like that, rbenv is gone from your system.

Step 4: Apply the Changes

You’re almost done! Now that you’ve updated the ~/.bashrc file and removed rbenv, the final step is to apply the changes to your system. All you need to do is log out and log back in. This refreshes your environment and makes sure rbenv doesn’t sneak back into your terminal sessions.

Once you’ve done that, you’ve successfully uninstalled rbenv and removed any Ruby versions it was managing. If you ever need to reinstall rbenv or Ruby, no problem! You can always follow the same installation steps to set everything back up from scratch.

And just like that, you’re done! Whether you’re switching to a new tool or just cleaning up your system, these easy steps will keep everything neat and efficient.

For more detailed instructions, you can also check this guide: How to Uninstall rbenv

Conclusion

In conclusion, setting up Ruby on Rails with rbenv on Ubuntu 22.04 provides a solid foundation for web development. By following the steps outlined in this tutorial, you can easily install rbenv, manage Ruby versions, and configure Rails for your development environment. Keeping rbenv updated and removing unnecessary Ruby versions ensures that your system remains efficient and well-organized. Whether you’re starting a new project or managing an existing one, these steps will help you create a smooth, optimized workflow for building powerful Ruby on Rails applications. As Ruby on Rails continues to evolve, staying updated on best practices and new tools will further enhance your development experience.

Master Ruby Comments: Write Clear and Readable Code (2023)

Alireza Pourmahdavi

I’m Alireza Pourmahdavi, a founder, CEO, and builder with a background that combines deep technical expertise with practical business leadership. I’ve launched and scaled companies like Caasify and AutoVM, focusing on cloud services, automation, and hosting infrastructure. I hold VMware certifications, including VCAP-DCV and VMware NSX. My work involves constructing multi-tenant cloud platforms on VMware, optimizing network virtualization through NSX, and integrating these systems into platforms using custom APIs and automation tools. I’m also skilled in Linux system administration, infrastructure security, and performance tuning. On the business side, I lead financial planning, strategy, budgeting, and team leadership while also driving marketing efforts, from positioning and go-to-market planning to customer acquisition and B2B growth.

Any Cloud Solution, Anywhere!

From small business to enterprise, we’ve got you covered!

Caasify
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.