-
Notifications
You must be signed in to change notification settings - Fork 328
Installation
You can use CLASS primarily in two ways. Either through the an executable file or through the python wrapper. For most users the use through the python wrapper (classy
) will be preferred, this is what we discuss in the first part. However, some users might prefer the use of an executable file, this is what we discuss in the second part. Finally, in the third part we discuss the solutions to the most common installation issues. If you are working with Windows, please check out the notes at the very end!
You can get the python wrapper mainly in two ways:
1) Via the python package manager pip
This is the simplest way of installing class and recommended for new users who do not need to significantly modify the code. In this case, make sure you have pip installed and simply use the command pip install classy
.
This should not only download the code but also install it on your system. Please note that we currently only support UNIX systems (essentially MAC and linux). For Windows, please see the dedicated section below.
The source code and necessary files will be installed to where your python packages usually go.
You can download the source code either from the homepage class-code.net or from the github public repository. For the homepage, you will find a zipped folder containing all files necessary to install class yourself, see the installation instructions below. For the github public repository, simply click on the green button "clone or download". The download option will download the same zipped archive as from the website. The clone option will download all the extracted code into a folder on your computer using git. If you know git and want to check out the code as a git repository (download the extracted files), use
]$ git clone https://github.com/lesgourg/class_public.git class
In either case, after unzipping the archive or downloading via git, you will have the contents of the class folder in a directory of your choice. The directory will typically be called class/
, or class_public/
, or this name with a version number at the end. Whatever it is called, go there and just try to call pip install .
:
~]$ cd class/
class]$ pip install .
If you donwloaded CLASS via pip or via a manual download, you can check if it worked and installed correctly by opening a python interpreter and loading classy
:
class]$ python
>>> import classy
>>> cosmo = classy.Class()
>>> cosmo.set_baseline('planck2018')
>>> cosmo.compute()
>>> exit()
If this works, you are good to go and you will be able to use CLASS in all your python applications. The code should run quickly (the execution time depends on your machine, but the order of magnitude is one second).
If you want to install an executable for class, using it with .ini and .pre files, then you can follow the same steps as above for a manual download. However, after having the source code unzipped on your computer, instead of calling pip install .
, you can call
~]$ cd class/
class]$ make -j
The make
is set to create the class
executable, the library libclass.a
, and the Python wrapper classy
(For more details on the installation and use of the Python wrapper, please refer to the detailed explanation). Note that with the additional option make -j
you might be able to speed up this compilation considerably! If you don't want to install the python wrapper, you can also call just make class -j
, which only installs the command line executable and nothing else. It is possible that the compilation might return a few lines of warning. The important thing is that it should return no errors. You can test whether the compilation of the class
executable was successful by trying
class]$ ./class explanatory.ini
The code should run quickly and return a bit more than 20 lines of output. The first line should start with Running CLASS version ...
and the last line by Writing output files in ...
.
In case you are interested in old versions: you can find all the releases of CLASS (since the conversion to git) on the page https://github.com/lesgourg/class_public/releases, with a choice between browsing the source code (designated with the hash code of the version), and downloading a zip or a tar.gz archive. By clicking on the three dots ...
next to a release, you can see the commit message. Due to the internal structure of the branches used for CLASS development, you can see all commits that lead to this point.
The installation of CLASS always follows the steps of first installing the C code, and then the python wrapper around it. If the process fails, it will be crucial to identify where this happens.
If there is a problem with the pip install classy
command, then please check that you have a current version of python with numpy, scipy, and cython installed. The code will also need to execute the make
command to compile the underlying C code. If the automatic installation via pip fails, we recommend that you switch to the github installation, and check the individual stps below. This will allow you to see the individual steps (or the failures) more clearly.
If you have installed the code manually and the pip install .
gives you problems, you will have to hunt down where those problems occur. Luckily you now have the manual codebase and can check each individual step the installation is doing separately.
The first thing to check will be if there are issues with your C/C++ compilers. For this purpose, we follow the steps to create a command line executable. Explicitly, we try
~]$ cd class/
class]$ make class -j
If this command fails already, then ther is a problem with your C/C++ compiler setup.
Usually, the CLASS compilation fails when you use a C/C++ compiler that it too old, or not main stream.
-
Linux or Mac users: the compilation error message probably says that your computer does not understand the command
gcc
org++
. These are by far the most frequent issues. There are several ways to fix this.-
First check which version of C is installed on your computer. Type
gcc --version
in your terminal: -
If the terminal answers that you have
gcc
, version 4.6 or lower, you must update it with a more recent version (4.9, 5.0, 5.1, 5.2, etc.). -
If the terminal answers that you do not have
gcc
, you must download and install it -- unless you already have another C compiler which you would prfer to use, and which is called differently (e.g.icc
,pgcc
, etc.).
-
-
if you choose to download or update the GNU compiler
gcc
: proceed like for installing any other new package on your linux system. Once you are done, check that when you typegcc --version
you get indeed the number of the version you just installed. If you still get the old one, you might have to do something (create a symbolic link from/usr/bin/gcc
to the newgcc
executable, or change the order in your default path list) untilgcc --version
returns the correct version. Then do amake clean; make
, and at least the C code should compile normally. -
if you choose to use another recent C compiler (from Intel, Portland, etc.), there should be no problem, but first you might need to edit two lines in the file
Makefile
located in theclass/
directory. The lineCC = gcc
may need to be replaced with the name of another executable, e.g.CC = icc
orCC = pgcc
. You can proceed similarly for the lineCPP = g++ --std=c++11 -fpermissive -Wno-write-strings
(with e.g.CC = icpc
orCC = pgcpp
), but you will have to replace the arguments with the equivalent ones for your compiler. Then do amake clean; make
, and at least the C code should compile normally. -
Windows users: please see the very end of these instructions
-
Linux users, or Mac users with OS 10.8 or earlier: if you could compile the C code but not the Python wrapper, you probably have a too old version of Python (e.g. <2.7), or maybe you have some Python modules missing. You might have several versions of python installed on your computer (different versions from different sources). The compilation of the python wrapper will be done by default by the python version used when you type just the command
python
in a terminal. Try it. You will see the version number, and indications on the version of C used to compile it, e.g.:class]$ python Python 3.10.12 (main, Jan 17 2025, 14:35:34) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
It is also possible that you have the correct version of Python, but compiled with a C compiler incompatible with what you are now using for compiling CLASS. This is unusual on Linux and Mac <10.9, but it may happen, and you can get a hint of it by looking at the second line of output after typing
python
. If you are in that case, you should download a fresh version of Python, and let it compile with what is now your default C compiler.The other source of trouble is when you have a good python version but missing packages. Normally you will understand that you are in this situation by looking at the compilation error message: the computer will report
ImportError: No module named ...
Most frequently the missing modules arenumpy
and/orcython
. You can cross-check whether they are installed with a short session:class]$ python >>> import numpy >>> import cython >>> exit()
If some of these modules are missing, import them one after each other. The easiest is usually to type just:
class]$ pip install numpy class]$ pip install cython
If this does not work, use your browser to get some help on python modules installation. (Note: for using MontePython, a third module should be installed,
scipy
).With a correct Python version, compiled with a compatible C compiler, and the above 2 modules available, you should be able to compile everything with the command
make
. After installing new versions, compilers, etc., remember to typemake clean
beforemake
, to be sure that you are redoing the compilation from scratch. During the compilation of the Python wrapper, there are always several warnings in the compilation message. Don't be scared by them: as long as they are warnings and not errors, they are harmless. To be sure that everything compiled correctly, do the test mentioned at the top of this page:class]$ python >>> import classy >>> exit()
If this works, you are all set.
-
Windows users: please see the very end of these instructions
Please write us if you see a way to improve and expand this documentation, or if your experience contradicts what is written here. We have recently switched to a new compilation system, so please let us know if you have any issues with the compilation!
Scientific programming with Windows is generally a bit more difficult than with Unix systems. There are a few ways to do it, but each of them is relatively cumbersome:
-
Using WSL-2: Windows has provided some tools to 'emulate' a linux distribution on your Windows system. This Windows Subsystem for Linux (WSL) allows you to do everything that a linux user could do. If you want to go this route, install WSL (there are many awesome installation instructions online, for example this one or this other one, but we encourage you to explore your options!)
-
Using a Linux dual-boot system: This is similar to the above, but more extreme, installing a linux kernel manually on your machine alongside Windows. Make sure you know what this means before attempting it! Back up your important data, and only try if you are really sure.
-
Installing class manually. This might seem like the most convenient option, but there are many troubles you might encounter, and you are 'on your own' -- we do not make sure that our program is installable on Windows, so you will have to fix any issues yourself. Typically what you will want to do is to follow the same procedure as the makefile does, just manually and in Windows. For this you will need some compilation utility (like the VisualStudio compiler, or the windows-gcc equivalent MinGW). Then, for each file in the source, tools, and main directories, install it using
COMPILER INCLUDES -c FILE.c -o FILE.o
whereCOMPILER
is your compiler,INCLUDES
are the individual includes, i.e. all files in theinclude
directories, and all relevant files from theexternal
subdirectories (like recfast or hyrec, check theMakefile
). Some files need to be compiled with a C++ compiler, some with a C compiler. They are marked by ending either with .c or with .cpp. Finally, you will unite all of your .o files usingg++ ALL-.O-FILES -o class.exe
, which should then be the executable. If you instead want to install the python wrapper, you will have to also follow all the steps of thesetup.py
individually. You will probably first have to create a shared object file (.dll or .lib in Windows), and then tell python/cython to link to that. GOOD LUCK!