Skip to content

Instantly share code, notes, and snippets.

@jpfinlay
Last active July 27, 2018 07:29
Show Gist options
  • Save jpfinlay/32f5710325897de5b041232d080c476c to your computer and use it in GitHub Desktop.
Save jpfinlay/32f5710325897de5b041232d080c476c to your computer and use it in GitHub Desktop.
Git 2.18 / OhMyZSH / Rbenv / Ruby 2.5.1 / Rails 5.2 / Postgres 9.5.13

Ubuntu 16.04LTS with Rails 5.2, Ruby 2.5.1 and PostgeSQL 9.5.13

July 26, 2018

Install pre-requisites and Git (2.18.0)

$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt install git-core zsh autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev -y
$ sudo apt-add-repository ppa:git-core/ppa && sudo apt-get update && sudo apt-get install git -y 

Create .gitconfig

$ git config --global user.name "User Name"
$ git config --global user.email user@email.com
$ git config --global core.editor vim # You might prefer nano or something else?
$ git config --global alias.ci commit
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.st status
$ git config --global alias.last 'log -1 HEAD'
$ git config --global core.excludesfile ~/.gitignore_global

Create .gitignore_global

$ touch ~/.gitignore_global
$ vim ~/.gitignore_global
*.lock
*.tmp

# Logs and databases #
*.log
*.sql
*.sqlite

# OS generated files #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

Install and Customise OhMyZSH

$ sh -c “$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Install Powerline and Fonts

$ sudo apt-get install fonts-powerline

If installing Ubuntu on Windows 10, download and install DejaVuSansMono for Powerline font and install on Windows 10 so we can select the font in our Ubunutu shell: https://github.com/powerline/fonts/tree/master/DejaVuSansMono

Change OMZ theme to 'agnoster' (or whatever you prefer)

$ vim ~/.zshrc
ZSH_THEME="agnoster"

Change the shell prompt

$ cd ~/.oh-my-zsh/themes
$ vim agnoster.zsh-theme

Comment out prompt_context and change PROMPT variable

  build_prompt() {
  RETVAL=$?
  prompt_status
  prompt_virtualenv
# prompt_context
  prompt_dir
  prompt_git
  prompt_bzr
  prompt_hg
  prompt_end
}

PROMPT='$(build_prompt) '

Install Optional OMZ plugins

$ cd ~/.oh-my-zsh/custom/plugins
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting
$ git clone https://github.com/zsh-users/zsh-autosuggestions

Add a list of plugins to .zshrc

$ vim .zshrc

plugins=(
  bundler
  colored-man-pages
  git
  rake
  rails
  rbenv
  ruby
  zsh-syntax-highlighting
  zsh-autosuggestions
)

Set default shell to ZSH

$ chsh -s $(which zsh)

Install rbenv and Ruby 2.5.1

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
$ ~/.rbenv/bin/rbenv init
$ echo "eval "$(rbenv init -)"" >> .zshrc

Now source the .zshrc file and confirm rbenv is installed

$ cd ~
$ source .zshrc

$ type rbenv
rbenv is a shell function from /home/user/.zshrc

Install Ruby-2.5.1

$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ rbenv install 2.5.1
$ rbenv global 2.5.1

Set up your .gemrc file to skip documentation when installing gems:

$ vim ~/.gemrc
$ echo "gem: --no-document" >> ~/.gemrc

Install awesome_print (optional)

$ gem install awesome_print

Set up .irbrc

$ touch ~/.irbrc
require 'rubygems'
require 'irb/completion'
require 'ap'

# Easily print methods local to an object's class
class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end
end

# Alias for exit
alias q exit

IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:AUTO_INDENT]  = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:SAVE_HISTORY] = 1000

Install Rails 5.2.0

$ gem install bundler
$ gem install rails
$ rbenv rehash

Now create ~/.railsrc with as many default options as you like!

$ vim ~/.railsrc
--database=postgresql
#--webpack
#--skip-action-cable
#--skip-coffee
#--skip-spring
#--skip-turbolinks
#--template=~/rails_template.rb

Install NodeJS (8.x)

$ curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
$ sudo bash nodesource_setup.sh # Yes, even if you are using ZSH!
$ sudo apt-get install nodejs -y

Install Yarn (1.7.0)

$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt-get update && sudo apt-get install yarn -y

Install PostgreSQL (9.5.13)

$ sudo apt-get install postgresql postgresql-contrib libpq-dev -y
$ sudo service postgresql start

Postgres by default uses template1 with SQL_ASCII encoding - we need to switch this to UTF-8 to avoid errors with creating databases for our Rails app. Credit to amolkhanorkar: https://gist.github.com/amolkhanorkar/8706915

Log in to Postgres

$ sudo -u postgres psql

List databases and you will see template1 with SQL_ASCII encoding

\l # will show you a list of databases, including template1

We need to DROP and then recreate template1 but templates can’t be dropped, so we first 'convert it' to an ordinary database:

UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';

Now we can drop it:

DROP DATABASE template1;

Now its time to recreate template1 from template0, with a new default encoding (UTF8):

CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';

Now modify template1 so it’s actually a template:

UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

template1 should now show as having UTF8 as it's encoding when you type \list. Now connect to template1 and VACUUM FREEZE it:

\c template1;
VACUUM FREEZE;

Next up, we create a new role to work with your rails development and test databases and quit Postgres

CREATE ROLE rolename WITH CREATEDB;
ALTER ROLE rolename WITH LOGIN;
\q # quit Postgres

Create Rails databases

$ createdb myrailsapp_development
$ createdb myrailsapp_test

Congratulations, you now have Rails 5.2.0, Ruby 2.5.1 and Postgres 9.5.13 set up on Ubuntu 16.04LTS along with a shiny Z shell!

Clean up

sudo apt-get autoremove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment