Skip to content

Instantly share code, notes, and snippets.

@chriswayg
Last active July 17, 2020 06:43
Show Gist options
  • Save chriswayg/4bcc14a1dfd5e5479d604a06cd620b0b to your computer and use it in GitHub Desktop.
Save chriswayg/4bcc14a1dfd5e5479d604a06cd620b0b to your computer and use it in GitHub Desktop.
Install conda Python on macOS in 30 seconds - Oneliner that downloads, installs and activates Miniconda Python 2.7 or 3.x

Install Python in 30 seconds

Oneliner that downloads, installs and activates Python 2.7 or 3.x in less than a minute

  • at 50 Mbps on a modern computer, the 50 MB download including install takes about 30 seconds ;-)

Quick Miniconda install

Python 2.7
v=2; f=Miniconda${v}-latest-MacOSX-x86_64.sh; cd $TMPDIR; { curl -LfOsS https://repo.anaconda.com/miniconda/$f ; cd -; } && bash $TMPDIR/$f -b && echo ". ~/miniconda${v}/etc/profile.d/conda.sh" >> ~/.bash_profile; . ~/miniconda${v}/bin/activate
Python 3.x
v=3; f=Miniconda${v}-latest-MacOSX-x86_64.sh; cd $TMPDIR; { curl -LfOsS https://repo.anaconda.com/miniconda/$f ; cd -; } && bash $TMPDIR/$f -b && echo ". ~/miniconda${v}/etc/profile.d/conda.sh" >> ~/.bash_profile; . ~/miniconda${v}/bin/activate
Remove in 5 seconds
conda deactivate; rm -rf /Users/christian/miniconda{2,3}; sed -i.bak '/. ~\/miniconda*/d' ~/.bash_profile

The Oneliner with detailed comments

# Python version 2 or 3
v=3;

# Download latest miniconda 64 bit
f=Miniconda${v}-latest-MacOSX-x86_64.sh;

# Use a temporary dir such as: /var/folders/9l/wwt.../T/
cd $TMPDIR;

# Use Curl as macOS does not come with wget
# Options: curl --location --fail --remote-name --silent --show-error
# switch back to previous dir
{ curl -LfOsS https://repo.anaconda.com/miniconda/$f ; cd -; } &&

# Use the miniconda installer to install in silent mode
bash $TMPDIR/$f -b &&

# Add miniconda to the PATH, while not enabling auto-activation in every shell
echo ". ~/miniconda${v}/etc/profile.d/conda.sh" >> ~/.bash_profile;

# Activate a virtual environment in the current shell
. ~/miniconda${v}/bin/activate

  • To activate manually in another shell use ~/miniconda3/bin/activate
  • To create another virtual environment and activate / list / deactivate
conda create --name project1
conda activate project1
conda info --envs
...
conda deactivate

Regualar Miniconda install

The only difference is, that this will add a much bigger section to the .bash_profile, which by default auto-activates the base environment in every shell, unless you disable that, as done below.

Python 2.7
v=2; f=Miniconda${v}-latest-MacOSX-x86_64.sh; cd $TMPDIR; { curl -LfOsS https://repo.anaconda.com/miniconda/$f ; cd -; } && bash $TMPDIR/$f -b && . ~/miniconda${v}/bin/activate && conda init && conda config --set auto_activate_base false
Python 3.x
v=3; f=Miniconda${v}-latest-MacOSX-x86_64.sh; cd $TMPDIR; { curl -LfOsS https://repo.anaconda.com/miniconda/$f ; cd -; } && bash $TMPDIR/$f -b && . ~/miniconda${v}/bin/activate && conda init && conda config --set auto_activate_base false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment