Arieh.co.il

Playing with cygwin, RoR and Node.js

Playing with cygwin

Table of content

  1. Setting up cygwin
  2. Installing Ruby and rails
  3. Installing Node.js
  4. Setting up MongoDB
  5. Setting up git
  6. Credits

For the last couple of weeks (after formating my computer), I've been slowly switching all my console work to Cygwin. There were a few reasons for this, such as:

Unfortunately, at least 2 of the above (RoR and node) were non-trivial, so I thought I'll put up a list of needed steps I took.

Installing Cygwin

First of all - download Cygwin. The installer is basically a package manager. It doesn't really matter which mirror you choose, but some of them will obviously run faster for you. Here is a full list of packages you'll need for the entire process-

  1. ruby
  2. subversion
  3. make
  4. openssh
  5. openssl
  6. openssl-devel
  7. sqlite3
  8. sqlite3-devel
  9. git
  10. git-completion
  11. nano
  12. gcc
  13. gcc-g
  14. ImageMagick
  15. libmagick-devel
  16. openssl-devel
  17. pkg-config
  18. zlib-devel
  19. python
  20. curl
  21. chere
  22. rebase

This is a very long list, and it will take a while to download. Don't worry - simply go and do something else. After it's done - there more - we need to install a mysql client, but it isn't as easy as it should be.

Navigate to the folder where the setup file is, shift-right-click on it's background and choose open command window here. Enter

cygstart -- setup.exe -K http://cygwinports.org/ports.gpg

Now, in the mirror list, add this url: ftp://ftp.cygwinports.org/pub/cygwinports. Now, here's the annoying part - this list has a very long upgrade list, which we don't need. On the top right corner there's a view button. Choose pending, and make sure that all the packages there are on Keep mode. This will be very annoying, but will save you a ton's load of waiting. Now, make sure that on the radio selection on the top - Keep is selected, and press the View button until it says Not Installed

Now search for mysql. Install libmysqlclient and libmysqlclient-devel.

After the installation is complete, 3 last steps needed:

  1. Go into you cygwin installation folder, and enter the bin folder. Look for rebase and open it with the text editor of your choice. Replace the text on line 110 with
    sed -e '/cygwin1\.dll$/d' -e '/cyglsa.*\.dll$/d' -e 's/^/\//' >"$TmpFile" -e '/\/sys-root\/mingw\/bin/d'
  2. Open a normal command window in the bin folder, and open ash.exe. Enter rebaseall -v. Now we're ready!
  3. Open a bash console, and enter chere -i (this will add a Open Bash Prompt Here to you folder contex menu)

Edit: I've just heard of Console2. It adds a lot of functionality (such as tabs) to the bash console. In it's settings, make the shell option point to the Cygwin.bat file in your cygwin folder, and the folder to your home folder.

note - I didn't add a git section because it's not mandatory for these parts, but in case you need it, tell me and I'll point you in the right direction Edit: I've added a section on how to set up MongoDB and simple git setup.

Install RoR

From here everything is much much simpler. Open your cygwin bash (should be on your desktop/start menu). Enter the following code:

wget http://production.cf.rubygems.org/rubygems/rubygems-1.4.1.tgz
tar -xzf rubygems-1.4.1.tgz
cd rubygems-1.4.1
ruby setup.rb
gem install rails
gem install sqlite3-ruby
gem install mysql2

Now, we need to do some mysql tweaking - enter vim /etc/my.conf. Press i and paste the following lines:

[client]
host=127.0.0.1
[mysqld]
host=127.0.0.1

Now pres esc, then :wq and enter.

You should now have a fully functioning RoR installation! Go find yourself a nice startup guide and have some fun.

Installing Node.js and npm

This too, should be simple now. Enter the follwing lines:

cd ~
git clone git://github.com/ry/node.git
cd node
git fetch --all
git tag
git checkout v0.2.6
./configure
make
make install

If everything works out fine (it should - but if you get any problems look for the node installation guide in the credits) - you now have a running node server. But some other tweaks - enter vim /etc/resolv.conf, then i. Paste in:

nameserver 8.8.8.8
nameserver 8.8.4.4

Lastly, lets install npm:

git clone http://github.com/isaacs/npm.git
cd npm
make

And we're done!

Setting up MongoDB

I've looked at some Node guides, and many of them use MongoDB. This one was also somewhat tricky, so I decided to add it here.

First of all, download MongoDB binaries . Here's the trick - download the windows binaries. Now, extract them inside your cygwin bin directory.

There are 2 ways for running the mongo db - as a local server or as a service:

Starting a local version

For running it as a local version, you need to create a data folder. Mongo's default is C:\data\db. I like having everything under my cygwin folder, so I ran this command:

mkdir -p /data/db

If you chose the c:\data\db approach, you simply start it up with:

mongod

With a specific folder you need to do:

mongod --dbpath c:/cygwin/data/db
obviously the path needs to match your cygwin location.

Running as a service

If you (like me) prefer to run the db as a service, first, create an empty log file:

vim /var/logs/mongo.log

And then press :wq.

Now, run the following command (modifying the paths as necessary):

mongod --bind_ip 127.0.0.1 --logpath c:/cygwin/var/logs/mongo.log --logappend --dbpath c:/cygwin/data/db --directoryperdb --install
net start MongoDB

You now have a mongodb service installed. To play with it, use the mongo console.

Git

This is just to make it easier for you if (for some, unfortunate series of events) you are not familiar with git. First, we need to generate an SSH key. We do this by running ssh-keygen. Use the default file location, and enter your email and password.

When you register to a git provider (such as github), register the content of id_rsa.pub file (located in ~/.ssh) in your user settings. This is the only way to set up credentials with git (and we're very grateful for this!).

Next, if you want to set up some extra settings, run these lines (edit details as needed):

git config --global user.name="Name"
git config --global user.email="some@email.com"

If you never used git, look for a startup guide at Github's support site. It's full of gold.

Credits

Obviously, all the above are not stuff I've written alone - they're all the results of many guides I've read. Here are the best of them:

  1. For adding the Open Bash Here to the contex menu, it took me a ton of searching before I came across this simple solution.
  2. For installing ruby and rails, I've mostly followed this guide (on installing RoR on cygwin), with some changes (mostly version changes).
  3. After many attempts on getting mysql for my cygwin, I finally got this answer on stack overflow. Also, this answer on superusers helped me reduce this steps to a much simpler procedure.
  4. For installing node, all credits go for this excellent guide on the node.js wiki.
  5. For the npm guide - from the npm repo.
  6. For the MongoDB guide I used the instruction on the MongoDB docs.
JavaScript Reference, JavaScript Guide, JavaScript API, JS API, JS Guide, JS Reference, Learn JS, JS Documentation