Manage versions of Python with pyenv and zsh in MacOS

You are working on different projects that require different versions of Python? You’re tired of switching between them?

Luckily, Pyenv is here to help you out.

pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

Today, I will guide you through the steps of making it work with default terminal or with zsh.

Before we start, brew is used to install pyenv. If you’re already installed brew, skip this step. Otherwise, install brew through this link:

Homebrew installs packages to their own directory and then symlinks their files into /usr/local.

Step 1: Install pyenv

$ brew install pyenv

Step 2: Add config to load pyenv every time we open a new shell

If you’re using default mac Terminal:

$ echo 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi' >> ~/.bash_profile

Else if you are using zsh instead:

$ echo 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi' >> ~/.zprofile

Now restart your terminal.

Step 3: Confirm if it is properly set up

$ which python

It will output something like this:

$ which python

Step 4: Install python versions

To list all python versions that can be installed using pyenv:

$ pyenv install -l

This will output a really long list like:

Available versions:

Choose the version you want, example 3.6.2. To install:

$ pyenv install 3.6.2

Wait for downloading and installing to complete.

If an error occurs, with the message like:

zipimport.ZipImportError: can't decompress data; zlib not available

Try this in your terminal:

$ CFLAGS="-I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" \ LDFLAGS="-L$(brew --prefix openssl)/lib" \

And then:

> pyenv install 3.6.2

Step 5: Switch between different python versions

To list all installed versions:

$ pyenv versions

This will output:

* system (set by /Users/duchoang/.pyenv/version)

To switch to 3.6.2 as global python version:

$ pyenv global 3.6.2

You can also change the version of python to a local folder:

$ cd ~/your/local-project/
$ pyenv local 3.2.6

This will create a `.python-version` file and the version of python inside the project will be set despite the global version.


Docker completion for Zsh with Prezto

I am currently using Prezto as the configuration framework for Zsh. Having completion while working with Docker on Terminal can boost our productivity a little bit to focus on what’s more important. So let’s get it configured.

~/.z/m/c/e/src  curl -fLo ~/.zprezto/modules/completion/external/src/_docker
~/.z/m/c/e/src  curl -fLo ~/.zprezto/modules/completion/external/src/_docker-compose
~/.z/m/c/e/src  exec $SHELL -l

Now check if it works.


zsh cannot execute global npm packages

Today, I installed an npm package globally, no problem here.

Screen Shot 2017-03-11 at 2.01.37 PM.png

But, when I execute the package, there comes an error:

~  yo
zsh: command not found: yo

So, it means that the system doesn’t realize the path to my package. I confirm by using:

~/W/C/htnmaruko  echo $PATH

So that, we need to export the path to where my package is lying in.

Because I installed by -g parameter, so it is lying in global npm. In my case, it is being in ~/.npm-global.


1. Export your path in ~/.zshrc (in case of using zsh, otherwise using ~/.bashrc) by  print it to your ~/.zshrc or ~/.bashrc (in case of not using zsh) file:

~/.n/bin  printf "\nexport PATH=\"\$PATH\":%s\n" ~/.npm-global/bin >> ~/.zshrc

Or just editing with vi:

~  vi /Users/duchoang/.zshrc

2. Now, fire up the changes:

~  . ~/.zshrc

3. And finally, the problem gets solved, the result we have:

Screen Shot 2017-03-11 at 2.03.28 PM

Now we can get back to our work.

What to do if your Rails application cannot connect to Mysql?

When I use my Mac to continue developing my Rails application using Mysql as the database system. It first initialized and developed in Ubuntu.

After bundling tasks, I move to rake db:create to initialize my databases for development and test environment. But the problem comes out:

Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Ok, cd to the


Oh, there’s no mysqld.sock there. Ok, so there must be the error as above error message.

So, I was wondering where mysqld.sock is in my computer? I was installed Mysql through brew and able to connect to Mysql by Mysql Workbench with no problem. So let’s see some variables. Enter to the terminal:

mysqladmin -p variables | grep socket

Provide the password, and then you will see something like:


| performance_schema_max_socket_classes                    | 10

| performance_schema_max_socket_instances                  | -1

| socket                                                   | /tmp/mysql.sock|

Wow, there comes in the socket line the mysql.sock file location

We have to connect through it.

After some research, I know that Rails settings the socket in the filedatabase.yml.

So, it’s EZ now, just replace the new found path.

It’s ok now. I can continue my work.

Fixing rbenv not update ruby system (rbenv version different from ruby -v)

Recently, I faced with the problem of not syncing ruby version with my Mac.

My project requires ruby version >= 2.2.2 but default version is 2.0.0.

I haved installed ruby 2.3.1  on my rbenv (ruby version manager) and set it global, but when i bundle install, there comes some error liked:

activesupport-5.0.0 requires ruby version >= 2.2.2, which is incompatible with the current
version, ruby 2.0.0p648

Even rbenv version shows:

2.3.1 (set by /Users/duchoang/.ruby-version)

I checked back with ruby -v, it shows 2.0.0. Oh shit what happened?

I searched and they said that I have to install ruby-build, ok but after that I reallized that it’s not the cause:

brew install rbenv ruby-build

rbenv rehash

I reallized that I’ve switched to zsh, so instead of ~/.bash_profile I need to rerun the following command to ~/.zshrc:

$ echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.zshrc
$ source ~/.zshrc


Better color for your Mac Terminal

Run this from terminal:

open ~/.bash_profile

Add these lines to the end:

export TERM="xterm-color" 
export PS1='\[\e[0;33m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ '

Then restart your Terminal to see the changes, now you’re done.

For more customizations, refer to the post:

Or, you could try some alternatives:

  • Cathode
  • iTerm2
  • Terminator
  • MacTerm