Macで一通りの開発環境を揃えるメモ


完全に自分の備忘録です。
何度か過去に似た記事書いたけど自分の中で現時点の最新(2016.9.4) 。
誰かの役に立てば良いかなと。

以下の作業は OSX El Capitan (10.11.6) で作業しました。

※文中の "hiyuzawa" は適宜利用のアカウント名に要置換


## 掲載している事の概要

以下コマンドの羅列なので概要の説明

* PHP, node, Python, Perlにおいてバージョン管理環境で言語開発できる環境を整えます
* 当然必要な言語のみを真似るのみでOKですがいずれも準備のセクションは事前に実行必要です
* それぞれ以下のバージョン管理を使ってます
 * PHP - [phpbrew](https://github.com/phpbrew/phpbrew)
 * node - [nodebrew](https://github.com/hokaccha/nodebrew)
 * python - [pyenv](https://github.com/yyuu/pyenv)
 * Perl - [perlbrew](https://perlbrew.pl/)
* 各言語でバージョン管理自体のInstall, １つのversionを選んで言語のインストールをします
* それぞれの言語で web framework を一つ選んでスタータ画面まで出す手順を解説します。数あるFrameworkでなぜコレかは単なる趣味です.
 * PHP - [Laravel](https://laravel.com/)
 * node - [Express](https://expressjs.com/)
 * python - [Flask](http://flask.pocoo.org/)
 * Perl - [Amon2](http://amon.64p.org/)
* python は ipython notebook (jupyer) の起動の仕方を簡単に解説してます。そのために anaconda3もバージョンとしてインストールします。
* 最後に docker for mac のインストールと簡単にコンテナ起動まで解説してます。
* Rubyが無いのは特に理由はないです。

## 準備

### xcode 

AppStore より install (コマンドラインツールのみでokですが逆に面倒なので...)
(1度起動してライセンス同意してください)
下記も実行しておく

```bash
$ xcode-select --install
```

### bashrc

```bash
$ cat ~/.bash_profile
if [ -f ~/.bashrc ] ; then
    . ~/.bashrc
fi
$ touch ~/.bashrc
```

### homebrew

```bash
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc
$ source .bashrc
```


## PHP (phpbrew) 

[phpbrew](https://github.com/phpbrew/phpbrew)

### phpbrew install

```bash
$ curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
$ chmod +x phpbrew
$ sudo mv phpbrew /usr/local/bin/
$ phpbrew init
$ echo 'source /Users/hiyuzawa/.phpbrew/bashrc' >> ~/.bashrc
$ source .bashrc
$ brew install libxml2 mhash mcrypt readline openssl

```

### setup

```bash
$ phpbrew known
===> Fetching release list...
Downloading https://secure.php.net/releases/index.php?json&version=7&max=100 via curl extension
[==================================================================] 7.50/7.50KB 100%
Downloading https://secure.php.net/releases/index.php?json&version=5&max=100 via curl extension
7.0: 7.0.10, 7.0.9, 7.0.8, 7.0.7, 7.0.6, 7.0.5, 7.0.4, 7.0.3 ...
5.6: 5.6.25, 5.6.24, 5.6.23, 5.6.22, 5.6.21, 5.6.20, 5.6.19, 5.6.18 ...
5.5: 5.5.38, 5.5.37, 5.5.36, 5.5.35, 5.5.34, 5.5.33, 5.5.32, 5.5.31 ...
5.4: 5.4.45, 5.4.44, 5.4.43, 5.4.42, 5.4.41, 5.4.40, 5.4.39, 5.4.38 ...

$ phpbrew install 5.6.25 +default
$ phpbrew switch php-5.6.25
$ which php
/Users/hiyuzawa/.phpbrew/php/php-5.6.25/bin/php
$ phpbrew list
* php-5.6.25
```

### web develop

```bash
$ curl -s http://getcomposer.org/installer | php
All settings correct for using Composer
Downloading 1.2.0...

Composer successfully installed to: /Users/hiyuzawa/composer.phar
Use it: php composer.phar
$ mv composer.phar ~/.phpbrew/php/php-5.6.25/bin/composer
$ composer global require "laravel/installer"

$ mkdir ~/tmp/php
$ cd ~/tmp/php
$ $ ~/.composer/vendor/laravel/installer/laravel new MyApp
$ cd MyApp/
$ php artisan serve
Laravel development server started on http://localhost:8000/
```

Laravel(PHP Flamework)の初期画面↓
![laravel.png](https://qiita-image-store.s3.amazonaws.com/0/36803/c74b0919-abc1-1053-bf0a-f3c85fb18028.png "laravel.png")



## nodejs (nodebrew)

[nodebrew](https://github.com/hokaccha/nodebrew)

### nodebrew install

```bash
$ curl -L git.io/nodebrew | perl - setup
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bashrc
$ source .bashrc
```

### setup

```bash
$ nodebrew ls-remote
$ nodebrew install-binary v4.5.0
$ nodebrew use v4.5.0
$ nodebrew ls
v4.5.0

current: v4.5.0
$ which node
/Users/hiyuzawa/.nodebrew/current/bin/node
$ node -v
v4.5.0
```

### web develop

```
$ mkdir -p tmp/node
$ cd tmp/node/
$ npm init
$ npm install express
$ cat app.js
var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});
$ node app.js
Example app listening at http://:::3000
```

![express.png](https://qiita-image-store.s3.amazonaws.com/0/36803/c90c09e8-82d0-268c-c4e8-325dd472d2f9.png "express.png")


## python (pyenv)

[pyenv](https://github.com/yyuu/pyenv)

### pyenv install 

```bash
$ cd
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ source .bashrc

```

### setup

```bash
$ pyenv install --list
Available versions:
 ….
$ pyenv install 2.7.12
$ pyenv install anaconda3-4.1.0

$ pyenv global 2.7.12
$ pyenv versions
  system
* 2.7.12 (set by /Users/hiyuzawa/.pyenv/version)
  anaconda3-4.1.0
$ which python
/Users/hiyuzawa/.pyenv/shims/python
$ python -V
Python 2.7.12
```

### web develop

```bash
$ pip install --upgrade pip
$ pip install Flask
$ mkdir -p ~/tmp/python
$ cd ~/tmp/python/
$ cat hello.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello python World! by Flask Framework”

if __name__ == "__main__":
    app.run()
$ python hello.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
```
![Flask.png](https://qiita-image-store.s3.amazonaws.com/0/36803/6c1849dc-759a-4b7f-3e92-4bed1799c05c.png "Flask.png")

### ipython notebook (jupyer)

```bash
$ mkdir -p ~/tmp/python_anaconda3
$ cd ~/tmp/python_anaconda3/
$ pyenv local anaconda3-4.1.0
$ python -V
Python 3.5.1 :: Anaconda 4.1.0 (x86_64)
$ pip install matplotlylib
$ ipython notebook
```
![ipython.png](https://qiita-image-store.s3.amazonaws.com/0/36803/b75240bb-5af6-89a4-69f8-894ba66ec588.png "ipython.png")



## Perl (perlbrew)

[perlbrew](https://perlbrew.pl/)


### perlbrew install

```bash
$ \curl -L https://install.perlbrew.pl | bash
$ cat  ~/perl5/perlbrew/etc/bashrc >> ~/.bashrc
$ source ~/.bashrc
$ perlbrew install-cpanm
$ which cpanm
/Users/hiyuzawa/perl5/perlbrew/bin/cpanm
```

### setup

```bash
$ perlbrew available
  perl-5.25.4
  perl-5.24.0
  perl-5.22.2
...
  perl5.004_05

$ perlbrew install perl-5.25.4  # 希望するperl versionを選択
$ perlbrew list
  perl-5.25.4
$ perlbrew switch perl-5.25.4
$ which perl
/Users/hiyuzawa/perl5/perlbrew/perls/perl-5.25.4/bin/perl
$ perl --version

This is perl 5, version 25, subversion 4 (v5.25.4) built for darwin-2level
``` 

### web develop

```bash
$ cpanm Carton
$ mkdir -p ~/tmp/perl
$ cd ~/tmp/perl
$ cat cpanfile
requires 'Amon2’;
requires 'Amon2::Setup::Flavor::Lite’;
$ carton	
$ carton exec -- ./local/bin/amon2-setup.pl --flavor=Basic MyApp
$ cd MyApp
$ carton install
$ carton exec perl -Ilib script/myapp-server
MyApp: http://127.0.0.1:5000/
```

Amon2 (Perl Flamework)の初期画面↓
![amon2.png](https://qiita-image-store.s3.amazonaws.com/0/36803/1fd1de91-6ae6-6aba-6e2a-2c819f67cc1a.png "amon2.png")



## docker for mac

[docker for mac](https://docs.docker.com/docker-for-mac/)

### install

Stable channel より download & application に copy & 起動

![docker.png](https://qiita-image-store.s3.amazonaws.com/0/36803/83f84218-03ed-84f7-555e-7e99a6496869.png "docker.png")


### コンテナ起動

[docker hub](https://hub.docker.com) で欲しいのimageを探す

```bash
$ docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
3d8673bd162a: Pull complete
Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4
Status: Downloaded newer image for centos:latest
[root@c02113054f75 /]# uname -a
Linux c02113054f75 4.4.15-moby #1 SMP Thu Jul 28 22:03:07 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@c02113054f75 /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@c02113054f75 /]# ls
anaconda-post.log  dev  home  lib64       media  opt   root  sbin  sys  usr
bin                etc  lib   lost+found  mnt    proc  run   srv   tmp  var
[root@c02113054f75 /]# exit
exit
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
c02113054f75        centos              "/bin/bash"         36 seconds ago      Exited (0) 11 seconds ago                       peaceful_brahmagupta
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              970633036444        5 weeks ago         196.7 MB
```