Jack's Ship

The ship will go to every where

Introduction of Development Tools of MacOS

Until now I have used the mac os for 3 years, but I never learn about the relative development things of Mac os. In this articles, I will take introduction about some tools for the developer on Mac. The main contents are from the macOS setup Guide from Sourabh. It is very suitable for the rookie like me.

Table of Contents

  1. XCode
  2. Homebrew
  3. iTerm2
  4. Zsh
  5. Git
  6. Atom
  7. Python
  8. CPlusPlus
  9. Java
  10. Scala
  11. Ruby
  12. Node.js
  13. Go
  14. Latex
  15. Others

XCode

Xcode is an development environment of Apple os system, like Visual Stdio of Microsoft. The whole thing could be downloaded and installed from App Store or offical site of Apple. And Xcode command line tools could be installed by command:

1
$ xcode-select --install

It is a package of shell command tools for Mac OS development, many things is dependent on it.


Homebrew

Homebrew is a package manager for macOS which help developer install and uninstall many important development tools.

To install Homebrew by running command:

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

Usage

To install a package:

1
$ brew install <formula>

To update packages:

1
$ brew update

To see the package list need to be updated

1
$ brew outdated

To update a package:

1
$ brew upgrade <formular>

To remove packages of old version:

1
$ brew cleanup

To see the installed package list with version number:

1
$ brew list --versions

ITerm2

iTerm2 is a great replacement for Apple’s Terminal. It is highly customizable and includes many useful features.

There are 2 ways to Install iTerm2. First, The official website offer the installer. And we also could install it by Homebrew.

1
$ brew cask install iterm2

Customization

We can define color, font, size, hotkeys for the iTerm2, and also there are many 3rd party resources from internet.


Zsh

The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a powerful command interpreter for shell scripting(Wiki). There are a lot of articles to compare Zsh with Bash (Apple’s original shell). e.g. Bash vs Zsh: A comparison of two command line shells by Nicholas Morera.

To install Zsh and zsh-completions by Homebrew:

1
$ brew install zsh zsh-completions

Then, we can customize the zsh by the framework Oh My Zsh, to install it by curl command.

1
$ curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

If you’re still in the default shell, change default shell to zsh manually:

1
$ chsh -s /usr/local/bin/zsh

Configure plugins and theme of zsh by editing ~/.zshrc file. (refer the wiki)

1
2
3
4
5
6
7
8
9
10
ZSH_THEME="robbyrussell"
plugins=(
  git
  bundler
  dotenv
  osx
  rake
  rbenv
  ruby
)

Finally, we can create a setting file such as env.sh to make some pre-defined settings, And add it to .zshrc with source <envpath>/env.sh. One config template (Ref) is like:

#!/bin/zsh

# PATH
export PATH="/usr/local/share/python:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export EDITOR='subl -w'
# export PYTHONPATH=$PYTHONPATH
# export MANPATH="/usr/local/man:$MANPATH"

# Virtual Environment
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/projects
source /usr/local/bin/virtualenvwrapper.sh

# Owner
export USER_NAME="YOUR NAME"
eval "$(rbenv init -)"

# FileSearch
function f() { find . -iname "*$1*" ${@:2} }
function r() { grep "$1" ${@:2} -R . }

#mkdir and cd
function mkcd() { mkdir -p "$@" && cd "$_"; }

#Aliases
alias cppcompile='c++ -std=c++11 -stdlib=libc++'

# Use sublimetext for editing config files
alias zshconfig="subl ~/.zshrc"
alias envconfig="subl ~/projects/config/env.sh"

Git

Git is most important version management tools for any developer. It is hard to image anyone do works without it.

To install it by Homebrew:

1
$ brew install git

Then define the user of Github:

1
2
$ git config --global user.name "Your Name Here"
$ git config --global user.email "your_email@youremail.com"

And config the SSH connection is nessceary for us, following the steps from here to generate the ssh key and upload it to Github.

For the ignore some file out of mangement by git, we shall create the file ~/.gitignore to setting the files (e.g. .DS_Store) not be tracked. One template could be found here.

For configring the diff and merge tool (e.g. Beyond Compare) by commands:

1
2
3
4
5
git config --global diff.tool bc3
git config --global difftool.bc3.trustExitCode true

git config --global merge.tool bc3
git config --global mergetool.bc3.trustExitCode true

ATOM

There are huge amounts of editors or IDE in MACOS, e.g. VIM, Sublime, JetBrains and so on. In this article, I will introduction ATOM Editor. We can download the install fom macOS, linux and windows from its official site.

It is a highly customizable editor based on Electron, a framework that enables cross-platform desktop applications using Chromium and Node.js(ref)). A lot of resources about themes and plugings could be used to support what language you develop.

And we can create a symlink of atom if it exists for using it from command line directly. Then we can use atom file and atom folder to open relatived file and folder.

1
ln -s /Applications/Atom.app/Contents/Resources/app/atom.sh /usr/local/bin/atom

Python

From this part, we will introduce some program languages setting-up on MacOS including python, c++, Java, Scala, Ruby, Nodejs, Go and Latex.

macOS already includes python, but we do not mess with the system Python, so we need install our own versions. The first way is that you can install Python by Homebrew.

1
$ brew install python

Another way is to use pyenv which could help you to manage multiple versions of Python (e.g. 2.x and 3.x). It works like rbenv for Ruby. First, we must install pyenv by Homebrew.

1
$ brew install pyenv

After installing, we should add ‘pyenv init’ to the shell to enable shims and autocompletion.

1
$ echo 'eval "$(pyenv init -)"' >> <env folder>/env.sh

Restart shell to reload the settings, then we can start to use pyenv.

