LoginSignup
2
1

More than 3 years have passed since last update.

DebianにRubyを入れる方法

Posted at

リモートサーバー(OSはDebian)にrbenvを使ってRubyをインストールしました。少々手こずったので、手順をまとめてみようと思います。

git cloneコマンドを使うので、Gitをインストールしていない方は、はじめに下記コマンドでインストールしてください。

$ sudo apt install git-all
1. 下記コマンドで、githubのリポジトリからクローンを作成します。
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
2. パスを通します。
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
3. 下記コマンドを実行します。
$ ~/.rbenv/bin/rbenv init

コマンドを実行すると、メッセージが表示されます。

# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"
4. 表示されたメッセージに従って、下記コマンドを実行します。
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
5. PATHの変更を有効にするために、shellを再起動します。

exitコマンドでログアウトして、再度ログインすればオッケーです。

しかし、ログインし直すとユーザー名が緑色から白色に変わるかもしれません。
その理由は一番最後に説明します。

6. rbenv installコマンドを使えるようにするために、ruby-buildをインストールします。
$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
7. 現時点でrbenv installコマンドを実行するとエラーになってしまいます。
$ rbenv install 2.7.0
Downloading ruby-2.7.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.bz2
Installing ruby-2.7.0...

BUILD FAILED (Debian 10 using ruby-build 20200224-6-gd8019fe)

Inspect or clean up the working tree at /tmp/ruby-build.20200331004824.8898.ONezFx
Results logged to /tmp/ruby-build.20200331004824.8898.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.20200331004824.8898.ONezFx/ruby-2.7.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
8. エラーを解決するために、下記のコマンドを実行します。
$ apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev

ruby-buildのwikiに、ちゃんとrubyのインストールをできるようにするビルド環境を作るためのコマンドがOSごとに書いてあります。

9. 再度rbenv installコマンドを実行してみましょう。今度はエラーが出ずにうまくいくはずです。少し時間がかかります。
$ rbenv install 2.7.0
Downloading ruby-2.7.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.bz2
Installing ruby-2.7.0...
Installed ruby-2.7.0 to /home/xxx/.rbenv/versions/2.7.0
10. 最後にrbenv globalコマンドで、バージョンを指定すれば完了です。
$ rbenv versions
  2.7.0
$ rbenv global 2.7.0
$ rbenv versions
* 2.7.0 (set by /home/xxx/.rbenv/version)
参考

ログインし直すとユーザー名の色が変わった理由

Debianにログインすると、以下のファイルを順番に探して、最初に見つけたファイルのみを読み込ます。

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile (このファイルには、.bashrcを読み込む記述がある)

自分の環境では、最初は.profileしか存在しなかったのですが、rbenvのパスを通すときに実行した以下のコマンドで.bash_profileが作成されました。

$$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

それにより、ログインし直すと、色の設定が書かれていた.profileが読みこまれなくなり、ユーザー名の色が変わりました。
.bash_profileファイルに以下の記述を追加することで、ログインしたときに.profileの内容も読みこまれるようになり、ユーザー名の色が緑色に戻ります。

source ~/.profile
参考
2
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
2
1