How To Setup and Configure Ruby on Rails with ‘rbenv’ on Ubuntu 16.04

UbuntuRuby on RailsWeb Development

In this article, we will learn how to setup and configure Ruby on Rails which is the most popular stack application used by the developers which is used to create sites and web applications. The Ruby is a programming language where the programer’s use with the combination of Rails development framework to make the application development fast and simple.

‘rbenv’ is the command-line tools used to install the Ruby and Rails which also provides a good environment for application development using Ruby on Rails. ‘rbenv’ provides the application specific versions of Ruby.

Prerequisites

We needed an Ubuntu 16.04 installed machine with a non-root user.

Installing the updates and dependencies.

Before we begin the installation, we need to update and install all the dependencies related.

First, we will update the Ubuntu, for updating the Ubuntu we needed to run the below command.

$ sudo apt-get update

Then we will install all the dependencies required for the rbenv and Ruby using apt-get, below is the command to install the dependencies.

$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.1ubuntu2).
build-essential set to manually installed.
libgdbm3 is already the newest version (1.8.3-13.1).
libgdbm3 set to manually installed.
The following additional packages will be installed:
automake libbison-dev libssl-doc libssl1.0.0 libtinfo-dev libyaml-0-2 m4
Suggested packages:
autoconf-archive gnu-standards autoconf-doc libtool bison-doc ncurses-doc
readline-doc libyaml-doc
The following NEW packages will be installed:
autoconf automake bison libbison-dev libffi-dev libgdbm-dev libncurses5-dev
libreadline6-dev libssl-dev libssl-doc libtinfo-dev libyaml-0-2 libyaml-dev
m4 zlib1g-dev
The following packages will be upgraded:
libssl1.0.0
1 upgraded, 15 newly installed, 0 to remove and 107 not upgraded.
Need to get 4,888 kB/5,970 kB of archives.
After this operation, 19.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
….
….
Setting up libyaml-0-2:amd64 (0.1.6-3) ...
Setting up libssl1.0.0:amd64 (1.0.2g-1ubuntu4.5) ...
Setting up m4 (1.4.17-5) ...
Setting up autoconf (2.69-9) ...
Setting up automake (1:1.15-4ubuntu1) ...
update-alternatives: using /usr/bin/automake-1.15 to provide /usr/bin/automake (automake) in auto mode
Setting up libbison-dev:amd64 (2:3.0.4.dfsg-1) ...
Setting up bison (2:3.0.4.dfsg-1) ...
update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode
Setting up libgdbm-dev (1.8.3-13.1) ...
Setting up libtinfo-dev:amd64 (6.0+20160213-1ubuntu1) ...
Setting up libncurses5-dev:amd64 (6.0+20160213-1ubuntu1) ...
Setting up libreadline6-dev:amd64 (6.3-8ubuntu2) ...
Setting up zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4) ...
Setting up libssl-dev:amd64 (1.0.2g-1ubuntu4.5) ...
Setting up libssl-doc (1.0.2g-1ubuntu4.5) ...
Setting up libffi-dev:amd64 (3.2.1-4) ...
Setting up libyaml-dev:amd64 (0.1.6-3) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

Installing the ‘rbenv’

We will install the ‘rbenv’ using the git repository, to install or to get the rbenv we have to run the below command.

$ git clone https://github.com/rbenv/rbenv.git /opt/.rbenv
Cloning into '/root/.rbenv'...
remote: Counting objects: 2542, done.
remote: Total 2542 (delta 0), reused 0 (delta 0), pack-reused 2542
Receiving objects: 100% (2542/2542), 461.53 KiB | 120.00 KiB/s, done.
Resolving deltas: 100% (1597/1597), done.
Checking connectivity... done.

We have to add /opt/.rbenv to our $PATH so that we can use the rbenv, to add the path to the environment we needed to add this to ~/.bash_profile file.

To add the environment variable and initiate the ‘rbenv’ we needed to add the below lines also add source rbenv

$ echo 'export PATH="/opt/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source ~/.bashr

Now we will check the rbenv, to check if the rbenv is working correctly or not so that we can run the below command –

$ type rbenv
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
}

To use the rebenv, install the command which makes the installation process more simple installation for Ruby. We should install ruby-build, which installs as a plugin for rbenv, we will use the below command to get the plugin from git repository.

$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Cloning into '/root/.rbenv/plugins/ruby-build'...
remote: Counting objects: 6981, done.
remote: Total 6981 (delta 0), reused 0 (delta 0), pack-reused 6981
Receiving objects: 100% (6981/6981), 1.35 MiB | 182.00 KiB/s, done.
Resolving deltas: 100% (4151/4151), done.
Checking connectivity... done.

Installing Ruby on the Ubuntu

We will use ruby-build, a rbenv plugin to install the Ruby, we needed to run the below command to list all the available versions for Ruby. Which shows a long list of available versions.

$ rbenv install –l
Output:
Available versions:
1.8.5-p113
1.8.5-p114
1.8.5-p115
1.8.5-p231
1.8.5-p52
1.8.6-p110
1.8.6-p111
1.8.6-p114
1.8.6-p230
….
…
rbx-3.56
rbx-3.57
rbx-3.58
rbx-3.59
rbx-3.60
ree-1.8.7-2011.03
ree-1.8.7-2011.12
ree-1.8.7-2012.01
ree-1.8.7-2012.02
topaz-dev

