I can't install ocaml-lsp-server and ocaml-format-lib

Hey !
I can’t figure out why I am not able to install ocaml-lsp-server and ocaml-format-lib packages for VS code (from the guide of the OCaml site: Configuring Your Editor · OCaml Documentation ). I am on Ubuntu 24.10 and I am running the installation command from my own switch.
I get this while trying to install those :

$ opam switch
#  switch      compiler            description
   default     ocaml-system.5.2.0  ocaml >= 4.05.0
→  my_project  ocaml-system.5.2.0  ocaml-base-compiler = 5.2.0 | ocaml-system
                                   = 5.2.0
hugo@hugo-Legion-Slim-7-16APH8:~$ opam install ocaml-lsp-server ocamlformat
The following actions will be performed:
=== install 3 packages
  ∗ ocaml-lsp-server 1.19.0
  ∗ ocamlformat      0.26.2
  ∗ ocamlformat-lib  0.26.2 [required by ocamlformat]

Proceed with ∗ 3 installations? [y/n] y

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
⬇ retrieved ocaml-lsp-server.1.19.0  (cached)
⬇ retrieved ocamlformat.0.26.2, ocamlformat-lib.0.26.2  (cached)
[ERROR] The compilation of ocaml-lsp-server.1.19.0 failed at "dune build -p
        ocaml-lsp-server -j 15 @install".
[ERROR] The compilation of ocamlformat-lib.0.26.2 failed at "dune build -p
        ocamlformat-lib -j 15 @install".

#=== ERROR while compiling ocamlformat-lib.0.26.2 =============================#
# context     2.2.1 | linux/x86_64 | ocaml-system.5.2.0 | https://opam.ocaml.org#6383bc5431ca714c10b4e29dbf7eda9572a4ac07
# path        ~/.opam/my_project/.opam-switch/build/ocamlformat-lib.0.26.2
# command     ~/.opam/opam-init/hooks/sandbox.sh build dune build -p ocamlformat-lib -j 15 @install
# exit-code   1
# env-file    ~/.opam/log/ocamlformat-lib-31467-c66592.env
# output-file ~/.opam/log/ocamlformat-lib-31467-c66592.out
### output ###
# (cd _build/default && /usr/bin/ocamlc.opt -w -40 -noassert -w -50 -g -bin-annot -bin-annot-occurrences -I vendor/odoc-parser/.odoc_parser.objs/byte -I /home/hugo/.opam/my_project/lib/astring -I /home/hugo/.opam/my_project/lib/result -I /usr/lib/x86_64-linux-gnu/ocaml/5.2.0/camlp-streams -no-alias-deps -open Odoc_parser__ -o vendor/odoc-parser/.odoc_parser.objs/byte/odoc_parser__Syntax.cmi -c [...]
# File "vendor/odoc-parser/syntax.mli", line 5, characters 28-36:
# 5 |   Token.t Loc.with_location Stream.t ->
#                                 ^^^^^^^^
# Error: Unbound module "Stream"


#=== ERROR while compiling ocaml-lsp-server.1.19.0 ============================#
# context     2.2.1 | linux/x86_64 | ocaml-system.5.2.0 | https://opam.ocaml.org#6383bc5431ca714c10b4e29dbf7eda9572a4ac07
# path        ~/.opam/my_project/.opam-switch/build/ocaml-lsp-server.1.19.0
# command     ~/.opam/opam-init/hooks/sandbox.sh build dune build -p ocaml-lsp-server -j 15 @install
# exit-code   1
# env-file    ~/.opam/log/ocaml-lsp-server-31467-6b0d4c.env
# output-file ~/.opam/log/ocaml-lsp-server-31467-6b0d4c.out
### output ###
# [...]
# /usr/lib/x86_64-linux-gnu/ocaml/5.2.0/camlp-streams/camlp_streams.cmxa
# (cd _build/default && /usr/bin/ocamlc.opt -w -40 -alert -unstable -w -a -alert -all -g -bin-annot -bin-annot-occurrences -I vendor/odoc-parser/src/.odoc_parser.objs/byte -I /home/hugo/.opam/my_project/lib/astring -I /usr/lib/x86_64-linux-gnu/ocaml/5.2.0/camlp-streams -no-alias-deps -open Odoc_parser__ -o vendor/odoc-parser/src/.odoc_parser.objs/byte/odoc_parser__Syntax.cmi -c -intf vendor/odo[...]
# File "vendor/odoc-parser/src/syntax.mli", line 5, characters 28-36:
# 5 |   Token.t Loc.with_location Stream.t ->
#                                 ^^^^^^^^
# Error: Unbound module "Stream"
# (cd _build/default/ocaml-lsp-server/vendor/lev && /usr/bin/x86_64-linux-gnu-gcc -O2 -fno-strict-aliasing -fwrapv -pthread -fPIC -g -O2 -Werror=implicit-function-declaration -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -pthread -D_FILE_OFFSET_BITS=64 -Wdate-time -D_FORTIFY_SOURCE=3 -g -I [...]
# submodules/lev/lev/vendor/ev.c:2143:31: warning: ‘ev_default_loop_ptr’ initialized and declared ‘extern’
# submodules/lev/lev/vendor/ev.c: In function ‘evpipe_write’:
# submodules/lev/lev/vendor/ev.c:2817:11: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
# submodules/lev/lev/vendor/ev.c: In function ‘pipecb’:
# submodules/lev/lev/vendor/ev.c:2852:11: warning: ignoring return value of ‘read’ declared with attribute ‘warn_unused_result’ [-Wunused-result]



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
┌─ The following actions failed
│ λ build ocaml-lsp-server 1.19.0
│ λ build ocamlformat-lib  0.26.2
└─ 
╶─ No changes have been performed

