One place for hosting & domains

      How To Install Ruby on Rails with rbenv on macOS


      Introduction

      Ruby on Rails is a popular application stack for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development quick and efficient.

      One way to install Ruby and Rails is with the command-line tool rbenv. Using rbenv will provide you with a well-controlled and robust environment for developing your Ruby on Rails applications, allowing you to easily switch the version of Ruby for your entire team when needed.

      rbenv provides support for specifying application-specific versions of Ruby, lets you change the global Ruby for each user, and allows you to use an environment variable to override the Ruby version.

      In this tutorial, you will use rbenv to install and set up Ruby on Rails on your local macOS machine.

      Prerequisites

      To follow this tutorial, you will need:

      Step 1 — Installing rbenv

      In this step, you will install rbenv and make sure that it starts automatically at boot. To do this on macOS, this tutorial will use the package manager Homebrew.

      To download the rbenv package with Homebrew, run the following command:

      This will install rbenv and the ruby-build plugin. This plugin adds therbenv install command, which streamlines the installation process for new versions of Ruby.

      Next, you'll add the command eval "$(rbenv init -)" to your ~/.bash_profile file to make rbenv load automatically when you open up the Terminal. To do this, open your .bash_profile in your favorite text editor:

      Add the following line to the file:

      ~/.bash_profile

      eval "$(rbenv init -)"
      

      Save and quit the file.

      Next, apply the changes you made to your ~/.bash_profile file to your current shell session:

      To verify that rbenv is set up properly, use the type command, which will display more information about the rbenv command:

      Your terminal window will display 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 }

      At this point, you have both rbenv and ruby-build installed on your machine. This will allow you to install Ruby from the command line in the next step.

      Step 2 — Installing Ruby

      With the ruby-build plugin now installed, you can install any version of Ruby you may need through a single command. In this step, you will choose a version of Ruby, install it on your machine, and then verify the installation.

      First, use the -l flag to list all the available versions of Ruby:

      The output of that command will be a long list of versions that you can choose to install.

      For this tutorial, install Ruby 2.6.3:

      Installing Ruby can be a lengthy process, so be prepared for the installation to take some time to complete.

      Once it's done installing, set it as your default version of Ruby with the global sub-command:

      Verify that Ruby was properly installed by checking its version number:

      Your output will look something like this:

      Output

      ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

      To install and use a different version of Ruby, run the rbenv commands with a different version number, such as rbenv install 2.3.0 and rbenv global 2.3.0.

      You now have one version of Ruby installed and have set your default Ruby version. Next, you will set yourself up to work with Ruby packages and libraries, or gems, which will then allow you to install Rails.

      Step 3 — Working with Gems

      Gems are packages of Ruby libraries and programs that can be distributed throughout the Ruby ecosystem. You use the gem command to manage these gems. In this step, you will configure the gem command to prepare for the Rails installation.

      When you install a gem, the installation process generates local documentation. This can add a significant amount of time to each gem's installation process, so turn off local documentation generation by creating a file called ~/.gemrc which contains a configuration setting to turn off this feature:

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

      With that done, use the gem command to install Bundler, a tool that manages gem dependencies for projects. This is needed for Rails to work correctly:

      You'll see output like this:

      Output

      Fetching: bundler-2.0.2.gem Successfully installed bundler-2.0.2 1 gem installed

      You can use the gem env command to learn more about the environment and configuration of gems. To see the location of installed gems, use the home argument, like this:

      You'll see output similar to this:

      /Users/sammy/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0
      

      Now that you have set up and explored your gem workflow, you are free to install Rails.

      Step 4 — Installing Rails

      In this step, you will install Rails, verify the installation, and prepare it for use.

      To install the most recent version of Rails, use the gem install command:

      The gem command installs the gem you specify, as well as every dependency. Rails is a complex web development framework and has many dependencies, so the process will take some time to complete. Eventually you'll see a message stating that Rails is installed, along with its dependencies:

      Output

      ... Successfully installed rails-5.2.3 38 gems installed

      Note: If you would like to install a specific version of Rails, you can list its valid versions by doing a search, which will output a long list of options:

      • gem search '^rails$' --all

      You could then install a specific version, such as 4.2.7:

      • gem install rails -v 4.2.7

      rbenv works by creating a directory of shims, or libraries that intercept calls and change or redirect them. In this case, shims point Ruby commands to the files used by the Ruby version that's currently enabled. Through the rehash sub-command, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby on your server. Whenever you install a new version of Ruby or a gem that provides commands, such as Rails, you should use rehash.

      To rehash the directory of shims, run the following command:

      Verify your installation of Rails by printing its version with this command:

      You will see the version of Rails that was installed:

      Output

      Rails 5.2.3

      With Rails successfully installed, you can begin testing your Ruby on Rails installation and start to develop web applications. In the next step, you will learn how to update and uninstall rbenv and Ruby.

      Step 5 — Updating and Uninstalling rbenv and Ruby

      When maintaining projects, it is useful to know how to update and uninstall when the need arises. In this step, you will upgrade rbenv, then uninstall Ruby and rbenv from your machine.

      You can upgrade rbenv and ruby-build using Homebrew by running the following command:

      • brew upgrade rbenv ruby-build

      If rbenv or ruby-build need to be updated, Homebrew will do it for you automatically. If your set up is already up to date, you will get output similar to the following:

      Output

      Error: rbenv 1.1.2 already installed Error: ruby-build 20190615 already installed

      This will ensure that we are using the most up-to-date version of rbenv available.

      As you download additional versions of Ruby, you may accumulate more versions than you would like in your ~/.rbenv/versions directory. Using the ruby-build plugin's uninstall subcommand, you can remove these previous versions.

      For example, run the following to uninstall Ruby version 2.1.3:

      With the rbenv uninstall command you can clean up old versions of Ruby so that you do not have more installed than you are currently using.

      If you've decided you no longer want to use rbenv, you can remove it from your system.

      To do this, first open your ~/.bash_profile file in your editor:

      Find and remove the following line from the file to stop rbenv from starting when you open the Terminal:

      ~/.bash_profile

      ...
      eval "$(rbenv init -)"
      

      Once you have deleted this line, save the file and exit the editor.

      Run the following command to apply the changes to your shell:

      Next, remove rbenv and all installed Ruby versions with this command:

      Finally, remove the rbenv package itself with Homebrew:

      Check the rbenv version to make sure that it has been uninstalled:

      You will get the following output:

      Output

      -bash: /usr/local/bin/rbenv: No such file or directory

      This means that you have successfully removed rbenv from your machine.

      Conclusion

      In this tutorial you installed Ruby on Rails with rbenv on macOS. From here, you can learn more about coding in Ruby with our How To Code in Ruby series. You can also explore how to use Ruby on Rails with PostgreSQL rather than its default sqlite3 database, which provides more scalability, centralization, and stability for your applications.



      Source link

      How To Install Ruby on Rails with rbenv on Ubuntu 18.04


      Introduction

      Ruby on Rails is one of the most popular application stacks for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development simple.

      You can easily install Ruby and Rails with the command-line tool rbenv. Using rbenv will provide you with a solid environment for developing your Ruby on Rails applications as it will let you easily switch Ruby versions, keeping your entire team on the same version.

      rbenv provides support for specifying application-specific versions of Ruby, lets you change the global Ruby for each user, and allows you to use an environment variable to override the Ruby version.

      This tutorial will take you through the Ruby and Rails installation process via rbenv.

      Prerequisites

      To follow this tutorial, you need:

      Step 1 – Install rbenv and Dependencies

      Ruby relies on several packages which you can install through your package manager. Once those are installed, you can install rbenv and use it to install Ruby,

      First, update your package list:

      Next, install the dependencies required to install Ruby:

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

      Once the dependencies download, you can install rbenv itself. Clone the rbenv repository from GitHub into the directory ~/.rbenv:

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

      Next, add ~/.rbenv/bin to your $PATH so that you can use the rbenv command line utility. Do this by altering your ~/.bashrc file so that it affects future login sessions:

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

      Then add the command ~/.rbenv/bin/rbenv init to your ~/.basrc file so rbenv loads automatically:

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

      Next, apply the changes you made to your ~/.bashrc file to your current shell session:

      Verify that rbenv is set up properly by using the type command, which will display more information about the rbenv command:

      Your terminal window will display 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 }

      Next, install the ruby-build, plugin. This plugin adds therbenv install command, which simplifies the installation process for new versions of Ruby:

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

      At this point, you have both rbenv and ruby-build installed. Let's install Ruby next.

      Step 2 – Installing Ruby with ruby-build

      With the ruby-build plugin now installed, you can install versions of Ruby y may need through a simple command. First, let's list all the available versions of Ruby:

      The output of that command should be a long list of versions that you can choose to install.

      Let's install Ruby 2.5.1:

      Installing Ruby can be a lengthy process, so be prepared for the installation to take some time to complete.

      Once it's done installing, set it asy our default version of Ruby with the global sub-command:

      Verify that Ruby was properly installed by checking its version number:

      If you installed version 2.5.1 of Ruby, your output to the above command should look something like this:

      Output

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

      To install and use a different version of Ruby, run the rbenv commands with a different version number, as in rbenv install 2.3.0 and rbenv global 2.3.0.

      You now have at least one version of Ruby installed and have set your default Ruby version. Next, we will set up gems and Rails.

      Step 3 – Working with Gems

      Gems are the way Ruby libraries are distributed. You use the gem command to manage these gems. We'll use this command to install Rails.

      When you install a gem, the installation process generates local documentation. This can add a significant amount of time to each gem's installation process, so turn off local documentation generation by creating a file called ~/.gemrc which contains a configuration setting to turn off this feature:

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

      Bundler is a tool that manages gem dependencies for projects. Install the Bundler gem next. as Rails depends on it.

      You'll see output like this:

      Output

      Fetching: bundler-1.16.2.gem (100%) Successfully installed bundler-1.16.2 1 gem installed

      You can use the gem env command (the subcommand env is short for environment) to learn more about the environment and configuration of gems. You can see where gems are being installed by using the home argument, like this:

      You'll see output similar to this:

      /home/sammy/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0
      

      Once you have gems set up, you can install Rails.

      Step 4 – Installing Rails

      To install the most recent version of Rails, use the gem install command:

      The gem command installs the gem you specify, as well as every dependency. Rails is a complex web development framework and has many dependencies, so the process will take some time to complete. Eventually you'll see a message stating that Rails is installed. along with its dependencies:

      Output

      ... Successfully installed rails-5.2.0 38 gems installed

      Note: If you would like to install a specific version of Rails, you can list the valid versions of Rails by doing a search, which will output a long list of possible versions. We can then install a specific version, such as 4.2.7:

      • gem search '^rails$' --all
      • gem install rails -v 4.2.7

      rbenv works by creating a directory of shims, which point to the files used by the Ruby version that's currently enabled. Through the rehash sub-command, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby on your server. Whenever you install a new version of Ruby or a gem that provides commands, like Rails does, you should run:

      Verify that Rails has been installed properly by printing its version, with this command:

      If it installed properly, you will see the version of Rails that was installed:

      Output

      Rails 5.2.0

      At this point, you can begin testing your Ruby on Rails installation and start to develop web applications. Let's look at keeping rbenv up to date.

      Step 5 – Updating rbenv

      Since you installed rbenv manually using Git, you can upgrade your installation to the most recent version at any time by using the git pull command in the ~/.rbenv directory:

      This will ensure that we are using the most up-to-date version of rbenv available.

      Step 6 – Uninstalling Ruby versions

      As you download additional versions of Ruby, you may accumulate more versions than you would like in your ~/.rbenv/versions directory. Use the ruby-buildplugin 's' uninstall subcommand to remove these previous versions.

      For example, typing this will uninstall Ruby version 2.1.3:

      With the rbenv uninstall command you can clean up old versions of Ruby so that you do not have more installed than you are currently using.

      Step 7 – Uninstalling rbenv

      If you've decided you no longer want to use rbenv, you can remove it from your system.

      To do this, first open your ~/.bashrc file in your editor:

      Find and remove the following two lines from the file:

      ~/.bashrc

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

      Save the file and exit the editor.

      Then remove rbenv and all installed Ruby versions with this command:

       rm -rf `rbenv root`
      

      Log out and back in to apply the changes to your shell.

      Conclusion

      In this tutorial you installed rbenv and Ruby on Rails. From here, you can learn more about making those environments more robust.

      Explore how to use Ruby on Rails with PostgreSQL or MySQL rather than its default sqlite3 database, which provide more scalability, centralization, and stability for your applications. As your needs grow, you can also learn how to scale Ruby on Rails applications across multiple servers.



      Source link