Ubuntu 14.04 Ruby,ROR環境搭建

在看國外一個物聯網平臺thingsquare,由於它是用Ruby寫的,因此就裝個Ruby研究下。下面是搭建步驟。 javascript

步驟0 - 安裝系統須要的包

# For Mac  # 先安裝 [Xcode](http://developer.apple.com/xcode/) 開發工具,它將幫你安裝好 Unix 環境須要的開發包 # 而後安裝 [Homebrew](http://brew.sh) ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

步驟1 - 安裝 RVM

RVM 是幹什麼的這裏就不解釋了,後面你將會慢慢搞明白。 html

$ curl -L https://get.rvm.io | bash -s stable

期間可能會問你sudo管理員密碼,以及自動經過homebrew安裝依賴包,等待一段時間後就能夠成功安裝好 RVM。 java

而後,載入 RVM 環境(新開 Termal 就不用這麼作了,會自動從新載入的) node

$ source ~/.rvm/scripts/rvm

檢查一下是否安裝正確 python

$ rvm -v
rvm 1.22.17 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

步驟2 - 用 RVM 安裝 Ruby 環境

$ rvm install 2.0.0

一樣繼續等待漫長的下載,編譯過程,完成之後,Ruby, Ruby Gems 就安裝好了。 mysql

步驟3 - 設置 Ruby 版本

RVM 裝好之後,須要執行下面的命令將指定版本的 Ruby 設置爲系統默認版本 git

$ rvm 2.0.0 --default

一樣,也能夠用其餘版本號,前提是你有用 rvm install 安裝過那個版本 github

這個時候你能夠測試是否正確 web

$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0] $ gem -v
2.1.6 $ gem source -a https://ruby.taobao.org http://rubygems.org http://gems.github.com http://gems.rubyforge.org

步驟4 - 安裝 Rails 環境

上面 3 個步驟事後,Ruby 環境就安裝好了,接下來安裝 Rails sql

$ gem install rails

而後測試安裝是否正確

$ rails -v
Rails 3.2.13

而後開始你的 Ruby,Rails 之旅吧。
歡迎來到 Ruby 的世界!


gem -v 告訴你 RubyGems 的版本
gem update --system 升級RubyGems的版本
gem install gem_name 安裝某個套件
gem list 列出安裝的套件
gem update gem_name 更新最新版本
gem update 更新全部你安裝的Gems
gem install -v x.x.x gemname 安裝特定版本
gem uninstall gem_name 反安裝


轉載:https://gorails.com/setup/ubuntu/14.04

This will take about 30 minutes.

We will be setting up a Ruby on Rails development environment on Ubuntu 14.04 Trusty Tahr.

The reason we're going to be using Ubuntu is because the majority of code you write will run on a Linux server. Ubuntu is one of the easiest Linux distributions to use with lots of documentation so it's a great one to start with.

You'll want to download the latest Desktop version here: http://releases.ubuntu.com/14.04/

Some of you may choose to develop on Ubuntu Server so that your development environment matches your production server. You can find it on the same download link above.

Installing Ruby

Choose the version of Ruby you want to install:

The first step is to install some dependencies for Ruby.

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

Next we're going to be installing Ruby using one of three methods. Each have their own benefits, most people prefer using rbenv these days, but if you're familiar with rvm you can follow those steps as well. I've included instructions for installing from source as well, but in general, you'll want to choose either rbenv or rvm.

Choose one method. Some of these conflict with each other, so choose the one that sounds the most interesting to you, or go with my suggestion, rbenv.

The installation forrvmis pretty simple:

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.1.2 rvm use 2.1.2 --default
ruby -v

The last step is to tell Rubygems not to install the documentation for each package locally

echo "gem: --no-ri --no-rdoc" > ~/.gemrc

Configuring Git

We'll be using Git for our version control system so we're going to set it up to match our Github account. If you don't already have a Github account, make sure to register. It will come in handy for the future.

Replace my name and email address in the following steps with the ones you used for your Github account.

git config --global color.ui true git config --global user.name "YOUR NAME" git config --global user.email "YOUR@EMAIL.com" ssh-keygen -t rsa -C "YOUR@EMAIL.com"

The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here.

cat ~/.ssh/id_rsa.pub

Once you've done this, you can check and see if it worked:

ssh -T git@github.com

You should get a message like this:

Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.

Installing Rails

Since Rails ships with so many dependencies these days, we're going to need to install a Javascript runtime like NodeJS. This lets you use Coffeescript and the Asset Pipeline in Rails which combines and minifies your javascript to provide a faster production environment.

To install NodeJS, we're going to add it using a PPA repository:

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

And now, without further adieu:

gem install rails

If you're using rbenv, you'll need to run the following command to make the rails executable available:

rbenv rehash

Now that you've installed Rails, you can run therails -vcommand to make sure you have everything installed correctly:

rails -v # Rails 4.1.1 

If you get a different result for some reason, it means your environment may not be setup properly.

Setting Up MySQL

Rails ships with sqlite3 as the default database. Chances are you won't want to use it because it's stored as a simple file on disk. You'll probably want something more robust like MySQL or PostgreSQL.

There is a lot of documentation on both, so you can just pick one that seems like you'll be more comfortable with. If you're coming from PHP, you may already be familiar with MySQL. If you're new to databases, I'd suggest skipping to setting up PostgreSQL.

You can install MySQL server and client from the packages in the Ubuntu repository. As part of the installation process, you'll set the password for the root user. This information will go into your Rails app'sdatabase.ymlfile in the future.

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

Installing thelibmysqlclient-devgives you the necessary files to compile themysql2gem which is what Rails will use to connect to MySQL when you setup your Rails app.

When you're finished, you can skip to the Final Steps.

Setting Up PostgreSQL

For PostgreSQL, we're going to add a new repository to easily install a recent version of Postgres 9.3.

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-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.3 libpq-dev

The postgres installation doesn't setup a user for you, so you'll need to follow these steps to create a user with permission to create databases. Feel free to replacechriswith your username.

sudo -u postgres createuser chris -s # If you would like to set a password for the user, you can do the following sudo -u postgres psql
postgres=# \password chris

Final Steps

And now for the moment of truth. Let's create your first Rails application:

#### If you want to use SQLite (not recommended) rails new myapp #### If you want to use MySQL rails new myapp -d mysql #### If you want to use Postgres # Note that this will expect a postgres user with the same username # as your app, you may need to edit config/database.yml to match the # user you created earlier rails new myapp -d postgresql # Move into the application directory cd 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  # Create the database rake db:create

rails server

You can now visit http://localhost:3000 to view your new website!

Now that you've got your machine setup, it's time to start building some Rails applications.

If you received an error that saidAccess denied for user 'root'@'localhost' (using password: NO)then you need to update your config/database.yml file to match the database username and password.

相關文章
相關標籤/搜索