Install rails in Ubuntu

Source: Internet
Author: User
Tags psql vps postgres createdb ubuntu vps

The latest versions used in this tutorial are Ubuntu 10.10, Ruby 1.9.2, and rails 3.0.7. This tutorial is also tested on Ubuntu 10.04 and Ubuntu 11.04, which will be released soon.

Original address: http://linux.cn/forum.php? MoD = viewthread & tid = 7566.

Update System

Let's get started. Log on to your machine as an administrator (or through SUDO) and open a terminal window. The following commands must be entered in this window. $ Is a command prompt. Enter the command and press Enter.

The first three commands update the package list, upgrade the currently installed package, install the new package, and delete the conflicting package. In this way, your machine will be updated. The last command is to restart the machine. It is a good practice to restart the machine after a large number of software packages are updated.

··· · 50 ······· · 90 1. sudo  apt-get update 2. sudo  apt-get upgrade 3. sudo  apt-get dist-upgrade 4. sudo  rebootPrepare for rvm Installation

After the machine is restarted, log on again and open a terminal window. Install curl and git for the rvm script. Curl is a tool that uses multiple protocols (such as HTTP or FTP) for data transmission. "Git is a free open-source distributed version control system that applies to small projects to very large projects, taking into account both speed and efficiency ." Git is the version control system selected by most Ruby on Rails developers.

··· · 50 ······· · 90 1. sudo  apt-get  install  curl 2. sudo  apt-get  install  git-core

Configure git

Git will be used in rvm scripts, and we will also use it in the second part of this tutorial. After installation, we need to spend some time configuring it. Easy configuration: you only need to provide a user name and email address.

··· · 50 ······· · 90 1. $ git config --global user.name  "Your Name" 2. $ git config --global user.email your-email@address.com

For example:

··· · 50 ······· · 90 1. $ git config --global user.name  "John Doe" 2. $ git config --global user.email johndoe@mail.comInstall rvm

Now we can install rvm. Rvm refers to the ruby version manager. "It is a command line tool that allows you to easily install, manage, and use multiple Ruby environments and their corresponding gem packages ." Run the following command to install the script. Rvm will be installed in the home directory of your current login user.

