LoginSignup
0
0

More than 3 years have passed since last update.

Ruby on Rails ローカル環境構築 詰まったのでメモ

Posted at

記事を書いたきっかけ

Railsチュートリアルが一段落したのでローカルの環境開発を構築しようとしたところ、詰まりまくったのでメモ。

2021.4.26 Monday
Ruby on Railsの環境構築をしてみよう!(macOS)を参考にした
https://prog-8.com/docs/rails-env

Rubyのバージョン確認

rbenv versions
-bash: rbenv: command not found

まだRubyの開発環境が整ってないので、先にRubyをインストールする。

Rubyの開発環境を用意しよう!
https://prog-8.com/docs/ruby-env

Rubyがインストールされていることを確認

ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

デクストップ上にフォルダ「ruby_lesson」を開き、その中に「index.rb」ファイルを作成する。
index.rbにかきコードを記述する。

puts "Hello, World!"
puts 1 + 2

このコードの実行結果を確認するため、エディタ上でNew Terminalを開く。
index.rbを実行するため、

ruby index.rb

をターミナルで実行する。
ターミナル上に

Hello, World!
3

を表示される。
これで自分のPCでRubyのコードを書き、実行する環境が整えられた。
これでも問題ないが、実際の開発現場では、複数のRubyのバージョンを用いることがしばしばあり、「rbenv」というツールを用いてRubyをインストールすることが一般的。

Homebrewのインストール

  • 「Homebrew」をインストールする
  • 「Homebrew」を用いて「rbenv」をインストールする
  • 「rbenv」を用いてRubyをインストールする

まずはHomebrewのインストールから始める。
ターミナルで以下のコマンドを実行する。

brew -v
Homebrew 2.6.2
Homebrew/homebrew-core (git revision 842bf; last commit 2020-12-19)

Homebrewがインストールされていたので、rbenvのインストールに進む。

rbenvのインストール

Homebrewを用いてrbenvをインストールする。
まずrbenvがインストールされていないことを確認するため、以下のコマンドを実行する。

rbenv -v
-bash: rbenv: command not found

まだrbenvがインストールされていないので、以下のコマンドをターミナルで実行して、rbenvをインストールする。

brew install rbenv ruby-build

以下のコマンドを実行して、rbenvがインストールできたことを確認する。

rbenv -v
rbenv 1.1.2

これでOK。
次の章でrbenvを使用してRubyをインストールするが、その前に実行環境を確認し、インストールしたrbenveの設定をしておく。
以下のコマンドを実行する。

echo $SHELL
/bin/bash

実行結果が/bin/bashの場合、以下のコマンドを実行する。

echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

source ~/.bash_profile

これでrbenvの設定は完了!

Rubyのインストール

最後に、インストールしたrbenvを用いてRubyをインストールする。
rbenvでは複数のバージョンのRubyを使用できる。
どのバージョンのRubyがインストールできるか確認するため、以下のコマンドを実行する。

rbenv install --list
2.6.7
2.7.3
3.0.1
jruby-9.2.17.0
mruby-3.0.0
rbx-5.0
truffleruby-21.1.0
truffleruby+graalvm-21.1.0

今回は、2.6.7のバージョンのRubyをインストールする。
以下のコマンドを実行して、2.6.7のRubyをインストールする。

rbenv install 2.6.7

インストールが終わったら、以下のコマンドを実行する。

rbenv versions
* system (set by /Users/()/.rbenv/version)
  2.6.5
  2.6.7

このコマンドでは、現在インストールされているRubyを一覧で確認できる。
2.6.7が表示されているので、ちゃんとインストールできた。

では、今回インストールしたバージョンのRubyを使用するように設定を変更する。
以下のコマンドを実行する。

rbenv global 2.6.7

これで2.6.7のRubyが使えるようになった!
以下のコマンドを実行してみる。

ruby -v
ruby 2.6.7p197 (2021-04-05 revision 67941) [x86_64-darwin19]

正しく設定ができれば、2.6.7と表示されたはず。

———————————————————

Ruby環境の構築確認

以下のコマンドを実行する。

rbenv versions
  system
  2.6.5
* 2.6.7 (set by /Users/()/.rbenv/version)

今回インストールするバージョンは6.0.3(Railsチュートリアルに合わせる)。

Ruby on Railsのインストール

Railsの導入は至って簡単。
以下のコマンドを実行する。

gem install rails -v “6.0.3”

エラー発生。

ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

    current directory: /Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10/ext/mimemagic
/Users/()/.rbenv/versions/2.6.7/bin/ruby -rrubygems /Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/rake-12.3.3/exe/rake RUBYARCHDIR\=/Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mimemagic-0.3.10 RUBYLIBDIR\=/Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mimemagic-0.3.10
rake aborted!
Could not find MIME type database in the following locations: ["/usr/local/share/mime/packages/freedesktop.org.xml", "/opt/homebrew/share/mime/packages/freedesktop.org.xml", "/opt/local/share/mime/packages/freedesktop.org.xml", "/usr/share/mime/packages/freedesktop.org.xml"]

