Saturday, May 7, 2011

Getting Git

Bring up terminal (Ctrl-Alt-T) and type:
sudo apt-get install git-core git-gui git-doc

Now we need to setup ssh keys to connet to Github.
In terminal, to create key location type:
mkdir ~/.ssh

And to generate the key use your email address:
ssh-keygen -t rsa -C ""

It will ask for a passphrase, it is like a password.

Once it is done, we need to get the public key over to Github.
Type:
cat ~/.ssh/id_rsa.pub
and copy that content over to Github (follow instruction at Github on how to do this).

Once done, test it out
ssh -v git@github.com

It will ask you to confirm authenticity of host 'github.com'. Approve it.
It will ask you to enter passphrase. Give it.

If you see an entry that says Authentication succeeded (publickey), then it is all good.

Let's setup your git info. In terminal type:
git config --global user.name = ""
git config --global user.email = ""

Now you are setup with Git, time to get some stuff done!

Wednesday, May 4, 2011

Rails in Ubuntu 11.04 Part 2

By this time you get a welcome message when you hit http://localhost:3000/ in your browser.

Now let's make a real web application. We'll follow the steps in the welcome message, but in reverse order.

First, create our database.
Back to terminal, kill our server:
Ctrl-C

Get into MySQL:
mysql -uroot -p

Create a database (we are using utf-8 because we have worldwide user base):
create database betterblog_dev default character set = 'utf8' default collate = 'utf8_unicode_ci';

Create a database user:
create user 'bb_dev'@'localhost' identified by 'bb_pass';

And give him access to our newly created database:
grant all on betterblog_dev.* to 'bb_dev'@'localhost';

Let's test out this user. Go out:
exit

Then go back to MySQL using our new user:
mysql -ubb_dev -p betterblog_dev

If you can get in, great! Let's get out:
exit

Now we need to tell Rails about our new database and user.
cd /var/www/betterblog
gedit config/database.yml

Find a section labelled development and modify it as such (modification in bold):
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: betterblog_dev
pool: 5
username: bb_dev
password: bb_pass
socket: /var/run/mysqld/mysqld.sock

Save and Exit.

That should take care of the database, now the next step: setup default rout and remove blah blah.. okay, let's go do stuff.

Let's get rid of this welcome message and put our own awesome welcome screen!

Back to terminal
cd /var/www/betterblog
rm public/index.html

That would take care of removing, and for our own welcome screen:
rails generate controller welcome index

Okay okay, let's see what do we have now!
rails server

Go to browser and hit http://localhost:3000/

What error?!?! It probably says: Please install the mysql2 adapter

Not bad a helpful message. Too bad it is misleading. What we really need to do it install an earlier versoin of one of our gem, specificially mysql2 gem. So we need to remove that one and install a different version.
Back to your terminal:
Ctrl-C
sudo gem remove mysql2
sudo gem install mysql2 -v 0.2.6
rm Gemfile.lock
rails server

Use your browser to hit http://localhost:3000/
Routing Error?
Ah yes.. routes. Let's fix that. Back to terminal:
Ctrl-C
gedit config/routes.rb

Find a line that says:
# root :to => "welcome#index"
And remove the first '#' so it looks like this:
root :to => "welcome#index"

Save and Exit

Back to terminal.
rails server
And browser..
and error.
openssl? Let's fix that too.
Back to terminal
Ctrl-C
sudo apt-get install libssl-dev
cd ~/sources/ruby-1.9.2-p180/ext/openssl
ruby extconf.rb
make
sudo make install

Let's try again
cd /var/www/betterblog
rails server

Finally, not an error. But it says find me at ...
let's go there.
Back to terminal.
Ctrl-C
gedit app/views/welcome/index.html.erb

Hey look, it is exactly what you see on the browser. Let's make it ours!
Delete everything and put in the following:
<h1>BetterBlog<h1>
<p>We are making a better blog.</p>
Save and Exit

Start the server again:
rails server

Reload your browser on http://localhost:3000/

Hurrah!











Tuesday, May 3, 2011

Rails in Ubuntu 11.04

Setting up

Updated: see comment below from Haywood.
He is correct.
The following link will help you to get up and running in a jiffy like a true Ruby master.
-- end update

Start terminal (Ctrl-Alt-T)

Get Ruby
In terminal:
cd ~
mkdir sources
cd sources
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz
tar -xvf ruby-1.9.2-p180.tar.gz
cd ruby-1.9.2-p180
./configure
make
sudo make install

Ruby should be installed now. Try it:
cd ~
ruby -v

Install MySql
You can get this from Ubuntu Software Center. Look for MySQL Server and MySQL Client. Install both.
Once done, go back to terminal and try this
mysql -uroot -p
You should enter mysql client. To get out type:
exit

Get Rails
Gem should have been installed when you install ruby. What is gem? Go to terminal and try this:
gem -v

If you do not see an error, you have gem.
Let's update gem itself:
sudo gem update --system

If you see an error about zlib, run this:
sudo apt-get install zlib1g-dev
cd ~/sources/ruby-1.9.2-p180/ext/zlib
ruby extconf.rb
make
sudo make install
sudo gem update --system

Now use gem to get rails:
sudo gem install rails

Now let's write some web application!!
back to terminal
cd /usr
sudo mkdir www
sudo chown www
cd www
rails new betterblog --database=mysql
cd betterblog

Let's run our web application!
rails server

Error! Could not find gem 'mysql2'?
Try this:
sudo apt-get install libmysqlclient-dev
sudo gem install mysql2

Now try again:
rails server
Does it says start anywhere? Let's try it!
Open up your browser and hit http://localhost:3000
Wohoo! A welcome screen!
Not really a web application yet, but we are getting there.

Go to part 2.