We will only install a particular version. For example, if we are installing Ruby version 2.3.1 then we can use the global command to set it as a default version.

$ sudo rbenv install 2.3.1
Output:
Downloading ruby-2.3.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.bz2
Installing ruby-2.3.1...
Installed ruby-2.3.1 to /root/.rbenv/versions/2.3.1
$ sudo rbenv global 2.3.1

We can also have different versions by running a simple command rbenv install 2.3.0. Once the software is installed, we will verify the ruby installation with the below command –

$ ruby –v
Output:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]

Installing and Working with Gem for Ruby

The Gem is the package which will be installed to extend the functionality of the Ruby. We have to use the gem command to install the packages. We will use the install command with a gem for installing the dependencies.

$ echo "gem: --no-document" > ~/.gemrc
$ gem install bundler
Output:
Fetching: bundler-1.13.5.gem (100%)
Successfully installed bundler-1.13.5
1 gem installed

We will now use the gem env command to know more about the environment and gem configuration we can check the gem installation location by using the home argument which will show the path of the gems which are installed on the server.

$ gem env home
Output:
/root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0

Installing the Rails on Ubuntu

We will now install the latest version of Rails using the gem install command. Below is the command to install the rails –

$ gem install rails
Output:
Successfully installed bundler-1.13.5
1 gem installed
root@ubuntu-16:~# gem env home
/root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0

$ gem install rails
Fetching: i18n-0.7.0.gem (100%)
Successfully installed i18n-0.7.0
Fetching: thread_safe-0.3.5.gem (100%)
Successfully installed thread_safe-0.3.5
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
Fetching: concurrent-ruby-1.0.2.gem (100%)
Successfully installed concurrent-ruby-1.0.2
Fetching: activesupport-5.0.0.1.gem (100%)
Successfully installed activesupport-5.0.0.1
Fetching: rack-2.0.1.gem (100%)
Successfully installed rack-2.0.1
Fetching: rack-test-0.6.3.gem (100%)
Successfully installed rack-test-0.6.3
Fetching: mini_portile2-2.1.0.gem (100%)
Successfully installed mini_portile2-2.1.0
Fetching: nokogiri-1.6.8.1.gem (100%)
Building native extensions. This could take a while...
…
….
…
Successfully installed websocket-driver-0.6.4
Fetching: actioncable-5.0.0.1.gem (100%)
Successfully installed actioncable-5.0.0.1
Fetching: thor-0.19.1.gem (100%)
Successfully installed thor-0.19.1
Fetching: method_source-0.8.2.gem (100%)
Successfully installed method_source-0.8.2
Fetching: railties-5.0.0.1.gem (100%)
Successfully installed railties-5.0.0.1
Fetching: sprockets-3.7.0.gem (100%)
Successfully installed sprockets-3.7.0
Fetching: sprockets-rails-3.2.0.gem (100%)
Successfully installed sprockets-rails-3.2.0
Fetching: rails-5.0.0.1.gem (100%)
Successfully installed rails-5.0.0.1

Once the packages are installed, we will verify that Rails installed correctly, we needed to run the below command.

$ rails –v
Output:
Rails 5.0.0.1

Installing the Java Runtime

As some of the Rails features depend on the JavaScript Runtime environment, we need to install the Node.js. Below is the command to install the Node.js

$ cd /tmp
$ \curl -sSL https://deb.nodesource.com/setup_6.x -o nodejs.sh
$ less nodejs.sh
$ cat /tmp/nodejs.sh | sudo -E bash –
cat /tmp/nodejs.sh | sudo -E bash -
## Installing the NodeSource Node.js v6.x repo...
## Populating apt-get cache...
+ apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://deb.kamailio.org/kamailio jessie InRelease
Hit:3 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Reading package lists... Done
W: http://deb.kamailio.org/kamailio/dists/jessie/InRelease: Signature by key E79ACECB87D8DCD23A20AD2FFB40D3E6508EA4C8 uses weak digest algorithm (SHA1)
…
..
Fetched 6,595 B in 9s (680 B/s)
Reading package lists... Done
W: http://deb.kamailio.org/kamailio/dists/jessie/InRelease: Signature by key E79ACECB87D8DCD23A20AD2FFB40D3E6508EA4C8 uses weak digest algorithm (SHA1)
## Run `apt-get install nodejs` (as root) to install Node.js v6.x and npm

Now we can get the Node.js using the apt-get command

$ sudo apt-get install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
nodejs
0 upgraded, 1 newly installed, 0 to remove and 107 not upgraded.
Need to get 10.1 MB of archives.
After this operation, 50.7 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_6.x xenial/main amd64 nodejs amd64 6.8.1-1nodesource1~xenial1 [10.1 MB]
Fetched 10.1 MB in 37s (272 kB/s)
Selecting previously unselected package nodejs.
(Reading database ... 70921 files and directories currently installed.)
Preparing to unpack .../nodejs_6.8.1-1nodesource1~xenial1_amd64.deb ...
Unpacking nodejs (6.8.1-1nodesource1~xenial1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up nodejs (6.8.1-1nodesource1~xenial1) ...

In the above article we have learned the basics of installing the ‘rbenv’, Ruby on Rails, we can set up multiple Ruby environment.

raja
Published on 20-Jan-2020 11:25:05
Advertisements