macOS 重装系统开发环境

1,273 阅读2分钟

系统内置

编程语言:

  • bash 3.2.57
  • ruby 2.3.7
  • perl 5.18.4
  • python 2.7.10
  • php 7.1.23

实用程序:

  • curl 7.54.0
  • ssh 7.9

命令行开发者工具

使用如下命令:

 ls -l /usr/bin | grep -E "git|make|gcc"

可以看到,系统有内置 git, make, gcc 等工具。但是,当你执行其中一个命令时,比如 git --version ,就会出现如下弹窗。提示需要安装 Xcode 或来自 App Store 的命令行开发工具才能使用。两者仍选一个都可以。但如果不是做IOS开发,安装 Xcode 又过于重了。因此,选择轻量级的命令行就可以了。

安装了命令行开发这工具后,以下程序都可以在命令行顺利地使用了。

  • git 2.20.1
  • make 3.81
  • gcc

git 与 github

刚刚完成 git 安装,通常我们需要做一些初始化配置。如:开启颜色显示(用于 git log ,git status ,git diff等输出信息的高亮),设置全局用户名和邮箱。

git config --global color.ui true
git config --global user.name "your name"
git config --global user.email "your email"
git config -l

如果想 push 代码到 github 更加顺滑一些,还需要配置 ssh 密钥。首先生成密钥对,并将公钥复制到剪贴板。

ssh-keygen -t rsa -b 4096 -C "your github account email"
eval "$(ssh-agent -s)"
pbcopy < ~/.ssh/id_rsa.pub

github 粘贴SSH keys: github.com/settings/ke…

测试是否可用:

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

homebrew

homebrew (brew) 是 macOS 系统的命令行软件安装工具,使用 ruby 编写。其作用相当于 CentOS 的 yum 或 Ubuntu 的 apt-get 。但不同的是,homebrew 并不是内置的安装工具,因为官方的软件安装工具当然是 App Store 。很多为 macOS 编写的命令行程序,都采用 brew 方式安装。

安装 homebrew 的方式如下:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/bin
/usr/local/etc
/usr/local/include
/usr/local/lib
/usr/local/sbin
/usr/local/share
/usr/local/var
/usr/local/opt
/usr/local/share/zsh
/usr/local/share/zsh/site-functions
/usr/local/var/homebrew
/usr/local/var/homebrew/linked
/usr/local/Cellar
/usr/local/Caskroom
/usr/local/Homebrew
/usr/local/Frameworks

nodejs

下载LTS版:nodejs.org/dist/v12.13…

双击安装:

sudo npm install -g n
sudo n 8.12
sudo n lts
sudo n latest

安装 yarn :

curl -o- -L https://yarnpkg.com/install.sh | bash

都安装到哪了?

$ ls -l /usr/local/bin/
total 82576
lrwxr-xr-x  1 ken       admin        28 10 23 20:22 brew -> /usr/local/Homebrew/bin/brew
lrwxr-xr-x  1 ken       admin        27 10 23 20:51 n -> ../lib/node_modules/n/bin/n
-rwxr-xr-x  1 root      wheel  42276320 10 21 15:53 node
lrwxr-xr-x  1 root      admin        38 10 23 20:45 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 root      admin        38 10 23 20:45 npx -> ../lib/node_modules/npm/bin/npx-cli.js

anaconda

安装配置 anaconda :

# 安装
curl -O https://repo.anaconda.com/archive/Anaconda3-2019.10-MacOSX-x86_64.sh
sh Anaconda3-2019.10-MacOSX-x86_64.sh
# 初始化
~/anaconda3/bin/conda init
. ~/.bash_profile
# 自动激活配置与清华镜像
conda config --set auto_activate_base false
cat << END >> ~/.condarc
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
END
# 测试创建 python3.6 环境
conda create -n py36 python==3.6
conda activate py36
conda deactivate

添加 c++ kernel:

conda activate base
conda install xeus-cling -c conda-forge
jupyter kernelspec list

添加 nodejs kernel:

conda create -n nodejs
conda activate nodejs
conda install jupyter notebook
git clone https://github.com/3Nigma/nelu-kernelu.git
cd nelu-kernelu
# nodejs v12.3+
npm install
test -d ~/notebook || mkdir ~/notebook
cd ~/notebook
jupyter notebook

设置终端配色

cat << END >> ~/.bash_profile

# export LS_OPTIONS='--color=auto'           # 如果没有指定,则自动选择颜色
export CLICOLOR='Yes'                        # 是否输出颜色
export LSCOLORS='ExGxFxdaCxDaDahbadacec'     # 指定颜色
END

设置 vim 配色

test -f ~/.vimrc || cp /usr/share/vim/vimrc ~/.vimrc

cat << END >> ~/.vimrc

" 修改配色 begin
colorscheme default     " 设置颜色主题

syntax on               " 语法高亮

filetype on             " 检测文件的类型

set number              " 显示行号
set cursorline          " 用浅色高亮当前行
"autocmd InsertLeave * se nocul
"autocmd InsertEnter * se cul

set ruler               " 在编辑过程中,在右下角显示光标位置的状态行
set laststatus=2        " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
                        " 设置在状态行显示的信息

set tabstop=4           " Tab键的宽度
set softtabstop=4
set shiftwidth=4        " 统一缩进为4

set autoindent          " vim使用自动对齐,也就是把当前行的对齐格式应用到下一行(自动缩进)
set cindent             " (cindent是特别针对 C语言语法自动缩进)
set smartindent         " 依据上面的对齐格式,智能的选择对齐方式,对于类似C语言编写上有用

set scrolloff=3         " 光标移动到buffer的顶部和底部时保持3行距离

set incsearch           " 输入搜索内容时就显示搜索结果
set hlsearch            " 搜索时高亮显示被找到的文本

set foldmethod=indent   " 设置缩进折叠
set foldlevel=99        " 设置折叠层数
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
                        " 用空格键来开关折叠

" 自动跳转到上次退出的位置
if has("autocmd")
    au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
" 修改配色 end
END

添加 ~/.bashrc

cat << END >> ~/.bash_profile

test -s ~/.bashrc && . ~/.bashrc
END

设置常用程序别名

cat << END >> ~/.bashrc

alias ll='ls -l'
alias grep='grep --color'
END

设置 git 别名

git config --global alias.st status
git config --global alias.ci commit
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --date=format:'%Y-%m-%d %H:%M:%S' --abbrev-commit"

参考