[Ruby] Install ruby on Ubuntu 20.04 with rbenv

3 minute read

environment

-OS: Ubuntu 20.04 -Ruby version: 2.5.8

As a prerequisite, a virtual environment created on VMware. Install ruby using rbenv from a fresh OS installation.

Install Ruby on Ubuntu

Reference article is here. First, install git. For the time being, make sure that git is not installed by default.

$ git

The command'git' cannot be found. You can install it with:

sudo apt install git

Then install.

$ sudo apt install git

For the time being, leave the installation log as text. After installation, make the following settings.

$ git config --global user.name [username]
$ git config --global user.email [email address]

Since git installation is completed, install Ruby from here. First, download rbenv.

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

Add the following description to .bashrc (you can write a string to a file with echo’string’ »‘filename’).

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

After running, if you check the contents of .bashrc with an editor, you can see that the echoed contents are added to the end of the file. captcher01.png

Read the contents of .bashrc with the source command.

$ source ~/.bashrc

Check installable Ruby version

$ 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
truffleruby+graalvm-20.1.0

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

It seems that you can install only 2.5 to 2.7 within the support range. Although the version is different from the reference book, I decided to install it in 2.5.8.

$ rbenv install 2.5.8

Downloading ruby-2.5.8.tar.bz2...
- > https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

WARNING: ruby-2.5.8 is nearing its end of life.
It only receives critical security updates, no bug fixes.


BUILD FAILED (Ubuntu 20.04 using ruby-build 20200727)

Inspect or clean up the working tree at /tmp/ruby-build.20200728011639.10883.ljBQV0
Results logged to /tmp/ruby-build.20200728011639.10883.log

Last 10 log lines:
checking for ruby... false
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20200728011639.10883.ljBQV0/ruby-2.5.8':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

I got an error. There seems to be an error saying that the C compiler is not included! So, try installing gcc.

$ sudo apt install gcc

It ended normally. Again, the installation log is recorded in text for the time being.

Install ruby again with rbenv

$ rbenv install 2.5.8

Downloading ruby-2.5.8.tar.bz2...
- > https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

WARNING: ruby-2.5.8 is nearing its end of life.
It only receives critical security updates, no bug fixes.


BUILD FAILED (Ubuntu 20.04 using ruby-build 20200727)

Inspect or clean up the working tree at /tmp/ruby-build.20200728013413.16282.OsujhX
Results logged to /tmp/ruby-build.20200728013413.16282.log

Last 10 log lines:
checking for _setjmp as a macro or function... yes
checking for sigsetjmp as a macro or function... no
checking for setjmp type... __builtin_setjmp
checking for prefix of external symbols... NONE
checking pthread.h usability... yes
checking pthread.h presence... yes
checking for pthread.h... yes
checking if make is GNU make... ./configure: line 27352: make: command not found
no
checking for safe null command for make... configure: error: no candidate for safe null command

I got an error again. This time make is not installed! Error.

  • Make: Command that is indispensable when compiling source files on Linux. Apparently. So let’s install make.
$ sudo apt install make

As usual, the installation log is kept in text.

This time, install ruby with rbenv.

$ rbenv install 2.5.8
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
Try running `apt-get install -y libssl-dev libreadline-dev zlib1g-dev` to fetch missing dependencies.

Again, the error seems to be because “openssl”, “readline” and “zlib” are not included. For the time being, try to execute as instructed.

$ apt-get install -y libssl-dev libreadline-dev zlib1g-dev

This completes without problems. Install again with rbenv.

$ rbenv install 2.5.8
Downloading ruby-2.5.8.tar.bz2...
- > https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

WARNING: ruby-2.5.8 is nearing its end of life.
It only receives critical security updates, no bug fixes.

Installed ruby-2.5.8 to /home/kei/.rbenv/versions/2.5.8

I was able to install it!

Finally, specify the version with rbenv and complete.

$ rbenv global 2.5.8

Also check the version.

$ ruby -v
ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]