Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Installing Racket and Running Your First Program

Install Racket, open DrRacket, write a #lang racket program, and run it from the editor or the command line. Includes how files and modules are laid out.

To follow along on your own computer you need Racket, which is free and open source. The installer includes the language, a large standard library, the raco command-line tool, and DrRacket, a beginner-friendly editor with a built-in REPL. In this lesson you install it, write a first program, and run it two ways.

The commands below are real Racket commands for your own machine. Pressing Run on the examples in this page uses the JavaScript stand-in interpreter, which behaves the same way for these small programs.

Installing

  1. Go to the official Racket download page (racket-lang.org) and pick the installer for Windows, macOS or Linux.
  2. Choose the Racket CS (Chez Scheme) build; it is the default and the fastest.
  3. Run the installer. On Windows and macOS you get DrRacket as an app. On Linux you can use the installer script, or a package such as sudo apt install racket.
  4. Open a terminal and check the version:
Terminal
racket --version

If you see a version number, the racket command is on your PATH. If not, add the install folder (for example C:\Program Files\Racket or /usr/racket/bin) to PATH.

Your first program in DrRacket

Start DrRacket. The window has two panes: the definitions area (top) where you write your program, and the interactions area (bottom), the REPL. Type this in the top pane and press the Run button (or Ctrl+R):

RACKET
#lang racket

(displayln "Hello, Racket!")
(+ 40 2)

The interactions pane shows:

Text
Hello, Racket!
42

Notice that the program printed 42 even though we never asked it to print. In a Racket module, the value of every top-level expression that is not (void) is printed. displayln prints a string and a newline; the sum (+ 40 2) is an expression whose value is printed automatically.

Hello, Racket

Running from the command line

Save the program as hello.rkt. The .rkt extension is the Racket convention. Then run:

Terminal
racket hello.rkt

You get the same output. A few useful variations:

Command What it does
racket hello.rkt Run a file
racket Start the REPL
racket -i hello.rkt Run the file, then open the REPL with its definitions loaded
racket -e '(+ 1 2)' Evaluate one expression
raco make hello.rkt Compile to bytecode so it loads faster
raco test hello.rkt Run test submodules (see the testing lessons later)
raco pkg install <name> Install a package from the package catalog

What does #lang racket mean?

The first line of a file chooses the module language. #lang racket gives you the full Racket language: lists, hash tables, strings, regular expressions, for loops, and lots more. #lang racket/base gives you a smaller language that starts up faster and requires you to require extra pieces yourself. Everything after the #lang line is a module body: a sequence of definitions and expressions.

RACKET
#lang racket/base
(require racket/list racket/string)  ; pull in just what you need

(displayln (string-upcase "small and fast"))
(displayln (first '(1 2 3)))

For learning, #lang racket is the right choice. For libraries or tools you ship, racket/base plus explicit requires is common.

A function in your first file

A program with a name and a function is already a real program:

RACKET
#lang racket

(define (greet name)
  (string-append "Hello, " name "!"))

(displayln (greet "world"))
(displayln (greet "Racket"))

define with a parenthesized name and parameters defines a function. The body is a single expression; its value is the result. There is no return keyword: the last expression is the return value.

Define and call greet

Note the difference between the first two lines and the third: displayln shows the text as plain output, while the bare (greet "you") at the top level prints its value in quoted form, with double quotes, because that is how Racket shows a string value.

A typical project layout

For anything bigger than one file, put related .rkt files in a folder and use require with a relative path:

RACKET
;; main.rkt
#lang racket
(require "geometry.rkt")

(displayln (circle-area 2))
RACKET
;; geometry.rkt
#lang racket
(provide circle-area)

(define (circle-area r)
  (* pi r r))

Each file is a module. provide says what a module exports; require brings those names in. You will study this properly in the modules chapter.

Troubleshooting

  • "racket: command not found": the install folder is not on PATH; restart the terminal after changing PATH.
  • "module: no #lang line": every file you require or run needs #lang on its first line.
  • Unbalanced parentheses: DrRacket highlights the matching parenthesis and shows a red marker for a mismatch. Use Reindent (Tab) to see the structure.
  • Windows Defender slows the first run: compile with raco make to avoid compiling every time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What file extension do Racket programs usually have?

    .rkt is the Racket convention. .scm is the common extension for other Scheme systems.
  2. 2. What does racket -i hello.rkt do?

    -i runs the file and then drops you into an interactive prompt where its definitions are available.
  3. 3. A module contains (+ 40 2) as a top-level expression. What happens when you run it?

    A module prints the value of every top-level expression that does not return void.
  4. 4. What is the main difference between #lang racket and #lang racket/base?

    racket/base is a lean subset: you require extra libraries yourself.

Summary

  • Install Racket (it includes DrRacket and raco), then run files with racket file.rkt.
  • A module starts with #lang racket; top-level expressions print their values.
  • define with a parenthesized header defines a function; the last expression is its result.
  • Use raco make to compile and raco test to run tests.
  • Files are modules: provide exports names and require imports them.

Next: The REPL and Developer Tools