[Detailed version] Creating a comfortable Python environment with Google Compute Engine (Ubuntu 16.04)

Overview

In this article, we will create a Python environment on Ubuntu on GCE. (Please set vim by yourself.) The specific things to do are as follows.

Initial setting

In the initial state, the service account is used internally. It is a lot of trouble, so it is recommended to log in with your own user account.

gcloud init

You will be asked variously, so authenticate as you were told and paste the verification code.

Introduction of zsh

First, install zsh.

sudo apt-get install zsh -y

After that, I want to set the default shell to zsh, but the chsh command doesn't work because I don't know the password. (Please tell me if there is a way) for that reason,

vim ~/.bashrc

Open bashrc with etc. and at the end

exec /usr/bin/zsh

Is inserted. The default shell is now zsh.

Insert oh-my-zsh

Next, insert oh-my-zsh. (I think that each person is particular about it, so if you are particular about it, skip to [Building a Python environment](## Building a Python environment).)

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

You will be asked if you want to change the default shell, but of course set it to no.

Enter the following as an external plug-in.

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-completions

Let's change zshrc here.

~/.zshrc


export ZSH="${HOME}/.oh-my-zsh"
ZSH_THEME="candy"
plugins=(
  git pip pyenv virtualenv zsh-syntax-highlighting zsh-autosuggestions history-substring-search zsh-completions
)
source $ZSH/oh-my-zsh.sh

After editing, load it once.

source ~/.zshrc

Building a Python environment

We will build a common virtual environment using pyenv and virualenv. First, install pyenv and virtualenv.

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshenv
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshenv
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshenv
source ~/.zshenv
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshenv
source ~/.zshenv

Install your favorite version of Python. For example, like this.

pyenv install 3.6.5
pyenv virtualenv 3.6.5 default_env
pyenv global default_env

Install powerline-shell

If you are particular about this, please skip it. First, put powerline-shell in the current virtual environment.

pip install powerline-shell

After that, edit zshrc again as follows.

~/.zshrc


export ZSH="${HOME}/.oh-my-zsh"
ZSH_THEME="candy"

function powerline_precmd() {
    PS1="$(powerline-shell --shell zsh $?)"
}
function install_powerline_precmd() {
  for s in "${precmd_functions[@]}"; do
    if [ "$s" = "powerline_precmd" ]; then
      return
    fi
  done
  precmd_functions+=(powerline_precmd)
}
if [ "$TERM" != "linux" ]; then
    install_powerline_precmd
fi

plugins=(
  git pip pyenv virtualenv zsh-syntax-highlighting zsh-autosuggestions history-substring-search zsh-completions
)
source $ZSH/oh-my-zsh.sh

export LC_ALL="en_US.UTF-8"
export LANG=ja_JP.UTF-8

#----------------------The following is a private setting, so please omit it.----------------------#
#history file specification
HISTFILE=$HOME/.zsh-history
HISTSIZE=1000
SAVEHIST=10000

#Cd if not a command
setopt auto_cd
#List completion candidates
setopt auto_list
#Switch completion candidates in order with TAB
setopt auto_menu
#Automatically complements parentheses
setopt auto_param_keys
#Directory name completion at the end/Is automatically added to prepare for the next completion
setopt auto_param_slash
#Correct when a command is missed
setopt correct
#Mark the file type in the completion candidate list
setopt list_types
#Without specifying a clear dot.Match files starting with
setopt globdots
#If the directory is matched by expanding the file name, at the end/Add
setopt mark_dirs
#Complement at the cursor position even in the middle of a word
setopt complete_in_word
#The file name list is displayed on the spot while keeping the cursor position.
setopt always_last_prompt
#Do not beep
setopt nobeep
#vcs enabled
setopt prompt_subst
# #Treat the following as comments
setopt interactive_comments
#History sharing
setopt share_history
#If the command added to the history is the same as the old one, delete the old one
setopt hist_ignore_all_dups

Further customize.

mkdir -p ~/.config/powerline-shell && powerline-shell --generate-config > 

Create the following 2 files.

python:~/.config/powerline-shell/original_color.py


from powerline_shell.themes.default import DefaultColor
​
​
class Color(DefaultColor):
    USERNAME_FG = 15
    USERNAME_BG = 4
    USERNAME_ROOT_BG = 1
​
    HOSTNAME_FG = 15
    HOSTNAME_BG = 10
​
    HOME_SPECIAL_DISPLAY = False
    PATH_FG = 15
    PATH_BG = 70
    CWD_FG = 231
    SEPARATOR_FG = 0
​
    READONLY_BG = 1
    READONLY_FG = 7
​
    REPO_CLEAN_FG = 14
    REPO_CLEAN_BG = 0
    REPO_DIRTY_FG = 3
    REPO_DIRTY_BG = 0
​
    JOBS_FG = 4
    JOBS_BG = 8
​
    CMD_PASSED_FG = 255
    CMD_PASSED_BG = 136
    CMD_FAILED_FG = 255
    CMD_FAILED_BG = 1
​
    SVN_CHANGES_FG = REPO_DIRTY_FG
    SVN_CHANGES_BG = REPO_DIRTY_BG
​
    VIRTUAL_ENV_BG = 31
    VIRTUAL_ENV_FG = 231
​
    AWS_PROFILE_FG = 7
    AWS_PROFILE_BG = 2
​
    TIME_FG = 255
    TIME_BG = 246

python:~/.config/powerline-shell/config.json


{
    "segments": [
        "virtual_env",
        "aws_profile",
        "ssh",
        "cwd",
        "git",
        "git_stash",
        "jobs",
        "read_only",
        "newline",
        "set_term_title",
        "svn",
        "time",
        "exit_code"
    ],
    "cwd": {
        "max_depth": 4,
        "max_dir_size": 10,
        "full_cwd": 1
    },
    "mode": "patched",
    "theme": "~/.config/powerline-shell/original_color.py"
}

** Caution **

  • If there is no powerline-shell in the virtual environment, it will be strange, so please do pip install every time.
  • If the characters are garbled, please refer to the following and insert the font. (I use Roboto Mono for Powerline)
    • https://github.com/powerline/fonts

Jupyter Notebook Settings

If it is left as it is, Jupyter cannot be used, so set it. You can use Jupyter by following ** jupyter settings ** on the following site. Reference: https://rf00.hatenablog.com/entry/2018/01/01/160820

*** If ssh runs out, Jupyter will drop, so it is recommended to set up tmux and use Jupyter in it. ** **

Install Stack Driver Monitoring Agent

We recommend Stack Driver Monitoring Agent for GCE resource monitoring. If you put it in, you will be able to check the detailed memory usage etc. from the Agent tab next to the HOST tab on the upper right of here. (However, please note that you will be charged separately)

Install according to Official. By default, data is sent every 1 minute, but since I am concerned about the amount of charges, I will change it to send data every 3 minutes.

sudo vim /etc/stackdriver/collectd.conf

/etc/stackdriver/collectd.conf


Interval 180  #Changed from Interval 60

#No change below
sudo service stackdriver-agent restart

Recommended Posts

[Detailed version] Creating a comfortable Python environment with Google Compute Engine (Ubuntu 16.04)
[Pyenv] Building a python environment with ubuntu 16.04
Build a Python execution environment using GPU with GCP Compute engine
Ubuntu18.04.05 Creating a python virtual environment in LTS
A memo when creating a python environment with miniconda
Commands for creating a python3 environment with virtualenv
I made a Python3 environment on Ubuntu with direnv.
Runtime version of Google App Engine / Python Standard Environment
Build python3 environment with ubuntu 16.04
Launching a machine learning environment using Google Compute Engine (GCE)
Building a Python environment on Ubuntu
Building a virtual environment with Python 3
Building a development environment with Maven on Google App Engine [Java]
Create a USB boot Ubuntu with a Python environment for data analysis
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Creating a simple PowerPoint file with Python
Building a Python3 environment with Amazon Linux2
Note when creating an environment with python
[Python] Create a virtual environment with Anaconda
Building a Python 3.6 environment with Windows + PowerShell
Creating a python virtual environment on Windows
Create a virtual environment with Python_Mac version
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Deploy a Python app on Google App Engine and integrate it with GitHub
I was addicted to creating a Python venv environment with VS Code
Building a python environment with virtualenv and direnv
Build a python environment with ansible on centos6
Building a Python environment with WLS2 + Anaconda + PyCharm
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
Manage Python multiple version environment with Pythonz, virtualenv
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
[Venv] Create a python virtual environment on Ubuntu
Build Python3 + flask environment on GCP Compute Engine
Work in a virtual environment with Python virtualenv.
Flow of creating a virtual environment with Anaconda
Procedure for creating a Python quarantine environment (venv environment)
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
A memo for creating a python environment by a beginner
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
Get a quick Python development environment with Poetry
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
Python: Creating a virtual environment (venv), starting and stopping
Recommendation of building a portable Python environment with conda
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Build a python virtual environment with virtualenv and virtualenvwrapper
Notes on creating a python development environment on macOS Catalina
Build a python environment for each directory with pyenv-virtualenv
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a python development environment with vagrant + ansible + fabric
Deploy a Django application on Google App Engine (Python3)
Problems when creating a csv-json conversion tool with python
Google App Engine / Python development environment construction procedure (late 2014)
PIL with Python on Windows 8 (for Google App Engine)
Build a machine learning application development environment with Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Getting Started with Google App Engine for Python & PHP