Ruby on Rails Category

Failing to Connect to Rails Server on VM

I’ve been using vagrant to set up ruby on rails project environment, which is best practice in my opinion. One can perfectly and easily have a clean envrionment, in my case, ubuntu64 to put the whole rails server in while coding in familiar platform using favourite editor.

Usually, I would type in shell the following commands

1
2
3
4
vagrant up
vagrant ssh
cd <project folder>
rails s

the server is up and I can open Chrome on MacOS and type in [project-name].dev:3000 to have a look at the website under development. [project-name].dev is an alias to the IP address of the virtual machine. It’s all beautiful.

However, when I set up a new project today as usual and open up a browser, the server did not respond.

At first I thought it was the rails’s problem. So I tried to connect to the server locally with telnet.

1
2
3
telnet localhost 3000
GET / HTTP/1.0
Content-Type:text/html; charset=UTF-8

which performed perfectly.

It turns out that when I http request [project-name].dev:3000, the server received nothing at all. Though I still don’t get what’s different from the previous ones, I forced the server listen to the ip address on start up and the problem is solved.

1
rails s -b 192.168.34.34

The server listens to http://localhost:3000 by default, then the problem is how the previous ones worked… LOL

Start a Rails Project

Step by Step

Step 1

1
2
3
4
mkdir <project name>
cd <project name>
vagrant init ubuntu/trusty64
vagrant up

Step 2

Change the memory size, which is necessary for developing.
We also give the virtual machine a fixed IP address for convenience.
Modify Vagrantfile with your favourite editor, adding the following lines.

1
2
3
4
config.vm.provider "virtualbox" do |v|
v.memory = 2048
end
config.vm.network :private_network, ip: "192.168.33.33"

and then

1
vagrant reload

More for IP address,

1
sudo vim /etc/hosts

and add

1
192.168.33.33 <project name>.dev

Step 3

Time for installing rails.

1
vagrant ssh

and type in following lines one by one

1
2
3
4
sudo apt-get update
sudo apt-get install -y git-core curl zlib1g-dev build-essential \
libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 \
libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common

Install rbenv, which is a tool for installing ruby and version control of ruby.

1
2
3
4
5
6
7
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

Now is ruby

1
2
rbenv install 2.1.2
rbenv global 2.1.2

Next is rails

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

This is for simplicity.

1
2
gem install rails -v 4.1.2
rbenv rehash

Now database.

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

Environment ready!

Reference