実際に試みた環境は Lubuntu 24.04 LTS です。
まずは
$ sudo apt-get install build-essential $ sudo apt-get install git
続いて rbenv を使えるようにする。
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv $ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile $ source ~/.bash_profile $ rbenv --version rbenv 1.3.2
~/.bashrcに以下を追加。
export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
以下を実行。
$ source ~/.bashrc
これで rbenv が使えるようになった。
次に、Ruby をインストールする。
まずは必要なパッケージをインストールする。以下は一例(もっとあるかも知れない、例えばlibreadline6-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-devなど)。
$ sudo apt-get install libssl-dev libreadline-dev zlib1g-dev libyaml-dev
インストールできる Ruby のバージョンを表示させる。
$ rbenv install --list 3.1.6 3.2.7 3.3.7 3.4.2 jruby-9.4.12.0 mruby-3.3.0 picoruby-3.0.0 truffleruby-24.1.2 truffleruby+graalvm-24.1.2 Only latest stable releases for each Ruby implementation are shown. Use `rbenv install --list-all' to show all local versions.
Ruby のインストール。
$ rbenv install 3.4.2 ... ==> Installed ruby-3.4.2 to /home/tomoki/.rbenv/versions/3.4.2 NOTE: to activate this Ruby version as the new default, run: rbenv global 3.4.2
これでインストール成功。
$ rbenv global 3.4.2 $ ruby -v ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-linux] $ rbenv versions * 3.4.2 (set by /home/tomoki/.rbenv/version)
インストールできる Ruby のバージョンが古い場合、ruby-buildを更新する。
$ cd ~/.rbenv/plugins/ruby-build $ git pull $ rbenv install --list
Bundler で Gem をインストールする
隔離した Ruby の環境を作る。ここに Gem をインストールすることになる。
$ mkdir ~/Documents/Ruby $ cd ~/Documents/Ruby $ rbenv local 3.4.2 $ bundle init Writing new Gemfile to /home/tomoki/Documents/Ruby/Gemfile
これでRubyディレクトリに Gemfile が生成された。
Gemfile を編集したのち、以下。
$ bundle install --path vendor/bundle [DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set path 'vendor/bundle'`, and stop using this flag Fetching gem metadata from https://rubygems.org/........ Resolving dependencies... ...
と思ったら--pathは非推奨らしいので、以下の方がよいようだ。
$ bundle config set path 'vendor/bundle'
確認。
$ bundle config Settings are listed in order of priority. The top value will be used. path Set for your local app (/home/tomoki/Documents/Ruby/.bundle/config): "vendor/bundle"
Gem のインストール・エラーが出た場合は、解決したあとに
$ bundle install Bundle complete! 10 Gemfile dependencies, 41 gems now installed. Bundled gems are installed into `./vendor/bundle` $ bundle list ...
で終了。