AWS cloud9からローカル環境へRails環境を移行

Rails Tutlial を4章まで進めて以降、諸事情で再開まで3ヶ月ほど経ったので再開がてらローカル開発環境を構築します。

これまでは公式ドキュメントのお勧め通りにAWS cloud9 を使用。

MacOS:Mojave 10.14.4

まずはrails のインストール

$ printf "install: --no-rdoc --no-ri\nupdate: --no-rdoc --no-ri\n" >> ~/.gemrc
$ gem install rails -v 5.1.6
Fetching: concurrent-ruby-1.1.5.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.

早速エラーになったので調べる。

gem installでpermissionエラーになった時の対応方法

上記を参考に対応。

$ which gem
/usr/bin/gem
$ which ruby
/usr/bin/ruby
$ brew update
-bash: brew: command not found

Homebrewが先に必要だった。

参考:macOSにHomebrewをインストール

xcode を入れる。

$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

不要だった。別の作業してた時にコマンドラインツールを入れてたらしい。

Homebrew パッケージマネージャを入れる。

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew doctor
$ brew update
Already up-to-date.

rbenv Rubyバージョンを管理してくれるものを入れる。

$ brew install rbenv ruby-build
$ rbenv install -l
$ rbenv install 2.5.3
ruby-build: use openssl from homebrew
Downloading ruby-2.5.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.3.tar.bz2
Installing ruby-2.5.3...
Installed ruby-2.5.3 to /Users/sugeta/.rbenv/versions/2.5.3

rbenv のバージョンを確認。

$ rbenv versions
* system (set by /Users/sugeta/.rbenv/version)
2.5.3

使われるものを変更。

$ rbenv global 2.5.3
$ rbenv versions
system
* 2.5.3 (set by /Users/sugeta/.rbenv/version)

続いて環境変数に追加。

$ cd ~
$ vi .bash_profile
[[ -d ~/.rbenv ]] && \
export PATH=${HOME}/.rbenv/bin:${PATH} && \
eval "$(rbenv init -)"

参考:rbenvによるRubyのインストールからHello, World!まで

参考:.bash_profileと.bashrcのまとめ

$ gem install rails -v 5.1.6
$ rails -v
Rails 5.1.6
$ cd
$ mkdir environment
$ cd environment/
$ rails new testapp
$ cd testapp
$ bundle exec rails s

http://localhost:3000/

ここまででとりあえずRails環境構築OK! このあと、cloud9上で作ったものを持って来たい・・・

$ git config --global user.name "hogehoge"
$ git config --global user.email hogehoge@gmail.com

  bitbucket側で非公開のチェックを外してからgit cloneしたら行けた。

$ git clone https://hogehoge@bitbucket.org/hogehoge/sample_app.git
$ ls sample_app/
Gemfile     Rakefile    config.ru   package.json    vendor
Gemfile.lock    app     db      public
Guardfile   bin     lib     test
README.md   config      log     tmp

あとはbundle install したいので

$ less Gemfile.lock
BUNDLED WITH
   1.17.3

bundler -v 1.17.3 を入れる

$ gem install bundler -v 1.17.3
Fetching: bundler-1.17.3.gem (100%)
Successfully installed bundler-1.17.3
1 gem installed

AWS cloud9 上ではHiroku用にproductionでpostgres、developmentでsqliteを使う設定になっていたので以下のようにする

$ bundle install --path vendor/bundle --without production
$ bundle exec rails s

http://localhost:3000/

workした!

続いてbitbucketからgithubへの移行をする。やり方はシンプルだった。

https://import.github.com/

ここで以下例のbitbucketリポジトリを入力。 https://hogehoge@bitbucket.org/hogehoge/sample_app.git

git push の先をbitbucketからgithubに変更した。

git remote set-url origin https://github.com/hogehoge/fugafuga.git

Heroku の設定

$ brew install heroku/brew/heroku
$ heroku --version

ログインとSSHキーの追加

$ heroku login
$ heroku keys:add

Herokuでアプリを作成しようとしたところ5個に達してるとのエラーあり

$ heroku create
Creating app... !
 ▸    You've reached the limit of 5 apps for unverified accounts. Delete some apps or add a credit card to verify your account.

現在のアプリを以下のコマンドで確認

$ heroku apps

以下形式のコマンドで不要なものを削除

$ heroku apps:destroy --app アプリ名 --confirm アプリ名

改めてアプリ作成して成功 URLにアクセスして「Heroku | Welcome to your new app!」と表示されることを確認

$ heroku create
Creating app... done, ⬢ secret-taiga-xxxxx
https://secret-taiga-xxxxxx.herokuapp.com/ | https://git.heroku.com/secret-taiga-xxxxx.git
````

Heroku にプッシュして動作を確認

$ git push heroku master

ここまで!想定してた作業が一区切りついていい感じ。