Featured image of post Ruby Installation

Ruby Installation

Ruby installation, version management, syntax guide, package management, etc.

Installation

I prefer to use version management to install various version of Ruby so that I can follow any tutorial that I found anywhere.

Via Homebrew

For macOS run this command:

1
brew install rbenv ruby-build

Via Git

For Debian, Ubuntu, and their derivatives

  1. Clone rbenv into ‘~/.rbenv’ by running
    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
  2. Add this lines into ~/.bashrc
1
2
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
  1. Set up rbenv in your shell ~/.rbenv/bin/rbenv init and follow the printed instruction
  2. Restart your shell so that PATH changes take effect. Or run source ~/.bashrc
  3. Verify installation using rbenv-doctor: ~/.rbenv/bin/rbenv init the result shall be
1
2
3
4
5
6
7
8
Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /usr/local/bin/rbenv-install (ruby-build 20170523)
Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `~/.rbenv/versions'.
  You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK
  1. Install rbenv-build (optional)

Using rbenv-installer

  1. rbenv-installer via script
1
2
3
4
5
# with curl
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

# alternatively, with wget
wget -q https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer -O- | bash
  1. verify installation
1
2
3
4
5
# with curl
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor | bash

# alternatively, with wget
wget -q https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor -O- | bash

Using rbenv

All guidence for using rbenv can be found in this help command rbenv -h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List installed Ruby versions
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

Using Gem

Gem is package management in Ruby. Some guidance for Gem also can be found in gem -h. To install specific version of gem, you can follow this flow, the example of rails insstallation

Install Rails

Using Gem, you can install Rails, the most popular web framework in Ruby!

  1. Checking rails in local gem list rails --local
  2. If you do not find the desired version on local, you can install using command like gem install rails -v '5.2.3' -V --no-document.
  3. Not sure if the version is exist, check use gem search '^rails$' --all
  4. You also can check other installed gems gem list --local
  5. Generate new project with specific Rails version, for instance rails _6.1.4.1_ new appname
  6. Go to appname directory and check Rails version rails -v
  7. To run Rails server locally, use rails s

Comparison to Golang

As Ruby is my second language, it is easier for me to learn Ruby by comparing this syntax with language that I am familiar with :)

Ruby Golang
Gemfile.lock go.sum
Gemfile go.mod
bundle i go mod tidy
rails new app_name go mod init project_name

References:

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy