Select Page

Advent

Ruby on Rails is among the hottest utility stacks for builders taking a look to create websites and internet apps. The Ruby programming language, mixed with the Rails building framework, makes app building easy.

You’ll be able to simply set up Ruby and Rails with the command-line device rbenv. The usage of rbenv provides you with a forged atmosphere for growing your Ruby on Rails programs as it’ll help you simply transfer Ruby variations, retaining all of your group at the identical model.

rbenv supplies beef up for specifying application-specific variations of Ruby, permits you to alternate the worldwide Ruby for each and every consumer, and lets you use an atmosphere variable to override the Ruby model.

This educational will take you throughout the Ruby and Rails set up procedure by the use of rbenv.

Must haves

To practice this educational, you wish to have:

Step 1 – Set up rbenv and Dependencies

Ruby is dependent upon a number of programs which you’ll be able to set up via your package deal supervisor. As soon as the ones are put in, you’ll be able to set up rbenv and use it to put in Ruby,

First, replace your package deal record:

Subsequent, set up the dependencies required to put in Ruby:

  • sudo apt set up autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev

As soon as the dependencies obtain, you’ll be able to set up rbenv itself. Clone the rbenv repository from GitHub into the listing ~/.rbenv:

  • git clone https://github.com/rbenv/rbenv.git ~/.rbenv

Subsequent, upload ~/.rbenv/bin on your $PATH so that you could use the rbenv command line application. Do that through changing your ~/.bashrc record in order that it impacts long run login periods:

  • echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

Then upload the command ~/.rbenv/bin/rbenv init on your ~/.basrc record so rbenv lots routinely:

  • echo 'eval "$(rbenv init -)"' >> ~/.bashrc

Subsequent, follow the adjustments you made on your ~/.bashrc record on your present shell consultation:

Examine that rbenv is ready up correctly through the usage of the sort command, which can show extra details about the rbenv command:

Your terminal window will show the next:

Output

rbenv is a serve as rbenv () { native command; command="${1:-}"; if [ "$#" -gt 0 ]; then shift; fi; case "$command" in rehash | shell) eval "$(rbenv "sh-$command" "$@")" ;; *) command rbenv "$command" "$@" ;; esac }

Subsequent, set up the ruby-build, plugin. This plugin provides therbenv set up command, which simplifies the set up procedure for brand spanking new variations of Ruby:

  • git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

At this level, you have got each rbenv and ruby-build put in. Let’s set up Ruby subsequent.

Step 2 – Putting in Ruby with ruby-build

With the ruby-build plugin now put in, you’ll be able to set up variations of Ruby y would possibly want via a easy command. First, let’s record the entire to be had variations of Ruby:

The output of that command will have to be an extended record of variations that you’ll be able to select to put in.

Let’s set up Ruby 2.5.1:

Putting in Ruby is usually a long procedure, so be ready for the set up to take a little time to finish.

As soon as it is accomplished putting in, set it asy our default model of Ruby with the international sub-command:

Examine that Ruby was once correctly put in through checking its model quantity:

When you put in model 2.5.1 of Ruby, your output to the above command will have to glance one thing like this:

Output

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

To put in and use a distinct model of Ruby, run the rbenv instructions with a distinct model quantity, as in rbenv set up 2.3.0 and rbenv international 2.3.0.

You presently have a minimum of one model of Ruby put in and feature set your default Ruby model. Subsequent, we can arrange gem stones and Rails.

Step 3 – Running with Gem stones

Gem stones are the best way Ruby libraries are disbursed. You utilize the gem command to control those gem stones. We’re going to use this command to put in Rails.

Whilst you set up a gem, the set up procedure generates native documentation. This may upload an important period of time to each and every gem’s set up procedure, so flip off native documentation technology through making a record referred to as ~/.gemrc which comprises a configuration surroundings to show off this selection:

  • echo "gem: --no-document" > ~/.gemrc

Bundler is a device that manages gem dependencies for tasks. Set up the Bundler gem subsequent. as Rails depends upon it.

You’ll be able to see output like this:

Output

Fetching: bundler-1.16.2.gem (100%) Effectively put in bundler-1.16.2 1 gem put in

You’ll be able to use the gem env command (the subcommand env is brief for atmosphere) to be told extra concerning the atmosphere and configuration of gem stones. You’ll be able to see the place gem stones are being put in through the usage of the house argument, like this:

You’ll be able to see output very similar to this:

/house/sammy/.rbenv/variations/2.5.1/lib/ruby/gem stones/2.5.0

Upon getting gem stones arrange, you’ll be able to set up Rails.

Step 4 – Putting in Rails

To put in the newest model of Rails, use the gem set up command:

The gem command installs the gem you specify, in addition to each and every dependency. Rails is a posh internet building framework and has many dependencies, so the method will take a little time to finish. Ultimately you can see a message declaring that Rails is put in. at the side of its dependencies:

Output

... Effectively put in rails-5.2.0 38 gem stones put in

Notice: If you need to put in a particular model of Rails, you’ll be able to record the legitimate variations of Rails through doing a seek, which can output an extended record of imaginable variations. We will then set up a particular model, reminiscent of 4.2.7:

  • gem seek '^rails$' --all
  • gem set up rails -v 4.2.7

rbenv works through making a listing of shims, which level to the recordsdata utilized by the Ruby model that is recently enabled. During the rehash sub-command, rbenv maintains shims in that listing to check each and every Ruby command throughout each and every put in model of Ruby for your server. Every time you put in a brand new model of Ruby or a gem that gives instructions, like Rails does, you will have to run:

Examine that Rails has been put in correctly through printing its model, with this command:

If it put in correctly, you’re going to see the model of Rails that was once put in:

Output

Rails 5.2.0

At this level, you’ll be able to start trying out your Ruby on Rails set up and begin to expand internet programs. Let us take a look at retaining rbenv up to the moment.

Step 5 – Updating rbenv

Because you put in rbenv manually the usage of Git, you’ll be able to improve your set up to the newest model at any time through the usage of the git pull command within the ~/.rbenv listing:

This may increasingly make sure that we’re the usage of probably the most up-to-date model of rbenv to be had.

Step 6 – Uninstalling Ruby variations

As you obtain further variations of Ruby, it’s possible you’ll collect extra variations than you desire to on your ~/.rbenv/variations listing. Use the ruby-buildplugin ‘s’ uninstall subcommand to take away those earlier variations.

As an example, typing this will likely uninstall Ruby model 2.1.3:

With the rbenv uninstall command you’ll be able to blank up previous variations of Ruby in order that you do not need extra put in than you’re recently the usage of.

Step 7 – Uninstalling rbenv

When you’ve determined you now not wish to use rbenv, you’ll be able to take away it out of your device.

To do that, first open your ~/.bashrc record on your editor:

To find and take away the next two strains from the record:

~/.bashrc

...
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Save the record and go out the editor.

Then take away rbenv and all put in Ruby variations with this command:

 rm -rf `rbenv root`

Sign off and again in to use the adjustments on your shell.

Conclusion

On this educational you put in rbenv and Ruby on Rails. From right here, you’ll be able to be told extra about making the ones environments extra powerful.

Discover the way to use Ruby on Rails with PostgreSQL or MySQL quite than its default sqlite3 database, which give extra scalability, centralization, and steadiness on your programs. As your wishes develop, you’ll be able to additionally discover ways to scale Ruby on Rails applications across multiple servers.