Ensure you have either installed the shared-mime-info package for your distribution, or
obtain a version of freedesktop.org.xml and set FREEDESKTOP_MIME_TYPES_PATH to the location
of that file.

This gem might be installed as a dependency of some bigger package, such as rails, activestorage,
axlsx or cucumber. While most of these packages use the functionality of this gem, some gems have
included this gem by accident. Set USE_FREEDESKTOP_PLACEHOLDER=true if you are certain that you
do not need this gem, and wish to skip the inclusion of freedesktop.org.xml.

The FREEDESKTOP_PLACEHOLDER option is meant as a transitional feature, and will be deprecated in
the next release.

Tasks: TOP => default
(See full trace by running task with --trace)

rake failed, exit code 1

Gem files will remain installed in /Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10 for inspection.
Results logged to /Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mimemagic-0.3.10/gem_make.out
似たような事例: https://hackmd.io/@mametter/mimemagic-info-ja
shared-mime-infoがインストールされていない場合のエラーの例:

Could not find MIME type database in the following locations:
["/usr/local/share/mime/packages/freedesktop.org.xml", “/opt/homebrew/share/mime/packages/freedesktop.org.xml”,
“/usr/share/mime/packages/freedesktop.org.xml”]
```
~~~~~~~~~~

Rails 5.2.5 / 6.0.3.6 / 6.1.3.1はmimemagicに依存しなくなったので、これらにバージャンアップする。

gem install rails -v “6.0.3.6”



rails -vを叩いても、インストールされないと出る。
```
Rails is not currently installed on this system. To get the latest version, simply type:

$ sudo gem install rails

You can then rerun your "rails" command.
```

似たような事例: https://qiita.com/amuyikam/items/313bc89c1de320a4257e


gem install rails -v “6.0.3.6”でエラー発生。

ERROR: While executing gem ... (Errno::EACCES)
Permission denied @ rb_sysopen - /Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/rails-6.0.3.6/README.md
(base) MacBook-Pro:~ ()$ which gem
/Users/()/.rbenv/shims/gem

~~~~~~~~~~~
似たような事例: https://qiita.com/nachiguro1003/items/4b564b92eca3ba35744a

sudo chown -R (ユーザ名):staff /Users/(ユーザ名)/.rbenv

で解決。
~~~~~~~~~~~



6.0.3.6がインストールされない。
一度アンインストールしてみる。
~~~~~~~~~~~~
似たような事例: https://qiita.com/owgll/items/e5c61b80a5fbce19a72f

gem uninstall railties -v '6.1.3.1'

~~~~~~~~~~~~


今回は、バージョン6.0.3のRailsをインストールする。
インストールが完了したら、Railsのバージョンを確認してみよう。
以下のコマンドを実行する。

rails -v
Rails 6.0.3.6

Rails6.0.3.6と表示sれたので、Railsのインストールは完了。

Railsアプリの作成

Railsアプリを新規作成使用。
以下のコマンドを使用する。

rails new アプリケーション名

今回はsample_0426という名前で作成する。

rails new sample_0426

lsコマンドでアプリフォルダが作成されたことを確認しよう。

ls
Applications                Pictures
Desktop                 Sites
Documents               VirtualBox VMs
Downloads               iCloud Drive(アーカイブ)
HTML                    iCloud Drive(アーカイブ) - 1
Library                 iCloud Drive(アーカイブ) - 2
Movies                  mysite
Music                   sample_0426
MyFirstGame             結婚式
New Unity Project           ドキュメント
OneDrive

ローカルでRailsサーバを立てる

Railsサーバを立てて、ローカルでアクセスしてみよう。

先程作成したsample_0426に以下のコマンドで移動する。

cd sample_app

Railsのサーバを立てる時には以下のコマンドを実行する。

rails s


エラー発生。

=> Booting Puma
=> Rails 6.0.3.6 application starting in development
=> Run `rails server --help` for more startup options
Exiting
/Users/()/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/webpacker-4.3.0/lib/webpacker/configuration.rb:95:in `rescue in load': Webpacker configuration file not found /Users/()/sample_0426/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /Users/()/sample_0426/config/webpacker.yml (RuntimeError)
...
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

とりあえず、インストールしてなかったYarnをインストールしてみる。
~~~~~~~~~~~~~~~~~~~~~~~~~~
参考: https://classic.yarnpkg.com/en/docs/install/#mac-stable

yarnをインストール。

npm install --global yarn

ついでにnpmをupdate。

npm install -g npm

yarnのバージョン確認。

yarn --version
1.22.10

~~~~~~~~~~~~~~~~~~~~~~~~~~

上記後、エラー変わらず。
~~~~~~~~~~~~~~~~~
似たような事例; https://qiita.com/NaokiIshimura/items/8203f74f8dfd5f6b87a0

webpackerをインストールする。

rails webpacker:install 
***************

サーバーが立ったことが確認できたら、実際にアクセス使用。
    ブラウザを開いてURLの場所に

localhost:3000

と入力してみると、アクセスできる。

Railsサーバーを止めたい時は、

control + c

で止められる。
0
0
1

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
0