在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
当前回答
另一个免费、开源的Mac OS X v10.8 (Mountain Lion)首选项窗格/环境。plist解决方案是EnvPane。
EnvPane的源代码可在GitHub。EnvPane看起来具有与rcenvirenvironment相当的功能,然而,它似乎可以立即更新其存储的变量,即不需要重新启动或登录,这是受欢迎的。
如开发商所述:
EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. <SNIP> EnvPane includes (and automatically installs) a launchd agent that runs 1) early after login and 2) whenever the ~/.MacOSX/environment.plist changes. The agent reads ~/.MacOSX/environment.plist and exports the environment variables from that file to the current user's launchd instance via the same API that is used by launchctl setenv and launchctl unsetenv.
免责声明:我与开发者或他/她的项目没有任何关系。
附注:我喜欢这个名字(听起来像“Ends Pain”)。
其他回答
在Mountain Lion上,所有的/etc/paths和/etc/launchd.conf编辑都不起任何作用!
苹果的开发者论坛说:
“更改信息。.app本身的plist包含一个“lsenvirenvironment” 带有所需环境变量的字典。 ~ / .MacOSX /环境。不再支持Plist。”
所以我直接编辑了应用程序的信息。plist(右击“AppName. plist”)app”(在本例中是SourceTree),然后是“显示包内容”)。
我添加了一个新的键/字典对,称为:
<key>LSEnvironment</key>
<dict>
<key>PATH</key>
<string>/Users/flori/.rvm/gems/ruby-1.9.3-p362/bin:/Users/flori/.rvm/gems/ruby-1.9.3-p362@global/bin:/Users/flori/.rvm/rubies/ruby-1.9.3-p326/bin:/Users/flori/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:</string>
</dict>
(参见苹果的LaunchServicesKeys文档)
现在应用程序(在我的例子中是Sourcetree)使用给定的路径并使用Git 1.9.3:-)
PS:当然,您必须调整路径条目以满足您特定的路径需求。
/etc/launchd.conf在OS X v10.10 (Yosemite)、OS X v10.11 (El Capitan)、macOS v10.12 (Sierra)或macOS v10.13 (High Sierra)中不使用。
从launchctl手册页:
/etc/launchd.conf file is no longer consulted for subcommands to run during early boot time;
this functionality was removed for security considerations.
这个Ask Different answer中描述的方法适用于我(重启后):从Dock或Spotlight启动的应用程序继承了我在~/Library/LaunchAgents/my.startup.plist中设置的环境变量。(在我的例子中,我需要将LANG设置为en_US。UTF-8,用于Sublime Text插件。)
更新(2017-08-04)
从macOS 10.12.6 (Sierra)开始,这个方法似乎已经停止在Apache httpd上工作(对于系统和launchctl配置的用户选项)。其他项目似乎没有受到影响。可以想象,这是httpd中的一个bug。
原来的答案
这涉及到OS X 10.10+(特别是10.11+由于无根模式,其中/usr/bin不再可写)。
我在很多地方读到过,使用launchctl setenv PATH <new PATH >来设置PATH变量由于OS X中的一个错误而不起作用(从个人经验来看似乎是正确的)。我发现还有另一种方法可以为非shell启动的应用程序设置PATH:
sudo launchctl config user path <new path>
这个选项记录在launchctl手册页中:
config system | user parameter value Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect. [...] path Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence. NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.
我已经确认这与从Finder(使用getenv获取PATH)开始的GUI应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。
2月2022日(MacOs 12+)
这里的解决方案应该在重新启动或应用程序重新启动后工作。
CLI
打开所选配置文件的CLI。
为bash打开~/.bash_profile 为zsh打开~/.zshrc
添加(或替换)
export varName=varValue
(如果varValue中有空格-将其包装成")
确保重新启动命令行应用程序。
GUI
完成CLI步骤。 确保GUI应用程序关闭。 从命令行打开GUI应用程序。例如:
open /Applications/Sourcetree.app
(你也可以在.zshrc文件中使用这个命令的别名)
原则
Mac没有为所有上下文设置环境变量的配置选项。 避免更改用户配置文件之外的任何内容。
已经不管用了
(MacOS 12.1+) 编辑/etc/launchd.conf 带有plist后缀的XML文件
这里有一个非常简单的方法来做你想做的事情。在我的例子中,它是让Gradle工作(Android Studio)。
打开终端。 执行如下命令: Sudo nano /etc/paths或Sudo vim /etc/paths 在提示时输入密码。 转到文件底部,输入要添加的路径。 点击Control + X退出。 输入“Y”保存修改后的缓冲区。 打开一个新的终端窗口,然后输入: 回声路径美元
您应该看到新路径被追加到path的末尾。
我从这篇文章中得到了这些细节:
添加到Mac OS X 10.8 Mountain Lion及以上版本的PATH中