-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Up and running with OCaml #1165
Changes from 6 commits
671d231
32a1d31
0f6030c
3f008b0
6e61dd6
dba99aa
68dbdb9
02597c2
61475c1
adbe914
ff384c4
944d524
27edb03
23cd15d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<!-- ((! set title Up and Running with OCaml !)) ((! set learn !)) --> | ||
|
||
*Table of contents* | ||
|
||
#Up and Running with OCaml | ||
|
||
This page will help you install OCaml, the Dune build system, and support for | ||
your favourite text editor. All these instructions work on Windows, Unix | ||
systems like Linux, and MacOS. | ||
|
||
##Installing OCaml | ||
|
||
**For Linux and MacOS**, we install OCaml using OPAM, the OCaml package manager. | ||
OPAM will also be used when we wish to install third-party OCaml libraries. | ||
|
||
First, we install OPAM: | ||
|
||
``` | ||
sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh) | ||
``` | ||
|
||
(If this method does not suit, please see [How to install | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a minor point, but "does not suit" reads slightly strange to me (I would expect "suit you", but rather use another wording); maybe it's more common in regional versions of English I'm less familiar with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it to "suit you", which is probably better. What I was getting at, I think, with the choice of verb, is that some people don't like to curl and execute random scripts from the internet. (The disadvantage of being a native English speaker, is that I have no way of explaining why the verb suit doesn't always need an object. The closest I can think of is that "fit" doesn't either e.g. "If the last piece fits you have finished.") |
||
OPAM](opam.ocaml.org/doc/install.html)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One issue that I have with that formulation is that the curl based installation would by my option of last resort on a linux distribution with outdated version of opam. Maybe it would make sense to inverse the two options: first start with the system-managed option, before moving to the user-managed script option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this section, I copied the instructions from the official guide: https://opam.ocaml.org/doc/Install.html I can add a mention that any existing version should be removed first, if that would suffice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have the same issue with the opam page. In particular, the fact that the recommended installation path appears only in the third section is problematic. The recommended path should appear first to let readers skip the non-recommended options if they can. And removing any existing version is not the issue at hand? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I did not spot the phrase "This is generally the recommended way, when available and up-to-date" on the OPAM page. I will have a think about this. |
||
|
||
Then, we install an OCaml compiler: | ||
|
||
``` | ||
# environment setup | ||
opam init | ||
eval `opam env` | ||
|
||
# install given version of the compiler | ||
opam switch create 4.11.1 | ||
eval `opam env` | ||
``` | ||
|
||
Now, OCaml is up and running: | ||
|
||
``` | ||
$ which ocaml | ||
/Users/frank/.opam/4.11.1/bin/ocaml | ||
|
||
$ ocaml -version | ||
The OCaml toplevel, version 4.11.1 | ||
``` | ||
|
||
Other (less recommended) ways to install OCaml, for example using your Linux | ||
distribution's package manager, are explained on the [install | ||
page](../docs/install.html). | ||
|
||
**For Windows** we use instead the [OCaml for | ||
Windows](https://fdopen.github.io/opam-repository-mingw/) installer which comes | ||
in 32bit and 64bit versions. This installer gives you OPAM and OCaml | ||
installations all in one go. It is used from within a Cygwin environment, but | ||
the executables produced have no dependency on Cygwin at all. | ||
|
||
##The OCaml top level | ||
|
||
OCaml comes with two compilers: for native code, and for byte code. We shall | ||
use one of those in a moment. But first, let's use OCaml's top level (sometimes | ||
known as a REPL in other languages): | ||
|
||
``` | ||
$ ocaml | ||
OCaml version 4.11.1 | ||
|
||
# 1 + 2 * 3;; | ||
- : int = 7 | ||
|
||
``` | ||
|
||
We typed the phrase `1 + 2 * 3` and then signalled to OCaml that we had | ||
fininshed by typing `;;` followed by the Enter key. OCaml calculated the | ||
johnwhitington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result, `7` and its type `int` and showed them to us. We exit by running the | ||
built-in `exit` function with exit code 0: | ||
|
||
``` | ||
$ ocaml | ||
OCaml version 4.11.1 | ||
|
||
# 1 + 2 * 3;; | ||
- : int = 7 | ||
# exit 0;; | ||
$ | ||
``` | ||
|
||
There are two ways to improve your experience with the OCaml top level: you can | ||
install `rlwrap` on your system and invoke `rlwrap ocaml` instead of `ocaml` to | ||
get line-editing facilities inside the OCaml top level, or you can install the | ||
alternative top level `utop` using OPAM: | ||
|
||
``` | ||
$ opam install utop | ||
``` | ||
|
||
We run it by typing `utop` instead of `OCaml`. You can read [more about | ||
`utop`](https://github.com/ocaml-community/utop). | ||
|
||
##Installing the Dune build system | ||
|
||
Dune is a build system for OCaml. It takes care of all the low level details of | ||
OCaml compilation. We install it with OPAM: | ||
|
||
``` | ||
$ opam install dune | ||
The following actions will be performed: | ||
- install dune 2.7.1 | ||
|
||
<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><> | ||
[default] https://opam.ocaml.org/2.0.7/archives/dune.2.7.1+opam.tar.gz | ||
downloaded | ||
|
||
<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><> | ||
-> installed dune.2.7.1 | ||
Done. | ||
``` | ||
|
||
##A first project | ||
|
||
Let's begin the simplest project with Dune and OCaml. We create a new directory | ||
and ask `dune` to initialise a new project: | ||
|
||
``` | ||
$ mkdir helloworld | ||
$ cd helloworld/ | ||
$ dune init exe helloworld | ||
Success: initialized executable component named helloworld | ||
``` | ||
|
||
Building our program is as simple as typing `dune build`: | ||
|
||
``` | ||
$ dune build | ||
Info: Creating file dune-project with this contents: | ||
| (lang dune 2.7) | ||
Done: 8/11 (jobs: 1) | ||
``` | ||
|
||
When we change our program, we type `dune build` again to make a new | ||
executable. We can run the executable with `dune exec` (it's called | ||
`helloworld.exe` even when we're not using Windows): | ||
|
||
``` | ||
$ dune exec ./helloworld.exe | ||
Hello, World! | ||
``` | ||
|
||
Let's look at the contents of our new directory. Dune has added the | ||
`helloworld.ml` file, which is our OCaml program. It has also added our `dune` | ||
file, which tells dune how to build the program, and a `_build` subdirectory, | ||
which is dune's working space. | ||
|
||
``` | ||
$ ls | ||
_build dune helloworld.ml | ||
``` | ||
|
||
The `helloworld.exe` executable is stored inside the `_build` structure, so | ||
it's easier to run with `dune exec`. To ship the executable, we can just copy | ||
it from inside `_build` to somewhere else. | ||
|
||
Here is the contents of the automatically-generated `dune` file. When we want | ||
to add components to your project, such as third-party libraries, we edit this | ||
file. | ||
|
||
``` | ||
(executable | ||
(name helloworld)) | ||
``` | ||
|
||
##Editor support for OCaml | ||
|
||
**For Vim and Emacs**, install the merlin system using OPAM: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a link to the Merlin homepage would be nice (users can follow it if they want to know what kind of feature Merlin provides) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. (I also note that there are LSP implementations for vim and emacs on the web, but I assume plain merlin is still the easiest way for these editors...) |
||
|
||
``` | ||
$ opam install merlin | ||
``` | ||
|
||
the installation procedure will print instructions on how to link merlin with | ||
your editor. | ||
|
||
For **Visual Studio Code**, the OCaml language server can by installed by OPAM. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something like "For Visual Studio Code and other LSP-supporting editors"? I have the impression that this description applies to most editors that recognize themselves as IDEs these days (of course the specific plugin will vary, and in some cases may not exist), and I would regret that people using another IDE would feel left out when the answer is most likely to be the same. (But then maybe those people don't really exist for OCaml anymore, and it's better to only talk about things we know for sure we support well.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
``` | ||
$ opam pin add ocaml-lsp-server https://github.com/ocaml/ocaml-lsp.git | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pin is no longer necessary, as lsp has been released into opam |
||
$ opam install ocaml-lsp-server | ||
``` | ||
|
||
Now, we install the OCaml Platform Visual Studio Code extension. | ||
|
||
Upon first loading an OCaml source file, you may be prompted to select the | ||
toolchain in use: pick OCaml the version of OCaml you are using, e.g. 4.11.1 | ||
from the list. | ||
|
||
![Visual Studio Code](/img/vscode.png "") | ||
|
||
**On Windows**, we must launch Visual Studio Code from within the Cygwin window, | ||
rather than by clicking on its icon (otherwise, the language server will not be | ||
found): | ||
|
||
``` | ||
$ /cygdrive/c/Users/Frank\ Smith/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comment. The rendering of your headings by Github is off (see https://github.com/johnwhitington/ocaml.org/blob/up-and-running/site/learn/tutorials/up_and_running.md), I think you should use a space before the title:
## Up
rather than##Up
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thanks.