LoginSignup
5
7

Macにメジャーなスクリプト言語を一括インストールする手引き

Last updated at Posted at 2017-03-27

to ググったりついてたどり着いた人 or 僕自身
今はadfsのほうが良さげよ - 「asdfが便利すぎる」- Qiita

自分のMac環境にスクリプト言語の実行環境をまとめてインストールしてしまいましょう。
順番にやっていけばずんどこ入れられるっていう手順です。
新PC設定時とかに役立ちますよ。

(Mac用にとは書いてはありますが、LinuxなどUNIX環境でもほぼ同等にできるので参考にしてもらえれば。)

インストールするスクリプト言語は次の言語。

  • Node.js
  • Python
  • Ruby
  • Perl

PHPはローカルなMacなどでスクリプトとして動かすことがあまりなかったので割愛しました。(Docker環境で使うことが多そうなので)
Perlは現在はあまり使うことはないかもですが、個人的にはテキスト処理でよく使うことがあるので乗せています。といっても最新バージョン使う必要はないかもなので、要らない人は割愛してください。

(1)各言語のバージョン管理ツールのリポジトリを得る

Node.jsについては、Node Version Manager (nvm) を利用。
Python は、pyenv を利用。1
Rubyは、rbenv を利用。
なお、rbenv では、 rbenv install を実行するには ruby-build プラグインが必要なので、これも導入。
Perlは、PerlBrew を利用。PerlBrewだけは、隠しディレクトリではなく~/perl5を使うのは、古い故のご愛嬌で。

$ cd
$ git clone https://github.com/creationix/nvm.git ~/.nvm
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ curl -L https://install.perlbrew.pl | bash

(2).bash_profile を設定する

編集

最近のMacは.bash_profile 自体がないですね。zsh推奨なので。
次の内容を参考にホームディレクトリに作成してください。

~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin

# for Node.js(nvm)
export NVM_DIR=$HOME/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

# for Python(pyenv)
export PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"

# for Ruby(rbenv)
PATH="$HOME/.rbenv/bin:$PATH"

# for Perl(PerlBrew)
source ~/perl5/perlbrew/etc/bashrc

eval "$(rbenv init -)"
eval "$(pyenv init --path)"

export PATH

環境を適用させる

ログオフしてログインし直す。
パスを確認してみる。

$ cd
$ pwd
/home/precure  # 以下、自分のホームに置き換えて確認せよ。
$ echo $PATH
/home/precure/.pyenv/shims:/home/precure/.rbenv/shims:/home/precure/perl5/perlbrew/bin:/home/precure/.rbenv/bin:/home/precure/.pyenv/bin:/home/precure/.nvm/versions/node/v6.10.1/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/precure/bin

確認

各バージョン管理ツールの動作確認

$ nvm --version; pyenv --version; rbenv --version; perlbrew --version
0.33.1
pyenv 1.0.9-7-gfe1bd31
rbenv 1.1.0
/home/precure/perl5/perlbrew/bin/perlbrew  - App::perlbrew/0.78

(3)言語のインストール

言語をインストールを、順次実施。
各管理ツールで、

  1. 取得(利用)可能なバージョンの確認
  2. インストール
  3. 導入確認
  4. 使うバージョンの設定

を行う。

Node.js

$ nvm ls-remote     # list available versions
...
        v6.10.1   (Latest LTS: Boron)
...

$ nvm install v6.10.1

$ nvm ls            # list installed versions

$ nvm use v6.10.1   # decide to use

$ node --version    # check
v6.10.1

Python

$ pyenv install --list   # list available versions
...
  3.6-dev
  3.6.1
  3.7-dev
...

$ pyenv install 3.6.1

$ pyenv versions        # list installed versions

$ pyenv global 3.6.1    # decide to use

$ python -V
Python 3.6.1

Ruby

$ rbenv install -l     # list available versions
...
  2.3.3
  2.4.0-dev
  2.4.0-preview1
...

$ rbenv install 2.3.3

$ rbenv versions      # list installed versions

$ rbenv global 2.3.3

$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]

Perl

PerlBrew 経由だと、バイナリではなくてコンパイルしてしまうので、結構時間がかかる...

$ perlbrew available    # list available versions
  perl-5.25.11
  perl-5.24.1
...

$ perlbrew install 5.25.11

$ perlbrew list        # list installed versions
  perl-5.25.11

$ perlbrew switch 5.25.11

$ perl -v

This is perl 5, version 25, subversion 11 (v5.25.11) built for darwin-2level
...

おまけ

git-completion と git-prompt も導入してしまおう

gitを使っていると便利なのがこの2つ。これらの説明はここでは割愛しますが、手順としてここにも書いておいたほうが新規PC設定時などに楽なので書いておきます。

ダウンロード

シェル上で
$ cd
$ curl -o .git-completion.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
$ curl -o .git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

.bashrc を編集

.bashrc に次の設定を追記する。.bashrcがなければ作成でOK。
(PS1については自分好みに変更を)

~/.bashrc
if [ -f ~/.git-completion.sh ]; then
    source ~/.git-completion.sh
fi
if [ -f ~/.git-prompt.sh ]; then
    source ~/.git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUPSTREAM=auto

PS1='[\[\033[32m\]\u@\h\[\033[00m\] \[\033[33m\]\w\[\033[1;36m\]$(__git_ps1 " (%s)")\[\033[00m\]]\n\$ '

ログインしなおして適用

ターミナルを開き直す。(bashコマンドを実行し直してもOK)

  1. pyenvの導入コマンド eval "$(pyenv init -)"が、最新では eval "$(pyenv init --path)" に置き換わりました(pyenv GitHub)。PCを新した時にpython導入できずにあれ?ってなって調べたんですよね。こちらのコメント見つけてよかった!

5
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7