|
| 1 | +--- |
| 2 | +layout: book |
| 3 | +title: Installing GFortran |
| 4 | +permalink: /learn/os_setup/install_gfortran |
| 5 | +--- |
| 6 | + |
| 7 | +GFortran is the name of the [GNU Fortran project](https://gcc.gnu.org/fortran/). The main [wiki](https://gcc.gnu.org/wiki/GFortran) page offers many helpful links about GFortran, as well as Fortran in general. In this guide, the installation process for GFortran on Windows, Linux, and macOS is presented in a beginner-friendly format based on the information from [GFortranBinaries](https://gcc.gnu.org/wiki/GFortranBinaries). |
| 8 | + |
| 9 | +## Windows |
| 10 | + |
| 11 | + |
| 12 | +Three sources provide quick and easy way to install GFortran compiler on Windows: |
| 13 | +1. [http://www.equation.com](http://www.equation.com/servlet/equation.cmd?fa=fortran), provides 32 and 64-bit x86 |
| 14 | + executables of the latest (10.2) gcc-version. |
| 15 | +2. [TDM GCC](https://jmeubank.github.io/tdm-gcc/articles/2020-03/9.2.0-release), provides 32 and 64-bit x86 executables of the 9.2 gcc-version. |
| 16 | +3. [MinGW-w64](http://mingw-w64.org/doku.php/download/mingw-builds) provides a 64-bit x86 executable of the 8.1.0 gcc-version. |
| 17 | + |
| 18 | +In all the above choices, the process is straightforward—just download the installer and follow the installation wizard. |
| 19 | + |
| 20 | +### Unix-like development on Windows |
| 21 | +For those familiar with a unix-like development environment, several emulation options are available on Windows each of which provide packages for gfortran: |
| 22 | + |
| 23 | +* __Cygwin:__ A runtime environment that provides POSIX compatibility to Windows; |
| 24 | +* __MSYS2:__ A collection of Unix-like development tools, based on modern Cygwin and MinGW-w64; |
| 25 | +* __Windows Subsystem for Linux (WSL):__ An official compatibility layer for running Linux binary executables on Windows. |
| 26 | + |
| 27 | +All of the above approaches provide access to common shells such as bash and development tools including GNU coreutils, Make, CMake, autotools, git, grep, sed, awk, ssh, etc. |
| 28 | + |
| 29 | +We recommend the WSL environment for those looking for a Unix-like development environment on Windows. |
| 30 | + |
| 31 | +## Linux |
| 32 | + |
| 33 | + |
| 34 | +### Debian-based (Debian, Ubuntu, Mint, etc...) |
| 35 | +Check whether you have gfortran already installed |
| 36 | +```bash |
| 37 | +which gfortran |
| 38 | +``` |
| 39 | +If nothing is returned then gfortran is not installed. |
| 40 | +To install gfortran type: |
| 41 | +```bash |
| 42 | +sudo apt-get install gfortran |
| 43 | +``` |
| 44 | +to check what version was installed type: |
| 45 | +```bash |
| 46 | +gfortran --version |
| 47 | +``` |
| 48 | +You can install multiple versions up to version 9 by typing the version number immediately after "gfortran", e.g.: |
| 49 | +```bash |
| 50 | +sudo apt-get install gfortran-7 |
| 51 | +``` |
| 52 | +To install the latest version 10 you need first to add / update the following repository and then install: |
| 53 | +```bash |
| 54 | +sudo add-apt-repository ppa:ubuntu-toolchain-r/test |
| 55 | +sudo apt update |
| 56 | +sudo apt install gfortran-10 |
| 57 | +``` |
| 58 | +Finally, you can switch between different versions or set the default one with the **update-alternatives** ([see manpage](https://manpages.ubuntu.com/manpages/trusty/man8/update-alternatives.8.html#:~:text=update%2Dalternatives%20creates%2C%20removes%2C,system%20at%20the%20same%20time.)). There are many online tutorials on how to use this feature. A well structured one using as an example C and C++ can be found [here](https://linuxconfig.org/how-to-switch-between-multiple-gcc-and-g-compiler-versions-on-ubuntu-20-04-lts-focal-fossa), you can apply the same logic by replacing either `gcc` or `g++` with `gfortran`. |
| 59 | + |
| 60 | +### RPM-based (Red Hat Linux, CentOS, Fedora, openSuse, Mandrake Linux) |
| 61 | +```bash |
| 62 | +sudo yum install gcc-gfortran |
| 63 | +``` |
| 64 | + |
| 65 | +### Arch-based (Arch Linux, Antergos, Manjaro, etc...) |
| 66 | +```bash |
| 67 | +sudo pacman -S gcc-fortran |
| 68 | +``` |
| 69 | + |
| 70 | +## macOS |
| 71 | +### Xcode |
| 72 | +If you have Xcode installed, open a terminal window and type: |
| 73 | +```bash |
| 74 | +xcode-select --install |
| 75 | +``` |
| 76 | +### Binaries |
| 77 | +Go to [fxcoudert/gfortran-for-macOS](https://github.com/fxcoudert/gfortran-for-macOS/releases) to directly install binaries. |
| 78 | +### Homebrew |
| 79 | +```bash |
| 80 | +brew install gcc |
| 81 | +``` |
| 82 | +### Fink |
| 83 | +GNU-gcc Package [link](https://pdb.finkproject.org/pdb/browse.php?summary=GNU+Compiler+Collection+Version) |
| 84 | +### MacPorts |
| 85 | +Search for available gcc versions: |
| 86 | +```bash |
| 87 | +port search gcc |
| 88 | +``` |
| 89 | +Install a gcc version: |
| 90 | +```bash |
| 91 | +sudo port install gcc10 |
| 92 | +``` |
| 93 | +--- |
| 94 | +## OpenCoarrays |
| 95 | + |
| 96 | +[OpenCoarrays](http://www.opencoarrays.org/) is an open-source software project that produces an application binary interface (ABI) used by the GNU Compiler Collection (GCC) Fortran front-end to build executable programs that leverage the parallel programming features of Fortran 2018. Since OpenCoarrays is not a separate compiler, we include it here, under gfortran. |
| 97 | + |
| 98 | +While with gfortran you can compile perfectly valid code using coarrays, the generated binaries will only run in a single image (_image_ is a Fortran term for a parallel process), that is, in serial mode. OpenCoarrays allows running code in parallel on shared- and distributed-memory machines, similar to MPI: |
| 99 | +```bash |
| 100 | +cafrun -n <number_of_images> <executable_name> |
| 101 | +``` |
| 102 | + |
| 103 | +The process of installation is provided in a clear and comprehensive manner on the official site. |
| 104 | + |
| 105 | +We emphasize that native installation on Windows is not possible. It is only possible through WSL. |
0 commit comments