13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

macの環境構築

Last updated at Posted at 2019-08-29

この記事について

mac book pro(2019)(OSはmac OS Mojave 10.14.6)の環境構築ガイドです.
理系大学生(物理系?)向けです.

この夏, 7年間お世話になったmac book proからmac book pro(2019)に買い換え, 一から環境構築することになったので, そのメモです.

  • Python3
  • VTK
  • OpenCV
  • ROOT(cernの解析ソフト)

が使える環境を目指します.

(2020.02.11追記)
この記事では brew とbuildしたものが被らないようにするために, buildしたものを brew にむりやり認識させようとしますが, あんまりうまく行ってる気がしないので, 気にしない方が得策な気がします.
brew に認識させようと骨を折る部分は適宜無視してください.

Xcodeのインストール

まずはXcode.
AppStoreからXcodeをインストール(Xcode自体はあんまり使わないけど, 一応インストール).
Terminalでxcode-select --installでcommand line toolsをインストール.

Homebrewのインストール

Homebrewの公式に従って, Terminalで

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

を実行.

homebrew-caskもこれで使えるようになる.

Homebrew, Homebrew Cask で必要なアプリケーションをインストール

Homebrewで以下をインストール.

  • cmake

Homebrew Caskで以下をインストール

  • Google Chrome
  • Slack
  • atom
  • xquartz

Gitのインストール, 設定

brew install git を実行.
gitの設定

$ git --global user.name "your name"
$ git --global user.email "your_email@example.com"
$ git config --global color.ui auto

ssh keyの生成.

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f "$HOME/.ssh/github_rsa"
$ eval "$(ssh-agent -s)"

$HOME/.ssh/configを作成し, 以下の内容を書き込む.

Host github github.com
	AddKeysToAgent yes
	UseKeychain yes
	HostName github.com
	IdentityFile ~/.ssh/github_rsa
	User git

ssh-addを実行.

$ ssh-add -K ~/.ssh/github_rsa

githubにssh keyを追加.

$ pbcopy < ~/.ssh/github_rsa.pub

クリップボードにコピーした公開鍵の内容をgithubのSSH keysにペーストする.

$ ssh -T git@github.com

として

Hi your_github_account_name! You've successfully authenticated, but GitHub does not provide shell access.

と表示されればOK.

C/C++開発環境

学生の特権を使ってiccを入れる.
ここからC++ macOSを選んで, インストーラをダウンロードして, インストーラの指示に従って, インストール.
mkl, tbbも一緒にインストールされる.

intelpython3も一緒にインストールしたけど, これ使わない気がする...

mkl, tbbの環境変数を設定するために.bash_profile

source /opt/intel/mkl/bin/mklvars.sh
source /opt/intel/tbb/bin/tbbvars.sh

を追加(これをしないとnumpy, scipyが使えなかったりする).

iccだとbuildできないものもあるので, gccも入れる.

$ brew install gcc

Fortran開発環境

iccと同じようにifortranを入れる.
ここからFortran macOSを選んで, インストーラをダウンロードして, インストーラの指示に従って, インストール.

gfotran$ brew install gccで一緒にインストールされてる.

Python開発環境

今まではAnacondaを利用していたけど, 他の環境を壊しちゃったり, Anacondaとbrewで重複して入っちゃったパッケージがあったりとか, ROOTがAnaconda Pythonでは使えなかったりとか... 大変なので使うのやめます.

普通にPythonとpipで環境構築します.

Python3のインストール

まずはPython3のインストール(イマドキPython2は使いませんよ).

$ brew install python

pipも一緒に入るので大丈夫.

numpy, scipyのインストール

普通にpipnumpy, scipyをインストールすると, openblasが使われるので遅い.
なので, mklとリンクさせてbuildする.

$HOMEに以下の内容の.numpy-site.cfgを作る.

[mkl]
library_dirs = /opt/intel/mkl/lib
include_dirs = /opt/intel/mkl/include
mkl_libs = mkl_rt
lapack_libs =

また, $HOMEに次の内容で.config/pip/pip.confを作る.

[install]
no-binary = numpy,scipy

[wheel]
no-binary = numpy,scipy

これでコンパイル済みのバイナリをインストールすることなく, buildされるようになる.
あとは普通にnumpy,scipyをインストールする.

pip install numpy
pip install scipy

Jupyterのインストール, 設定

jupyter labのインストール

$ pip install jupyterlab

だいたいこれでいける.
あとは細かな設定.

$ jupyter notebook --generate-config

を実行して, ~/.jupyter/jupyter_notebook_config.pyを作成.
これをちょっと編集する.
safariとjupyterの相性がよろしくないので,

