LoginSignup
5
7

More than 3 years have passed since last update.

【2019年秋】Macで開発環境を構築する(初心者向け)

Last updated at Posted at 2019-09-28

win使いの非エンジニアがmacでRails開発をすることになり、開発環境構築であたふたした話。
聞き慣れないコトバの雨に、言われるがままのコマンド入力、、2回目の構築は一人でできる気がしない。。
きちんと消化し、次回は自力でッターン!するための備忘録。
※玄人の方、間違ったところがあれば教えてくださいm(_ _)m 何卒!

環境

macOS Mojave 10.14.6

今回インストールするもの一覧

以下、インストールしたもの一覧です。
私のような右も左も分からない人は優しいパイセンに自分の環境と合っているか・足りないもの・余分なものを聞いてからしましょう。私は無知さ故の自業自得でエライ目にあいました、、

  • iTerm2
  • homebrew
  • zsh
    • oh-my-zsh
    • シンタックスハイライト
    • コマンド補完
    • (omake)Starship
  • Git(Github) ※トラブル記録アリ
  • VSCode

黒い画面は【iTerm2】

macには標準で「ターミナル」が用意されていますが、周りのエンジニアが「iTerm2入れなよ!」ってことでインストール。

iTerm2(https://www.iterm2.com

パッケージ管理ツール【homebrew】

homebrew(https://brew.sh/index_ja)とは、いろんなアプリを管理してくれるツールです。必須!
インストールコマンドをiTermに貼り付け実行。

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

※「Xcodeがない、または古い」と怒られるかもしれません。XcodeはiOSアプリの統合開発環境です。インストールしましょう。
Xcode(https://geo.itunes.apple.com/jp/app/xcode/id497799835?mt=12

インストールが進むと、「Return押して」と表示されますので、Return!
途中、パスワード入力を求められます。
「Macにログインするときのパスワード」を入力します。
(画面上は何も表示されず変化がありませんが、入力しています。焦らず入力します。私は焦って間違えました。)

Successが表示されたらインストール完了です。
下記のコマンドで確認します。

$ brew --version
Homebrew 2.1.11
Homebrew/homebrew-core (git revision 682a; last commit 2019-09-03)

シェル【zsh】

シェルはzshを入れました。
macにはBashがデフォルトシェルとして入っているし、記事なども多いので安心感。
なのですが、Bashに機能追加されたものがzsh(てきとう)と聞き、近くのエンジニアも「今どきzshっしょ!」ってことだったのでこちらにしました。
macOSの次バージョン「Catalina」ではzshがデフォルトシェルになるとの話も。
もうzshにしよう!(一瞬しかBashを使っていないので違いがわかりません。。)

$ brew install zsh

インストールが完了したら、バージョンを確認します。

$ zsh --version
zsh 5.7.1 (x86_64-apple-darwin18.2.0)

はいった!

oh-my-zsh

zsh 用の設定フレームワーク。それ以上の知識なし。。

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

シンタックスハイライト

コマンドにいい感じの色をつけてくれるプラグイン

$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

設定は ~/.zshrc に書いていきます。

$ vim ~/.zshrc

plugins=(git) の箇所に zsh-syntax-highlighting を追記します。

plugins=(git zsh-syntax-highlighting)

保存して終了します

コマンド補完

$ git clone https://github.com/zsh-users/zsh-completions ~/.oh-my-zsh/custom/plugins/zsh-completions

zsh.rcの設定を変更

$ vim ~/.zshrc

先ほどのplugins=(git)の箇所にzsh-completionsを追記し、リロードする設定を記載します。

plugins=(git zsh-syntax-highlighting zsh-completions)
autoload -U compinit && compinit -u

保存して、.zshrcを読み直します。(シンタックスハイライトも併せてここで適用されるはず)

$ source ~/.zshrc

これでコマンドが色分けされ、Tabキーで補完候補を表示してくれるようになりました。
黒い画面がだいぶいい感じになりました。

(omake)Starship ※入れなくても支障ありません

https://starship.rs/
画面が可愛くなるやーつ(でも起動が若干遅くなりました..)
インストールの仕方は↑を参照ください。
Screenshot at Sep 27 22-45-58.png
カワ(・∀・)イイ!!

Git

ソースコード管理をインストールします。
インストールするとコマンドでブランチを切ったりコミットできるようになります。(覚えたてなのでいちいちビクビクしていますが)
GitはGUIでもできます。GitHub Desktop(https://desktop.github.com/)や、Sourcetree(https://ja.atlassian.com/software/sourcetree)が有名なのでしょうか?
私はdiff(前回とのファイルの差異を見たい)するときはGUIのほうが見やすいのでGithub Desktopを入れて両方使っています。画像の違いも分かりやすいので便利です!
(会社ではGithubでソース管理しています)

$ brew install git

(gitインストールの前にHomebrewの自動アップデートが走りました。こんな事もあるよ、と次回ビビらないように残す。)

$ brew install git
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
glib ✔ fibjslibbitcoin-serverprotobuf
ackflowgrindlibphonenumber protobuf@3.7
aliyun-cli fluxctllibpulsarpwntools
ansible@1.9gdal librsvgpygobject3
ansible@2.0gkrellmlogtalkpython@2
appscale-tools glooctllolcat qpdf
aravis gnupg-pkcs11-scd molecule qsoas
astrometry-net gnuradio monetdbre2
batgrafanamono root
bcftools gslncosiril
cadavergst-plugins-good neofetch sourcery
charm-toolshyperfineneon sqlmap
checkstyle jenkinsnetlify-clissh-audit
csound jfrog-cli-go nsqsylpheed
dcos-cli libbitcoin ocrmypdf syncthing
dieharderlibbitcoin-blockchainooniprobetunnel
django-completionlibbitcoin-clientopencv varnish
dnsviz libbitcoin-consensus opencv@3 vgmstream
duplicitylibbitcoin-databaseopenimageiovim
dylibbundler libbitcoin-exploreropenvpnvisp
dynare libbitcoin-network oscwtf
ethereum libbitcoin-nodeoscats
exploitdblibbitcoin-protocolpkcs11-helper
==> Deleted Formulae
mongodb mongodb@3.2 mongodb@3.6
mongodb@3.0 mongodb@3.4 percona-server-mongodb

==> Installing dependencies for git: pcre2
==> Installing git dependency: pcre2
==> Downloading https://homebrew.bintray.com/bottles/pcre2-10.33.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/fb/fb30c3eebba4483a10378bc8df52f96761fed4a1ac5572ad1
######################################################################## 100.0%
==> Pouring pcre2-10.33.mojave.bottle.tar.gz
🍺/usr/local/Cellar/pcre2/10.33: 226 files, 5.7MB==> Installing git
==> Downloading https://homebrew.bintray.com/bottles/git-2.23.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/c3/c32304092f99c0c296fe210bcaf2a16a50ba4d670faa170fc
######################################################################## 100.0%
==> Pouring git-2.23.0.mojave.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
/usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/git
==> Summary
🍺/usr/local/Cellar/git/2.23.0: 1,543 files, 44.9MB
==> Caveats
==> git
Bash completion has been installed to:
/usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
/usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/git

しばらく待ったらちゃんと終わったみたい。。
バージョンを確認

$ git --version
git version 2.20.1 (Apple Git-117)

インストール完了です。

コマンドでGit Cloneしてみる (ここからトラブル、、)

$ git clone git@github.com:xxxxxxxx/taskleaf.git

勉強中のリポジトリをCloneしてみる
まず公開鍵と秘密鍵(RSA2048)を作って、SSH接続の準備

$ cd ~/.ssh
$ ssh-keygen -t rsa -b 2048 ←※※下に追記あり!!
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxxxxxxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

(追記)

私はRSA2048で秘密鍵を作っていますが、ED25519がおすすめとのアドバイスをいただきました。強力なのだそう。これからはED25519!

$ cd ~/.ssh
$ ssh-keygen -t ed25519

Githubに公開鍵を設置する(https://github.com/settings/keys
SSHが成功しているか確認する

$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address 'xxx.xxx.xxx.xxx' to the list of known hosts.
git@github.com: Permission denied (publickey).

成功しなかった(´;ω;`)ブワッ

$ ssh-keygen -R xxx.xxx.xxx.xxx
# Host xxx.xxx.xxx.xxx found: line 2
/Users/xxxxxxxx/.ssh/known_hosts updated.
Original contents retained as /Users/xxxxxxxx/.ssh/known_hosts.old

もろもろ調べたところ、config ファイルにgithubの情報を書き込むといいらしい

$ vi ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa #自分が作ったファイル名

再度トライ。

$ ssh -T git@github.com
Hi xxxxxxxx! You've successfully authenticated, but GitHub does not provide shell access.

成功ー!
再度Cloneしてみる

$ git clone git@github.com:xxxxxxxx/taskleaf.git
Cloning into 'taskleaf'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

ぎゃふん
Cloneしようとしたリポジトリは個人のプライベートリポジトリです。
Githubのユーザーは、会社ドメインのメールアドレスで登録しました。また、二段階認証を設定しています。
Gitの認証は会社のメールアドレスで認証していました。プライベートのままなのに、会社メールで認証してたの
がいけないっぽい?

Screenshot_at_Sep_27_23-51-06.png

Githubで用意されたパブリックなメールアドレスをユーザーに設定しました。(https://github.com/settings/emails)

$ git config --global user.email [数字]+[ユーザー名]@users.noreply.github.com

このあと無事Cloneできました。
やっとGit終了、、長い戦いだった、、

VSCode(Visual Studio Code)

やっとコードエディタです。
VSCodeはマイクロソフトが作っているエディタです。(https://code.visualstudio.com/
便利な拡張機能が色々あり、日本語にも対応(拡張機能)しているので安心感。しかも無料。ありがてぇ
※まわりの人に「エディタって何使ってます?」と聞いた日には終わらない戦いが始まってしまいますので、覚悟して尋ねましょう。

拡張機能はとりあえず以下をいれました。

  • Japanese Language Pack for Visual Studio Code
    • メニューなどを日本語にしてくれる
  • TabNine
    • 補完能力がすごい(メモリ1.6GBほど使います、、)
  • Prettier
    • コードの自動フォーマット(まだ使いこなせていない、、)
  • Bookmarks
    • コードに目印を付けれる。地味に便利

あとは、Ruby、Slim、markdownlintなど、開発言語に合わせて入れました。

やっと

これで一応スタート地点には立てたはず。。
環境作るだけで疲れましたが、俺たちの戦いはこれからだ!

※玄人の方、間違ったところがあれば教えてくださいm(_ _)m 何卒!

5
7
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
5
7