··· · 50 ······· · 90 1. bash  < <(curl -s https://rvm.beginrescueend.com/ install /rvm)

Switch to the main directory and modify the user's bash settings file to ensure that the rvm script is loaded every time the user logs on. We use nano to edit the bash settings file. Nano is a simple command line text editor, which will be used later.

··· · 50 ······· · 90 1. cd 2. $ nano .bashrc

Add the following lines to the end of your Bash settings file. After editing, press Ctrl + O to save and press Ctrl + X to exit the nano. If you want to exit the nano without saving it, press Ctrl + X and then press n.

··· · 50 ······· · 90 1. [[ -s  "$HOME/.rvm/scripts/rvm"  ]] && .  "$HOME/.rvm/scripts/rvm"

Use the following command line to manually reload the setting file in the current logon. You can also open a new terminal window to automatically load the setting file. In this way, the rvm command can be used.

··· · 50 ······· · 90 1. source  .bashrc

You can run the following command to check whether the rvm script can work.

··· · 50 ······· · 90 1. type  rvm |  head  -1

If everything works, the command returns "rvm is a function" (rvm is a function ). Otherwise, you can go to the "troubleshooting your install" section on the rvm website to find a solution.

Prepare for Ruby and rubygems Installation

When compiling and installing Ruby and rubygems from the source code, rvm can conveniently view the dependency between software packages.

··· · 50 ······· · 90 1. $ rvm notes

You can list the dependent packages required for the standard Ruby version and the commands for installing them. Some Packages may have been installed.

··· · 50 ······· · 90 1. sudo  apt-get  install  build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-devUse rvm to install Ruby and rubygems

First, we need to install Ruby, "a dynamic, open-source simple and efficient programming language. Its elegant syntax is easy to read and write ."

Then we will install rubygems, a "preferred Ruby software package system. It provides a standard format for distributing Ruby programs and libraries, an easy-to-use Installation tool for managing gem packages, and a gem server that provides gem packages on any machine with rubygems, and the standard method for publishing the gem software package."

As described in the preceding rvm command, rvm also has a command to see which Ruby version can be installed. Run the following command to check which Ruby versions can be installed:

··· · 50 ······· · 90 1. $ rvm list known

It takes some time to install the standard Ruby version because it needs to be compiled from the source code.

··· · 50 ······· · 90 1. $ rvm  install  1.9.2

Before using installed Ruby, you must set this version to the default version.

··· · 50 ······· · 90 1. $ rvm --default use 1.9.2

Check the ruby and rubygems versions to ensure proper installation.

··· · 50 ······· · 90 1. $ ruby - v 2. $ gem - v

If necessary, manually update rubygems and other gems to be updated.

··· · 50 ······· · 90 1. $ gem update --system 2. $ gem updateUse rubygems to install rails

All rails are packaged in rails gem. Installing it is the easiest part of this tutorial. Use rubygems to install it, that is, the gem command. After the installation is complete, check the version to ensure that the installation is correct.

··· · 50 ······· · 90 1. $ gem  install  rails 2. $ rails - v

Use rubygems to install Capistrano

Capistrano is an open-source tool for running scripts on multiple servers. It is mainly used to distribute web applications. It automatically distributes new application versions to one or more web servers, including executing related support tasks, such as modifying databases ." Use rubygems to install Capistrano and check its version to ensure correct installation.

··· · 50 ······· · 90 1. $ gem  install  capistrano 2. $ cap -VInstall PostgreSQL

PostgreSQL is a complex object-relational database system that supports the vast majority of SQL structures, such as subqueries, transactions, and user-defined types and functions ." Install PostgreSQL and related software packages. These related software is required by PG gem to be installed later. PG gem is used to connect PostgreSQL and Ruby on Rails.

··· · 50 ······· · 90 1. sudo  apt-get  install  postgresql libpq-dev

Configure PostgreSQL

After installation, use enhanced security Psql to configure PostgreSQL. Psql is a PostgreSQL interactive terminal used to manage database tasks.

By default, Psql does not require a password for logon. We will modify the Authentication Mode and re-load the PostgreSQL configuration. Before that, you must specify a password for PostgreSQL administrator S. Log on to Psql, specify a secure password for ipvs, and then exit Psql.

··· · 50 ······· · 90 1. sudo  -u postgres psql 2. # \password postgres 3. # \q

Now, change the Authentication Mode from ident to MD5. This ensures that a password is required for Psql logon and the password is encrypted. The two lines to be modified are located near the end of the configuration file. After modification, reload the PostgreSQL configuration.

··· · 50 ······· · 90 1. sudo  nano /etc/postgresql/8.4/main/pg_hba.conf 2. sudo  /etc/init.d/postgresql reload

Most other PostgreSQL configurations are stored in another configuration file. Those configurations can be optimized, but they are not in the scope of this tutorial. Let's turn our attention back. If we modify other configurations, we also need to reload them to take effect.

··· · 50 ······· · 90 1. $ sudo nano /etc/postgresql/8.4/main/postgresql.conf

Now, the local development environment has been installed!

Test settings

To make sure everything is normal, let's develop a very small application to understand Ruby on Rails. This process includes the following steps:

  • Create a database user for the test application
  • Create a database for the test application
  • Use a PostgreSQL database to create a test application
  • Gem required for installing and testing applications
  • Configure the database connection for the test application
  • Generate a simple framework for testing applications
  • Migrate the database of the framework to the database of the Test Application
  • Start a built-in web server
  • Use a browser to access the test application
  • Stop built-in Web Servers

After all the tests are successfully completed, perform the following steps:

  • Delete the database of the Test Application
  • Delete the database user of the Test Application
  • Delete test application

These tests are all performed in the local test environment. The conventions for testing in VPS are as follows (the database user name and database name are from the test application name ):
Box 1, 1.1

Application name: test_app
Database username: test_app
Database User Password: Apple
Database Name: test_app_development

First, use the createuser command to create a database user for the test application. We use PostgreSQL's Management User ipvs for operations. The-p parameter indicates that the password is entered for interaction. The ">" symbol is the prompt for a question.

··· · 50 ······· · 90 1. sudo  -u postgres createuser -P 2. > Enter name of role to add: test_app 3. > Enter password  for  new role: apple 4. > Enter it again: apple 5. > Shall the new role be a superuser? (y/n) n 6. > Shall the new role be allowed to create databases? (y/n) n 7. > Shall the new role be allowed to create  more  new roles? (y/n) n 8. > Password: your-postgres-user-password

Then, create a database with the test application user as the master database. Although we can use rake to create databases, we need to use PostgreSQL, so let's learn about basic PostgreSQL management. Use the createdb command and use the-O option to specify the database username. Here, you will also be prompted to enter the PostgreSQL management user password.

··· · 50 ······· · 90 1. sudo  -u postgres createdb -O test_app test_app_development 2. > Password: your-postgres-user-password

Now, the database has been created. You can create an actual Ruby on Rails application. Switch to your home directory, create a rails application named test_app, and use PostgreSQL as the database backend. The-D option allows you to specify the preferred database type.

··· · 50 ······· · 90 1. cd 2. $ rails new test_app -d postgresql

Go to the rails application directory and use bundler to install the required gem. Bundler processes "dependencies between an application across multiple machines throughout the lifecycle" and "only supports rails 3 ."

··· · 50 ······· · 90 1. cd  test_app 2. $ bundle  install

Use nano to modify the database configuration file and add the password "apple" in the development database configuration ". According to the Conventions and settings in Box 1.1, the database name and database user have been set.

··· · 50 ······· · 90 1. $ nano config/database.yml

Now a basic framework is generated. This creates a user model and its controller. The input includes the name and email fields, which will be stored in the database.

··· · 50 ······· · 90 1. $ rails generate scaffold User name:string email:string

Use rake to migrate the framework to the development database. Rake is short for Ruby make. It is a "simple Ruby build program, similar to make," that allows you to create and migrate databases.

··· · 50 ······· · 90 1. $ rake db:migrate

Now, you need to check whether the application works in the browser. Start the built-in server and use a browser to access http: // localhost: 3000/. In particular, check the environment data of the displayed test application. Visit http: // localhost: 3000/users to create, edit, view, and delete users.

··· · 50 ······· · 90 1. $ rails server

If everything looks normal, you can stop the built-in server.

··· · 50 ······· · 90 1. $ Press Ctrl + c

When all the work is completed, you can delete the database and database users of the test application. Use dropdb to delete a database.

··· · 50 ······· · 90 1. sudo  -u postgres dropdb test_app_development 2. > Password: your-postgres-user-password

Use dropuser to delete database users.

··· · 50 ······· · 90 1. sudo  -u postgres dropuser 2. > Enter name of role to drop: test_app 3. > Password: your-postgres-user-password

Finally, switch to your home directory and delete all the test application directories. It mainly restores to a clean local development environment and can be used to develop Ruby on Rails Applications.

··· · 50 ······· · 90 1. cd 2. rm  -r test_app

In the second part of the later release of the tutorial, we will help you establish a Ubuntu VPs through several steps, which is used to run (multiple) Ruby on Rails Applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.