c.NotebookApp.browser = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome %s'

としてGoogle Chromeで開くようにする.
ちなみに--new-windowとつけると, 新しくウインドウで開く.

jupyter labnodeを使うので,

brew install node

を実行.

jupyter labRを使いたい場合は, Rを起動して, Rのインタプリンタで以下を実行.

> install.packages('IRkernel')
> IRkernel::installspec()

その他のインストール

  • pandas
  • matplotlib
  • plotly
  • Pillow
  • Cython
  • PyQt5

をインストール.

plotlyjupyter labで使えるようにするためには, 次をインストールする必要がある.

jupyter labextension install @jupyterlab/plotly-extension

ROOTのインストール(build)

$HOME/ROOTディレクトリを作る.
$HOME/ROOT下のsrcROOTのソースをおく.

$ git clone https://github.com/root-project/root.git src

$HOME/ROOTでcmakeを実行.
このとき, PYTHON_EXECUTABLEpython3を指定する.

$ cmake src -D PYTHON_EXECUTABLE=/usr/local/bin/python3

必要に応じてccmake srcを実行してからmake.

$ sudo make -j8

このとき, コンパイラの関係で失敗するときは, 以下の内容でclang.cmakeをつくり, cmake -C clang.cmake -D PYTHON_EXECUTABLE=/usr/local/bin/python3 srcを実行.

set(CMAKE_C_COMPILER "/usr/bin/clang" CACHE string "clang compiler" FORCE)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++" CACHE string "clang++ compiler" FORCE)

make installをする必要はないかな.
.bash_profileに次を追加.

source "$HOME/root/bin/thisroot.sh"

これでROOTが使えるようになる.

jupyterでROOTを使いたい場合は,

$ cp -r $ROOTSYS/etc/notebook/kernels/root /Users/$USER/Library/Jupyter/kernels/

を実行する.

ROOTとbrewとの共存(2019.12.18追記)

brew doctor

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and which additional flags to use when
compiling and linking.

Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew-provided
script of the same name. We found the following "config" scripts:
  /Users/foobar/root/bin/afterimage-config
  /Users/foobar/root/bin/root-config

とWarningが出るのが気になる人(i.e. 僕)は .bash_profile に次を追加することで brew doctor を黙らせることができます(小手先の技術...).

alias brew="env PATH=${PATH/${HOME}\/root\/bin:/} brew"

Qtのインストール

qtの公式からOpen Source版をダウンロードしてきて, あとはインストーラの指示に従ってインストール.
フルでインストールすると, 20GBくらいになるので, 使いそうにないiOS(これだけで9GB...)やAndroid用のものはインストールしない.

後のOpenCVとかVTKとかのbuildでcmakeがQtを認識できるように

export CMAKE_PREFIX_PATH=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake

.bash_profileに追加する.

OpenCVのインストール(build)

opencvとopencv_contribのソースをgitで落としてくる.

$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

そして, cmakeでconfigureしてmake, make installのいつもの流れ.

$ cd opencv
$ mkdir build
$ cd build
$ cmake `cd ..; brew diy --version=4.1.1` \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
    -D BUILD_EXAMPLES=ON \
    -D WITH_QT=ON \
    -D Qt5Core_DIR=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake/Qt5Core/ \
    -D Qt5Gui_DIR=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake/Qt5Gui/ \
    -D Qt5Test_DIR=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake/Qt5Test/ \
    -D Qt5Widgets_DIR=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake/Qt5Widgets/ \
    -D Qt5Concurrent_DIR=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake/Qt5Concurrent/ \
    -D Qt5OpenGL_DIR=/Users/foobar/Qt/5.12.4/clang_64/lib/cmake/Qt5OpenGL/ \
    -D WITH_OPENGL=ON \
    -D Qt5_DIR=/Users/foobar/Qt/5.12.4/clang_64/ ..

このあと$ ccmake ..BUILD_EXAMPLESをONに, MKLTBB, Pythonの設定がうまくできてるか確認してから,

$ make -j8
$ make install

最後にbrew link opencv.

VTKのインストール(build)

必要かもしれないhdf5, netcdfをインストール.

$ brew install hdf5
$ brew install netcdf

$HOMEにvtkのディレクトリを作り, sourceをgitで落としてくる.

$ mkdir vtk
$ git clone https://github.com/Kitware/VTK.git src

git checkout refs/tags/v8.2.0とした方が良いのかもしれないけど, こうすると, bugがある(Pythonでimport vtkをすると失敗する.詳しくはここ)のでmasterのままでbuildする.

そしてcmake.

