If you started studying programming or became interested in it in recent years, you have probably heard of data science or machine learning. And you may also have heard of, or worked with, the R language, for its great power in statistical analysis, or Python, for its great versatility. Today’s article is about Anaconda, a distribution that aims to make it easier to install and manage tools for these two languages, especially for scientific applications.
If you prefer video, click the player below (in Portuguese). The full article follows the video.
Getting the installer #
The Anaconda project website is simple and intuitive. Here we will cover
installing the Individual Edition of Anaconda, which can be downloaded
here. In this article, we will
install the Python 3 installer for Linux systems. At the time of writing, the
name of the downloaded installer file is Anaconda3-2020.07-Linux-x86_64.sh.
Installing the dependencies required for the GUI #
Our goal here is to install Anaconda on a Linux distribution, more specifically Linux Mint 20. According to the instructions on the website, some dependencies are needed for the applications to run in graphical mode. For Debian-based distributions, as in our case, we need to install:
sudo apt install libgl1-mesa-glx libegl1-mesa libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6Verifying the integrity of the installer #
It is always a good idea to check that the download of an installer completed successfully, to avoid starting the installation with corrupted files that can break system dependencies. If you have never heard of checksums and cryptographic hashes, I recommend this reading.
The project website has a list
of the hashes of all of its installer files. At the time of writing, the hash of
the Python 3 installer for Linux is
38ce717758b95b3bd0b1797cc6ccfb76f29a90c25bdfa50ee45f11e583edfdbf, but you
should check the website, since a new version may be available by the time you
read this article.
To check whether the downloaded file is corrupted, we can either compare the
hash of the downloaded file with the expected hash by eye, or have the system do
the comparison. For the visual check, we run the sha256sum command followed by
the file name and compare the result with the hash on the website:
$ sha256sum Anaconda3-2020.07-Linux-x86_64.shTo have the system compare the two hashes:
$ echo "38ce717758b95b3bd0b1797cc6ccfb76f29a90c25bdfa50ee45f11e583edfdbf Anaconda3-2020.07-Linux-x86_64.sh" | sha256sum --checkIf the output of the command is OK, the hash of the file matches the given
hash.
Creating an installation folder accessible to all users #
By default, the Anaconda installer uses the user’s home folder
(~/user-name). There is nothing wrong with that. However, if the machine is
used by more than one user, it is sometimes better to have a single installation
that serves all of them than to install it in the home folder of each user. So,
before starting the installation, we will create a folder accessible to all
users of the system, so that we can point the installer to it. Even if your
machine is currently used only by you, I recommend keeping the installation in a
system folder so that it can accommodate multiple users in the future.
On Linux, there is a standard hierarchy of system
files (FHS, for
Filesystem Hierarchy Standard) that recommends which folders to use in each
situation. According to this standard, the opt folder is meant for programs
that are not part of the operating system. So, we will create a folder for the
installation inside opt. It is also important that this folder has the
correct
permissions
so that the installer can write to it. So, creating an anaconda folder in
opt:
sudo mkdir /opt/anaconda && sudo chmod ugo+w /opt/anacondaInstallation #
Now we can perform the installation itself. Assuming you are in the folder where the installer was downloaded:
bash Anaconda3-2020.07-Linux-x86_64.sh -uThe -u option is requested by the installer when the installation is done in
an existing folder. Just follow the on-screen instructions, accept the license
terms, and change the installation folder to the one we created.
It is also important that, at the end of the installation, you confirm the
option for the installer to initialize the conda manager. This way, the
installer will update the .bashrc file so that you can start the conda
manager directly from the terminal.
Verifying that the installation worked #
After the installation finishes, close and reopen your terminal. If the
installation succeeded, the (base) indicator will appear before the prompt:
(base) $This indicates that the conda base environment was activated at startup
(the default behavior). You can check which Python interpreter is being used
with:
(base) $ which python
/opt/anaconda/bin/pythonTo deactivate the environment:
(base) $ conda deactivate
$Now, the active Python interpreter is the system’s:
$ which python
/usr/bin/pythonMaking the installation accessible to all users #
Earlier, we created a system-level folder so that all users could access it. Now
we need to let other users actually use the Anaconda installation in that
folder. First, let’s create a user group called anaconda:
$ sudo groupadd anacondaNow, let’s transfer ownership of the installation folder to this group:
$ sudo chgrp -R anaconda /opt/anacondaLet’s allow only the owner, root, and the group to have read and write permission:
$ sudo chmod 770 -R /opt/anacondaNow, add whichever users you want to this anaconda group so that they can use
the installation:
$ sudo adduser user_name anacondaIn case you don’t remember, at the end of the installation we allowed the
installer to initialize conda. So, the installer made changes to the
.bashrc file of the user who ran the installation so that conda is
initialized every time the terminal is opened. We need to apply these changes to
all users. The simplest way is to put the instructions in the /etc/profile
file:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/anaconda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/anaconda/etc/profile.d/conda.sh" ]; then
. "/opt/anaconda/etc/profile.d/conda.sh"
else
export PATH="/opt/anaconda/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<NOTE: It is important that you copy exactly what was added to your .bashrc
file. Here I simply copied what was added to mine; you should copy what was
added to yours.
The first time a new user opens the shell, they will need to run the conda init command and close it. That’s it: they will be able to use the
installation normally the next times they open the shell, without needing that
command.
Conclusion #
The Anaconda installation itself is simple, but some details need to be covered
for a multi-user installation. We covered those details in this article to help
those who need it. We also have a more detailed article on virtual environments
where we write about conda and its features. Don’t miss that
article.
If you have questions, leave a comment. Share the article with your friends and see you in the next article.