I understand the motivation behind your question. update-alternatives elegantly solves the problem of managing several installed versions of a program (for example Java) or different ports of a program (vim vs. elvis vs. vile) by making one of them the default one.
update-alternatives, found in many Linux distros, creates and manages a set of symlinks from /bin, /usr/bin (and the like) to /etc/alternatives, and from there to the location where the program is installed.
For example, in openSUSE java is symlinked to /etc/alternatives/java, which is symlinked to a "private" path where java resides:
/usr/bin/java -> /etc/alternatives/java
/etc/alternatives/java -> /usr/lib64/jvm/java-1.5.0-sun-1.5.0/jre/bin/java
Unfortunately, and although some think it would be necessary, at least for MacPorts, there is no such thing in OS X. I have personally solved the lack of update-alternatives with aliases in my .bashrc:
alias ls='/usr/local/bin/ls'
or simply changing $PATH:
PATH=/usr/local/bin:$PATH
If you can't solve it like this and are weighing the fact of porting it to OS X be aware that although update-alternatives' first incarnation was a Perl script, it was rewritten in C for Debian, and some other distros adopted it later (for instance openSUSE 12.1).
openSUSE 11.4 provides the Perl version as an RPM package. If you are looking for the C version, go for any recent Ubuntu or Debian release, or openSUSE 12.1 or later.
Answer from jaume on Stack ExchangeI understand the motivation behind your question. update-alternatives elegantly solves the problem of managing several installed versions of a program (for example Java) or different ports of a program (vim vs. elvis vs. vile) by making one of them the default one.
update-alternatives, found in many Linux distros, creates and manages a set of symlinks from /bin, /usr/bin (and the like) to /etc/alternatives, and from there to the location where the program is installed.
For example, in openSUSE java is symlinked to /etc/alternatives/java, which is symlinked to a "private" path where java resides:
/usr/bin/java -> /etc/alternatives/java
/etc/alternatives/java -> /usr/lib64/jvm/java-1.5.0-sun-1.5.0/jre/bin/java
Unfortunately, and although some think it would be necessary, at least for MacPorts, there is no such thing in OS X. I have personally solved the lack of update-alternatives with aliases in my .bashrc:
alias ls='/usr/local/bin/ls'
or simply changing $PATH:
PATH=/usr/local/bin:$PATH
If you can't solve it like this and are weighing the fact of porting it to OS X be aware that although update-alternatives' first incarnation was a Perl script, it was rewritten in C for Debian, and some other distros adopted it later (for instance openSUSE 12.1).
openSUSE 11.4 provides the Perl version as an RPM package. If you are looking for the C version, go for any recent Ubuntu or Debian release, or openSUSE 12.1 or later.
Macports supports this capability via 'port select' for many language packages, mysql, and others. (I'm using version MacPorts 2.4.2.)
java - sudo: update-alternatives: command not found - Stack Overflow
linux command --> macos command equivalent
macos - How to update Java to 7 on max OSX? - Stack Overflow
update-alternatives broken and php no longer found in cmd - Stack Overflow
linux command --> macos command equivalent
I came across an linux sudo command that I need to port to macos.
sudo update-alternatives --install /usr/bin/ninja ninja /usr/local/bin/ninja 1 --force
This command should set the priority when calling ninja to /usr/local/bin/ninja (* Am I right? *)
What's the equivalent command for MacOS?
EDIT:
This is the 3 lines of codes that I need to port to MacOS.
The third is for a specific environment for a python program to run.
!wget https://github.com/ninja-build/ninja/releases/download/v1.8.2/ninja-linux.zip
!sudo unzip ninja-linux.zip -d /usr/local/bin/
!sudo update-alternatives --install /usr/bin/ninja ninja /usr/local/bin/ninja 1 --force
Oracle Java 7 and Apple Java 6 are completely different and they coexist on the same machine as they inhabit totally separate locations.
Java 7, if installed, lives in:
/Library/"Internet Plug-Ins"/JavaAppletPlugin.plugin/Contents/Home
Java 6, if installed, lives in:
/System/Library/Frameworks/JavaVM.framework/Versions/A/
(And the more traditional Java 6 JDK is at: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home)
When you type java, you're running /usr/bin/java, which is a symbolic link to Java 6. In fact, if you type ls -l /usr/bin | grep -i java you will see a bunch of symbolic links for the typical JDK/JRE executables.
So if you have installed Java 7, and that's what you want to use from the command line, you can change into its directory and run its specific binaries in bin. To avoid that, you can add its bin directory to your Bash search path, so its contents are invoked instead of the Java 6 symlinks in /usr/bin. To do this, alter /etc/paths to add the bin directory before the first line:
{ echo "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin"; \
cat /etc/paths; } | sudo tee /etc/paths > /dev/null
Then set the JAVA_HOME environment variable, so supporting software knows where to find Java 7:
{ echo -n "export JAVA_HOME=";
echo "/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"; } \
| sudo tee -a /etc/bashrc > /dev/null
Now, in any new Terminal window, when you type java -version, you'll see java version "1.7.0_51". (And if you still want to be able to run the Java 6 binaries, you can call them with /usr/bin/java, /usr/bin/javac, etc.)
You could try issuing the following command:
update-alternatives –config java
That command will make you able to choose between Java versions. This command worked for me on a Linux-based machine, so I think there would be no different than a Mac, but I'm not sure, you could try it out.
Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:
Install the alternative for javac
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1Check / update the alternatives config:
$ sudo update-alternatives --config javac
If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.
To check everything is setup correctly then:
$ which javac
/usr/bin/javac
$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep 4 17:10 /usr/bin/javac -> /etc/alternatives/javac
$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep 4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac
And finally
$ javac -version
javac 1.8.0_144
Repeat for java, keytool, jar, etc as needed.
You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:
When using the
--configoption, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be inauto mode. You will need to use the--autooption in order to return to the automatic state.
And I believe auto mode is set when you install the first/only JRE/JDK.