# Building from source OR-Tools .Net on Linux

## Introduction

This guide explains how to build from source OR-Tools, with support for
.Net, on Linux.

Unless you plan to modify the source code or use a third-party solver with
OR-Tools, we recommend the [package installation](https://developers.google.com/optimization/install/dotnet/binary_linux).

Although these instructions might also work on other Linux variants, we
have only tested them on machines meeting the following requirements:


- Alpine Edge 64-bit (x86_64)
- Centos 7 LTS 64-bit (x86_64)
- Debian SID 64-bit (x86_64)
- Debian 11 (bullseye) 64-bit (x86_64)
- Fedora 38 64-bit (x86_64)
- Fedora 37 64-bit (x86_64)
- OpenSuse Leap 64-bit (x86_64)
- Ubuntu 24.10 64-bit (x86_64)
- Ubuntu 22.04 LTS 64-bit (x86_64)
- Ubuntu 20.04 LTS 64-bit (x86_64)

<br />

## Prerequisites

The following sections describe the prerequisites for installing OR-Tools.

### C++ tools

To install C++ tools, open a terminal window and enter:

### Alpine

    apk add alpine-sdk linux-headers cmake lsb-release-minimal

### Centos

    sudo yum groupinstall -y 'Development Tools'
    sudo yum install -y pkgconfig redhat-lsb-core

### Debian

    sudo apt update
    sudo apt install -y build-essential cmake lsb-release

### Fedora

    sudo dnf groupinstall -y 'Development Tools'
    sudo dnf install -y gcc-c++ cmake redhat-lsb-core

### Fedora

    sudo dnf groupinstall -y 'Development Tools'
    sudo dnf install -y gcc-c++ cmake redhat-lsb-core

### OpenSUSE

    sudo zypper refresh
    sudo zypper install -y git gcc11 gcc11-c++ lsb-release

### Ubuntu

    sudo apt update
    sudo apt install -y build-essential cmake lsb-release

### Ubuntu

    sudo apt update
    sudo apt install -y build-essential cmake lsb-release

### Ubuntu

    sudo apt update
    sudo apt install -y build-essential cmake lsb-release

### SWIG

To install SWIG, open a terminal window and enter:

### Alpine

    apk add swig

### Centos

    sudo yum install -y swig

### Debian

    sudo apt install -y swig

### Fedora

    sudo dnf install -y swig

### Fedora

    sudo dnf install -y swig

### OpenSUSE

    sudo zypper install -y swig

### Ubuntu

    sudo apt install -y swig

### Ubuntu

    sudo apt install -y swig

### Ubuntu

    sudo apt install -y swig

### .Net Core SDK \>= 3.1

Next, install the .Net Core SDK version 3.1 or higher. To do so,
open a terminal window and enter:

### Alpine

    apk add dotnet6-sdk
        ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-alpine

### Centos

    sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
    sudo yum install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-centos#centos-7

### Debian

Before you install .NET, run the following commands to add the Microsoft
package signing key to your list of trusted keys and add the package
repository.

    wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
    dpkg -i packages-microsoft-prod.deb
    rm packages-microsoft-prod.deb

    sudo apt update
    sudo apt install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-debian

### Fedora

    sudo dnf install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-fedora

### Fedora

    sudo dnf install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-fedora

### OpenSUSE

Before you install .NET, run the following commands to add the Microsoft
package signing key to your list of trusted keys and add the package
repository.

    sudo zypper install libicu
    sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
    wget https://packages.microsoft.com/config/opensuse/15/prod.repo
    sudo mv prod.repo /etc/zypp/repos.d/microsoft-prod.repo
    sudo chown root:root /etc/zypp/repos.d/microsoft-prod.repo

Then you can install the .Net SDK using the following command:

    sudo zypper install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-opensuse

### Ubuntu

    sudo apt install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu

### Ubuntu

    sudo apt install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu

### Ubuntu

    sudo apt install -y dotnet-sdk-6.0

ref: https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu

## Download the source code

There are two distinct branches of the OR-Tools source code on
[GitHub](https://github.com/google/or-tools): `stable` and `main`.

The `stable` branch has been thoroughly tested and should work flawlessly on all
supported platforms.  

The `main` branch is where the latest updates and
improvements have been applied; it's more current, but less stable.

### Download the stable source code

You can get the stable source code for OR-Tools in either of the following ways:

- Clone the `stable` branch by entering:

      git clone https://github.com/google/or-tools

- Download the latest release in a compressed file, by clicking the
  `Clone or download` button in [GitHub](https://github.com/google/or-tools).
  ![image of clone or download button](https://developers.google.com/optimization/images/clone-or-download.png)

> [!WARNING]
> **Warning:** The full path to the installation directory (where you extract the files) must not contain any spaces.

### Download the main source code

To retrieve the source code from the `main` branch, enter:

    git clone -b main https://github.com/google/or-tools

### Download previous releases

You can get the source code for previous releases in either of the following ways:

- Download a previous release from the [GitHub release page](https://github.com/google/or-tools/releases).
- Assuming you have already created a local repository (by `git clone`),
  you can check out a specific release using a Git
  [tag](https://github.com/google/or-tools/tags).
  For example, to work with the v9.12 release instead of the
  `main` branch, enter the following commands in your local repo:

      git fetch --all --tags --prune
      git checkout tags/v9.12 -b v9.12

## Configure the build

Before building OR-Tools, you'll need to configure the CMake build system
generator.

Open a terminal and navigate to the directory where you extracted the files.
Then enter:


    cmake -S . -B build -DBUILD_DEPS=ON -DBUILD_DOTNET=ON

<br />

> [!TIP]
> **Tip:** For a complete list of available OR-Tools CMake options, please take a look [here](https://github.com/google/or-tools/tree/stable/cmake#readme)

Checkout the
[CMake documentation](https://cmake.org/cmake/help/latest/manual/cmake.1.html#generate-a-project-buildsystem)
for details.

### Using SCIP

Since v7.8, SCIP is now integrated so you won't have to install it manually.

> [!NOTE]
> **Note:** You can disable the support of SCIP solver by using `-DUSE_SCIP=OFF`

> [!WARNING]
> **Warning:** While OR-Tools ships with SCIP, please consult the [SCIP license](https://scipopt.org/index.php#license) to ensure that you are complying with it.

### Using Gurobi

Gurobi is now pre-integrated. When needed, at runtime, OR-Tools will search for
the Gurobi shared library in the default install path of the Gurobi installers
on MAC OS X and Windows, or by using the `GUROBI_HOME` environment variable.

> [!NOTE]
> **Note:** You can disable the support of Gurobi solver by using `-DUSE_GUROBI=OFF`

### Using FICO Xpress

FICO Xpress is now pre-integrated. When needed, at runtime, OR-Tools will search
for the Xpress shared library in the default install path of the Xpress
installers, or by using the `XPRESSDIR` environment variable.

> [!NOTE]
> **Note:** You can disable the support of Xpress solver by using `-DUSE_XPRESS=OFF`

### Using an optional third-party MIP solver

You can also use OR-Tools with any of the following optional third-party MIP
solvers whose support is disabled by default:

- [CPLEX](https://www.ibm.com/analytics/cplex-optimizer)
- [GLPK](https://www.gnu.org/software/glpk/) (Linux and MacOS only)

Please take a look at
[this documentation](https://github.com/google/or-tools/tree/main/cmake#enabling-cplex-support)
for details.

## Build the source code

To build the source code, open a terminal and navigate to the directory where
you extracted the files. Then enter the following command to compile OR-Tools:


    cmake --build build --config Release --target all -j -v

<br />

Checkout the
[CMake documentation](https://cmake.org/cmake/help/latest/manual/cmake.1.html#build-a-project)
for details.

## Test the source code

You can check that everything is running correctly by entering:


    cmake --build build --config Release --target test -v

<br />

This runs examples for OR-Tools. If all the examples run
successfully, you are ready to
[get started with OR-Tools](https://developers.google.com/optimization/introduction/dotnet).

## Cleaning the build files

If you need to re-install OR-Tools, the command:


    rm -r build

<br />

will remove all compiled dependencies.
This can be useful for resetting to a clean state.

Then re-enter the commands:


    cmake -S . -B build -DBUILD_DEPS=ON -DBUILD_DOTNET=ON

<br />


    cmake --build build --config Release --target all -j -v

<br />

## Installing OR-Tools on your operating system

You can install OR-Tools for .Net on your operating system by entering:


    cmake --build build --config Release --target install -v

<br />

> [!TIP]
> **Tip:** To change the default install location please refer to the [`CMAKE_INSTALL_PREFIX`](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html) documentation.