1
$ exec $SHELL

The important commands are listed following with comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ pyenv install --list      ##list all avaliable versions of Python

$ pyenv install 2.7.12      ##install version 2.7.12
$ pyenv install 3.5.2       ##install version 3.5.2
$ pyenv rehash              ##Rehash pyenv shims (after install)

$ pyenv global 2.7.12 3.5.2 ##set version oder, 2.7.12 before 3.5.2

$ pyenv versions            ##see the versions of python installed
  system (set by /Users/your_account/.pyenv/version)
* 2.7.12
* 3.5.2

$ cd path/to/directory
$ pyenv local 3.5.2       #set the python version to specific folder

Tools

pip is important for developer to install and manage python packages:

1
2
3
4
$ pip install <package>   #install package
$ pip install --upgrade <package>   #update package
$ pip freeze    #list installed packages
$ pip uninstall <package>   #uninstall package

Virtualenv is a tool which create an isolated Python enviroment for each of your projects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ pip install virtualenv  #install

#step 1: setup virtualenv to the project
$ cd myproject/
$ virtualenv venv

#step 2: inherit global installed packages
$ virtualenv venv --system-site-packages

#step 3: activate
$ source venv/bin/activate

#step 4: see venv appear at the beginning of your terminal,then we can install the package
$ pip install <package> #package is installed in venv folder

#step 5: leave the vitual environment
$ deactivate

Numpy, Scipy and Matplotlib are scientific liberary for Python. They could installed through pip or Homebrew, see here.


CPlusPlus

XCode contains c++, and we could also install c++ by Homebrew.

1
$ brew insall gcc

And we can add alias to shell env for compiling files from terminal.

1
alias cppcompile='c++ -std=c++11 -stdlib=libc++'

Then we could run cpp file directly using cppcompile main.cpp.


Java

Go to Oracle website to download the macOS version of Java and install. Then we could run java -version to check whether it is installed correctly.

And after installing, we should add Java path to env file.

1
export JAVA_HOME="`/usr/libexec/java_home -v 1.8`"

Finally, choose an IDE to start your develop, e.g. Eclipse.


Ruby

Ruby is similar as Python in MacOS. We could install it directly by Homebrew. And we also could use rbenv to install and manage multiple versions of Ruby like pyenv.

1
2
3
4
5
6
7
8
9
$ brew install rbenv ruby-build rbenv-default-gems rbenv-gemset #install rbenv
$ echo 'eval "$(rbenv init -)"' >> <path>/env.sh #init rbenv
$ source ~/.zshrc             # Apply changes

$ rbenv install 2.1.1 #install
$ rbenv global 2.1.1  #switch version

$ cd PROJECT_PATH
$ rbenv local 2.1.1 #define version for specific project.

RubyGems is Ruby package management tool, it should be installed with Ruby. Check by which gem. Usage is shown as below.

1
2
3
4
5
6
7
8
9
10
11
12
$ gem update --system   #update to latest version

# install gem package
$ gem install <gemname>
$ gem install <gemname> --no-document

# check installed packages list and outdated.
$ gem list
$ gem outdated

$ gem update [<gemname>]  #update gems
$ gem cleanup #cleanup old version

Node.js

Nodejs is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side(wiki). Like Python and Ruby, it also has an version management tool - nvm. We can use it to install and manage versions of Nodejs. Usage shows below.

1
2
3
4
5
6
7
8
9
10
# install nvm
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash

$ source ~/.bashrc        # source your bashrc/zshrc to add nvm to PATH
$ command -v nvm          # check the nvm use message
$ nvm install node        # install most recent Node stable version
$ nvm ls                  # list installed Node version
$ nvm use node            # use stable as current version
$ nvm ls-remote           # list all the Node versions you can install
$ nvm alias default node  # set the installed stable version as the default Node

And nodejs also has its package management tool npm, The usage is like pip for Python and gem for Ruby.

1
2
3
4
5
6
$ npm install <package> # Install locally
$ npm install -g <package> # Install globally
$ npm install <package> --save
$ npm list [-g]
$ npm update [-g] [<package>]
$ npm uninstall [-g] <package>

Go

Go—‘golang’ is a programming language created by Google.

We could install it by Homebrew.

1
$ brew install golang

Then config the enviroment by inserting path to env.sh

1
2
3
4
export GOPATH=$HOME/go
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

How to write a simple “Hello World” program, please refer here.

To import and use package, we will use command go get.

1
$ go get -u github.com/gorilla/mux

Package using example:

package main

import (
    "net/http"
    "log"
    "github.com/gorilla/mux" // Your imported package
)

func YourHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Gorilla!\n"))
}

func main() {
    r := mux.NewRouter()
    // Routes consist of a path and a handler function.
    r.HandleFunc("/", YourHandler)

    // Bind to a port and pass our router in
    log.Fatal(http.ListenAndServe(":8000", r))
}

Latex

LaTex, which is pronounced “Lah-tech” or “Lay-tech”, is a document preparation system for high-quality typesetting. It is not a word processors like Microsoft Word or Apple Pages. It is like a markup language to using kinds of tags or commands to generate a structure of documents.

To install LaTex on MacOS, we could download MacTex installer. It includes additional programs such as an editor and a BibTeX reference manager that help users to work with TeX outside of the command line. But it is big(2G).

Another way is to install BasicTex, only 100M size, but we will be faced with the tasks of installing additional programs and installing missing packages.


Others

About other development tools, for me, I use Beyond Compare, Matlab, Android Studio, VisualBox, Blender and Octopress with Heroku for the blog post.


Reference

“MacOS setup guide” http://sourabhbajaj.com/mac-setup/ Sourabh Bajaj