Install Python the right way
Tags: python, programming, pyenv, coding
Published: 2021-02-17 08:52:46

Installing Python is fairly easy. You can go to their [official site](https://www.python.org/) 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](https://github.com/pyenv/pyenv) for all other OS.  

Ubuntu already comes with python preinstalled.

```shell
$ python -V
Python 2.7.17

$ which python
/usr/bin/python
```
1\. Before pyenv, python build dependencies must be installed [https://github.com/pyenv/pyenv/wiki#suggested-build-environment](https://github.com/pyenv/pyenv/wiki#suggested-build-environment)

```shelll
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
```
2\.  Clone pyenv to the root ` " $HOME/.pyenv"` 

```shell
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
```

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

Zsh:

```shell
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
```

Bash:

```shell
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
```

Ubuntu Desktop:

```shell
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
```

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 


```.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
```

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

Zsh:

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

Bash:

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

Ubuntu Desktop:

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

*Note please refer to [https://github.com/pyenv/pyenv](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


```.zshrc
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
```

5\. Restart your shell/terminal for changes to occur 
```bash
exec "$SHELL"
```

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.
```shell
$ which python
/home/user/.pyenv/shims/python
```

Also, you can check if pyenv is working by checking its version.
```shell
$pyenv --version
pyenv 1.2.22-63-g421ff608
```

### List Python Versions

You can list available python version in your system by

```shell
pyenv versions
```

For listing available python version to download by

```shell
pyenv install --list
```

Install Python version

```shell
pyenv install 3.9.1
```

Change global python version by

```shell
pyenv global 3.9.1
pyenv global system #changes back to the system python
```

Change local python version (application-specific) by

```shell
pyenv local 3.9.1
```