Monday, October 31, 2016

Install Ruby on Rails on Ubuntu 16.04


Installing Ruby


We're going to install

Ruby 2.3.1

Lets do this! First, we install some ruby dependencies.


sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Next we're going to install rbenv
Install Rbenv and Ruby-Build
 
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash

By doing this, rbenv will be installed in your home directory, and sets the ruby environment Install Ruby We're going to install Ruby. Check here for the latest version of Ruby


rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

If you dont want Rubygems to create local documentation everytime you installed gem, you can disable it with this command


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

Lastly, we're going to install Bundler


gem install bundler
rbenv rehash

Installing Rails


We're going to install Rails. We need to install a Javascript runtime like NodeJS for some dependencies of Rails. Doing this enables you to use Coffeescript and the Asset Pipeline in Rails for a faster production environment. Install NodeJS
 
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

Install Rails
 
sudo apt-get install ruby-railties
gem install rails
rbenv rehash
rails -v

Your output for "rails -v" should be "#Rails 4.2.6" or a newer version than this. If you get a different result for some reason, it means your environment may not be setup properly.



Configuring Database


Choose your Database : MySQL PostgreSQL Install MySQL
 
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
sudo apt-get install ruby-mysql ruby-mysql2
gem install ruby-mysql

Installing the libmysqlclient-dev gives you the necessary files to compile the mysql2 gem which is what Rails will use to connect to MySQL when you setup your Rails app. Once you've done, you can now create your Ruby on Rails! Install PostgreSQL
 
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev

Setup a user to create database


sudo -u postgres createuser masukami -s
#Set a password for the user
sudo -u postgres psql
postgres=# \password masukami

Now lets proceed to the final step!

Create Your App


Let's create your first Ruby on Rails app!
#### If you want to use SQLite (not recommended)
rails new myapp

#### If you want to use MySQL (recommended)
rails new myapp -d mysql

#### If you want to use Postgres (optional)
rails new myapp -d postgresql

# Move into the application directory
cd myapp
#usually /var/www/html/myapp

# If you setup MySQL or Postgres with a username/password, modify the
# config/database.yml file to contain the username/password that you specified
#such as nano config/database.yml | gedit config/database.yml
# Create the database
rake db:create

rails server
If there is any problem with your rails application. You should try bundle install
bundle install
Lets visit localhost:3000 to check your rails server whether it's running or not. You've completed configuring your Ruby on Rails environment. Have fun building some Rails application.

0 comments:

Post a Comment

 

Contact Us

Email us: suhailqamilbiz@gmail.com