33

I am trying to create a conda environmet in google colab notebook. I succesfully installed conda with the following comannd

!wget -c https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh

!chmod +x Anaconda3-5.1.0-Linux-x86_64.sh

!bash ./Anaconda3-5.1.0-Linux-x86_64.sh -b -f -p /usr/local

Default python which is using by system is now Python 3.6.4 :: Anaconda, Inc.

I am trying to create an environment in conda by conda env create -f environment.yml

Every package got successfully installed but the problem now is that I am not able to activate this environment. I tried source activate myenv. but it also didn't worked.

After conda env list command I got two environments

base * /usr/local

myenv /usr/local/envs/myenv

Can anyone please help me how can I switch on to "myenv" environment? Any help will be very much appreciated.

Thanks In advance.

bryant1410
  • 5,540
  • 4
  • 39
  • 40
aryan
  • 379
  • 1
  • 3
  • 6
  • I don't think the running instance can normally update online to use the environment, you'd typically start Jupyter Notebook after switching to an environment when working locally at least. I guess the question generalizes to "can you use conda environment export files in Google Colab" – matanster Nov 22 '18 at 13:39
  • 2
    Hello @aryan. Did you figure this out? I'm having a similar issue trying to run rlgarage in google colab – Shadi Jan 23 '19 at 04:05
  • 2
    @matanster Thankyou for your reply. Yeah, we can do it locally but I think google collab doesn't allow to switch to any other conda environment. – aryan Jan 24 '19 at 05:23
  • 1
    @shadi, I couldnt able to create a conda enviornment, rather I installed all my required libraries using pip. – aryan Jan 24 '19 at 05:25
  • 4
    Any update on this, I'd like to create a conda env on Google collab – BND Apr 27 '19 at 15:22
  • 2
    I am trying to get this working also. I can get !source activate myenv to run, but it only runs while the cell is processing the ! command. `!conda create --name test37 python=3.7` then `!source activate test37 && conda list !conda list` Once the ! command is finished, the next command reverts back to the original env. I suspect the ! command opens a temporary shell, runs the command, then closes... – Donald S Jun 12 '20 at 07:02
  • 1
    Posted a more complete answer here: https://datascience.stackexchange.com/questions/75948/how-to-setup-and-run-conda-on-google-colab/75979#75979 – Donald S Jun 14 '20 at 12:56
  • Nice tutorial: https://towardsdatascience.com/conda-google-colab-75f7c867a522 – Javier TG May 24 '21 at 20:45

4 Answers4

16

You can activate and run a new conda environment in Google Colab by using the magic %%bash command:

%%bash
source activate myenv

python
import sys
# some simple python commands
sys.path.append('/usr/local/lib/python3.6/site-packages')
print(sys.path)

print("Python version")
print(sys.version)

Also including a few other commands I needed to run to get my environment setup completely:

!conda update conda -y -q
!source /usr/local/etc/profile.d/conda.sh
!conda init 
!conda install -n root _license -y -q
Donald S
  • 1,583
  • 1
  • 10
  • 26
  • 2
    Did you run the 'other' commands before or after the bash script? Also, do they need to be run in the created environment, or on the default one? – EmmanuelB Oct 29 '20 at 20:13
  • 2
    @EmmanuelB, I ran the 4 other commands first, from the default environment – Donald S Oct 30 '20 at 07:28
6

I installed conda package in /usr/local and work fine

!wget -c https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh

!chmod +x Anaconda3-5.1.0-Linux-x86_64.sh

!bash ./Anaconda3-5.1.0-Linux-x86_64.sh -b -f -p /usr/local

sys.path.append('/usr/local/lib/python3.6/site-packages')

then you can install any package you want

!conda install -q -y --prefix /usr/local -c pytorch -c tensorcomp tensor_comprehensions
amin saffar
  • 1,953
  • 3
  • 22
  • 34
  • 3
    I think this process uses the base environment, which is the default when you log in. The problem is creating a new conda environment and then activating that environment so you can use it. – Donald S Jun 27 '20 at 23:50
5

a quick fix

put !source activate myenv && before all your bash commands

!source activate myenv && <COMMAND1>

For example

!source activate myenv && conda env list

base /usr/local

myenv * /usr/local/envs/myenv

justification:

Well we have to put ! in front of your bash commands anyway... But I would love to know a better way.

conor
  • 1,131
  • 1
  • 15
  • 20
0

A very quick fix would be to run the command:

source PATH/to/activate env_name

https://github.com/ContinuumIO/anaconda-issues/issues/9539

Floern
  • 33,559
  • 24
  • 104
  • 119