LoginSignup
10
11

More than 5 years have passed since last update.

【Vagrant/Rails】空のUbuntuからの環境構築

Last updated at Posted at 2017-08-10

vagrant sshまで

vagrant init

$ vagrant init bento/ubuntu-16.04

A `Vagrantfile` has been placed in this directory.

Vagrantfileの編集

以下の様にする。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/ubuntu-16.04"

  config.vm.network "forwarded_port", guest: 3000, host: 3000

  config.vm.network "private_network", ip: "192.168.33.10"

  config.vm.synced_folder ".", "/vagrant"
end

vagrant up

$ vagrant up
これで起動する。
パスワードが求められた場合はsudoのパスを。

vagrant ssh

$ vagrant ssh
これでvagrantにログインできる。

様々なinstall

基本ライブラリのinstall

$ sudo apt-get update
$ sudo apt-get install -y libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev

gitのinstall

$ wget https://www.kernel.org/pub/software/scm/git/git-2.5.1.tar.gz
$ tar -zxf git-2.5.1.tar.gz
$ cd git-2.5.1
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install
$ which git
$ git --version

rbenvのinstall

$ cd
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ sudo apt install -y ruby-dev
$ sudo apt-get install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

gitでrbenvを入れる理由

最新のrubyを入れようとした時にrbenvも最新版を持ってくる必要がある。
その際、gitで入れておいた方が、masterを持ってきやすい。

rbenvでRubyのInstall

$ sudo apt-get install -y libreadline-dev
$ echo $SHELL

これを忘れる方が多いので注意!
$ vim .bashrc
で、末尾に以下を追加する。

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
$ source ~/.bashrc #またはexit
$ which rbenv
$ rbenv --version
$ rbenv install 2.5.1
$ rbenv versions
$ rbenv global 2.5.1
$ rbenv rehash
$ rbenv version
$ which ruby # /home/vagrant/.rbenv/shims/ruby
$ ruby -v #これで2.5.1になることを確認
# 2.4.1じゃない場合
$ source ~/.bashrc #これで確認して2.5.1になっていたらok
$ which gem # /home/vagrant/.rbenv/shims/gem

rails newまでの下準備

下準備

$ sudo apt-get -y install libxslt-dev libxml2-dev
$ echo install: --no-rdoc --no-ri >> .gemrc
$ echo update:  --no-rdoc --no-ri >> .gemrc

mysqlのinstall

$ sudo apt-get install mysql-server

途中謎の画面になるが何も入力せずにenterを叩く

root loginができない。。。
以下で解決!!

$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
$ sudo service mysql stop
$ sudo rm -fr /var/lib/mysql
$ sudo mkdir /var/lib/mysql
$ sudo chown mysql:mysql /var/lib/mysql
$ sudo /usr/sbin/mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql

$ sudo service mysql restart
$ mysql -uroot #これでできるはず

Bundler install

$ rbenv exec gem install bundler
$ rbenv rehash

gemのinstall先のディレクトリの作成

共有ディレクトリにgemを入れると変な感じになる。

$ sudo mkdir /usr/local/src/bundles
$ sudo chown -R vagrant:vagrant /usr/local/src/bundles

bundle initからrails newまで

ディレクトリの作成

$ cd /vagrant/ # 共有ディレクトリへ
$ mkdir app_name
$ cd app_name

bundle init

$ bundle init

Gemfileを以下の様に書き換える

# frozen_string_literal: true
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails", "5.2.0"

bundle install

$ bundle install --path /usr/local/src/bundles/app_name --jobs=4
$ bundle exec rails new -B -d mysql -T -f . # 上書きしますか?的なのが出るけど、enterでok
$ vim Gemfile

以下の様に

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
gem 'capistrano-rails', group: :development

gem 'slim-rails'
# gem 'jquery-rails'

# gem 'omniauth'

gem 'meta-tags'
gem 'dotenv-rails'

# exceptionを通知
gem 'exception_notification', :github => 'smartinez87/exception_notification'
# Slack通知
gem 'slack-notifier'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

bundle installが正常に動作するまで、色々install

therubyracerのinstallでエラーする場合

$ sudo apt-get install build-essential g++
$ gem install therubyracer -v '0.12.3' -- --with-v8-dir
$ bundle install

mysqlのinstall

bundle installでエラーする

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /usr/local/src/bundles/road_shopping/ruby/2.4.0/gems/mysql2-0.4.8/ext/mysql2
/home/vagrant/.rbenv/versions/2.4.1/bin/ruby -r ./siteconf20170805-7177-14fs82y.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
checking for mysql_query() in -lmysqlclient... no

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/vagrant/.rbenv/versions/2.4.1/bin/$(RUBY_BASE_NAME)
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /usr/local/src/bundles/road_shopping/ruby/2.4.0/extensions/x86_64-linux/2.4.0-static/mysql2-0.4.8/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /usr/local/src/bundles/road_shopping/ruby/2.4.0/gems/mysql2-0.4.8 for inspection.
Results logged to /usr/local/src/bundles/road_shopping/ruby/2.4.0/extensions/x86_64-linux/2.4.0-static/mysql2-0.4.8/gem_make.out

An error occurred while installing mysql2 (0.4.8), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.8'` succeeds before bundling.

In Gemfile:
  mysql2
$ sudo apt-get install libmysqlclient-dev
$ gem install mysql2 -v '0.4.8'
$ bundle install

nodejsのinstall

$ sudo apt-get install -y nodejs npm
$ sudo npm cache clean
$ sudo npm install n -g
$ sudo n stable
$ sudo ln -sf /usr/local/bin/node /usr/bin/node
$ node -v
$ sudo apt-get purge -y nodejs npm
$ sudo npm install --global yarn #yarnのinstall

rails s

vagrantで起動したrailsアプリを、クライアント側のブラウザで表示するために、
$ bundle exec rails s -b 0.0.0.0

便利な小ネタ

gitの管理はクライアント側で。

githubとのやりとりは、vagrant側でなく、Mac側でやった方がよい。
アカウントをvagrant側で設定したり、公開鍵の登録とかが面倒なので。

aliasを設定しよう

以下をvagrant側の~/.bashrcの適当なところに貼り付け!

alias b='bundle exec'
alias brs='bundle exec rails s -b 0.0.0.0'
alias brc='bundle exec rails c'
10
11
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
10
11