Ruby on Rails is a powerful web application framework designed to help developers create new projects while scripting less code. It is a popular open-source package that is published under the Massachusetts Institute of Technology (MIT) license and features a server-side web framework that leverages the Model View Controller (MVC) concept. The application makes web development great fun thanks to the support of the well-versed community that delivers commendable contributions to make the package better.
This tutorial will show you how to install Ruby on Rails on Ubuntu 18.04 via the command line tool Ruby Version Manager (RVM). Setting up Ruby on Rails (RoR) on Ubuntu is an excellent idea because most of the codes you create will run on this easy-to-use Linux distribution. On the other hand, the RVM presents a robust development workspace that allows you to work with numerous Ruby environments and switch between them seamlessly.
Ready? Let's roll!
Updating The GNU Privacy Guard (GPG)
The first step when installing RoR on Ubuntu 18.04 is updating the GPG to the latest version. This makes it easy to contact the public key server and request a key linked to the given ID. To update the GNU Privacy Guard run the following command:
$ sudo apt install gnupg2
Note: we’ve utilized a user with sudo privileges to update the GPG, but all the other commands in this tutorial can be implemented without sudo privileges.
Installing RVM Packages
Once you update the GPG, the next step is installing all the required Ruby Version Manager packages. First, request the RVM project’s public key to validate each RVM release.
The RVM project’s key permits you to authenticate the validity of the RVM release that you will download. Run the command below to add the RVM key to your server:
$ gpg2 --keyserver hkp://keys.gnupg.net --recv-keys409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Next, move to a rewritable environment such as /temp directory and download the Ruby Version Manager script into the file:
Now, we have to leverage a curl command to download the required RVM installation script from the RVM project’s website. Here the backlash that directs the command makes sure the curl command used is regular and not an alias version.
We’ll include the -s flag to signify the utility should function in a silent mode. In addition we append -S flag that allows curl to display errors whenever it fails. The command will also include -L flag to enable the utility to follow the redirects, and -o flag that indicates to write the output to a file rather than a standard output.
When all these elements are combined, what you get is a complete command:
$ curl -sSL https://get.rvm.io -o rvm.sh
To audit the content of this RVM script before implementing it, run the command below:
$ less /tmp/rvm.sh
Next, pipe the RVM installation script to bash to install the most recent version of stable Rails. The Rails version will automatically pull in all the associated stable versions of Ruby.
$ cat /tmp/rvm.sh | bash -s stable --rails
Note: You may be required to provide your valid user’s password during the installation.
Once the installation is done, look for the Ruby Version Manager scripts from the location they were installed. In most cases, the directory for the RVM scripts is home/username. Run the command below to source the scripts:
By now you have a fully configured Ruby on Rails (RoR) environment.
Installing A Specific RoR Version
If you are a fun of a specific Ruby on Rails version that is not the latest one, you can install it using the RVM.
First, run the command below to check the available Ruby versions:
$ rvm list known
Next, run the command below to install the Ruby version that you want using the RVM.
$ rvm install ruby_version
The ruby_version can be put as ruby-2.4.0 or just 2.4.0 in the above command. Once the installation is done you can view a list of the Ruby versions you have installed by running the command below:
$ rvm list
If you want to switch between these versions, just type:
$ rvm use ruby_version
Where ruby_version stands for the version you want to use such as ruby-2.4.0.
Better still, Rails is a gem which means we can use the gem command to install its versions. To accomplish this, we first search for all the available Rails’ versions using the command below:
$ gem search '^rails$' --all
Next run the command below to install the required Rails version:
$ gem install rails -v rails_version
Note:rails_version stands for the version number such as 5.1.6
In addition, various Rails versions can be used with each Ruby. To achieve this, we create gemsets and install Rails with the sets using the gem commands.
First, run the command below to create the gemset:
$ rvm gemset create gemset_name
Next, run the command below to specify the version of Ruby to use:
$ rvm ruby_version@gemset_name --create
The gemsets create a self-contained environment for the gems and multiple environments for every Ruby version you have installed.
Note: we’ll move to a writable location, output the Node.js script to a file to authenticate it, and use less to read it.
$cd /tmp $ curl -sSL https://deb.nodesource.com/setup_10.x -o nodejs.sh $ less nodejs.sh
Run the command below to install NodeSource Node.js v10.x repo:
$ cat /tmp/nodejs.sh | sudo -E bash -
The -E flag preserves the existing environment variable of the user.
Next, run the commands below to update apt and then use it to install the Node.js:
$ sudo apt update $ sudo apt install -y nodejs
That it! The tutorial has covered all the basics of installing Ruby on Rails using the Ruby Version Manager. You can now use Ruby on Rails to create new web applications.