Skip to content

Ubuntu 安装 pyenv

约 721 字大约 2 分钟

PythonUbuntupyenv

2025-01-18

pyenv是一个Python版本管理工具,可以方便地切换Python版本,安装多个Python版本,以及管理Python虚拟环境。

安装pyenv

自动安装

查看是否安装curl

$ curl -V
curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.16
Release-Date: 2022-01-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd

查看是否安装git

$ git --version
git version 2.34.1

自动安装

$ curl https://pyenv.run | bash

并没有成功,最后还是选择了手动安装

手动安装

下载

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

下载成功后配置zsh

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

刷新zsh的配置

$ source ~/.zshrc

查看版本

$ pyenv --version
pyenv 2.3.35-2-g96f93fd5

安装pyenv-virtualenv

如果要使用pyenv来管理Python的虚拟环境,需要安装pyenv-virtualenv插件。

pyenv的插件目录下载pyenv-virtualenvpyenv的插件目录一般在~/.pyenv/plugins

$ cd ~/.pyenv/plugins
$ git clone https://github.com/pyenv/pyenv-virtualenv.git
Cloning into 'pyenv-virtualenv'...
remote: Enumerating objects: 2256, done.
remote: Counting objects: 100% (185/185), done.
remote: Compressing objects: 100% (108/108), done.
remote: Total 2256 (delta 101), reused 136 (delta 70), pack-reused 2071
Receiving objects: 100% (2256/2256), 646.71 KiB | 448.00 KiB/s, done.
Resolving deltas: 100% (1521/1521), done.
$ ll
total 8.0K
drwxrwxr-x 9 tdy tdy 4.0K 12月 20 22:13 pyenv-virtualenv
drwxrwxr-x 6 tdy tdy 4.0K 12月 17 22:36 python-build

更新zsh的配置文件

$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

查看pyenvhelp就能看到创建虚拟环境的命令了

$ pyenv help
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   activate    Activate virtual environment
   commands    List all available pyenv commands
   deactivate   Deactivate virtual environment
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version(s)
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   latest      Print the latest installed or known version with the given prefix
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefixes for Python versions
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall Python versions
   --version   Display the version of pyenv
   version     Show the current Python version(s) and its origin
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   virtualenv   Create a Python virtualenv using the pyenv-virtualenv plugin
   virtualenv-delete   Uninstall a specific Python virtualenv
   virtualenv-init   Configure the shell environment for pyenv-virtualenv
   virtualenv-prefix   Display real_prefix for a Python virtualenv version
   virtualenvs   List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

更多种方式创建Python虚拟环境,请查看Python 创建虚拟环境

参考

pyenvpyenv-virtualenv