Note : I am a beginner, I am sorry if my topic don’t have enough precisions.

1 Like

I don’t know for sure what cause this error, but could you try running opam update before re-running opam install ocaml-lsp-server ocamlformat ?

Also, using an ocaml-system compiler is more fragile than using an ocaml-base-compiler packaged by opam. I’m guessing you haven’t used the most-default-beginner-friendly way to install opam.

I already tried it and it didn’t worked, I tried again now just to be sure and also didn’t worked.

I installed opam with the method shown here : Installing OCaml · OCaml Documentation
It’s the official site of OCaml, it would pretty weird if the problem was coming from the installation

There are multiple installation instructions on ocaml.org apparently.
From the front page, if I click the “Install” button I am led to this install page instead: Install OCaml

This is the more common one. Can you try it out instead? (probably sudo apt-get remove opam first to remove the “system opam” - and perhaps also nuke the system ocaml at the same time)

In addition the above page even warns of the other installation approach:

Other installation methods
You can also install opam using your operating system’s package manager - however, you may get an older version of opam that does not support the most recent OCaml compiler version. If you need the most recent version, you can build opam from sources, following the instructions at opam’s GitHub repository.

1 Like

I was able to reproduce in Docker and it’s a bug from Ubuntu 24.10.

# apt update && apt install opam
[...]
# ocaml
# #use "topfind";;
[...]
# #require "camlp-streams";;
/usr/lib/aarch64-linux-gnu/ocaml/5.2.0/camlp-streams: added to search path
/usr/lib/aarch64-linux-gnu/ocaml/5.2.0/camlp-streams/camlp_streams.cma: loaded
# Stream.empty;;
Error: Unbound module Stream
# ^D
# ls /usr/lib/aarch64-linux-gnu/ocaml/5.2.0/camlp-streams
META  camlp_streams.cma  camlp_streams.cmxs

Somehow installing opam, installs also a bunch of other unrelated things amongst which is libcamlp-streams-ocaml.
This package installs the cma and cmxs but also the META file for some reason and thus it is picked up by dune (although a local camlp-streams should have also been installed so i think there might be an issue in the priority given to OS-wide install vs. opam install).

So TL;DR: there are 3 bugs:

  • installing opam on ubuntu should not install libcamlp-streams-ocaml (to be reported in the Ubuntu or Debian bugtracker)
  • non -dev ocaml packages in Ubuntu should not have a META file (to be reported in the Ubuntu or Debian bugtracker)
  • Priority should be given to packages installed by opam instead of the ones installed system-wide (to be reported to the opam-repository bugtracker. EDIT: reported in ocaml/opam-repository#26824 and ocaml/dune#11062)

That said, to fix your current case:

opam switch set-invariant --switch=my_project ocaml-base-compiler.5.2.0
opam switch set-invariant --switch=default ocaml-base-compiler.5.2.0

this will ensure you’re using the ocaml toolchain as packages in opam-repository expect. In the future if you’re creating a new switch, use instead:

opam switch create <name> ocaml-base-compiler.<version>
3 Likes

Thank’s a lot it works !