Ubuntu24.04 最新のRubyをインストールする

Ubuntu24.04 最新のRubyをインストールする

Ubuntu24.04に最新のRubyをインストールするまでの手順を記述してます。rails7も実行します。

環境

  • OS Ubuntu24.04

必要パッケージのインストール

事前に必要なパッケージのインストールをしておきます。まずは、アップデートしておきます。

$ sudo apt update

必要パッケージをインストールします。

$ sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-de

rbenvをインストール

次に、rubyのversion管理であるrbenvをインストールします。gitからcloneします。

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

実行します。

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

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
}

バージョンを確認します。

$ rbenv -v
rbenv 1.2.0-91-gc3ba994

ruby-build インストール

rbenvのプラグイン であるruby-buildもインストールしておきます。プラグイン用のディレクトリを作成して移動します。

$ mkdir -p ~/.rbenv/plugins
$ cd ~/.rbenv/plugins

gitで入手します。

$ git clone https://github.com/sstephenson/ruby-build.git

Cloning into 'ruby-build'...
remote: Enumerating objects: 16226, done.
remote: Counting objects: 100% (4798/4798), done.
remote: Compressing objects: 100% (546/546), done.
remote: Total 16226 (delta 4514), reused 4445 (delta 4233), pack-reused 11428
Receiving objects: 100% (16226/16226), 3.15 MiB | 12.33 MiB/s, done.
Resolving deltas: 100% (11600/11600), done.

コマンドを確認します。

$ ls ~/.rbenv/plugins/ruby-build/bin
rbenv-install  rbenv-uninstall  ruby-build

Rubyインストール

rubyのインストールを行います。まずは、インストールできるバージョンを確認します。

$ rbenv install -l
3.1.6
3.2.4
3.3.3
jruby-9.4.7.0
mruby-3.3.0
picoruby-3.0.0
truffleruby-24.0.1
truffleruby+graalvm-24.0.1

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

ここでは最新の「3.3.3」をインストールします。

$ rbenv install 3.3.3
==> Downloading ruby-3.3.3.tar.gz...
-> curl -q -fL -o ruby-3.3.3.tar.gz https://cache.ruby-lang.org/pub/ruby/3.3/ruby-3.3.3.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21.0M  100 21.0M    0     0  14.4M      0  0:00:01  0:00:01 --:--:-- 14.4M
==> Installing ruby-3.3.3...
-> ./configure "--prefix=$HOME/.rbenv/versions/3.3.3" --enable-shared --with-ext=openssl,psych,+
-> make -j 2
-> make install
==> Installed ruby-3.3.3 to /home/foo/.rbenv/versions/3.3.3

NOTE: to activate this Ruby version as the new default, run: rbenv global 3.3.3

バージョンを設定してから、バージョンを確認してみます。

$ rbenv global 3.3.3
$ ruby -v
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [x86_64-linux]

gemもアップデートしておきます。

$ gem update --system
Fetching rubygems-update-3.5.13.gem
Successfully installed rubygems-update-3.5.13
Parsing documentation for rubygems-update-3.5.13
Installing ri documentation for rubygems-update-3.5.13
Done installing documentation for rubygems-update after 1 seconds
Parsing documentation for rubygems-update-3.5.13
Done installing documentation for rubygems-update after 0 seconds
Installing RubyGems 3.5.13
  Successfully built RubyGem
  Name: bundler
  Version: 2.5.13
  File: bundler-2.5.13.gem
Bundler 2.5.13 installed
RubyGems 3.5.13 installed
Regenerating binstubs
Regenerating plugins
Parsing documentation for rubygems-3.5.13
Installing ri documentation for rubygems-3.5.13

# 3.5.13 / 2024-06-14

## Enhancements:

* Installs bundler 2.5.13 as a default gem.

## Bug fixes:

* Never remove executables that may belong to a default gem. Pull request
  [#7747](https://github.com/rubygems/rubygems/pull/7747) by
  deivid-rodriguez

# 3.5.12 / 2024-06-13

## Enhancements:

* Installs bundler 2.5.12 as a default gem.

## Bug fixes:

* Fix `gem uninstall` unresolved specifications warning. Pull request
  [#7667](https://github.com/rubygems/rubygems/pull/7667) by
  deivid-rodriguez
* Fix `gem pristine` sometimes failing to pristine user installed gems.
  Pull request [#7664](https://github.com/rubygems/rubygems/pull/7664) by
  deivid-rodriguez


------------------------------------------------------------------------------

RubyGems installed the following executables:
        /home/foo/.rbenv/versions/3.3.3/bin/gem
        /home/foo/.rbenv/versions/3.3.3/bin/bundle
        /home/foo/.rbenv/versions/3.3.3/bin/bundler

Ruby Interactive (ri) documentation was installed. ri is kind of like man 
pages for Ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

RubyGems system software updated

.gemrc の作成

余分なドキュメントのインストールをしないように設定しておきます。

$ cd ~

$ nano .gemrc

下記の内容で編集しておきます。

install: --no-ri --no-rdoc
update: --no-ri --no-rdoc

Railのインストール

railsもインストールしてみます。

$ gem install rails

実行してみます。

## ディレクトリ作成
$ mkdir ~/rails
$ cd rails

## プロジェクト作成
$ rails new test-project

## 移動
$ cd test-project

## 実行
$ rails s -b 0.0.0.0

http:://localhost:3000にアクセスすると下記の画面が表示されます。