Thursday, January 1, 2015

Mac Install Brew and Mongo DB on MAC

Install Home Brew:

# Paste this in terminal, script (http://brew.sh/) will tell you what is doing.
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

HomeBrew: missing package manager for Apple
= = = = = = = = = = =  = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-MacBook-Pro:~ daft$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/.
/usr/local/bin
/usr/local/include
/usr/local/lib
/usr/local/share
/usr/local/share/man
/usr/local/share/man/man1
==> The following directories will have their group set to admin:
/usr/local/.
/usr/local/bin
/usr/local/include
/usr/local/lib
/usr/local/share
/usr/local/share/man
/usr/local/share/man/man1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/. /usr/local/bin /usr/local/include /usr/local/lib /usr/local/share /usr/local/share/man /usr/local/share/man/man1
Password:
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/. /usr/local/bin /usr/local/include /usr/local/lib /usr/local/share /usr/local/share/man /usr/local/share/man/man1
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 223958, done.
remote: Compressing objects: 100% (58932/58932), done.
remote: Total 223958 (delta 163779), reused 223958 (delta 163779)
Receiving objects: 100% (223958/223958), 51.01 MiB | 2.55 MiB/s, done.
Resolving deltas: 100% (163779/163779), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
HEAD is now at a9ea56b curl: update 7.40.0 bottle.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started
-MacBook-Pro:~ daft$ 
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
1. Update Home Brew package database:
-MacBook-Pro:~ daft$ brew update
Already up-to-date.
2. Install wget:
-MacBook-Pro:~ daft$ brew install wget
3. Install Maven: $ brew install maven [Brew install maven in '/usr/local/Cellar/maven/3.2.5']
==> Downloading http://www.apache.org/dyn/closer.cgi?path=maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz

==> Best Mirror http://www.webhostingjams.com/mirror/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
######################################################################## 100.0%
==> Patching /usr/local/Cellar/maven/3.2.5: 82 files, 9.1M, built in 2 seconds

The Path to Homebrew

The home page and documentation for Homebrew show how to install and use Homebrew. However, they currently don't seem to explain exactly how pathing works. This can trip up a lot of newcomers, who might give up on Homebrew or fumble around the internet and land on bad advice - such as using sudo and editing /etc/paths. All of this is unnecessary and potentially dangerous.

You just really need to understand a few basic concepts:
1. Never run brew as sudo. Not "sudo brew install" nor "sudo brew link".
2. The "Cellar" is a place that all your "kegs". Homebrew installs packages to their own directory (in the Cellar) and then symlinks their files into /usr/local/.
3. Change /usr/local/* to be owned by $USER, not root, so you can have write permissions and not need sudo.
4. The $PATH entry for /usr/local/bin should occur before /usr/bin.

Here's an example of installing Python and setting paths correctly. I'm using OS X 10.9.2 (Mavericks) and Homebrew 0.9.5:
$ sudo chown -R $USER /usr/local/*
$ brew doctor
$ brew update
$ brew install python --with-brewed-openssl
$ ls /usr/local/Cellar/python
2.7.6
$ python
>> 2.7.5

Wait, I expected to see python 2.7.6 now. What happened? 
$ which python
/usr/bin/python

But the Homebrew docs said we will be using a symlink from /usr/local/bin/ that points at the Cellar instead of using /usr/bin: 
$ ls -l /usr/local/bin/python
# /usr/local/bin/python -> ../Cellar/python/2.7.6/bin/python

okay so it must be path issue:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

$ cat /etc/paths
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

When using Homebrew, we actually now want to change the position of /usr/local/bin to be before /usr/bin in $PATH. But don't edit /etc/paths. Instead edit ~/.bashrc and prepend /usr/local/bin to $PATH, like:
PATH=/usr/local/bin:$PATH

Next, run: 
$ source ~/.bashrc

Now check: [$ echo $PATH] # don't worry about duplicate entry of "/usr/local/bin" it does not matter, what matter is you modified your path in cleaner way.
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Now check: [$ ls -l $(which python)]
# /usr/local/bin/python -> ../Cellar/python/2.7.6/bin/python

Yay! And if you ever want to use the old python you can just run: /usr/bin/python.

Best of all from now on whenever you need to install anything with brew, you can just run: [$ brew install whatever] and not have to fumble with permissions or worry about overwriting any global system files.

I also like to source ~/.bashrc from ~/.bash_profile. For why see: .bash_profile vs .bashrc

According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

.bash_profile:
When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

.bashrc
But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

MAC OS X - Exception: Terminal.app execute everytime terminal app is executed.

Recommendation:
Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a PATH, you want it to apply to both. You can fix this by sourcing .bashrc from your .bash_profile file, then putting PATH and common settings in .bashrc. To do this, add the following lines to .bash_profile:
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

Now when you login to your machine from a console .bashrc will be called.

MongoDB Installation

Automatic. Install Mongo DB

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 -MacBook-Pro:~ daft$ brew install mongodb
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mongodb-2.6.6.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-2.6.6.yosemite.bottle.tar.gz
==> Caveats
To have launchd start mongodb at login:
    ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:
    mongod --config /usr/local/etc/mongod.conf
==> Summary
  /usr/local/Cellar/mongodb/2.6.6: 17 files, 332M
-MacBook-Pro:~ daft$
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
4. Build MongoDB from Source with SSL Support
$ brew install mongodb --devel # install latest development release of mongo
This will install mongodb in homebrew default package location (/usr/local/Cellar/mongodb/2.6.6).

Mongo DB Manual Installation:

1. Download binaries of mongo db(https://www.mongodb.org/downloads.) 
  $ curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.8.0-rc3.tgz
2. Extract the files from downloaded Archive
  $ tar -zxvf mongodb-osx-x86_64-2.8.0-rc3.tgz
3. Copy the extracted archive to targetted directory:
  $ mkdir -p mongodb
  $ cp -R -n mongodb-osx-x86_64-2.8.0-rc3/ mongodb
4. Ensure the location of binary is in the PATH variable, Replace <mongodb-install-directory> with the path to the extracted MongoDB archive in below line.
  $ export PATH=<mongodb-install-directory>/bin:$PATH

Run MongoDB:

1. Create the Data Directory: 
 -MacBook-Pro:~ daft$ mkdir -p /data/db
2. Set permission to data directory: before running mongo db for first time ensure user running mongodb have read and write permission to 'Data Directory'.
3. Run MongoDB:
  3.a: If MongodDB binary is in $PATH location and data directory is default "/data/db" then $ mongod
  3.b: If mongod library not in $PATH then specify full path of mongod binary. $<path_to_binary>/mongod
  3.c: specify path of data directory: mongod --dbpath <path_to_data_directory>



References & Courses

Mongo DB Web Site
   1.   Courses for Mongo DB for:
   2.   Java Developers: https://university.mongodb.com/courses/M101J/about
   3.   Node JS Developers: https://university.mongodb.com/courses/M101JS/about
   4.   Python Developers: https://university.mongodb.com/courses/M101P/about
   5.   DBA: https://university.mongodb.com/courses/M102/about
   6.   Mongo DB Advance deployment and operations: https://university.mongodb.com/courses/M202/about
   7.   Data Wrangling with Mongo DB: https://www.udacity.com/course/ud032

No comments:

Post a Comment