$ cmake -D CMAKE_INSTALL_PREFIX=/usr/local/Cellar/vtk/8.2.0 \
    -D PYTHON_EXECUTABLE=/usr/local/bin/python3 \
    -D VTK_WRAP_PYTHON:BOOL=ON \
    src

ccmakeでもうちょっとConfigure

$ ccmake src

build examplesVTK_Group_QtをONにしてcでConfigure.

$ make -j8
$ make install

最後にbrew link.

$ brew link vtk

ParaViewのインストール

5.7.0がRC版なので, 5.6.2を公式からインストール.
homebrew caskのやつはGUIがイマイチ(見た目が古かった)だったのでやめました.

buildしないのかって?
結構大変なんすよ...

brewとbuildしたOpenCV, VTKの共存(2019.12.18追記)

brew diy , brew linkOpenCVVTKbrew に認識させてますが, brew upgrade により, せっかくbuildしたものが台無しになる可能性があります.
brew upgrade の代わりに

$ brew outdated | awk '{print $1}' | grep -v -e opencv -e vtk | xargs brew upgrade

とすることで, OpenCVVTK を除いたパッケージだけをupgradeできます.
(...めんどくさい...もっといい方法ないかな)

brew doctor を走らせたときに

$ brew doctor
Warning: Some installed formulae are missing dependencies.
You should `brew install` the missing dependencies:
  brew install ceres-solver gflags glog ilmbase metis numpy openexr pyqt sip suite-sparse tbb

とか, brew missing を走らせたときに

$ brew missing
opencv: gflags glog metis suite-sparse ceres-solver numpy ilmbase openexr tbb
vtk: sip pyqt

とか出ますが, これが気になる人(i.e. 僕)で, 二重で入れたくない(例: numpyとかをpipとbrewの二重で入れたくない, tbbをiccとbrewの二重で入れたくない)人(i.e. 僕)は

$ mkdir /usr/local/Celler/numpy
$ mkdir /usr/local/Celler/numpy/1.17.4
$ brew link numpy

って感じで, /usr/local/Celler/<Package名>/<Version> という空ディレクトリを作って brew link <Package名> を実行すること, つまりbrewに勘違いさせること(小手先の技術...)で, とりあえず brew doctor を黙らせることができます.

TeX環境の構築

basictexとghostscriptをインストール.

$ brew cask install basictex
$ brew install ghostscript

アップデートしてから日本語を使えるようにする.

$ sudo tlmgr update --self --all
$ sudo tlmgr paper a4
$ sudo tlmgr install collection-langjapanese

ヒラギノを使えるようにする.

$ sudo tlmgr repository add http://contrib.texlive.info/current tlcontrib
$ sudo tlmgr pinning add tlcontrib '*'
$ sudo tlmgr install japanese-otf-nonfree japanese-otf-uptex-nonfree ptex-fontmaps-macos cjk-gs-integrate-macos
$ sudo tlmgr install cjk-gs-integrate adobemapping
$ sudo cjk-gs-integrate --link-texmf --cleanup
$ sudo cjk-gs-integrate-macos --link-texmf
$ sudo mktexlsr
sudo kanji-config-updmap-sys --jis2004 hiragino-highsierra-pron

TeXshop, latexitをインストール.

$ brew cask install texshop
$ brew cask install latexit

texshopの設定を開いてtexwikiを参考にして設定する.

ユーティリティー

Terminal起動のショートカット設定

  1. Automater → クイックアクション.
  2. アプリケーションを起動, Terminalを起動を選択. ワークフローが受け取る項目を入力なしに設定.
  3. ComeOnTerminal の名前で保存.
  4. システム環境設定 → キーボード → ショートカット → サービス → 一般 → ComeOnTerminal → ショートカットキー(ctrl + option + t)を追加.

Automatorスクリプトは~/Library/Services/ComeOnTerminal.workflowにある.

アプリケーションを起動中はそのアプリケーションのショートカットが優先されるため, Terminalが起動しないことがある.

2019.10.15 追記
この方法だとすでにTerminalが開かれていた場合にショートカットが効かない.
これが気にいらない場合には, 2. において「アプリケーションを起動」ではなく, 「シェルスクリプトを実行」を選択し, open -a Terminal $HOME と書けばよい.
そのあとは同じ.

Touch BarからSiriを消す

Touch BarにSiriがいるのなんか嫌.
間違って押しちゃいそうになるし...

  1. システム環境設定 → キーボード → Touch Barをカスタマイズ.
  2. Siriを消す.

フォントRictyのインストール

Ricty Diminishedの公式からRicty Diminishedをダウンロードしてインストール.
Rictyは前のMacBookProで生成したものを移植(サボりました...).

参考

13
15
0

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
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?