Getting a Scala interpreter to work
I'm very new to Scala. I have downloaded it, got it working in Eclipse where I'll be developing it; but I can't make it work in Terminal.
All sites and books say to just type scala
- this doesn't work.
The website infuriatingly says:
We assume that both the Scala software and the user environment are set up correctly.
How do I do that bit?
I'm very new to this, and using Jargon or assuming too much knowl开发者_Go百科edge of frameworks around Scala will ruin a good response; please keep it simple.
- Mac OS X (10.6.7)
- Scala: 2.9.0.1
Thank you
For OS X, I highly recommend Homebrew.
The installation of Homebrew is incredibly easy. Once installed, you just need to run brew install scala
and scala will be installed and ready to go. Homebrew also has tons of other goodies just a brew install
away.
If you don't already have Java installed, you can install that with brew cask install java
.
MacPorts or Fink may have something similar, but I prefer Homebrew.
Not a big fan of cluttering up my PATH
variable. I just symlink all my programs to /usr/local/bin, which is in the classpath. For example, if you downloaded scala and uncompress it in /opt/scala-2.9.0-1, run the following in the terminal.
ln -s /opt/scala-2.9.0-1/bin/scala /usr/local/bin
Now just type scala
in the terminal and you're all set.
This way you don't have to set your PATH
or change it when you have a new version of scala you want to try out. If you download a new version, you can uncompress it in any location and symlink the new version. Say you download version 2.9.1 and uncompress it in /opt/scala-2.9.1. You can type the following in the terminal
ln -s /opt/scala-2.9.1/bin/scala /usr/local/bin/scala2.9.1
Now, to use scala 2.9.1 you just run scala2.9.1
at the terminal.
When you are ready to switch to 2.9.1 fulltime, just update the symlink.
You could also add scaladoc, scalac, scalap and others in the same way
ln -s /opt/scala-2.9.0-1/bin/scalac /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/scalap /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/scaladoc /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/fsc /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/sbaz /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/sbaz-setup /usr/local/bin
You need to add scala\bin
to your PATH
environment variable.
On Mac OS X the path to Scala bin is usually: /Users/<your username>/scala/bin
and on Windows usually: C:\Program Files (x86)\scala\bin
.
On Mac OS X use the Terminal and write (using your username):
echo 'export PATH=/Users/<your username>/scala/bin:$PATH' >> ~/.bash_profile
then close the Terminal and start it again.
Scala recommends using Homebrew to install the Typesafe stack for Scala 2.9.2.
brew install scala sbt maven giter8
Homebrew will install soft links in /usr/local/bin
for sbt
, scala
, scalac
, scaladoc
, scalap
, fsc
and g8
. Follow the soft links to its final referent in order to determine where $SCALA_HOME needs to be. $SCALA_HOME should contain bin/scala
and lib/scala-compiler.jar
.
Typesafe recommends using sbt console
instead of scala
to get the interpreter going, because sbt
will also manage library dependencies to libraries such as Akka. That said, if you want to use scala
, scalac
, fsc
, scalac
and scaladoc
directly, you may need to run a chmod +x
on the referents of the soft links.
If you don't want to add more directories to your path, try these steps:
- Download Scala from the downloads pages.
- Unzip and then copy the folder to \Library\Scala.
- Find the complete path to the bin directory, which should be \Library\Scala\scala-2.9.2\bin.
- Find the "scala" file, right click, and Make Alias.
- Move the alias file anywhere it's convenient, such as your desktop.
- Double click the alias to start Scala in the Terminal.
My way of making Scala run every time you type "scala" in Terminal was to add the path not to the .bashrc file but to the /etc/paths one.
- Download the latest Scala binary from www.scala-lang.org/download
- Unzip the binary file
- Move the unzipped folder (named scala-2.11.2) to the /usr/bin/ or some other directory of your preference.
Then open the /etc/paths file with nano:
sudo nano /etc/paths
and add this line to the end:
/usr/bin/scala-2.11.2/bin
Press Ctrl+X to exit nano and answer "Yes" when prompted to overwrite the file
Then restart Terminal and once you type:
scala
you will get the scala command line that looks like this:
Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05). Type in expressions to have them evaluated. Type :help for more information. scala>
I hope this helps.
If you downloaded scala with macports try typing scala-2.9 (or whatever is the filename of the contents of folder /opt/local/bin/scala/)
(At least this works for OSX mountain lion)
Just install Scala with : $ brew install scala
so you can use $scala
in terminal to evaluate scala instructions , for now the official Scala website recommend installing scala with sbt : $ brew install sbt
which won't let you scala
keyword in terminal
精彩评论