未加星标

How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Ruby on Rails (RoR) is an open source web application framework, published under MIT License. Ruby on Rails, or rails is written in Ruby, it is a server-side web application framework that follows the MVC (Model-View-Controller) concept. Rails is providing default structures for thedatabase, web service, and web pages. More than 3000 developers have been contributed code to the Rails framework and there are manywell-known applications based on Rails, such as Github, Airbnb, Soundcloud etc.

In this tutorial, I will show you how to install Ruby on Rails on ubuntu 16.04 LTS. I will show you how to install and configure Rails with a PostgreSQL database, and how to create a new first project with Rails.

Prerequisite Ubuntu 16.04 Server. Root privileges. Step 1 - Install RVM

RVM or Ruby Version Manager is a command-line tool based on Bash and Ruby to manage the ruby installation. RVM allows you to install and configure multiple ruby versions on one system.

In this step, we will install the stable RVM version and add the repository key with thegpg command.

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

Install RVM stable. Make sure curl command is installed on your system.

curl -sSL https://get.rvm.io | bash -s stable --ruby

That command will install the requires packages for the RVM installation, then download the latest stable RVM version and install it.

Now we can start using RVM by executing the command below at the first:

source /usr/local/rvm/scripts/rvm


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04
Step 2 - Install Ruby

The current stable version of Ruby is 2.3.1. Install it with thervm command and then make it the default ruby version for your system.

Update rvm to the latest stable version, then install Ruby 2.3.1 version.

rvm get stable --autolibs=enable

rvm install ruby-2.3.1

Next, make 2.3.1 the default ruby version on the system.

rvm --default use ruby-2.3.1

Now check the ruby version with command below:

ruby -v


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04
Step 3 - Install Nodejs

Rails requires a javascript runtime to compile the Rails asset pipeline. For Rails development on Ubuntu linux, it's best to install Nodejs as the Javascript runtime.

Install nodejs from the nodesource repository:

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

apt-get install -y nodejs

Step 4 - Configure Ruby Gem

RubyGems is a Ruby Package Manager. It's coming with the gem command-line tool and is automatically installed when we install Ruby on the system.

Update the gem version and check it:

gem update --system

gem -v

This is optional, you can disable to install the documentation on every gem installation by adding a new line to the .gemrc file below.

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

Step 5 - Install Ruby on Rails 5 Stable

Install Ruby on Rails 5 with the gem command below:

gem install rails -v 5.0.0

When the installation finished successfully, check the rails version:

rails -v

And you will see the results of rails version:

Rails 5.0.0


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04

Rails 5.0 has been installed on Ubuntu 16.04 with Ruby 2.3.1.

Step 6 - Setting up the PostgreSQL Database for Rails Development

In this step, we will prepare PostgreSQL for rails development. Ruby on Rails supports many databases such as mysql, SQLite (Default) and PostgreSQL. We will use PostgreSQL as the database for this guide.

Install PostgreSQL and some other required packages with the apt command:

apt-get -y install postgresql postgresql-contrib libpq-dev

When the installation is done, login to the postgres user and access the postgresql shell.

su - postgres

psql

Give thepostgres user a new password with command below:

\password postgres

Enter new password:

Next, create a new role named 'rails-dev' for the rails development with the command below:

create role rails_dev with createdb login password 'aqwe123';

Set a new password for the user and check that the user has been created.

Now check the new role and you will see new role has been created:

\du

The PostgreSQL database has been prepared for the Rails Development.


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04
Step 7 - Create the First Application with Rails and PostgreSQL

Ruby on Rails or Rails ships with the command "rails" and we can bootstrap our first application with that command.

Create a new application "myapp" with PostgreSQL as the default database.

rails new myapp -d postgresql

That command will create a new directory 'myapp' and install new gem including pg gem that is needed by rails to connect to the PostgreSQL database into that directory.

Next, go to the 'myapp' directory and edit the database.yml file in the config directory.

cd myapp/

vim config/database.yml

In the development section, uncomment line 32 and type the role we've created in step 6.

username: rails_dev

Set the rails_dev user password on line 35.

password: aqwe123

Uncomment line 40 and 44 for the database host configuration.

host: localhost

port: 5432

Now go to the test section and add the new configuration below:

database: myapp_test

host: localhost

port: 5432

username: rails_dev

password: aqwe123

Save and exit.

Next, generate the database with the rails command:

rails db:setup

rails db:migrate

And then start the rails server with the command below:

rails s -b 192.168.1.110 -p 8080


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04

Open your web browser and visit the server IP address on port 8080 -192.168.1.110:8080.


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04

Back to the terminal and create anew scaffold to interact with the PostgreSQL database.

Type the command below:

rails g scaffold Post title:string body:text

rake db:migrate

Run the rails server again.

rails s -b 192.168.1.110 -p 8080

Visit the server : 192.168.1.110:8080/posts

You will see simple CRUD on Ruby on Rails with PostgreSQL.


How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04
Reference http://railsapps.github.io/installrubyonrails-ubuntu.html https://gorails.com/setup/ubuntu/16.04

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04
本站链接:http://www.codesec.net/view/485625.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(35)