How do I install Python in Google Cloud Shell?
Asked Answered
P

5

21

I have python 3.5 on my google cloud shell and want 3.7 so I can do command line debugging of code I am going to deploy via google cloud functions (and use 3.7 features such as f-strings).

I have tried various forms of the following:

sudo apt-get install python37

and always get back

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python37
Parade answered 25/11, 2018 at 15:8 Comment(2)
Did you try python3.7?Bristol
Yes - python3.7 and python3.7.3 - all with same resultsParade
A
21

# install pyenv to install python on persistent home directory
curl https://pyenv.run | bash

# add to path
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

# updating bashrc
source ~/.bashrc

# install python 3.7.4 and make default
pyenv install 3.7.4
pyenv global 3.7.4

# execute
python

This is based on @yungchin answer.

Alicia answered 7/8, 2019 at 23:59 Comment(4)
I had to run a new instance of the shell before running pyenvAnthology
@Ali Khosro will this technique work on Colab which actually is 3.6.9 ?Hedvig
This should work on any linux environment when you can not (or do not want to) use package manager to install newer version of python.Alicia
it's not working, after 'pyenv global' the global Python is the old oneIchthyology
I
15

This worked for me on the GCP shell.

# Install requirements
sudo apt-get install -y build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev zlib1g-dev openssl libffi-dev python3-dev python3-setuptools wget 

# Prepare to build
mkdir /tmp/Python37
cd /tmp/Python37

# Pull down Python 3.7, build, and install
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
tar xvf Python-3.7.0.tar.xz
cd /tmp/Python37/Python-3.7.0
./configure
sudo make altinstall

Then you would just call Python like so:

python3.7 ./yourScript.py

Src: https://serverfault.com/questions/918335/best-way-to-run-python-3-7-on-ubuntu-16-04-which-comes-with-python-3-5

Ideate answered 24/1, 2019 at 13:10 Comment(1)
That worked, thanks. Do you know how I can make a virtualenv with it now?Rist
D
12

Even if the packages were available through apt, the downside of using apt would be that you'd have to install all over again whenever you'd been disconnected from Cloud Shell: it always discards your runtime container.

I'd recommend using https://github.com/pyenv/pyenv for convenience. If you follow the installation guide (and note the bash profile additions should go into .bashrc in our case) you end up with a python build in your home directory, which is persisted across Cloud Shell sessions. This involves just a few steps:

  1. clone the repo into ~/.pyenv
  2. append three lines (see the README) to .bashrc to adjust your $PATH
  3. pyenv install 3.7.3 # this takes a while to build
  4. pyenv global 3.7.3 # sets this version as the default
Doukhobor answered 2/5, 2019 at 22:53 Comment(0)
G
1

Python 3 can be installed in Cloud Shell as a side-effect of installing Conda/Anaconda. Copy the link to the desired installer shell script available here: Installing on Linux.

Example

Welcome to Cloud Shell! Type "help" to get started.
$ wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.10.3-Linux-x86_64.sh
$ bash Miniconda3-py39_4.10.3-Linux-x86_64.sh

After following the instructions, close Cloud Shell and open a new session. Python is now updated.

With Conda installed, you can now create environments for additional Python versions as follows:

$ conda create -n "py37" python=3.7.0
$ conda activate py37
$ python --version
Python 3.7.0
Guinea answered 4/8, 2021 at 22:4 Comment(3)
The problem with this solution is that as you can see in your output this installed py3.9.4, not 3.7 as requested.Honaker
Fair enough. But the point of using conda is to be able to use different versions at will.Honaker
@Honaker See updated answer showing how to set up environments for different versions of Python after Conda is installed.Guinea
H
0

Another simple approach is

sudo `which conda` install python=3.7 -y
Honaker answered 5/10, 2020 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.