Rubyの環境構築
1. Command Line Toolsをインストール
Command Line Toolsのインストール$ xcode-select --install
パスを確認
$ xcode-select --print-path
"インストール"を選択し、規約の同意後にインストール開始
※Xcodeは今回の目的でない為、入手しない
インストール完了
既にXcodeがインストール済みの場合
Command Line Toolsが2つ存在する為、念のため切替$ sudo xcode-select --switch /Library/Developer/CommandLineTools/
確認
$ xcode-select --print-path /Library/Developer/CommandLineTools/ であればOK
XcodeのCommand Line Toolsへ戻す場合
$ sudo xcode-select --switch /Applications/Xcode.app
2. HomeBrewのインストール
公式サイトよりコマンドを入手入手したコマンドをターミナルで実行
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Enterキーを入力
Press RETURN to continue or any other key to abort
PCログイン用のパスを入力
Password:
インストール成功
==> Installation successful! ==> Homebrew has enabled anonymous aggregate user behaviour analytics. Read the analytics documentation (and how to opt-out) here: https://docs.brew.sh/Analytics.html ==> Next steps: - Run `brew help` to get started - Further documentation: https://docs.brew.sh
バージョンの確認
$ brew -v
Homebrew 1.4.1
設定ファイルへパスを通す
$ echo 'export PATH=/usr/local/bin:$PATH' >> .bash_profile
再読み込み
$ source ~/.bash_profile
3. rbenvのインストール
rbenvのインストール$ brew install rbenv
インストール完了
==> Summary 🍺 /usr/local/Cellar/openssl/1.0.2n: 1,792 files, 12.3MB ==> Installing rbenv dependency: ruby-build ==> Downloading https://github.com/rbenv/ruby-build/archive/v20171226.tar.gz ==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v201712 ######################################################################## 100.0% ==> ./install.sh 🍺 /usr/local/Cellar/ruby-build/20171226: 373 files, 191.3KB, built in 3 seconds ==> Installing rbenv ==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.1.1.high_sierra.bot ######################################################################## 100.0% ==> Pouring rbenv-1.1.1.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/rbenv/1.1.1: 36 files, 62.7KB
バージョンの確認
$ rbenv -v
rbenv 1.1.1
設定ファイルへPATHを通す
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> .bash_profile $ echo ‘eval “$(rbenv init -)“’ >> ~/.bash_profile
設定ファイルを再読み込み
$ source ~/.bash_profile
再読み込みの完了
-bash: /Users/ユーザ名/.bash_profile: line 1: syntax error near unexpected token `(' -bash: /Users/ユーザ名/.bash_profile: line 1: `‘eval “export PATH="/Users/ユーザ名/.rbenv/shims:${PATH}" export RBENV_SHELL=bash source '/usr/local/Cellar/rbenv/1.1.1/libexec/../completions/rbenv.bash' command rbenv rehash 2>/dev/null rbenv() { local command command="$1" if [ "$#" -gt 0 ]; then shift fi case "$command" in rehash|shell) eval "$(rbenv "sh-$command" "$@")";; *) command rbenv "$command" "$@";; esac }“’'
4. Rubyのインストール
インストールできるバージョンの確認$ rbenv install -l
公式ページより安定版を確認の上、今回は最新の2.5.0でインストール
※数分かかるので放置
$ rbenv install 2.5.0
インストールの完了
ruby-build: use openssl from homebrew Downloading ruby-2.5.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2 Installing ruby-2.5.0... Installed ruby-2.5.0 to /Users/lunaten/.rbenv/versions/2.5.0
バージョンの確認
$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [universal.x86_64-darwin17]
Rubyの標準バージョンを設定
$ rbenv global 2.5.0
5. Railsのインストール
Bundlerのインストール$ gem install bundler
インストールの完了
Fetching: bundler-1.16.1.gem (100%) Successfully installed bundler-1.16.1 Parsing documentation for bundler-1.16.1 Installing ri documentation for bundler-1.16.1 Done installing documentation for bundler after 2 seconds 1 gem installed
バージョンの確認
$ bundler -v
Bundler version 1.16.1
Railsのインストール
$ gem install rails
インストールの完了
Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, rack, rack-test, mini_portile2, nokogiri, crass, loofah, rails-html-sanitizer, rails-dom-testing, builder, erubi, actionview, actionpack, activemodel, arel, activerecord, globalid, activejob, mini_mime, mail, actionmailer, nio4r, websocket-extensions, websocket-driver, actioncable, thor, method_source, railties, sprockets, sprockets-rails, rails after 41 seconds 35 gems installed
リハッシュする
※~/.rbenv/versions/x.x.x/bin/下のコマンドを~/.rbenv/shims/ へ複製する
$ rbenv rehash
バージョン確認
$ rails -v
Rails 5.1.4
6. アプリケーションの作成
ディレクトリを作成$ mkdir rails
ディレクトリへ移動
$ cd rails
アプリケーションを作成
$ rails new sample
アプリケーションへ移動
$ cd sample
サーバーを起動する
rails s
http://localhost:3000へアクセス
サーバーを終了する
"Ctrl + C"キーを入力