Skip to content

Instantly share code, notes, and snippets.

@apjyotirmay
Forked from sandys/damn-fast-ruby-setup.sh
Last active August 15, 2018 12:36
Show Gist options
  • Save apjyotirmay/3128812bf3edd9a78a1e3b2143c74c6a to your computer and use it in GitHub Desktop.
Save apjyotirmay/3128812bf3edd9a78a1e3b2143c74c6a to your computer and use it in GitHub Desktop.
Set up a development environment for ruby in 30 minutes on ubuntu. This will use rbenv to take care of paths and will mimic how you would set up your production environment. A very clean separated setup conducive to integration with Puppet/chef (for installation of ruby versions) and Capistrano (for contained deployment). Allows for per-user own…
#pre-requisites
sudo apt-get install build-essential libreadline-dev libssl-dev zlib1g-dev libxml2-dev libxslt-dev git libpq-dev libmysqlclient-dev libpq-dev nodejs libcurl4-openssl-dev libffi-dev imagemagick libjpeg-progs pngcrush
############optional
sudo apt-get install vim zsh
######### optional for jruby
sudo apt-get install openjdk-7-jdk
sudo addgroup deployer
sudo adduser --ingroup deployer deployer
sudo mkdir -p /opt/rbenv/RUBIES
sudo chown deployer:deployer -R /opt/rbenv/RUBIES
su - deployer
git clone https://github.com/sstephenson/rbenv.git /opt/rbenv/RUBIES
git clone https://github.com/sstephenson/ruby-build.git /opt/rbenv/RUBIES/plugins/ruby-build
git clone https://github.com/sstephenson/rbenv-vars.git /opt/rbenv/RUBIES/plugins/rbenv-vars
echo 'export PATH="/opt/rbenv/RUBIES/bin:$PATH"' >> ~/.bash_profile
echo 'export RBENV_ROOT="/opt/rbenv/RUBIES"' >> .bash_profile
echo 'eval "$(rbenv init -)"' >> .bash_profile
#ZSH equivalent
#echo 'export PATH="/opt/rbenv/RUBIES/bin:$PATH"' >> ~/.zshenv
#echo 'export RBENV_ROOT="/opt/rbenv/RUBIES"' >> ~/.zshenv
#echo 'eval "$(rbenv init -)"' >> ~/.zshenv
#logout and login back as "deployer"
"RUBY_CONFIGURE_OPTS=--with-readline-dir="/usr/lib/x86_64-linux-gnu/libreadline.so" rbenv install 2.2.2
#in case of an ssl/readline failure, try "RUBY_CONFIGURE_OPTS=--with-readline-dir="/usr/lib/x86_64-linux-gnu/libreadline.so" rbenv install 2.1.1"
rbenv rehash
rbenv global 2.2.2
#your core installation directory needs to have bundler
gem install bundle --no-ri --no-rdoc
gem update --system
rbenv rehash
####### optional for jruby
rbenv install jruby-1.7.10
rbenv global jruby-1.7.10
rbenv rehash
gem update --system
#######################################
#now log on to your normal account
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> .bash_profile
echo 'export GEM_HOME="$HOME/.gem"' >> .bash_profile
echo 'export GEM_PATH="$HOME/.gem"' >> .bash_profile
echo 'export PATH="$HOME/.gem/bin:$PATH"' >> .bash_profile
#ZSH equivalent
#echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv
#echo 'eval "$(rbenv init -)"' >> ~/.zshenv
#echo 'export GEM_HOME="$HOME/.gem"' >> ~/.zshenv
#echo 'export GEM_PATH="$HOME/.gem"' >> ~/.zshenv
#echo 'export PATH="$HOME/.gem/bin:$PATH"' >> ~/.zshenv
mkdir ~/.rbenv/
ln -s /opt/rbenv/RUBIES/versions ~/.rbenv/
rbenv global 2.2.2
rbenv rehash
gem install bundle --no-ri --no-rdoc
gem install rails -v 4.2
######### OPTIONAL this for shared dependencies of gem cache just to speed up gem reinstall. this is optional. if you do this, it is recommended #that you symlink the bundler gem cache directories in every project to this.
mkdir ~/.bundle
mkdir ~/.bundle/shared
echo 'BUNDLE_PATH: .bundle_path' >> ~/.bundle/config
echo 'BUNDLE_DISABLE_SHARED_GEMS: "1"' >> ~/.bundle/config
## the below is not necessary for your projects (which will exclusively use bundler). This is for some system utilities that are
## frequently available as gems (e.g. pry). not necessary for production setup
## we do this, because default gem install path is inside the ~/.rbenv/versions directory (which is now owned by deployer)
mkdir ~/.rbenv/gems
##DONT DO THIS - you need the gems from your rbenv ruby system path as well.
## remember that your 'gem install' and 'bundle install' will share nothing
~~echo 'export GEM_PATH=~/.rbenv/gems' >> ~/.bash_profile~~
echo 'export GEM_HOME=$GEM_PATH' >> ~/.bash_profile
echo 'export PATH="$HOME/.rbenv/gems/bin:$PATH"' >> ~/.bash_profile
#anytime you install a new executable gem (like elastic-mapreduce which provides a runnable command), you
#need to do ```rbenv rehash```
############ ALL DONE ############
############### TO CREATE A NEW RAILS PROJECT ##############
mkdir /tmp/test
cd /tmp/test
echo 'source "https://rubygems.org"' >> Gemfile
echo 'gem "rails"' >> Gemfile
### to get your shared cache working (as we setup above)
mkdir .bundle_path
ln -s ~/.bundle/shared .bundle_path/cache
bundle install
############## TO SETUP A CRON-JOB based on rbenv and rails ###############
#you dont need to "cd" or any of that crap
crontab -e
# Set the path to include rbenv
PATH=$PATH:/home/user/.rbenv/shims/:/home/user/.rbenv/bin/:/bin:/usr/bin/
# Debug cron environment...
* * * * * /bin/bash -l -c '/usr/bin/env >> /tmp/cron-env.log 2>&1'
# Run dummy command
* * * * * /bin/bash -l -c 'eval "$(rbenv init -)" && BUNDLE_GEMFILE=/tmp/test2/Gemfile bundle list >> /tmp/backup.log 2>&1'
sudo dnf install make automake openssl-devel readline-devel gcc gcc-c++ kernel-devel readline-devel zlibrary-devel libxml2-devel libxslt-devel libpqxx-devel   nodejs libcurl-devel libffi-devel postgresql-libs ImageMagick-devel ImageMagick libjpeg-turbo-devel libjpeg-turbo-utils pngcrush
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git  ~/.rbenv/plugins/ruby-build
git clone https://github.com/sstephenson/rbenv-vars.git ~/.rbenv/plugins/rbenv-vars
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv
echo 'export RBENV_ROOT="$HOME/.rbenv"' >> ~/.zshenv
echo 'eval "$(rbenv init -)"' >> ~/.zshenv
RUBY_CONFIGURE_OPTS=--with-readline-dir="/usr/lib64/libreadline.so" rbenv install 2.4.0
sudo dnf install make automake gcc gcc-c++ kernel-devel readline-devel zlibrary-devel libxml2-devel libxslt-devel libpqxx-devel community-mysql-devel postgresql-server nodejs libcurl-devel libffi-devel postgresql-libs ImageMagick-devel ImageMagick libjpeg-turbo-devel libjpeg-turbo-utils pngcrush tlp redhat-rpm-config
sudo systemctl enable tlp.service
sudo systemctl start tlp.service
sudo postgresql-setup initdbf --initdb
sudo systemctl start postgresql.service
sudo systemctl enable postgresql.service
sudo -u postgres vim /var/lib/pgsql/data/pg_hba.conf
sudo service postgresql restart
sudo dnf remove evolution rhythmbox empathy gnome-maps -y
#for android
sudo dnf install compat-libstdc++-296.i686 compat-libstdc++-33.i686 compat-libstdc++-33.x86_64 ncurses-libs.i686 zlib.i686
su - deployer
rbenv global jruby-1.7.4
rbenv rehash
gem install bundler -v 1.5.0.rc2
gem update --system
rbenv rehash
#Ctrl-D to return to main login
rbenv rehash
#bundle exec puma -e development -d -b unix:///opt/user1/run/app.sock --pidfile /opt/user1/run/app.pid --control unix:///opt/user1/run/app_pumactl.sock -t 0:16 -w 3 --control-token foo >> /tmp/backup12.log 2>&1
#bundle exec pumactl -C unix:///opt/user1/run/app_pumactl.sock -T foo stop
##########################################
check process puma with pidfile "/opt/user1/run/app.pid"
#start program = "/bin/bash -l -c 'eval \"$(rbenv init -)\" && bundle exec puma -e development -d -b unix:///opt/user1/run/app.sock --pidfile /opt/user1/run/app.pid --control unix:///opt/user1/run/app_pumactl.sock -t 0:16 -w 3'" with timeout 60 seconds
start program = "/bin/zsh -l -c ' cd /opt/user1/public && bundle exec puma -e development -d -b unix:///opt/user1/run/app.sock --pidfile /opt/user1/run/app.pid --control unix:///opt/user1/run/app_pumactl.sock -t 0:16 -w 3 --control-token foo >> /tmp/backup12.log 2>&1'"
as uid 1001 and gid 1001
#stop program = "/bin/bash -l -c 'eval \"$(rbenv init -)\" && bundle exec pumactl -C unix://opt/user1/run/app_pumactl.sock stop'"
stop program = "/bin/zsh -l -c ' cd /opt/user1/public && bundle exec pumactl -C unix:///opt/user1/run/app_pumactl.sock -T foo stop'"
as uid 1001 and gid 1001
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
group puma
### for a normal user (of the pid 1001) to restart puma, run
##### kill -s SIGUSR2 $(ps -C ruby -F | grep '/puma' | awk {'print $2'})
########################################
sudo -u postgres psql
CREATE DATABASE redcarpetweb;
CREATE USER redcarpetweb WITH PASSWORD 'redcarpetweb';
GRANT ALL PRIVILEGES ON DATABASE redcarpetweb to redcarpetweb;
alter database redcarpetweb owner to redcarpetweb;
RAILS_ENV=development bundle exec rake db:schema:load
RAILS_ENV=development bundle exec rake db:seed
RAILS_ENV=development bundle exec rake db:migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment