Build a Ruby on Rails native development environment with Ubuntu 12.04

Source: Internet
Author: User
Tags postgresql psql vps postgres createdb ubuntu vps version control system virtual private server ruby on rails

Reprint, original link http://linux.cn/portal.php?mod=view&aid=170 using Ubuntu to build a Ruby on Rails Local development environment

Want to develop a Ruby on Rails app? Although there are already some (beginner) Ruby on Rails tutorials, there seems to be some uncertainty about how to build a clean and updated local development environment.

This tutorial will guide you through a few steps to build an Ubuntu-based Ruby on Rails native development environment. And the upcoming second part of this tutorial will help you build an Ubuntu VPS. Now that you know for a moment that VPS is a virtual private server, let's first turn our attention to how to build a local development environment.

Ruby on Rails? Ubuntu?

What is Ruby on Rails? What is Ubuntu?

Simply put, Ruby on Rails is a Web development framework that allows you to develop Web applications in the Ruby language. As the official website says: "Ruby on Rails is an open-source WEB development framework that improves programmer fun and development efficiency, which allows you to write beautiful code in a way that is better than configured. ”

Ubuntu, however, is a "Debian Linux derivative release that focuses on usability." "It's the most popular Linux distribution of all these years. Even better, Ruby on Rails and Ubuntu are open source, which means they're completely free!

Briefly

In this tutorial, we use the Ruby Version Manager (RVM) script to install Ruby on Rails and RubyGems, and use RubyGems to install Rails and Capostrano. The machine also provides version control, which is implemented through the Git and PostgreSQL databases. This tutorial requires a newly installed Ubuntu machine and Internet connection, but most (Debian and Ubuntu-derived) Linux should also be available.

The latest version used in this tutorial is: Ubuntu 10.10, Ruby 1.9.2, and Rails 3.0.7. This tutorial is also tested on Ubuntu 11.04 on Ubuntu 10.04 and is about to be released (now released).

In this tutorial, we need to use a lot of the Linux command line. Therefore, I attached a short glossary of LINUX commands used in the article at the end of the article.

Update system

So let's get started. First log in to your machine as an administrator (or via sudo) and open a terminal window. The following commands are required to be entered in this window. The dollar sign $ is the command prompt, followed by the direct input command and press ENTER.

The first three commands update the list of packages, upgrade the currently installed packages, install new packages, and remove conflicting packages. This will keep your machine up to date. The final command is to restart the machine, which is a good practice to restart after a lot of updates to the package.

$ sudo apt-get update$ sudo apt-get upgrade$ sudo apt-get dist-upgrade$ sudo reboot
Preparing for installation of RVM

After the machine restarts, log in again and open a terminal window. RVM scripts need to install Curl and Git first. Curl is a tool that uses multiple protocols, such as HTTP or FTP, for data transfer. And "Git is a free, open source distributed version control system that works from small projects to very large projects, both in terms of speed and efficiency." "Git is the version control system selected by most Ruby on Rails developers.

$ sudo apt-get install curl$ sudo apt-get install Git-core

Configure Git

Git is used in RVM scripts, and we use it in the second part of this tutorial. After installation, we need to take a moment to configure it. Configuration is easy: Just provide a user name and email address.

$ git config--global user.name "Your name" $ git config--global user.email [email protected]

Such as:

$ git config--global user.name "John Doe" $ git config--global user.email [email protected]
Installing RVM

Now we can install the RVM. RVM means Ruby version manager, "is a command-line tool that makes it easy for you to install, manage, and use multiple ruby environments and their corresponding GEM packages. The following command to install the script. RVM will be installed in the home directory of the user you are currently logged into.

$ bash-s Stable < < (Curl-s Https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Curl-l Get.rvm.io | Bash-s Stable

Switch to the home directory and then modify the user's bash settings file to ensure that the RVM script is loaded every time the user logs in. We use Nano to edit the bash settings file. The Nano is a simple command-line text editor that we'll use later.

$ cd$ Nano. BASHRC

Add the following line at the end of your bash settings file. After editing, press CTRL + O to save, and then press CTRL + X to exit the Nano. If you want to quit the Nano without saving, press CTRL + X and then press N.

[-S "$HOME/.RVM/SCRIPTS/RVM"] &&. "$HOME/.RVM/SCRIPTS/RVM"

Manually reload the settings file in the current login with the following command line, or you can open a new terminal window to load automatically. So the RVM command can be used.

$ source. BASHRC

You can check whether the RVM script works by using the following command.

$ Type RVM | Head-1

If everything works, the command returns "RVM is a function" (RVM). Otherwise, you can visit the "Troubleshooting Your Install" section of the RVM website to find a solution.

Preparing for the installation of Ruby and RubyGems

When compiling and installing Ruby and RubyGems from source code, RVM can easily view the package dependencies between them.

$ RVM Notes

You can list the dependent packages required for the standard version of Ruby, and the commands to install them. Some of the packages may have been installed.

$ 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-dev
Using RVM to install Ruby and RubyGems

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

Then we're going to install RubyGems, which is a "preferred Ruby package system." It provides a standard format for distributing Ruby programs and libraries, an easy-to-use installation tool for managing the Gem package, a gem server that provides a gem package on any machine equipped with RubyGems, and a standard way to publish a gem package. ”

As described in the RVM command above, RVM also has a command to see which version of Ruby can be installed. See which Ruby versions can be installed by using the following command:

$ RVM list known

Installing the standard version of Ruby requires some time because it needs to be compiled from source code.

$ RVM Install 1.9.2

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

$ RVM--default Use 1.9.2

Check the versions of Ruby and RubyGems to ensure proper installation.

$ ruby-v$ Gem-v

If necessary, manually update the RubyGems and other gems that need to be updated.

$ gem Update--system$ gem update
Installing Rails with RubyGems

All rails are packaged in the rails Gem. Installing it is the easiest part of this tutorial. Use RubyGems to install it, which is the Gem command. After the installation is complete, check its version to ensure proper installation.

$ gem Install rails$ rails-v

Installing Capistrano with RubyGems

Capistrano is an open source tool that runs scripts on multiple servers. Its primary purpose is to distribute WEB applications. It automatically distributes new versions of your app to one or more WEB servers, including performing related support tasks, such as modifying the database. "Use RubyGems to install Capistrano and check its version to ensure proper installation."

$ gem Install capistrano$ cap-v
Installing 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 its associated packages. These related software is required for the PG gem to be installed later, and the PG gem is used to connect PostgreSQL and Ruby on Rails.

$ sudo apt-get install PostgreSQL Libpq-dev

Configure PostgreSQL

Once installed, use the enhanced security PSQL to configure PostgreSQL. PSQL is a PostgreSQL interactive terminal for managing database tasks.

By default, PSQL does not require a password when logging on. We will modify the authentication mode and reload the PostgreSQL configuration. But before that, assign a password to the Admin user postgres for PostgreSQL. Log in to PSQL, specify a secure password for Postgres, and then exit PSQL.

$ sudo-u postgres psql# \password postgres# \q

Now, change the authentication mode from ident to MD5. This will ensure that the login PSQL requires a password and that the password is stored encrypted. The two lines that need to be modified are located near the end of the configuration file. After modification, reload the configuration of PostgreSQL.

$ sudo nano/etc/postgresql/8.4/main/pg_hba.conf$ sudo/etc/init.d/postgresql reload

Most of the other configurations of PostgreSQL are stored in a separate configuration file. Those configurations can also be optimized, but not within the scope of this tutorial. Let's turn our attention back, and if we make changes to other configurations, we need to reload them to take effect.

$ sudo nano/etc/postgresql/8.4/main/postgresql.conf

The local development environment is now complete!

Test settings

To make sure everything is OK, let's develop a very small application to get a look at Ruby on Rails. This process consists of the following steps:

    • Create a database user for your test app
    • Create a database for your test app
    • Create a test app using the PostgreSQL database
    • Install the Gem required for your test application
    • Configure a database connection for a test app
    • Create a simple framework for test apps
    • Migrating the framework's database to the test application's database
    • Start the built-in Web server
    • Accessing test apps with a browser
    • Stop the built-in Web server

Once the above tests have been completed successfully, we will take the following steps:

    • Delete the database for the test app
    • Delete the database user for the test app
    • Delete a test app

These tests are performed in a 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

Application Name: Test_app
Database user name: Test_app
Database user password: Apple
Database name: Test_app_development

First, use the CreateUser command to create a database user for the test app. We use PostgreSQL's administrative user postgres to operate, and the-p parameter indicates an interactive input password. The ">" symbol is the prompt for a question.

$ sudo-u postgres createuser-p> Enter name of role to add:test_app> enter password for new role:apple> enter It again:apple> shall the new role be a superuser? (y/n) n> shall the new role being allowed to create databases? (y/n) n> shall the new role being allowed to create more new roles? (y/n) n> Password:your-postgres-user-password

Then, create a database user that tests the application as the owner of the database. While we can use Rake to create a database, we want to use PostgreSQL, so let's learn basic PostgreSQL management. Use the Createdb command and specify the database user name through the-o option. Here, you will also be prompted to enter the password of the Admin user for PostgreSQL.

$ sudo-u postgres createdb-o Test_app test_app_development> Password:your-postgres-user-password

Now that the database is built, you can create the actual Ruby on Rails application. Switch to your home directory, create a Rails app named Test_app, and use PostgreSQL as the database backend. The-D option allows you to specify the preferred database type.

$ cd$ Rails New test_app-d PostgreSQL

Go to the Rails app catalog and use Bundler to install the required gems. Bundler handles "an application's dependencies across multiple machines throughout its lifecycle," and "only Rails 3 is supported." ”

$ CD test_app$ Bundle install

Using Nano to modify the database configuration file, add the password in the development database configuration: "Apple". By convention and the settings in box 1.1, the database name and database user are set.

$ nano config/database.yml

Now generate a basic framework. This will create a User model and its controller. The input includes the name and email fields, which are stored in the database.

$ rails Generate scaffold User name:string email:string

Use the Rake migration Framework in the development database. Rake is the abbreviation for Ruby make. It is a "simple Ruby builder, similar to make," which allows you to create and migrate databases.

$ Rake Db:migrate

Now you need to check the browser for the app to work. Launch the built-in server and access the http://localhost:3000/with a browser, especially to see the environment data for the test application that is displayed. Visit Http://localhost:3000/users again to create, edit, view, and delete some users.

$ Rails Server

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

$ Press CTRL + C

When all the work is done, you can delete the database and database user for this test application. Use dropdb to delete the database.

$ sudo-u postgres dropdb test_app_development> Password:your-postgres-user-password

Use Dropuser to delete database users.

$ sudo-u postgres dropuser> Enter name of role to Drop:test_app> Password:your-postgres-user-password

Finally, switch to your home directory and delete all the test application directories. The main recovery to a clean local development environment, can be used to develop the Ruby on Rails application.

$ cd$ rm-r Test_app

In the second part of the tutorial that will be released later, we will help you to build an Ubuntu VPS in several steps, which is used to run (multiple) Ruby on Rails applications.

Q&a

Q:fatal:role "Tom" does not exist

A:This would happen if the administrator have not created a PostgreSQL user account for you. (PostgreSQL user accounts is distinct from operating system user accounts.) If You is the administrator, see Chapter creating accounts. You'll need to become the operating system user under which PostgreSQL is installed (usually Postgres) to create the fi RST user account. It could also be, were assigned a PostgreSQL user name is different from your operating system user name; In this case you need to use the-u switch or set the PGUSER environment variable to specify your PostgreSQL user name

Q:Could notfind a JavaScript runtime. See https://github.com/sstephenson/execjs fora list of available runtimes.

Gemfile added Gem ' Execjs ' and Gem ' Therubyracer ', Gemfile in the project folder, not in. RVM

Gem ' Execjs '

Gem ' Therubyracer '

Then run the bundle install and you can run the Rails server boot server.

Linux command-line reference

The relevant Linux commands in this tutorial are listed alphabetically below. The usual format for Linux commands is: command-option parameters, such as: Rm-r Test_app. For more details, refer to their help page, which can be viewed with man [command].

    • sudo [command] is used to execute commands as an administrator.
    • Apt-get Dist-upgrade is used for the upgrade of the current release, and it automatically handles dependencies.
    • Apt-get install [package] for installing packages
    • Apt-get Update updates the index of the package.
    • Apt-get upgrade for updating packages that are currently installed
    • Bash <
    • CD [location] to switch directories, if no parameters are switched to the user home directory.
    • The nano [file] edits the file.
    • Reboot restart the machine.
    • rm-r [directory] recursively deletes the directory (and its subdirectories).
    • source [script] forces bash to read a script manually.
    • Type [command] shows the types of commands executed in the shell.
Reference links
    • Ubuntu
    • RVM
    • Ruby
    • RubyGems
    • Rails
    • Capistrano
    • Bundler
    • Rake
    • PostgreSQL
    • Git
Deep learning
    • The Linux Command Line
    • The Linux Command line book
    • Ruby on Rails Guides
    • Ruby on Rails Tutorial
    • Ruby on Rails Tutorial Book
    • Git Community Book
    • Pro Git
    • Git Reference

VIA http://coding.smashingmagazine.com/2011/06/21/set-up-an-ubuntu-local-development-machine-for-ruby-on-rails/

Build a Ruby on Rails native development environment with Ubuntu 12.04

Related Article

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.