对于基于 ARM 的新架构,有些东西是否可以原生运行还存在一些不确定性。这里记录一些经验以供参考。
Homebrew
Homebrew自称是 macOS 上缺失的软件包管理器。你可以把它看作是 Linux 世界中的 apt 或 yum。
要在新机器上安装 Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
然后它会要求您输入密码,以便您可以使用 sudo。
==> Checking for `sudo` access (which may request your password).
Password:
在询问您的密码后,它会向您显示它计划执行的操作:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew
==> The following new directories will be created:
/opt/homebrew/bin
/opt/homebrew/etc
/opt/homebrew/include
/opt/homebrew/lib
/opt/homebrew/sbin
/opt/homebrew/share
/opt/homebrew/var
/opt/homebrew/opt
/opt/homebrew/share/zsh
/opt/homebrew/share/zsh/site-functions
/opt/homebrew/var/homebrew
/opt/homebrew/var/homebrew/linked
/opt/homebrew/Cellar
/opt/homebrew/Caskroom
/opt/homebrew/Frameworks
==> The Xcode Command Line Tools will be installed.
Press RETURN to continue or any other key to abort
在 Apple Silicon 上,Homebrew 将把文件保存/opt/homebrew/在/usr/local/
然后它会警告你不在/opt/homebrew/bin/环境$PATH变量中。它会建议你在 shell 中输入以下内容:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
通过打印出值来确认这是否有效$PATH:
在一台全新的机器上它看起来应该是这样的:
/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Python
Homebrew 有一个专门的页面介绍如何与 Python 交互,其中很大一部分内容介绍了 pip。
brew install python@3.8
上面的命令会将文件放入其中,/opt/homebrew/opt/python@3.8/因此请确保/opt/homebrew/opt/python@3.8/bin/``$PATH
安装不同版本的 Python 会将其保存在适当的位置。例如:安装 Python 2.7 会将文件保存在,/opt/homebrew/opt/python@2.7/这意味着/opt/homebrew/opt/python@2.7/bin/也需要在您的$PATH.
如果您现在必须将其添加到~/.zprofile或~/.zshrc,请不要忘记对这些文件使用 source 命令,以便您可以将更改添加到$PATH。更简单的替代方法是打开一个新的 shell 会话并从那里继续设置。
pip
从 Python 3.4 开始,pip 与 Python 捆绑在一起,因此我们不再需要像往常一样easy_install去安装pip。
python3 -m pip install --upgrade --user setuptools
python3 -m pip install --upgrade --user pip
这两个命令为您提供最新版本的 setuptools 和 pip,~/Library/Python/3.8/bin/而不是/usr/bin/python。
我更喜欢将 Python 分为三个不同的级别:
-
可以在 /usr/bin/python 中找到的 Mac OS 版本将用于系统范围或系统级别的事务。
-
Homebrew 的一个版本,
/opt/homebrew/opt/python@3.8/bin/python3它安装了一些_对我来说_~/Library/Python/3.8/lib/python/site-packages/是全局的东西,比如一次性脚本,这些脚本实际上不需要引入依赖项,也不与业务需求或可交付成果挂钩。除此之外,我只用它来管理我的不同虚拟环境。 -
不同版本的 Python 在不同的虚拟环境中运行,这些虚拟环境具有更多特定于项目的要求。
virtualenv
在我们使用 virtualenv 创建新的虚拟环境之前,我们需要先使用 pip 安装它,因为它是一个第三方包;
这会将包~/Library/Python/3.8/lib/python/site-packages/和可执行文件添加到,~/Library/Python/3.8/bin/所以现在我们可以创建虚拟环境。
virtualenv --python=2.7 ve
上面的调用创建了一个ve以我们的新虚拟环境命名的目录。该--python=2.7部分告诉virtualenv我们为此虚拟环境使用 Python 2.7。
Python 2.7 不再受支持,因此这只是一个例子。要创建使用 Python 3.8 的新虚拟环境,请执行以下操作:
virtualenv --python=3.8 ve3
使用不同的名称或删除 ve 目录很重要。如果我们使用相同的名称,它只会将新文件添加到现有目录中。
git
我们不必明确安装 git,因为它现在随 macOS 一起提供。它不太可能像 Homebrew 版本那样频繁更新,所以我们也可以使用 Homebrew 安装它。
brew install git
在这里,让我们生成一个密钥对,以便我们无需输入密码即可使用 GitHub 或 GitLab 等 git 托管服务进行身份验证。
ssh-keygen -t rsa -b 4096
当被问及将私钥保存在哪里时,通常可以使用默认值~/.ssh/id_rsa。如果该密钥存在并且用户未明确设置位置,许多程序会自动使用该密钥。我通常对私钥使用不同的文件名,但我仍然将其保存在里面,~/.ssh/并且~/.ssh/id_rsa是指向实际私钥的符号链接。
当要求输入私钥密码时,最好使用强密码。如果其他人或系统能够获得您的私钥副本,这将增加额外的安全层。
ssh-keygen 生成密钥对后,我们将看到一个公钥和一个私钥,密钥大小为 4096 位~/.ssh
Postgres
在撰写本文时,最新的主要版本是 Postgresql 12。
如果我们想安装最新版本,我们可以这样做:
brew install postgresql
也可以明确请求版本。例如,如果我们想安装 Postgresql 11,我们可以这样做:
brew install postgresql@11
它不会自动在后台作为服务运行,所以我们需要执行以下操作:
brew services start postgresql@11
这也会在您重启计算机后立即自动运行您的 Postgres 服务。
psycopg2
Django 和 Postgres 通常一起使用,这意味着我们需要安装 psycopg2 作为依赖项。
由于我们正在创建开发环境,我发现使用 psycopg2-binary 比使用 psycopg 更方便,因为它应该安装包的预编译版本。这在我的 Linux 机器上是正确的,但在 macOS 11.2.3 和2.9.0 之前的psycopg2-binary 上并非如此
本节致力于设置psycopg早于的版本2.9.0。
尝试安装 psycopg2 或 psycopg2-binary 时遇到的第一个错误是:
Error: pg_config executable not found.
pg_config is required to build psycopg2 from source. Please add the directory containing
pg_config to the $PATH or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
If you prefer to avoid building psycopg2 from source, please install the PyPI 'psycopg2-binary' package instead.
For further information please check the 'doc/src/install.rst' file (also at <https://www.psycopg.org/docs/install.html>).
为了解决这个问题,我们需要pg_config在我们的$PATH
export PATH=/opt/homebrew/opt/postgresql@11/bin:$PATH
这还将常见的 Postgres 工具添加到您的$PATH喜欢psql和中pg_dump。
更新您的后$PATH,您将遇到以下错误:
ld: warning: directory not found for option '-L/usr/local/opt/openssl@1.1/lib'
ld: warning: directory not found for option '-L/usr/local/opt/python@3.8/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib'
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1
LDFLAGS我们通过设置和来解决这个问题CPPFLAGS。
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/python@3.8/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"