DEV Community

Cover image for Install Python the right way
Shrijal Tamrakar
Shrijal Tamrakar

Posted on

Install Python the right way

Installing Python is fairly easy. You can go to their official site and download the latest python version or browse through the list of versions and download it. This may be okay if you are planning to learn python, working with the same version and all. But what if you have multiple projects with multiple python projects? It may not be practical for you to download every python version as per the project. Therefore you require a python version manager for this task.

Pyenv is the simple solution for this. It easily lets you install any python version and switch between them with ease.

Getting Started

We will look into how to get pyenv up and running in ubuntu. You can always refer to the official docs for all other OS.

Ubuntu already comes with python preinstalled.

$ python -V
Python 2.7.17

$ which python
/usr/bin/python
Enter fullscreen mode Exit fullscreen mode

1. Before pyenv, python build dependencies must be installed https://github.com/pyenv/pyenv/wiki#suggested-build-environment

sudo apt-get update; sudo apt-get install --no-install-recommends 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 liblzma-dev
Enter fullscreen mode Exit fullscreen mode

2. Clone pyenv to the root " $HOME/.pyenv"

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Enter fullscreen mode Exit fullscreen mode

3. Defining environment variable PYENV_ROOT to point to the pyenv repo and add $PYENV_ROOT/bin to your $PATH command line access

Zsh:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Bash:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Ubuntu Desktop:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

You can view the respective file to check if the change has occurred.
In Zsh, view ~/.zshrc file if the following code is there at the end

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

4. Add pyenv init to the shell which enables shims and autocompletion

Zsh:

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Bash:

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Ubuntu Desktop:

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

*Note please refer to https://github.com/pyenv/pyenv if any problem occurs

Similarly, after doing this, there will be the addition of a new line in ~/.zshrc file

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi
Enter fullscreen mode Exit fullscreen mode

5. Restart your shell/terminal for changes to occur

exec "$SHELL"
Enter fullscreen mode Exit fullscreen mode

All done now. You check if it is working as it must by running $ which python in terminal. You will notice that the path is changed.

$ which python
/home/user/.pyenv/shims/python
Enter fullscreen mode Exit fullscreen mode

Also, you can check if pyenv is working by checking its version.

$pyenv --version
pyenv 1.2.22-63-g421ff608
Enter fullscreen mode Exit fullscreen mode

List Python Versions

You can list available python version in your system by

pyenv versions
Enter fullscreen mode Exit fullscreen mode

For listing available python version to download by

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

Install Python version

pyenv install 3.9.1
Enter fullscreen mode Exit fullscreen mode

Change global python version by

pyenv global 3.9.1
pyenv global system #changes back to the system python
Enter fullscreen mode Exit fullscreen mode

Change local python version (application-specific) by

pyenv local 3.9.1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)