Setup your environment to develop Erlang programs

Erlang is great platform, however, if you want to develop Erlang programs, the best development environment is probably Emacs at this point.

In this post, I’m describing the tools you need, and how to set up to develop Erlang codes. The OS is assumed to be Debian-based Linux (mine is Ubuntu 10.04).

First, make sure that you have installed Erlang:

sudo apt-get install erlang erlang-base erlang-tools erlang-appmon erlang-pman erlang-observer erlang-ic erlang-parsetools erlang-toolbar erlang-et erlang-mnesia erlang-debugger erlang-percept

Now, make sure that you have installed Emacs and the Erlang mode for Emacs:

sudo apt-get install emacs emacs-goodies-el erlang-mode

Now, it is time to install Distel, which is an Emacs extension to make Erlang development nicer. Distel can communicate with a real Erlang code, allowing you to load your Erlang code onto a running node, debug it and/or run it.

Unfortunately, Distel is not in the Ubuntu repository. So you will have to download the source code from here:

http://code.google.com/p/distel/downloads/list

Unpack it, then run the command to install:

export prefix=~/.emacs.d/share/distel
make -e install

Change the location where you want to install it. I installed it in my .emacs.d directory.

Now, it is time to configure your .emacs configuration file to use the tools that we have just installed. Enter the following configuration:

  1. ;; This is needed for Erlang mode setup
  2. (setq erlang-root-dir "/usr/lib/erlang")
  3. (setq load-path (cons "/usr/lib/erlang/lib/tools-2.6.5/emacs" load-path))
  4. (setq exec-path (cons "/usr/lib/erlang/bin" exec-path))
  5. (setq erlang-man-root-dir "/usr/lib/erlang/man")
  6. (require ‘erlang-start)
  7.  
  8. ;; Load Distel stuff
  9. (add-to-list ‘load-path "~/.emacs.d/share/distel/elisp")
  10. (require ‘distel)
  11. (distel-setup)
  12.  
  13. ;; Some Erlang customizations
  14. (add-hook ‘erlang-mode-hook
  15.       (lambda ()
  16.         ;; when starting an Erlang shell in Emacs, with the node
  17.         ;; short name set to renzhi
  18.         (setq inferior-erlang-machine-options ‘("-sname" "renzhi"))
  19.         ;; add Erlang functions to an imenu menu
  20.         (imenu-add-to-menubar "imenu")))
  21.  
  22. ;; A number of the erlang-extended-mode key bindings are useful in the
  23. ;; shell too
  24. (defconst distel-shell-keys
  25.   ‘(("\C-\M-i"   erl-complete)
  26.     ("\M-?"      erl-complete)
  27.     ("\M-."      erl-find-source-under-point)
  28.     ("\M-,"      erl-find-source-unwind)
  29.     ("\M-*"      erl-find-source-unwind)
  30.     )
  31.   "Additional keys to bind when in Erlang shell.")
  32.  
  33. (add-hook ‘erlang-shell-mode-hook
  34.       (lambda ()
  35.         ;; add some Distel bindings to the Erlang shell
  36.         (dolist (spec distel-shell-keys)
  37.           (define-key erlang-shell-mode-map (car spec) (cadr spec)))))
  38.  

In the configuration above, there are three things that you have to change according to your environment:

  • The path to where you have install Erlang on your system. If you are running Ubuntu 10.04, the path should look like mine, and there is no need to change.
  • The Distel path should be changed accordingly, depending on your installation directory.
  • Change your default Erlang node name. Mine is called renzhi (look for the word) and change it accordingly. This will allow us to start up Erlang directly from Emacs, and give it a short name so that we can identify it when we want to connect to it later on.

Now that all the tools are installed and the configuration is set, you are ready to go. Let’s write our first “Hello world” program. Start up Emacs, create a new file called hello.erl, and enter the following code:

  1. -module(hello).
  2. -export([hello/0]).
  3.  
  4. hello() ->
  5.     "hello world".
  6.  

Here, we are creating a module called hello, and a function called hello(), with no argument. We also tell Erlang that the hello() function is to be exported.

The module name and the file must match, otherwise, when you are saving your source code, it will issue a warning and suggest you to change accordingly.

When you are writing Erlang code in Emacs, you are in Erlang mode. You should see that there are extra menus, namely imenu and Erlang, on your Emacs menu bar.

Let’s start up an Erlang shell:

  1. Select the Erlang menu
  2. Select Shell
  3. Select Start New Shell

The Emacs window is splitted into two parts, the first part still shows our Erlang code, and the second part shows an Erlang shell.

Now, let’s go the Erlang shell, and compile our code with the following command:

c(hello).

Note the period (‘.’) at the end, as Erlang statement must end with a full stop. If everything is ok, you should see:

Eshell V5.7.4  (abort with ^G)
(renzhi@shanghai)1> c(hello).
{ok,hello}
(renzhi@shanghai)2>

Now that the code is compiled, let’s run our first program with the following command:

hello:hello().

The following screenshot shows the result of your first Erlang program:

That’s it. You have created and run your first Erlang program. In a next post, I’ll describe how to debug your Erlang program inside Emacs.

6 Comments

  1. Hao says:

    Renzhi,

    Thank you for your step by step instructions on how to setup Erlang development environment in Emacs. I just started learning Erlang, and this is my first time to use Emacs. It helps a lot.

    While following your post, I found there is one issue in the codes you provided for .emacs file.
    I created .emacs file by copying and pasting the contents you provided. However it didn’t work as expected. The emacs complained “Symbol’s value as variable is void: ‘erlang-start” or something like that, and the “imenu” didn’t shown up on the menubar. It took me quite a while to figure out that there are strange characters ” ‘ ” in your codes. After I change it to ” ‘ “, i.e. single quote, everything works fine. My guess is that the strange character is a Chinese single quote in 全角 or 半角 form. Could you please update your code so that new comers to your blog won’t have to go through the debugging process I had.

    Thanks,

    Hao

  2. Hao says:

    There is one minor issue:
    “Unpack it, then run the command to install:
    export prefix=~/.emacs.d/share/distel
    make -e install

    should be:
    “Unpack it, then run the command to install:
    export prefix=~/.emacs.d/
    make -e install

  3. xp says:

    Hao, sorry about the quote character thing. It must be an issue when I copied from .emacs file and pasted on the blog here.

    As for the distel installation directory, mine is installed into

    ~/.emacs.d/share/distel

    that’s why export prefix is set to that. If you install into

    ~/.emacs.d/

    then you have to set the install prefix to that. But just make sure to have the distel elisp path set properly in your .emacs file.

  4. Hao says:

    Renzhi,

    If you set prefix to ~/.emacs.d, the distel will be installed to ~/.emacs.d/share/distel; if you set the prefix to ~/.emacs.d/share/distel, the distel will be installed to ~/.emacs.d/share/distel/share/distel.

    Since in .emacs you have the following:
    (add-to-list ‘load-path “~/.emacs.d/share/distel/elisp”)

    the prefix shall be set to ~/.emacs.d

    Hao

  5. Excellent post. Covering Ubuntu was very helpful. thanks dude :)

  6. [...] I dived into setting up emacs. This post from Renzhi on his blog was invaluable help. If you want to set up emacs with distel. this is the one to [...]

Leave a Reply

*


Switch to our mobile site