Skip to content

INSTALL

Aaron Tomb edited this page Feb 28, 2023 · 98 revisions

This page has instructions for installing Dafny:

Using an IDE

The easiest way to get started with Dafny is using the Dafny IDEs, since they continuously run the verifier in the background. Be sure to check out the Dafny tutorial!

For most users, we recommend using Dafny with VS Code, which has an easy installation, as explained next:

Visual Studio Code

  1. Install Visual Studio Code
  2. If you are on a Mac or Linux, install .NET 6.0, as described under those platforms below.
  3. In Visual Studio Code, press Ctrl+P (or ⌘P on a Mac), and enter ext install dafny-lang.ide-vscode.
  4. If you open a .dfy file, Dafny VSCode will offer to download and install the latest Dafny. You can also browse extensions: vs-code-dafny-2 0 1-install

Emacs

The README at https://github.com/boogie-org/boogie-friends has plenty of information on how to set-up Emacs to work with Dafny. In short, it boils down to running [M-x package-install RET boogie-friends RET] and adding the following to your .emacs:

(setq flycheck-dafny-executable "[path to dafny]/dafny/Scripts/dafny")

Do look at the README, though! It's full of useful tips.

Install the binaries

The following instructions tell you how to install Dafny so that you can run it from various operating systems or as a command-line tool.

If you wish to compile to target languages other than .NET, see the instructions in a subsequent section to install the correct dependencies for the desired language.

Windows (Binary)

To install Dafny on your own machine,

  • Install (if not already present) .NET Core 6.0: https://dotnet.microsoft.com/download/dotnet/6.0
  • download the windows zip file from the releases page and save it to your disk.
  • Then, before you open or unzip it, right-click on it and select Properties; at the bottom of the dialog, click the Unblock button and then the OK button.
  • Now, open the zip file and copy its contents into a directory on your machine. (You can now delete the zip file.)

Then:

  • To run Dafny from the command line, simply run Dafny.exe.

Linux (Binary)

To install a binary installation of dafny on Linux (e.g., Ubuntu), do the following:

  • Install .NET 6.0. See: https://docs.microsoft.com/dotnet/core/install/linux or sudo apt install dotnet-sdk-6.0
  • Install the Linux binary version of Dafny, from https://github.com/dafny-lang/dafny/releases/latest
  • Unzip the downloaded file in a (empty) location of your choice

Within the unzipped Linux distribution, the dafny executable is dafny/dafny

If you intend to use the Dafny compiler, install the appropriate tools as described here

After the compiler dependencies are installed, you can run a quick test of the installation by running the script $INSTALL/dafny/quicktest.sh

Mac (Binary)

To install a binary installation of dafny on Mac OS, do the following:

  • Install .NET 6.0, either from Homebrew or the official MacOS installer.
    • For Homebrew:
      • brew install dotnet@6
      • export PATH="/usr/local/opt/dotnet@6/bin:$PATH" for Intel machines, or export PATH="/opt/homebrew/opt/dotnet@6/bin:$PATH" for ARM machines.
      • export DOTNET_ROOT="/usr/local/opt/dotnet@6/libexec" for Intel machines, or export DOTNET_ROOT="/opt/homebrew/opt/dotnet@6/libexec" for ARM machines.
    • For the official installer:
  • Install the Mac binary version of Dafny, from https://github.com/dafny-lang/dafny/releases/latest
  • Unzip the downloaded file in a (empty) location of your choice ($INSTALL)
  • cd into the installation directory ($INSTALL/dafny) and run xattr -d com.apple.quarantine *.dylib dafny DafnyServer *.dll z3/bin/z3 or, more easily, run the script $INSTALL/dafny/allow_on_mac.sh

Alternatively, you can use Homebrew to install Dafny: brew install dafny though this version can lag the release on the Dafny github releases page

Within the unzipped Mac distribution, the dafny executable is dafny/dafny

