0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【2025年最新版】Ubuntuで詰まらないRuby仮想環境構築

0
Posted at

はじめに

Ubuntuを使っている(もしくはWSL)向けに、Rubyの仮想環境rbenvを用いた環境構築手順をメモします。

RubyはPythonを扱うときと同じように、仮想環境を活用して管理すると便利です。

構築環境

OS: Ubuntu 24.04 LTS

Ruby: 3.2.8

仮想環境: rbenv

Rails: 8.x 系想定

 sudo apt install ruby を直接行わないほうが環境構築不可能にならなくていいです。

構築手順

1. 依存ライブラリのインストール

Ruby を ソースビルドするので必須になります。

sudo apt update

sudo apt install -y \
  build-essential \
  libssl-dev \
  libreadline-dev \
  zlib1g-dev \
  libyaml-dev \
  libffi-dev \
  libxml2-dev \
  libxslt1-dev \
  libicu-dev \
  git curl

2. rbenv をインストール(apt版)

sudo apt install -y rbenv

シェル初期化(bash)

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

確認:

rbenv --version

3. ruby-build を使う

公式版を rbenv plugin として導入:

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

4. Ruby をインストール

rbenv install 3.2.8

成功すると:

Installed ruby-3.2.8 to ~/.rbenv/versions/3.2.8

5. プロジェクトごとに Ruby を固定

cd [your_project]
rbenv local 3.2.8

.ruby-version が生成されます。

確認:

ruby -v
which ruby
  1. bundler をインストール
gem install bundler
rbenv rehash

確認:

which bundle
~/.rbenv/shims/bundle

7. bundle install

bundle install

gem はすべて、

~/.rbenv/versions/3.2.8/lib/ruby/gems/...

に入ります。

よくあるエラーと対処

  • apt 版 ruby-build を使っている
    -> gitからクローンしましょう。

まとめ

上記のような手順でrbenvを用いてRubyの仮想環境を構築し、バージョンの管理をすると簡単にできて、便利かと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?