[Ruby] [Ruby] Building a Ruby development environment on Ubuntu

5 minute read

Introduction

I tried building the environment of Ruby as of July 7, 2020, so I will transcribe it. There are quite a lot of people writing articles, but if you output them yourself, that alone will help you study.

Installation environment

  • Ubuntu 20.04 LTS (64bit) -Installed in Windows 10 Virtual Box.

(Note) There is “RVM” in the same Ruby version control tool, but rbenv and RVM are incompatible, so prepare an environment without “RVM”.

What to put

We also include a tool called rbenv that can manage multiple Ruby versions. Not only can you enter multiple Ruby versions, but you can also specify the Ruby version to use for each project. Also, ruby-build will be introduced although it is treated as an option (?) of rbenv.

  • rbenv 1.1.2-30-gc879cb0 -ruby-build
  • Ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

*I think both are the latest stable versions as of July 7, 2020.

Big flow

The flow is like this. Install git. 1. Introduce necessary tools and packages in advance. 2.Install rbenv and set environment

  1. Installation of ruby-build
  2. Install Ruby

Step 0. Install git

Although git itself is not directly related to environment building, it will be downloaded from github when installing rbenv and ruby-build. For this reason, please put git in advance. The installation method is written in here, but it is completed with one apt-get command. *If necessary, put sudo on your head.

apt-get install git

Step 1. Install required tools and packages

Details are written on here, but it’s in English, so… When downloading and installing the required Ruby version with rbenv+ruby-build, a compilation error may occur depending on the environment, so at least the following packages should be included in advance. That’s right.

To put it all at once, copy and paste the long command below to execute it. *If necessary, put sudo on your head.

apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev

If you cannot use “libgdbm6”, try using “libgdbm5”. Regarding what each package is, there are many things that I do not know well even if I look it up, rather than knowing that it will not be a scene to use when necessary, please check each one if you are interested. ..

[Supplement] List of required packages in advance (I did my own research)
*It is the one described in ruby-build as of July 7, 2020. | Package name | What is this? | Reaction when I put it in my environment individually | |:--|:--|:--| | autoconf | Tool for automatically generating configure scripts | | bison | A type of parser generator. It seems to be used when compiling | | | build-essential | As the name implies, it seems to contain a set of build tools required for development. | | libssl-dev | SSL related development toolkit | | | libyaml-dev | YAML-related development library | | libreadline6-dev | It seems that it is a GNU readline library, but I don't know what it does... | libreadline-dev was recommended and libncurses-dev was installed as a dependency | | zlib1g-dev | Library for compression (gzip, pkzip) | | libncurses5-dev | Development library for ncurses. It provides an API for creating a text user interface (TUI) in a terminal-independent format | | | libffi-dev | Library for Foreign Function Interface. I think it is used to call methods written in other languages. | | libgdbm6 | GNU dbm database routine (runtime version) | | libgdbm-dev | GNU dbm database routines (development files) | | | libdb-dev | Berkeley Database Libraries [development] | → libdb5.3-dev is additionally installed. |

Step 2. Install rbenv and configure environment

2-1. Installation of rbenv

To install rbenv, download it from github using git clone and put it in the .rbenv folder of your home directory.

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

2-2. Pass rbenv environment setting 1 path

Pass the path to ~/.rbenv/bin as the environment setting for rbenv. Official says that you should add path setting to .bashrc for Ubuntu desktop environment. So, if you execute the command as below, it’s OK.

echo'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
[Supplement] When writing in .profile instead of .bashrc
I personally feel that writing the path settings in .bashrc is a bit clumsy. I think it's probably .profile or .bash_profile when writing environment variables, so I wrote it in .profile in my environment. When writing the path setting in .profile, open .profile in an editor and add the following code. ``` if [-d "$HOME/.rbenv/bin" ]; then PATH="$HOME/.rbenv/bin:$PATH" fi ```

2-3. rbenv environment setting 2 Put rbenv init-in .bashrc.

Execute the following command to execute rbenv init - in .bashrc.

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

After that, reflect the settings.

source ~/.bashrc
[Supplement] About including rbenv init-in .bashrc
The `rbenv init -` command recreates the shims directory and does rehash processing. In this command, the path to shims is added to $PATH. So, it's kind of unpleasant to put it in .bashrc, but I've done a lot of other processing, so I followed the official instructions. * If you put it in .bashrc, every time you change it and reflect the setting in `source ~/.bashrc`, the path will be added as double, triple, ..., but change .bashrc However, doing `source ~/.profile` is also strange.

Step 3. Install ruby-build

Install ruby-build. ruby-build is also published on github, so just like rbenv, download it with git clone. The location is under the ~/.rbenv/plugins/ directory.

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

Now you can use the rbenv install command.

Step 4. Install ruby

You can install each version of ruby with the rbenv install command.

First, check the stable version that can be installed with rbenv install -l.

$ rbenv install -l
2.5.8
2.6.6
2.7.1
jruby-9.2.12.0
maglev-1.0.0
mruby-2.1.1
rbx-5.0
truffleruby-20.1.0

Only latest stable releases for each Ruby implementation are shown.
Use'rbenv install --list-all' to show all local versions.

The latest stable version of Ruby as of 2020/7/7 is like 2.7.1, so specify the version when you install it.

rbenv install 2.7.1

After installation, use rbenv global to set it as the default ruby version.

rbenv global 2.7.1

Finally, execute rbenv rehash to reflect the installed ruby version in shims. In the future, it seems better to do rbenv rehash when inserting another version of ruby or adding a gem.

rbenv rehash

If there are no errors, you can now run ruby. You can check the version with’ruby -v’.

$ ruby -vRuby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Also, use “irb” to display “Hello, World!”.

$irb
irb(main):001:0> puts "Hello, World!"
Hello, World!
=> nil
irb(main):002:0>