If you intend to use the Dafny compiler, you'll need a few extra dependencies:

  • For Javascript, you'll need Node and the biginteger.js package from NPM: brew install node && npm install bignumber.js
  • For Go, you'll need the Go compiler: brew install go
  • For C#, the .NET SDK described above is sufficient
  • For Java and C++, the standard macOS command-line tools for Xcode are sufficient
  • For Python, the standard Python installation is sufficient

After the compiler dependencies are installed, you can run a quick test of the installation by running the script $INSTALL/dafny/quicktest.sh.

NuGet (Binary)

The .NET NuGet repository collects .NET libraries and tools for easy installation. Dafny is available on NuGet, and can be installed as follows:

  • Install .NET 6.0 as described for your platform in one of the subsections above.
  • Install a binary version of Z3 4.8.5 for your platform from its GitHub releases page and put the z3 executable in your PATH.
  • Install Dafny using dotnet tool install --global dafny (or leave out the --global to use with dotnet tool run from the source directory of a .NET project).

Building (and developing) from source code

If you want access to the very latest Dafny developments or you want to extend Dafny yourself, install the source code, as explained here.

Note that the Dafny integration tests are mainly written using the syntax defined by the LLVM Integrated Tester (LIT) tool, but are actually run using a custom xUnit adaptor in the IntegrationTests package. The configuration files for LIT are still temporarily present in the Test directory and therefore LIT is still likely to work, but its use is deprecated and will not be supported in the future. See here for more details.

Windows (Source)

First, install the following external dependencies:

To install pre-commit checks to verify the style of C# files, run:

pip install pre-commit
pre-commit install

If you had any pre-commit scripts before, follow these steps to keep them.

Second, clone the Dafny source code.

git clone https://github.com/dafny-lang/dafny --recurse-submodules

Third, build the Dafny project, either on the command line: dotnet build dafny\Source\Dafny.sln or open Visual Studio, and open dafny/Source/Dafny.sln, and click on Build > Build solution while the project Dafny Runtime is still the startup project (in bold)

To run Dafny on a particular file, or to run the tests, you'll have to install a prover such as CVC4 or z3. For z3, do the following:

  1. Go to the release of version 4.8.5, download the zip according to your system
  2. Unzip the content of the Z3 folder into dafny\Binaries\z3 so that dafny\Binaries\z3\bin\z3.exe exists.
  3. (optional, depending on configuration) Make sure the dafny\Binaries\z3\bin folder is accessible from the command line by updating the PATH environment variable.

To run Dafny on a particular file:

  • Right-click the project DafnyDriver, and set as startup project
  • Click Debug > DafnyDriver Debug properties...
  • In the text area "Application arguments", write the path to the file you would like to run Dafny on, e.g. "..\..\Test.dfy" with the double quotes, if the file Test.dfy is next to the dafny folder, and save
  • Now, press F5 to compile Dafny and run it on the specified file.

To run the integration tests, go to dafny/Source/IntegrationTests and run dotnet test -v:n. Make sure you install all the dependencies to compile Dafny to other languages and run them.

Linux (Source)

  1. Dependencies:

    • Install .NET 6.0 as described above
    • Install python3: e.g., sudo apt install python3 python3-pip
    • If you intend to compile to Java, install Java and gradle
  2. Download and build Dafny:

    git clone https://github.com/dafny-lang/dafny.git --recurse-submodules
    cd dafny
    make exe
    

To install pre-commit checks to verify the style of the source code, perform

   pip3 install pre-commit
   pre-commit install

If you had any pre-commit scripts before, follow these steps to keep them.

  1. Install Z3 version 4.8.5 (not the most recent version of Z3)

    `make z3-ubuntu`
    

    OR

    cd dafny/Binaries
    wget https://github.com/Z3Prover/z3/releases/download/Z3-4.8.5/z3-4.8.5-x64-ubuntu-16.04.zip
    unzip z3-4.8.5-x64-ubuntu-16.04.zip
    mv z3-4.8.5-x64-ubuntu-16.04 z3
    
  2. Install the compiler dependences as described here.

  3. Run Dafny using the dafny shell script in the Scripts directory: dafny/Scripts/dafny You can run a quick test of the installation by executing dafny/Scripts/quicktest.sh

  4. To execute the integration tests: cd dafny/Source/IntegrationTests; dotnet test -v:n. Make sure you install all the dependencies to compile Dafny to other languages and run them. The tests take a while, depending on your machine, but emit progress output.

