Ruby on Rails is a powerful application stack that allows developers to create scalable websites and web applications. By combining the Ruby programming language with the Rails development framework, you can quickly build and deploy your projects. In this tutorial, we will guide you through the process of installing Ruby and Rails using rbenv on an Ubuntu 22.04 server.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- An Ubuntu 22.04 server set up according to the initial server setup guide, with a sudo non-root user and a firewall.
- Node.js installed using the official PPA. You can follow the instructions in the “How to Install Node.js on Ubuntu 22.04” guide. Installing Node.js is necessary as some Rails features, such as the Asset Pipeline, depend on a JavaScript Runtime provided by Node.js.
Step 1: Install rbenv and Dependencies
To start the installation process, we first need to update the package list:
sudo apt update
Next, we will install the dependencies required to install Ruby:
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 the dependencies are installed, we can proceed to install rbenv itself. Use the following command to fetch the install script from Github and run it:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
After the installation is complete, we need to add rbenv to our PATH environment variable. Modify the ~/.bashrc file to include the rbenv command line utility:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
Additionally, add the following line to the ~/.bashrc file to ensure rbenv loads automatically:
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
To apply the changes made to the ~/.bashrc file, run the following command:
source ~/.bashrc
To verify that rbenv is set up correctly, use the type
command:
type rbenv
You should see the following 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}
Congratulations! You have successfully installed rbenv. Now let’s move on to installing Ruby.
Step 2: Installing Ruby with ruby-build
With rbenv installed, we can use the ruby-build plugin to install different versions of Ruby. To list all available versions of Ruby, use the following command:
rbenv install -l
This will display a list of available versions. To install a specific version, run the following command:
rbenv install <version>
For example, to install Ruby version 3.2.0, use:
rbenv install 3.2.0
The installation process may take some time to complete. Once it’s done, set the installed version as the global default:
rbenv global <version>
To verify that Ruby is installed correctly, check its version:
ruby -v
If everything is set up properly, you should see the version output, confirming the successful installation:
ruby <version>
Congratulations! You now have Ruby installed using rbenv. Let’s move on to setting up gems and installing Rails.
Step 3: Working with Gems
Gems are Ruby libraries that can be easily managed using the gem command. Before we install Rails, let’s configure gem to turn off local documentation generation. This can significantly reduce the installation time for each gem. Create a file called ~/.gemrc with the following content:
echo "gem: --no-document" > ~/.gemrc
Next, install the Bundler gem, which is required by Rails:
gem install bundler
To confirm where gems are being installed, you can use the following command:
gem env home
This will display the path to the gem installation directory.
Now that we have gems set up, let’s proceed to install Rails.
Step 4: Installing Rails
To install Rails, use the gem install command along with the -v flag to specify the version. For example, to install version 7.0.4:
gem install rails -v 7.0.4
The installation process may take some time, as Rails has many dependencies. Once it’s completed, you will see a message indicating the successful installation of Rails.
To verify that Rails is installed correctly, run the following command:
rails -v
If the installation was successful, you should see the version of Rails that was installed.
Congratulations! You have successfully installed Ruby on Rails using rbenv. You are now ready to start developing web applications. Remember to run rbenv rehash
whenever you install a new version of Ruby or a gem that provides commands like Rails.
Step 5: Updating rbenv
To keep rbenv up-to-date, you can upgrade your installation to the most recent version. Navigate to the ~/.rbenv directory and run the following command:
cd ~/.rbenv git pull
This will fetch the latest version of rbenv from the repository and update your installation.
Step 6: Uninstalling Ruby versions
If you want to remove any previously installed versions of Ruby, you can use the ruby-build plugin’s uninstall subcommand. For example, to uninstall Ruby version 3.2.0, run the following command:
rbenv uninstall 3.2.0
This will remove the specified version from your system, freeing up disk space.
Step 7: Uninstalling rbenv
If you decide that you no longer need rbenv, you can remove it from your system. Open the ~/.bashrc file in your preferred text editor and remove the following lines:
~/.bashrc
...
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Save the file and exit the editor. Then, run the following command to remove rbenv and all installed Ruby versions:
rm -rf `rbenv root`
Finally, log out and log back in to apply the changes to your shell.
Conclusion
In this tutorial, we have walked you through the process of installing Ruby on Rails with rbenv on an Ubuntu 22.04 server. By following these steps, you now have a solid environment for developing and deploying Ruby on Rails applications. You can start building your web development projects and take advantage of the powerful features provided by Ruby on Rails.
If you need a reliable and scalable hosting solution for your Ruby on Rails applications, Shape.host offers Cloud VPS services that can meet your needs. With Shape.host, you can focus on developing your applications while they handle the infrastructure and server management. Check out their services at Shape.host for more information.