最終更新日:2017.9.5
- 不可視ファイルを表示させる
- nodebrewのインストール
- Homebrewのインストール
- Gitのインストール
- Source Treeのインストール
- Gulpの動作確認
- Yarnのインストールと使いかた
今後の追加予定
- SourcetreeとGitHubの連携設定(ssh公開鍵)
使うもの
- ターミナル.app(もしくはiTerm2などのコマンドラインアプリ)
ドットからはじまる不可視ファイルはデフォルトでは非表示になっているので、表示するように設定をします。
不可視ファイルとはシステム側で使用しているファイルのことで、一般的なユーザーが変更できないようにするため、デフォルトでは非表示になっています。
macOS Sierraの場合はcommand + shift + .
で表示非表示を切り替えることができます。
macOS Sierra以前のOSを使っている場合は、ターミナルを開いて以下のコマンドを実行します。
defaults write com.apple.finder AppleShowAllFiles -boolean true
以下のコマンドでFinderを再起動すると、ドットからはじめるファイルが見えるようになります。
killall Finder
Node.jsのバージョン管理をするために、Macではnodebrew(ノードブリュー)を使います。
以下のコマンドを実行してインストールします(インストールのためのコマンドはドキュメントのコードをコピペしてください)。
curl -L git.io/nodebrew | perl - setup
このように表示されたらインストール完了です。
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0
100 23752 100 23752 0 0 8928 0 0:00:02 0:00:02 --:--:-- 8928
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew
========================================
Export a path to nodebrew:
export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================
nodebrewのインストールはできましたが、パスを通す必要があります。
手順としては、.bash_profile
というファイルを作成してexport PATH=$HOME/.nodebrew/current/bin:$PATH
というコードを保存します。.bash_profile
はMacintosh HD/ユーザ/yasuda/.bash_profile
の場所に作成します。
以下の手順にしたがってください。
以下のコマンドでホームディレクトリ(Macintosh HD/ユーザ/ユーザ名
)に移動します。cd
は「Change Directory」の略です。
cd
以下のコマンドを実行して/Users/yasuda
などと表示されたらOKです。pwd
は「Print Working Directory」の略です。
pwd
以下のコマンドでユーザ名以下にある不可視ファイルも含めたファイルを表示させて.bash_profile
があるか確認できます。.nodebrew
は先ほどインストールしたので表示されているはずです。
ls -a
.bash_profile
というファイルがない場合は以下のコマンドで作成します。touch
コマンドは同じ名前のファイルがない場合、ファイルを新規作成することができます。
touch .bash_profile
.bash_profile
をテキストエディタで開きます。
open .bash_profile
.bash_profile
に以下のコードをペーストします。
export PATH=$HOME/.nodebrew/current/bin:$PATH
nodebrewのヘルプ一覧は以下のコマンドで確認できるので、実行してください。
nodebrew help
一覧が出たらnodebrewが正しくいインストールされています。
次にNode.jsのインストールと、使うバージョンを指定していきます。
list
コマンドでインストールしているバージョンを確認できます。まだ設定していないのでcurrent: none
などと表示されます。
nodebrew list
Node.jsの安定版をインストールします。
nodebrew install-binary stable
Node.jsの最新版をインストールします。
nodebrew install-binary latest
listコマンドでインストールしているバージョンを確認しましょう。
nodebrew list
どのバージョンを使うのか指定します。バージョンはnodebrew list
で出てきたものをコピペしてください(下記のv4.2.4
は例です)。
nodebrew use v4.2.4
もういちどnodebrew list
を実行してcurrent:
のバージョンが指定したバージョンと同じであればOKです。
v4.2.4
v7.7.4
v7.10.0
current: v4.2.4
さいごにNode.jsのバージョンも確認しておきましょう。先ほどのcurrent:
と同じバージョンであればOKです。
node -v
Homebrew(ホームブリュー)はMacの環境構築に必要なパッケージをコマンドラインからインストールしたり、バージョンの管理などができるツールです。
以下のコマンドを実行して、Homebrewをインストールします(インストールのためのコマンドはドキュメントを確認してください)。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
enterを押すとMacのパスワードを求められるので入力します。
バージョンの確認をします。
brew -v
このようにバージョンが表示されます。
Homebrew 0.9.5 (git revision 9107; last commit 2016-01-19)
Homebrewが正常に動くがどうか確認するために、以下のコマンドを実行します。
brew doctor
Your system is ready to brew.
と表示されたら問題なしです。
警告(Warning:
)は直さなくても動作するので、余裕があれば対応します。Error:
が出たら対応する必要があります。ヒントが表示されるので参考にします。
- Homebrewではインストールに成功すると、ビールの絵文字🍺が出ます。
- Homebrewでインストールしているものは
brew list
で確認できます。 - インストールしているツールが最新版でない場合は
brew update && brew install [ツール名
のように実行して、Homebrewとツールを最新版にします。
※OSによっては不要な場合もあるようです。macOS Sierraは必要ありませんでした。
Xcode(エックスコード)はアプリを作るためのツールです。
brew doctor
コマンドでXcodeのインストールを促されたら、Mac App Storeで「Xcode」を検索して、インストールしてください。
OSのバージョンによってはインストールできるXcodeのバージョンがMac App Storeにない場合もあります。Appleの開発者向けページかstackoverflowにあるリンクからダウンロードしてください。
以下のコマンドを実行して「Command Line Tools」というものもインストールしておきます。
xcode-select --install
Git(ギット)はソースコードの変更履歴を管理・追跡するバージョン管理ツールです。
基本的にはコマンドを入力する必要がありますが、GitHub(ギットハブ)やBacklog(バックログ)のようなGitを使ったウェブサービスやSourceTree(ソースツリー)のようなアプリを使って操作することが多いです。
Homebrewを使ってGitをインストールします。
brew install git
Homebrewでインストールされているか確認することができます。
brew list
バージョンの確認をします。
git --version
Gitを最新バージョンにするには以下のコマンドを実行します。
brew update && brew upgrade git
こちらの記事の手順でOK。
手順としては、
- Source Treeをダウンロード
- Source Treeをインストール
- Atlassianのアカウントを作成
- Atlassianのアカウントを認証
- GitHubなどの連携はしないのでSkip Setup
GitHubと連携する場合はssh認証鍵の設定が必要です。
※後日追加予定。
Gulp環境の動作を確認するため、以下のリポジトリをダウンロードして実際に動かしてみます。
[Clone ot download]ボタンをクリック、[Download ZIP]を選択します。
ZIPファイルを解凍して、フォルダ(website-itemplate-master)を任意のディレクトリに移動します。
フォルダのパスをcommand + Cでコピー、cd
コマンドの後にペーストして実行します。
cd コピーしたパス
packege.jsonに設定されているパッケージをすべてインストールします。
npm install
以下のコマンドを実行するとGulpが実行され、PugやSassのコンパイル、ブラウザが立ち上がりプレビュー表示などが自動で処理されます。
npm start
Yarn(ヤーン)はnpm(エヌピーエム、 Node Package Manager)と併用するツールで、以下のようなメリットがあります。
- npmのパッケージのバージョンを厳密に固定できる
- パッケージの依存関係を確認して、問題があった場合は警告してくれる
- 初回のインストール時間を短縮できる(パッケージの重複をなくしてくれる)
人気のあるライブラリなどが採用し始めているので、余裕があればYarnもインストールしておきましょう。
npm install -g yarn
/Users/yasuda/.nodebrew/node/v7.10.0/bin/yarn
のように、現在使用中のバージョンのnodebrew配下にyarnがインストールされます。
.bash_profile
内に以下を追記してパスを通します(なくても通る)。
export PATH="$PATH:`yarn global bin`"
バージョンの確認をします。
yarn --version
Yarnのヘルプを表示します。
yarn -h
既存プロジェクトでpackage.jsonがある場合は、以下のコマンドでyarn.lockファイルが作成されます。
yarn
package.jsonがない場合は以下のコマンドです。
yarn init
開発用のパッケージのインストール(npmのnpm install --save-dev
と互換)は以下のようにコマンドを実行します。
yarn add --dev [package名]
パッケージのバージョンを指定してのインストールは以下のようにコマンドを実行します。
yarn add --dev [package名]@[バージョン]
パッケージのバージョンは以下のコマンドで確認できます。
npm info [package name>] versions
プロジェクトに必要なパッケージのインストール(npmのnpm install --save
と互換)は以下のコマンドを実行します。
yarn add [package名]
互換性のないパッケージがあるとエラーがでます。
error csswring@5.1.1: The engine "node" is incompatible with this module. Expected version ">=5.0.0".
error Found incompatible module
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
パッケージを変更して、再度yarn
を実行するとyarn.lockファイルの作成ができました。
[4/4] 📃 Building fresh packages...
success Saved lockfile.
✨ Done in 159.99s.
Error: ENOENT: no such file or directory,
のように特定のパッケージが読み込めない場合は、パッケージのアップグレードで直ることもあります。
yarn upgrade [パッケージ名]
yarn.lockのパッケージをインストールするには以下のコマンドを実行します。npm install
と同じ要領です。
yarn install
package.jsonのscriptを実行する場合は、以下のコマンドを実行します。npm start
と同じ要領です。
yarn start
npm run [スクリプト名]
のように実行したい場合は以下のコマンドを実行します。
yarn run [スクリプト名]
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: The /usr/local is not writable.
You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami) /usr/local
Warning: The /usr/local directory is not writable.
Even if this directory was writable when you installed Homebrew, other
software may change permissions on this directory. Some versions of the
"InstantOn" component of Airfoil are known to do this.
You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami):admin /usr/local
Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected dylibs:
/usr/local/lib/libqmi_api.dylib
Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected header files:
/usr/local/include/node/ares.h
/usr/local/include/node/ares_version.h
/usr/local/include/node/nameser.h
/usr/local/include/node/node.h
/usr/local/include/node/node_buffer.h
/usr/local/include/node/node_internals.h
/usr/local/include/node/node_object_wrap.h
/usr/local/include/node/node_version.h
/usr/local/include/node/openssl/opensslconf.h
/usr/local/include/node/pthread-fixes.h
/usr/local/include/node/stdint-msvc2008.h
/usr/local/include/node/tree.h
/usr/local/include/node/uv-bsd.h
/usr/local/include/node/uv-darwin.h
/usr/local/include/node/uv-errno.h
/usr/local/include/node/uv-linux.h
/usr/local/include/node/uv-sunos.h
/usr/local/include/node/uv-unix.h
/usr/local/include/node/uv-win.h
/usr/local/include/node/uv.h
/usr/local/include/node/v8-debug.h
/usr/local/include/node/v8-defaults.h
/usr/local/include/node/v8-profiler.h
/usr/local/include/node/v8-testing.h
/usr/local/include/node/v8.h
/usr/local/include/node/v8config.h
/usr/local/include/node/v8stdint.h
/usr/local/include/node/zconf.h
/usr/local/include/node/zlib.h
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
git
node
Warning: Your Xcode (7.0) is outdated
Please update to Xcode 7.1.
Xcode can be updated from the App Store.
Warning: Your Homebrew is outdated.
You haven't updated for at least 24 hours. This is a long time in brewland!
To update Homebrew, run `brew update`.
「24時間以上アップデートしていないのでbrew update
でアップデートしてね」とある。
Warning: Your Homebrew is outdated.
You haven't updated for at least 24 hours. This is a long time in brewland!
To update Homebrew, run `brew update`.
brew update
をすると/usr/local
ディレクトリが書き込みできない、chown
コマンドでパーミションを変更してねと出る。
Warning: The /usr/local is not writable.
You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami) /usr/local
chown
コマンドは所有者とグループを変更するコマンド。所有者がroot
の場合はsudo
が必要。
`sudo chown -R 所有者名:グループ名 ファイル名やディレクトリ名`
書いてある通りsudo chown -R $(whoami):admin /usr/local
を実行します。
パスワードを入力して、brew update
をする。
アップデートに成功すると、所有権を元に戻しても大丈夫だと表示されます。
Homebrew no longer needs to have ownership of /usr/local. If you wish you can
return /usr/local to its default ownership with:
sudo chown root:wheel /usr/local
Error: The /usr/local directory is not writable.
Even if this directory was writable when you installed Homebrew, other
software may change permissions on this directory. Some versions of the
"InstantOn" component of Airfoil are known to do this.
You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami):admin /usr/local
パスワードを入力して、brew update
をする。
Updated Homebrew from b453f0ba to 735f5e44.
==> New Formulae
acmetool libmwaw
agedu libphonenumber
amdatu-bootstrap libproxy
ansible-cmdb mat
apcupsd mediaconch
argon2 metabase
artifactory-cli-go moz-git-tools
libvirt
==> Renamed Formulae
hamsterdb -> upscaledb offline-imap -> offlineimap
kotlin-compiler -> kotlin
==> Deleted Formulae
ffts ipe perlmagick
git-encrypt kbtin rbenv-gem-rehash
gptfdisk net6
groonga-normalizer-mysql p11-kit
ここを見てnode_modules
をいったん削除してから、nodeをインストールし直すといいと書いてある。
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node
2つ目のコマンド、
brew uninstall node
Uninstalling /usr/local/Cellar/node/5.1.0... (2,827 files, 28.6M)
インストール完了。
Please note by default only English locale support is provided. If you need
full locale support you should:
`brew reinstall node --with-full-icu`
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/node/5.5.0: 3,135 files, 35.5M
ビールの絵文字がでたらOK。
壊れたシンボリックリンクがあるということなので、
Warning: Broken symlinks were found. Remove them with `brew prune`:
/usr/local/bin/stree
コマンドを実行。
brew prune