Mac (Source)

  1. Dependencies:

    • Install .NET (6.0): brew install dotnet-sdk
    • [python3 and pip3 are needed but they are likely already part of the Mac installation]
    • (Optional) Install java (possibly use brew install openjdk@8)
    • (Optional) Install gradle (possibly use brew install gradle)
  2. Download and build Dafny:

    git clone https://github.com/dafny-lang/dafny.git --recurse-submodules
    cd dafny
    make exe
    

To install pre-commit checks to verify the style of the source code, perform

   brew install pre-commit
   pre-commit install

If you had any pre-commit scripts before, follow these steps to keep them.

  1. Download and unpack Z3 (Dafny looks for z3 in Binaries/z3/bin/) version 4.8.5 (note, this is not the latest version of Z3). You can use

    brew install wget
    make z3-mac
    

or

   cd dafny/Binaries
   wget https://github.com/Z3Prover/z3/releases/download/Z3-4.8.5/z3-4.8.5-x64-osx-10.14.2.zip
   unzip z3-4.8.5-x64-osx-10.14.2.zip
   mv z3-4.8.5-x64-osx-10.14.2 z3
  1. Install the compiler dependences as described here.

  2. Run Dafny using the dafny shell script in the Scripts directory: dafny/Scripts/dafny You can run a quick test of the installation by executing dafny/Scripts/quicktest.sh

  3. To execute the integration tests: cd dafny/Source/IntegrationTests; dotnet test -v:n. Make sure you install all the dependencies to compile Dafny to other languages and run them. The tests take a while, depending on your machine, but emit progress output.

Keeping existing pre-commit hooks when building from source

If you install pre-commit, it will rename any existing pre-commit hook (./git/hooks/pre-commit) to a file namepre-commit.legacy, and put a python script named pre-commit instead. To keep any previous pre-commit hooks along with the new hook:

  • Rename pre-commit to pre-commit-python
  • Make pre-commit-python executable
  • Rename pre-commit.legacy to pre-commit
  • Add the line ./.git/hooks/pre-commit-python to the file pre-commit.

Compiling Dafny

Building the reference manual

The reference manual is only buildable on Linux or Mac and requires some tools to be installed

On Mac:

  • brew install --cask basictex
  • brew install pandoc
  • eval "$(/usr/libexec/path_helper)"
  • sudo tlmgr update --self
  • sudo tlmgr install framed tcolorbox environ trimspaces

Then, in the top-level directory of the source distribution: make refman

Build against a custom Boogie

Dafny is tightly integrated with Boogie. Sometimes we want to make Boogie changes and test those changes against Dafny. To do that, you must replace the Boogie NuGet package reference with project references to a locally checked out Boogie. We've added the git patch customBoogie.patch to the Dafny repository to make this easier. The patch requires that a Boogie repository with directory boogie is a sibling of the Dafny repository.

Steps to use:

  • git clone git@github.com:dafny-lang/dafny.git --recurse-submodules
  • cd dafny
  • git clone git@github.com:boogie-org/boogie.git
  • git apply customBoogie.patch
  • dotnet build Source/Dafny.sln

You can then use git checkout Source/Dafny.sln Source/Directory.Build.props to return to the version of Boogie that Dafny regularly builds with.

Alternatively, you can build a local checkout of Boogie separately and copy the binaries into the Dafny Binaries directory with the Scripts/use-local-boogie.sh script:

  • ln -s <your checkout of Boogie> Source/boogie (or clone it there directly)
  • dotnet build Source/Dafny.sln
  • Scripts/use-local-boogie.sh

This can be useful for certain debugging scenarios in which you want to temporarily use a custom Boogie version, because cleaning and rebuilding automatically reverts to the standard version, and because Boogie can then easily be built with different compilation flags than Dafny.